| // Licensed to the Apache Software Foundation (ASF) under one | |
| // or more contributor license agreements. See the NOTICE file | |
| // distributed with this work for additional information | |
| // regarding copyright ownership. The ASF licenses this file | |
| // to you under the Apache License, Version 2.0 (the | |
| // "License"); you may not use this file except in compliance | |
| // with the License. You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, | |
| // software distributed under the License is distributed on an | |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| // KIND, either express or implied. See the License for the | |
| // specific language governing permissions and limitations | |
| // under the License. | |
| package com.cloud.vm; | |
| import static org.junit.Assert.assertEquals; | |
| import static org.junit.Assert.assertFalse; | |
| import static org.junit.Assert.assertTrue; | |
| import static org.mockito.ArgumentMatchers.any; | |
| import static org.mockito.ArgumentMatchers.anyLong; | |
| import static org.mockito.ArgumentMatchers.anyMap; | |
| import static org.mockito.ArgumentMatchers.anyString; | |
| import static org.mockito.ArgumentMatchers.nullable; | |
| import static org.mockito.Mockito.lenient; | |
| import static org.mockito.Mockito.mock; | |
| import static org.mockito.Mockito.when; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import com.cloud.configuration.Resource; | |
| import com.cloud.hypervisor.Hypervisor; | |
| import com.cloud.storage.DiskOfferingVO; | |
| import com.cloud.storage.VMTemplateVO; | |
| import com.cloud.storage.VolumeVO; | |
| import com.cloud.storage.dao.DiskOfferingDao; | |
| import com.cloud.storage.dao.VMTemplateDao; | |
| import com.cloud.user.ResourceLimitService; | |
| import com.cloud.user.dao.AccountDao; | |
| import org.apache.cloudstack.api.BaseCmd.HTTPMethod; | |
| import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd; | |
| import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd; | |
| import org.apache.cloudstack.context.CallContext; | |
| import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; | |
| import org.junit.After; | |
| import org.junit.Assert; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.mockito.InjectMocks; | |
| import org.mockito.Mock; | |
| import org.mockito.Mockito; | |
| import org.mockito.Spy; | |
| import org.mockito.junit.MockitoJUnitRunner; | |
| import org.powermock.core.classloader.annotations.PrepareForTest; | |
| import com.cloud.dc.DataCenterVO; | |
| import com.cloud.dc.dao.DataCenterDao; | |
| import com.cloud.exception.InsufficientAddressCapacityException; | |
| import com.cloud.exception.InsufficientCapacityException; | |
| import com.cloud.exception.InvalidParameterValueException; | |
| import com.cloud.exception.ResourceUnavailableException; | |
| import com.cloud.network.NetworkModel; | |
| import com.cloud.network.dao.NetworkDao; | |
| import com.cloud.network.dao.NetworkVO; | |
| import com.cloud.offering.ServiceOffering; | |
| import com.cloud.service.ServiceOfferingVO; | |
| import com.cloud.service.dao.ServiceOfferingDao; | |
| import com.cloud.storage.GuestOSVO; | |
| import com.cloud.storage.Storage; | |
| import com.cloud.storage.dao.GuestOSDao; | |
| import com.cloud.user.Account; | |
| import com.cloud.user.AccountManager; | |
| import com.cloud.user.AccountVO; | |
| import com.cloud.user.UserVO; | |
| import com.cloud.uservm.UserVm; | |
| import com.cloud.vm.dao.NicDao; | |
| import com.cloud.vm.dao.UserVmDao; | |
| import com.cloud.vm.dao.UserVmDetailsDao; | |
| @RunWith(MockitoJUnitRunner.class) | |
| public class UserVmManagerImplTest { | |
| @Spy | |
| @InjectMocks | |
| private UserVmManagerImpl userVmManagerImpl = new UserVmManagerImpl(); | |
| @Mock | |
| private ServiceOfferingDao _serviceOfferingDao; | |
| @Mock | |
| private DiskOfferingDao diskOfferingDao; | |
| @Mock | |
| private ServiceOfferingVO serviceOfferingVO; | |
| @Mock | |
| private DataCenterDao _dcDao; | |
| @Mock | |
| private DataCenterVO _dcMock; | |
| @Mock | |
| protected NicDao nicDao; | |
| @Mock | |
| private NetworkDao _networkDao; | |
| @Mock | |
| private NetworkOrchestrationService _networkMgr; | |
| @Mock | |
| private NetworkVO _networkMock; | |
| @Mock | |
| private GuestOSDao guestOSDao; | |
| @Mock | |
| private UserVmDao userVmDao; | |
| @Mock | |
| private UpdateVMCmd updateVmCommand; | |
| @Mock | |
| private AccountManager accountManager; | |
| @Mock | |
| private UserVmDetailsDao userVmDetailVO; | |
| @Mock | |
| private UserVmVO userVmVoMock; | |
| @Mock | |
| private NetworkModel networkModel; | |
| @Mock | |
| private Account accountMock; | |
| @Mock | |
| private AccountVO callerAccount; | |
| @Mock | |
| private UserVO callerUser; | |
| @Mock | |
| private VMTemplateDao templateDao; | |
| @Mock | |
| private AccountDao accountDao; | |
| @Mock | |
| ResourceLimitService resourceLimitMgr; | |
| private long vmId = 1l; | |
| private static final long GiB_TO_BYTES = 1024 * 1024 * 1024; | |
| private Map<String, String> customParameters = new HashMap<>(); | |
| private DiskOfferingVO smallerDisdkOffering = prepareDiskOffering(5l * GiB_TO_BYTES, 1l, 1L, 2L); | |
| private DiskOfferingVO largerDisdkOffering = prepareDiskOffering(10l * GiB_TO_BYTES, 2l, 10L, 20L); | |
| @Before | |
| public void beforeTest() { | |
| Mockito.when(updateVmCommand.getId()).thenReturn(vmId); | |
| when(_dcDao.findById(anyLong())).thenReturn(_dcMock); | |
| Mockito.when(userVmDao.findById(Mockito.eq(vmId))).thenReturn(userVmVoMock); | |
| Mockito.when(callerAccount.getType()).thenReturn(Account.ACCOUNT_TYPE_ADMIN); | |
| CallContext.register(callerUser, callerAccount); | |
| customParameters.put(VmDetailConstants.ROOT_DISK_SIZE, "123"); | |
| lenient().doNothing().when(resourceLimitMgr).incrementResourceCount(anyLong(), any(Resource.ResourceType.class)); | |
| lenient().doNothing().when(resourceLimitMgr).decrementResourceCount(anyLong(), any(Resource.ResourceType.class), anyLong()); | |
| } | |
| @After | |
| public void afterTest() { | |
| CallContext.unregister(); | |
| } | |
| @Test | |
| public void validateGuestOsIdForUpdateVirtualMachineCommandTestOsTypeNull() { | |
| Mockito.when(updateVmCommand.getOsTypeId()).thenReturn(null); | |
| userVmManagerImpl.validateGuestOsIdForUpdateVirtualMachineCommand(updateVmCommand); | |
| } | |
| @Test(expected = InvalidParameterValueException.class) | |
| public void validateGuestOsIdForUpdateVirtualMachineCommandTestOsTypeNotFound() { | |
| Mockito.when(updateVmCommand.getOsTypeId()).thenReturn(1l); | |
| userVmManagerImpl.validateGuestOsIdForUpdateVirtualMachineCommand(updateVmCommand); | |
| } | |
| @Test | |
| public void validateGuestOsIdForUpdateVirtualMachineCommandTestOsTypeFound() { | |
| Mockito.when(updateVmCommand.getOsTypeId()).thenReturn(1l); | |
| Mockito.when(guestOSDao.findById(Mockito.eq(1l))).thenReturn(Mockito.mock(GuestOSVO.class)); | |
| userVmManagerImpl.validateGuestOsIdForUpdateVirtualMachineCommand(updateVmCommand); | |
| } | |
| @Test(expected = InvalidParameterValueException.class) | |
| public void validateInputsAndPermissionForUpdateVirtualMachineCommandTestVmNotFound() { | |
| Mockito.when(userVmDao.findById(Mockito.eq(vmId))).thenReturn(null); | |
| userVmManagerImpl.validateInputsAndPermissionForUpdateVirtualMachineCommand(updateVmCommand); | |
| } | |
| private ServiceOfferingVO getSvcoffering(int ramSize) { | |
| String name = "name"; | |
| String displayText = "displayText"; | |
| int cpu = 1; | |
| int speed = 128; | |
| boolean ha = false; | |
| boolean useLocalStorage = false; | |
| ServiceOfferingVO serviceOffering = new ServiceOfferingVO(name, cpu, ramSize, speed, null, null, ha, displayText, Storage.ProvisioningType.THIN, useLocalStorage, false, null, false, null, | |
| false); | |
| return serviceOffering; | |
| } | |
| @Test | |
| @PrepareForTest(CallContext.class) | |
| public void validateInputsAndPermissionForUpdateVirtualMachineCommandTest() { | |
| Mockito.doNothing().when(userVmManagerImpl).validateGuestOsIdForUpdateVirtualMachineCommand(updateVmCommand); | |
| CallContext callContextMock = Mockito.mock(CallContext.class); | |
| Mockito.lenient().doReturn(accountMock).when(callContextMock).getCallingAccount(); | |
| ServiceOffering offering = getSvcoffering(512); | |
| Mockito.lenient().when(_serviceOfferingDao.findById(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering); | |
| Mockito.lenient().doNothing().when(accountManager).checkAccess(accountMock, null, true, userVmVoMock); | |
| userVmManagerImpl.validateInputsAndPermissionForUpdateVirtualMachineCommand(updateVmCommand); | |
| Mockito.verify(userVmManagerImpl).validateGuestOsIdForUpdateVirtualMachineCommand(updateVmCommand); | |
| Mockito.verify(accountManager).checkAccess(callerAccount, null, true, userVmVoMock); | |
| } | |
| @Test | |
| public void updateVirtualMachineTestDisplayChanged() throws ResourceUnavailableException, InsufficientCapacityException { | |
| configureDoNothingForMethodsThatWeDoNotWantToTest(); | |
| ServiceOffering offering = getSvcoffering(512); | |
| Mockito.when(_serviceOfferingDao.findById(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering); | |
| Mockito.when(userVmVoMock.isDisplay()).thenReturn(true); | |
| Mockito.doNothing().when(userVmManagerImpl).updateDisplayVmFlag(false, vmId, userVmVoMock); | |
| userVmManagerImpl.updateVirtualMachine(updateVmCommand); | |
| verifyMethodsThatAreAlwaysExecuted(); | |
| Mockito.verify(userVmManagerImpl).updateDisplayVmFlag(false, vmId, userVmVoMock); | |
| Mockito.verify(userVmDetailVO, Mockito.times(0)).removeDetails(vmId); | |
| } | |
| @Test | |
| public void updateVirtualMachineTestCleanUpTrue() throws ResourceUnavailableException, InsufficientCapacityException { | |
| configureDoNothingForMethodsThatWeDoNotWantToTest(); | |
| ServiceOffering offering = getSvcoffering(512); | |
| Mockito.when(_serviceOfferingDao.findById(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering); | |
| Mockito.when(updateVmCommand.isCleanupDetails()).thenReturn(true); | |
| Mockito.lenient().doNothing().when(userVmManagerImpl).updateDisplayVmFlag(false, vmId, userVmVoMock); | |
| Mockito.doNothing().when(userVmDetailVO).removeDetails(vmId); | |
| userVmManagerImpl.updateVirtualMachine(updateVmCommand); | |
| verifyMethodsThatAreAlwaysExecuted(); | |
| Mockito.verify(userVmDetailVO).removeDetails(vmId); | |
| Mockito.verify(userVmManagerImpl, Mockito.times(0)).updateDisplayVmFlag(false, vmId, userVmVoMock); | |
| } | |
| @Test | |
| public void updateVirtualMachineTestCleanUpTrueAndDetailEmpty() throws ResourceUnavailableException, InsufficientCapacityException { | |
| prepareAndExecuteMethodDealingWithDetails(true, true); | |
| } | |
| @Test | |
| public void updateVirtualMachineTestCleanUpTrueAndDetailsNotEmpty() throws ResourceUnavailableException, InsufficientCapacityException { | |
| prepareAndExecuteMethodDealingWithDetails(true, false); | |
| } | |
| @Test | |
| public void updateVirtualMachineTestCleanUpFalseAndDetailsNotEmpty() throws ResourceUnavailableException, InsufficientCapacityException { | |
| prepareAndExecuteMethodDealingWithDetails(false, true); | |
| } | |
| @Test | |
| public void updateVirtualMachineTestCleanUpFalseAndDetailsEmpty() throws ResourceUnavailableException, InsufficientCapacityException { | |
| Mockito.when(accountDao.findById(Mockito.anyLong())).thenReturn(callerAccount); | |
| prepareAndExecuteMethodDealingWithDetails(false, false); | |
| } | |
| private void prepareAndExecuteMethodDealingWithDetails(boolean cleanUpDetails, boolean isDetailsEmpty) throws ResourceUnavailableException, InsufficientCapacityException { | |
| configureDoNothingForMethodsThatWeDoNotWantToTest(); | |
| ServiceOffering offering = getSvcoffering(512); | |
| Mockito.when(_serviceOfferingDao.findById(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering); | |
| Mockito.when(_serviceOfferingDao.findByIdIncludingRemoved(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering); | |
| ServiceOfferingVO currentServiceOffering = Mockito.mock(ServiceOfferingVO.class); | |
| Mockito.lenient().when(currentServiceOffering.getCpu()).thenReturn(1); | |
| Mockito.lenient().when(currentServiceOffering.getRamSize()).thenReturn(512); | |
| List<NicVO> nics = new ArrayList<>(); | |
| NicVO nic1 = mock(NicVO.class); | |
| NicVO nic2 = mock(NicVO.class); | |
| nics.add(nic1); | |
| nics.add(nic2); | |
| when(this.nicDao.listByVmId(Mockito.anyLong())).thenReturn(nics); | |
| when(_networkDao.findById(anyLong())).thenReturn(_networkMock); | |
| lenient().doNothing().when(_networkMgr).saveExtraDhcpOptions(anyString(), anyLong(), anyMap()); | |
| HashMap<String, String> details = new HashMap<>(); | |
| if(!isDetailsEmpty) { | |
| details.put("", ""); | |
| } | |
| Mockito.when(updateVmCommand.getDetails()).thenReturn(details); | |
| Mockito.when(updateVmCommand.isCleanupDetails()).thenReturn(cleanUpDetails); | |
| configureDoNothingForDetailsMethod(); | |
| userVmManagerImpl.updateVirtualMachine(updateVmCommand); | |
| verifyMethodsThatAreAlwaysExecuted(); | |
| Mockito.verify(userVmVoMock, Mockito.times(cleanUpDetails || isDetailsEmpty ? 0 : 1)).setDetails(details); | |
| Mockito.verify(userVmDetailVO, Mockito.times(cleanUpDetails ? 1: 0)).removeDetails(vmId); | |
| Mockito.verify(userVmDao, Mockito.times(cleanUpDetails || isDetailsEmpty ? 0 : 1)).saveDetails(userVmVoMock); | |
| Mockito.verify(userVmManagerImpl, Mockito.times(0)).updateDisplayVmFlag(false, vmId, userVmVoMock); | |
| } | |
| private void configureDoNothingForDetailsMethod() { | |
| Mockito.lenient().doNothing().when(userVmManagerImpl).updateDisplayVmFlag(false, vmId, userVmVoMock); | |
| Mockito.doNothing().when(userVmDetailVO).removeDetails(vmId); | |
| Mockito.doNothing().when(userVmDao).saveDetails(userVmVoMock); | |
| } | |
| @SuppressWarnings("unchecked") | |
| private void verifyMethodsThatAreAlwaysExecuted() throws ResourceUnavailableException, InsufficientCapacityException { | |
| Mockito.verify(userVmManagerImpl).validateInputsAndPermissionForUpdateVirtualMachineCommand(updateVmCommand); | |
| Mockito.verify(userVmManagerImpl).getSecurityGroupIdList(updateVmCommand); | |
| Mockito.verify(userVmManagerImpl).updateVirtualMachine(nullable(Long.class), nullable(String.class), nullable(String.class), nullable(Boolean.class), | |
| nullable(Boolean.class), nullable(Long.class), | |
| nullable(String.class), nullable(Boolean.class), nullable(HTTPMethod.class), nullable(String.class), nullable(String.class), nullable(String.class), nullable(List.class), | |
| nullable(Map.class)); | |
| } | |
| @SuppressWarnings("unchecked") | |
| private void configureDoNothingForMethodsThatWeDoNotWantToTest() throws ResourceUnavailableException, InsufficientCapacityException { | |
| Mockito.doNothing().when(userVmManagerImpl).validateInputsAndPermissionForUpdateVirtualMachineCommand(updateVmCommand); | |
| Mockito.doReturn(new ArrayList<Long>()).when(userVmManagerImpl).getSecurityGroupIdList(updateVmCommand); | |
| Mockito.lenient().doReturn(Mockito.mock(UserVm.class)).when(userVmManagerImpl).updateVirtualMachine(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), | |
| Mockito.anyBoolean(), Mockito.anyLong(), | |
| Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(HTTPMethod.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyListOf(Long.class), | |
| Mockito.anyMap()); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressValid() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(0, "01:23:45:67:89:ab", "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressNull() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, null, "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressBlank() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, " ", "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressEmpty() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, "", "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressNotValidOption1() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, "abcdef:gh:ij:kl", "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressNotValidOption2() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, "01:23:45:67:89:", "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressNotValidOption3() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, "01:23:45:67:89:az", "01:23:45:67:89:ab"); | |
| } | |
| @Test | |
| public void validateOrReplaceMacAddressTestMacAddressNotValidOption4() throws InsufficientAddressCapacityException { | |
| configureValidateOrReplaceMacAddressTest(1, "@1:23:45:67:89:ab", "01:23:45:67:89:ab"); | |
| } | |
| private void configureValidateOrReplaceMacAddressTest(int times, String macAddress, String expectedMacAddress) throws InsufficientAddressCapacityException { | |
| Mockito.when(networkModel.getNextAvailableMacAddressInNetwork(Mockito.anyLong())).thenReturn(expectedMacAddress); | |
| String returnedMacAddress = userVmManagerImpl.validateOrReplaceMacAddress(macAddress, 1l); | |
| Mockito.verify(networkModel, Mockito.times(times)).getNextAvailableMacAddressInNetwork(Mockito.anyLong()); | |
| assertEquals(expectedMacAddress, returnedMacAddress); | |
| } | |
| @Test | |
| public void testValidatekeyValuePair() throws Exception { | |
| assertTrue(userVmManagerImpl.isValidKeyValuePair("is-a-template=true\nHVM-boot-policy=\nPV-bootloader=pygrub\nPV-args=hvc0")); | |
| assertTrue(userVmManagerImpl.isValidKeyValuePair("is-a-template=true HVM-boot-policy= PV-bootloader=pygrub PV-args=hvc0")); | |
| assertTrue(userVmManagerImpl.isValidKeyValuePair("nvp.vm-uuid=34b3d5ea-1c25-4bb0-9250-8dc3388bfa9b")); | |
| assertFalse(userVmManagerImpl.isValidKeyValuePair("key")); | |
| //key-1=value1, param:key-2=value2, my.config.v0=False" | |
| assertTrue(userVmManagerImpl.isValidKeyValuePair("key-1=value1")); | |
| assertTrue(userVmManagerImpl.isValidKeyValuePair("param:key-2=value2")); | |
| assertTrue(userVmManagerImpl.isValidKeyValuePair("my.config.v0=False")); | |
| } | |
| @Test | |
| public void configureCustomRootDiskSizeTest() { | |
| String vmDetailsRootDiskSize = "123"; | |
| Map<String, String> customParameters = new HashMap<>(); | |
| customParameters.put(VmDetailConstants.ROOT_DISK_SIZE, vmDetailsRootDiskSize); | |
| long expectedRootDiskSize = 123l * GiB_TO_BYTES; | |
| long offeringRootDiskSize = 0l; | |
| prepareAndRunConfigureCustomRootDiskSizeTest(customParameters, expectedRootDiskSize, 1, offeringRootDiskSize); | |
| } | |
| @Test(expected = InvalidParameterValueException.class) | |
| public void configureCustomRootDiskSizeTestExpectExceptionZero() { | |
| String vmDetailsRootDiskSize = "0"; | |
| Map<String, String> customParameters = new HashMap<>(); | |
| customParameters.put(VmDetailConstants.ROOT_DISK_SIZE, vmDetailsRootDiskSize); | |
| long expectedRootDiskSize = 0l; | |
| long offeringRootDiskSize = 0l; | |
| prepareAndRunConfigureCustomRootDiskSizeTest(customParameters, expectedRootDiskSize, 1, offeringRootDiskSize); | |
| } | |
| @Test(expected = InvalidParameterValueException.class) | |
| public void configureCustomRootDiskSizeTestExpectExceptionNegativeNum() { | |
| String vmDetailsRootDiskSize = "-123"; | |
| Map<String, String> customParameters = new HashMap<>(); | |
| customParameters.put(VmDetailConstants.ROOT_DISK_SIZE, vmDetailsRootDiskSize); | |
| long expectedRootDiskSize = -123l * GiB_TO_BYTES; | |
| long offeringRootDiskSize = 0l; | |
| prepareAndRunConfigureCustomRootDiskSizeTest(customParameters, expectedRootDiskSize, 1, offeringRootDiskSize); | |
| } | |
| @Test | |
| public void configureCustomRootDiskSizeTestEmptyParameters() { | |
| Map<String, String> customParameters = new HashMap<>(); | |
| long expectedRootDiskSize = 99l * GiB_TO_BYTES; | |
| long offeringRootDiskSize = 0l; | |
| prepareAndRunConfigureCustomRootDiskSizeTest(customParameters, expectedRootDiskSize, 1, offeringRootDiskSize); | |
| } | |
| @Test | |
| public void configureCustomRootDiskSizeTestEmptyParametersAndOfferingRootSize() { | |
| Map<String, String> customParameters = new HashMap<>(); | |
| long expectedRootDiskSize = 10l * GiB_TO_BYTES; | |
| long offeringRootDiskSize = 10l * GiB_TO_BYTES;; | |
| prepareAndRunConfigureCustomRootDiskSizeTest(customParameters, expectedRootDiskSize, 1, offeringRootDiskSize); | |
| } | |
| private void prepareAndRunConfigureCustomRootDiskSizeTest(Map<String, String> customParameters, long expectedRootDiskSize, int timesVerifyIfHypervisorSupports, Long offeringRootDiskSize) { | |
| VMTemplateVO template = Mockito.mock(VMTemplateVO.class); | |
| Mockito.when(template.getId()).thenReturn(1l); | |
| Mockito.when(template.getSize()).thenReturn(99L * GiB_TO_BYTES); | |
| ServiceOfferingVO offering = Mockito.mock(ServiceOfferingVO.class); | |
| Mockito.when(offering.getId()).thenReturn(1l); | |
| Mockito.when(templateDao.findById(Mockito.anyLong())).thenReturn(template); | |
| DiskOfferingVO diskfferingVo = Mockito.mock(DiskOfferingVO.class); | |
| Mockito.when(diskOfferingDao.findById(Mockito.anyLong())).thenReturn(diskfferingVo); | |
| Mockito.when(diskfferingVo.getDiskSize()).thenReturn(offeringRootDiskSize); | |
| long rootDiskSize = userVmManagerImpl.configureCustomRootDiskSize(customParameters, template, Hypervisor.HypervisorType.KVM, offering); | |
| Assert.assertEquals(expectedRootDiskSize, rootDiskSize); | |
| Mockito.verify(userVmManagerImpl, Mockito.times(timesVerifyIfHypervisorSupports)).verifyIfHypervisorSupportsRootdiskSizeOverride(Mockito.any()); | |
| } | |
| @Test | |
| public void verifyIfHypervisorSupportRootdiskSizeOverrideTest() { | |
| Hypervisor.HypervisorType[] hypervisorTypeArray = Hypervisor.HypervisorType.values(); | |
| int exceptionCounter = 0; | |
| int expectedExceptionCounter = hypervisorTypeArray.length - 4; | |
| for(int i = 0; i < hypervisorTypeArray.length; i++) { | |
| if (Hypervisor.HypervisorType.KVM == hypervisorTypeArray[i] | |
| || Hypervisor.HypervisorType.XenServer == hypervisorTypeArray[i] | |
| || Hypervisor.HypervisorType.VMware == hypervisorTypeArray[i] | |
| || Hypervisor.HypervisorType.Simulator == hypervisorTypeArray[i]) { | |
| userVmManagerImpl.verifyIfHypervisorSupportsRootdiskSizeOverride(hypervisorTypeArray[i]); | |
| } else { | |
| try { | |
| userVmManagerImpl.verifyIfHypervisorSupportsRootdiskSizeOverride(hypervisorTypeArray[i]); | |
| } catch (InvalidParameterValueException e) { | |
| exceptionCounter ++; | |
| } | |
| } | |
| } | |
| Assert.assertEquals(expectedExceptionCounter, exceptionCounter); | |
| } | |
| @Test (expected = InvalidParameterValueException.class) | |
| public void prepareResizeVolumeCmdTestRootVolumeNull() { | |
| DiskOfferingVO newRootDiskOffering = Mockito.mock(DiskOfferingVO.class); | |
| DiskOfferingVO currentRootDiskOffering = Mockito.mock(DiskOfferingVO.class); | |
| userVmManagerImpl.prepareResizeVolumeCmd(null, currentRootDiskOffering, newRootDiskOffering); | |
| } | |
| @Test (expected = InvalidParameterValueException.class) | |
| public void prepareResizeVolumeCmdTestCurrentRootDiskOffering() { | |
| DiskOfferingVO newRootDiskOffering = Mockito.mock(DiskOfferingVO.class); | |
| VolumeVO rootVolumeOfVm = Mockito.mock(VolumeVO.class); | |
| userVmManagerImpl.prepareResizeVolumeCmd(rootVolumeOfVm, null, newRootDiskOffering); | |
| } | |
| @Test (expected = InvalidParameterValueException.class) | |
| public void prepareResizeVolumeCmdTestNewRootDiskOffering() { | |
| VolumeVO rootVolumeOfVm = Mockito.mock(VolumeVO.class); | |
| DiskOfferingVO currentRootDiskOffering = Mockito.mock(DiskOfferingVO.class); | |
| userVmManagerImpl.prepareResizeVolumeCmd(rootVolumeOfVm, currentRootDiskOffering, null); | |
| } | |
| @Test | |
| public void prepareResizeVolumeCmdTestNewOfferingLarger() { | |
| prepareAndRunResizeVolumeTest(2L, 10L, 20L, smallerDisdkOffering, largerDisdkOffering); | |
| } | |
| @Test | |
| public void prepareResizeVolumeCmdTestSameOfferingSize() { | |
| prepareAndRunResizeVolumeTest(null, 1L, 2L, smallerDisdkOffering, smallerDisdkOffering); | |
| } | |
| @Test | |
| public void prepareResizeVolumeCmdTestOfferingRootSizeZero() { | |
| DiskOfferingVO rootSizeZero = prepareDiskOffering(0l, 3l, 100L, 200L); | |
| prepareAndRunResizeVolumeTest(null, 100L, 200L, smallerDisdkOffering, rootSizeZero); | |
| } | |
| @Test (expected = InvalidParameterValueException.class) | |
| public void prepareResizeVolumeCmdTestNewOfferingSmaller() { | |
| prepareAndRunResizeVolumeTest(2L, 10L, 20L, largerDisdkOffering, smallerDisdkOffering); | |
| } | |
| private void prepareAndRunResizeVolumeTest(Long expectedOfferingId, long expectedMinIops, long expectedMaxIops, DiskOfferingVO currentRootDiskOffering, DiskOfferingVO newRootDiskOffering) { | |
| long rootVolumeId = 1l; | |
| VolumeVO rootVolumeOfVm = Mockito.mock(VolumeVO.class); | |
| Mockito.when(rootVolumeOfVm.getId()).thenReturn(rootVolumeId); | |
| ResizeVolumeCmd resizeVolumeCmd = userVmManagerImpl.prepareResizeVolumeCmd(rootVolumeOfVm, currentRootDiskOffering, newRootDiskOffering); | |
| Assert.assertEquals(rootVolumeId, resizeVolumeCmd.getId().longValue()); | |
| Assert.assertEquals(expectedOfferingId, resizeVolumeCmd.getNewDiskOfferingId()); | |
| Assert.assertEquals(expectedMinIops, resizeVolumeCmd.getMinIops().longValue()); | |
| Assert.assertEquals(expectedMaxIops, resizeVolumeCmd.getMaxIops().longValue()); | |
| } | |
| private DiskOfferingVO prepareDiskOffering(long rootSize, long diskOfferingId, long offeringMinIops, long offeringMaxIops) { | |
| DiskOfferingVO newRootDiskOffering = Mockito.mock(DiskOfferingVO.class); | |
| Mockito.when(newRootDiskOffering.getDiskSize()).thenReturn(rootSize); | |
| Mockito.when(newRootDiskOffering.getId()).thenReturn(diskOfferingId); | |
| Mockito.when(newRootDiskOffering.getMinIops()).thenReturn(offeringMinIops); | |
| Mockito.when(newRootDiskOffering.getMaxIops()).thenReturn(offeringMaxIops); | |
| Mockito.when(newRootDiskOffering.getName()).thenReturn("OfferingName"); | |
| return newRootDiskOffering; | |
| } | |
| } |