|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.engine.orchestration; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.LinkedHashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 25 | +import com.cloud.offering.DiskOfferingInfo; |
| 26 | +import com.cloud.service.ServiceOfferingVO; |
| 27 | +import com.cloud.service.dao.ServiceOfferingDao; |
| 28 | +import com.cloud.storage.DiskOfferingVO; |
| 29 | +import com.cloud.storage.VMTemplateVO; |
| 30 | +import com.cloud.storage.dao.DiskOfferingDao; |
| 31 | +import com.cloud.storage.dao.VMTemplateDao; |
| 32 | +import com.cloud.utils.component.ComponentContext; |
| 33 | +import com.cloud.vm.VMInstanceVO; |
| 34 | +import com.cloud.vm.VirtualMachineManager; |
| 35 | +import com.cloud.vm.VmDetailConstants; |
| 36 | +import com.cloud.vm.dao.UserVmDetailsDao; |
| 37 | +import com.cloud.vm.dao.VMInstanceDao; |
| 38 | +import org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntityImpl; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.ArgumentCaptor; |
| 43 | +import org.mockito.InjectMocks; |
| 44 | +import org.mockito.Mock; |
| 45 | +import org.mockito.MockedStatic; |
| 46 | +import org.mockito.Mockito; |
| 47 | +import org.mockito.junit.MockitoJUnitRunner; |
| 48 | + |
| 49 | +@RunWith(MockitoJUnitRunner.class) |
| 50 | +public class CloudOrchestratorTest { |
| 51 | + |
| 52 | + private static final long VM_ID = 1L; |
| 53 | + private static final long SERVICE_OFFERING_ID = 2L; |
| 54 | + private static final long ROOT_DISK_OFFERING_ID = 3L; |
| 55 | + private static final String TEMPLATE_ID = "4"; |
| 56 | + |
| 57 | + @InjectMocks |
| 58 | + private CloudOrchestrator cloudOrchestrator = new CloudOrchestrator(); |
| 59 | + |
| 60 | + @Mock |
| 61 | + private VirtualMachineManager virtualMachineManager; |
| 62 | + @Mock |
| 63 | + private VMTemplateDao templateDao; |
| 64 | + @Mock |
| 65 | + private VMInstanceDao vmDao; |
| 66 | + @Mock |
| 67 | + private UserVmDetailsDao userVmDetailsDao; |
| 68 | + @Mock |
| 69 | + private ServiceOfferingDao serviceOfferingDao; |
| 70 | + @Mock |
| 71 | + private DiskOfferingDao diskOfferingDao; |
| 72 | + |
| 73 | + @Test |
| 74 | + public void createVirtualMachineSetsCustomIopsFromVmDetails() throws Exception { |
| 75 | + VMInstanceVO vm = Mockito.mock(VMInstanceVO.class); |
| 76 | + ServiceOfferingVO serviceOffering = Mockito.mock(ServiceOfferingVO.class); |
| 77 | + DiskOfferingVO rootDiskOffering = Mockito.mock(DiskOfferingVO.class); |
| 78 | + VMTemplateVO template = Mockito.mock(VMTemplateVO.class); |
| 79 | + VirtualMachineEntityImpl vmEntity = Mockito.mock(VirtualMachineEntityImpl.class); |
| 80 | + |
| 81 | + Mockito.when(vmDao.findByUuid("vm-uuid")).thenReturn(vm); |
| 82 | + Mockito.when(vm.getId()).thenReturn(VM_ID); |
| 83 | + Mockito.when(vm.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID); |
| 84 | + Mockito.when(vm.getInstanceName()).thenReturn("i-1-1-VM"); |
| 85 | + Mockito.when(serviceOfferingDao.findById(VM_ID, SERVICE_OFFERING_ID)).thenReturn(serviceOffering); |
| 86 | + Mockito.when(diskOfferingDao.findById(ROOT_DISK_OFFERING_ID)).thenReturn(rootDiskOffering); |
| 87 | + Mockito.when(rootDiskOffering.isCustomizedIops()).thenReturn(true); |
| 88 | + Mockito.when(templateDao.findById(Long.valueOf(TEMPLATE_ID))).thenReturn(template); |
| 89 | + |
| 90 | + Map<String, String> details = new HashMap<>(); |
| 91 | + details.put(VmDetailConstants.MIN_IOPS, "100"); |
| 92 | + details.put(VmDetailConstants.MAX_IOPS, "1000"); |
| 93 | + Mockito.when(userVmDetailsDao.listDetailsKeyPairs(VM_ID)).thenReturn(details); |
| 94 | + |
| 95 | + try (MockedStatic<ComponentContext> componentContext = Mockito.mockStatic(ComponentContext.class)) { |
| 96 | + componentContext.when(() -> ComponentContext.inject(VirtualMachineEntityImpl.class)).thenReturn(vmEntity); |
| 97 | + |
| 98 | + cloudOrchestrator.createVirtualMachine("vm-uuid", "owner", TEMPLATE_ID, "host", "display", HypervisorType.KVM.name(), |
| 99 | + 1, 1000, 1024, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyMap(), null, |
| 100 | + null, null, null, null, ROOT_DISK_OFFERING_ID); |
| 101 | + } |
| 102 | + |
| 103 | + ArgumentCaptor<DiskOfferingInfo> rootDiskOfferingInfo = ArgumentCaptor.forClass(DiskOfferingInfo.class); |
| 104 | + Mockito.verify(virtualMachineManager).allocate(Mockito.eq("i-1-1-VM"), Mockito.eq(template), Mockito.eq(serviceOffering), rootDiskOfferingInfo.capture(), |
| 105 | + Mockito.anyList(), Mockito.any(LinkedHashMap.class), Mockito.isNull(), Mockito.eq(HypervisorType.KVM), Mockito.isNull(), Mockito.isNull()); |
| 106 | + |
| 107 | + Assert.assertEquals(Long.valueOf(100), rootDiskOfferingInfo.getValue().getMinIops()); |
| 108 | + Assert.assertEquals(Long.valueOf(1000), rootDiskOfferingInfo.getValue().getMaxIops()); |
| 109 | + } |
| 110 | +} |
0 commit comments