Skip to content

Commit 3beb680

Browse files
committed
Fix unit test
1 parent d7cd7ef commit 3beb680

1 file changed

Lines changed: 71 additions & 84 deletions

File tree

server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java

Lines changed: 71 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@
5959
import java.util.TimeZone;
6060
import java.util.UUID;
6161

62+
import com.cloud.event.dao.UsageEventDao;
63+
import com.cloud.host.Status;
6264
import org.apache.cloudstack.acl.ControlledEntity;
6365
import org.apache.cloudstack.acl.SecurityChecker;
6466
import org.apache.cloudstack.api.ApiCommandResourceType;
6567
import org.apache.cloudstack.api.ApiConstants;
66-
import com.cloud.host.Status;
6768
import org.apache.cloudstack.api.BaseCmd;
6869
import org.apache.cloudstack.api.BaseCmd.HTTPMethod;
6970
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
@@ -83,17 +84,17 @@
8384
import org.apache.cloudstack.backup.dao.BackupScheduleDao;
8485
import org.apache.cloudstack.context.CallContext;
8586
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
87+
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
8688
import org.apache.cloudstack.resourcelimit.Reserver;
8789
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
8890
import org.apache.cloudstack.engine.subsystem.api.storage.Scope;
8991
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
9092
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
9193
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
9294
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
93-
import org.apache.cloudstack.storage.template.VnfTemplateManager;
94-
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
9595
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
9696
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
97+
import org.apache.cloudstack.storage.template.VnfTemplateManager;
9798
import org.apache.cloudstack.userdata.UserDataManager;
9899
import org.apache.cloudstack.vm.UnmanagedVMsManager;
99100
import org.apache.cloudstack.vm.lease.VMLeaseManager;
@@ -430,15 +431,15 @@ public class UserVmManagerImplTest {
430431
@Mock
431432
private VolumeDataFactory volumeDataFactory;
432433

433-
@Mock
434-
private VolumeDataFactory volFactory;
435-
436434
@Mock
437435
private VolumeOrchestrationService volumeMgr;
438436

439437
@Mock
440438
private TemplateDataStoreDao templateDataStoreDao;
441439

440+
@Mock
441+
private UsageEventDao usageEventDao;
442+
442443
@Mock
443444
private VolumeInfo volumeInfo;
444445

@@ -1686,93 +1687,79 @@ public void testRestoreVirtualMachineWhenHostRemoved() throws ResourceUnavailabl
16861687
boolean expunge = false;
16871688
Map<String, String> details = new HashMap<>();
16881689

1689-
UserVmVO vm = mock(UserVmVO.class);
1690-
when(vm.getId()).thenReturn(vmId);
1691-
when(vm.getAccountId()).thenReturn(accountId);
1692-
when(vm.getHostId()).thenReturn(null);
1693-
when(vm.getLastHostId()).thenReturn(lastHostId);
1694-
when(vm.getUuid()).thenReturn("test-uuid");
1695-
when(vm.getState()).thenReturn(VirtualMachine.State.Stopped);
1696-
when(vm.getTemplateId()).thenReturn(1L);
1697-
when(vm.getDataCenterId()).thenReturn(1L);
1698-
when(vm.isDisplay()).thenReturn(true);
1699-
1700-
CallContext mockCallContext = mock(CallContext.class);
1701-
when(mockCallContext.getCallingAccount()).thenReturn(accountMock);
1702-
when(mockCallContext.getCallingUserId()).thenReturn(1L);
1703-
1704-
CallContext mockVolumeContext = mock(CallContext.class);
1705-
when(CallContext.register(any(CallContext.class), any(ApiCommandResourceType.class))).thenReturn(mockVolumeContext);
1690+
try (MockedStatic<CallContext> ignored = Mockito.mockStatic(CallContext.class);
1691+
MockedStatic<UsageEventUtils> ignoredEventUtils = mockStatic(UsageEventUtils.class)
1692+
) {
1693+
UserVmVO vm = mock(UserVmVO.class);
1694+
when(vm.getId()).thenReturn(vmId);
1695+
when(vm.getAccountId()).thenReturn(accountId);
1696+
when(vm.getHostId()).thenReturn(null);
1697+
when(vm.getLastHostId()).thenReturn(lastHostId);
1698+
when(vm.getUuid()).thenReturn("test-uuid");
1699+
when(vm.getState()).thenReturn(VirtualMachine.State.Stopped);
1700+
when(vm.getTemplateId()).thenReturn(1L);
1701+
when(vm.getDataCenterId()).thenReturn(1L);
17061702

1707-
when(accountDao.findById(accountId)).thenReturn(callerAccount);
1708-
when(accountDao.findByIdIncludingRemoved(accountId)).thenReturn(callerAccount);
1709-
when(callerAccount.getState()).thenReturn(Account.State.ENABLED);
1710-
VMTemplateVO template = mock(VMTemplateVO.class);
1711-
when(templateDao.findById(anyLong())).thenReturn(template);
1712-
when(templateDao.findByIdIncludingRemoved(anyLong())).thenReturn(template);
1713-
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
1714-
when(template.getId()).thenReturn(1L);
1715-
when(template.getUuid()).thenReturn("template-uuid");
1716-
when(template.isDirectDownload()).thenReturn(false);
1717-
when(template.getSize()).thenReturn(10L * 1024 * 1024 * 1024L); // 10GB
1703+
CallContext mockCallContext = mock(CallContext.class);
1704+
when(mockCallContext.getCallingAccount()).thenReturn(accountMock);
17181705

1719-
TemplateDataStoreVO templateStore = mock(TemplateDataStoreVO.class);
1720-
when(templateDataStoreDao.findByTemplateZoneReady(1L, 1L)).thenReturn(templateStore);
1706+
CallContext mockVolumeContext = mock(CallContext.class);
1707+
when(CallContext.register(any(CallContext.class), any(ApiCommandResourceType.class))).thenReturn(mockVolumeContext);
17211708

1722-
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
1723-
when(vm.getServiceOfferingId()).thenReturn(serviceOfferingId);
1724-
when(_serviceOfferingDao.findById(vmId, vm.getServiceOfferingId())).thenReturn(serviceOffering);
1725-
1726-
List<VolumeVO> rootVols = new ArrayList<>();
1727-
VolumeVO rootVol = mock(VolumeVO.class);
1728-
when(rootVol.getId()).thenReturn(10L);
1729-
when(rootVol.getState()).thenReturn(Volume.State.Ready);
1730-
when(rootVol.getPoolId()).thenReturn(5L);
1731-
when(rootVol.getTemplateId()).thenReturn(1L);
1732-
when(rootVol.getSize()).thenReturn(20L * 1024 * 1024 * 1024L); // 20GB
1733-
when(rootVol.getDiskOfferingId()).thenReturn(100L);
1734-
when(rootVol.isDisplay()).thenReturn(true);
1735-
rootVols.add(rootVol);
1736-
DiskOfferingVO diskOffering = mock(DiskOfferingVO.class);
1737-
when(diskOfferingDao.findById(100L)).thenReturn(diskOffering);
1709+
when(CallContext.current()).thenReturn(mockCallContext);
1710+
when(accountDao.findById(accountId)).thenReturn(callerAccount);
1711+
when(accountDao.findByIdIncludingRemoved(accountId)).thenReturn(callerAccount);
1712+
when(callerAccount.getState()).thenReturn(Account.State.ENABLED);
1713+
VMTemplateVO template = mock(VMTemplateVO.class);
1714+
when(templateDao.findById(anyLong())).thenReturn(template);
1715+
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
1716+
when(template.getId()).thenReturn(1L);
1717+
when(template.isDirectDownload()).thenReturn(false);
1718+
when(template.getSize()).thenReturn(10L * 1024 * 1024 * 1024L); // 10GB
1719+
1720+
TemplateDataStoreVO templateStore = mock(TemplateDataStoreVO.class);
1721+
when(templateDataStoreDao.findByTemplateZoneReady(1L, 1L)).thenReturn(templateStore);
1722+
1723+
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
1724+
when(vm.getServiceOfferingId()).thenReturn(serviceOfferingId);
1725+
1726+
List<VolumeVO> rootVols = new ArrayList<>();
1727+
VolumeVO rootVol = mock(VolumeVO.class);
1728+
when(rootVol.getId()).thenReturn(10L);
1729+
when(rootVol.getState()).thenReturn(Volume.State.Ready);
1730+
when(rootVol.getPoolId()).thenReturn(5L);
1731+
when(rootVol.getTemplateId()).thenReturn(1L);
1732+
when(rootVol.getSize()).thenReturn(20L * 1024 * 1024 * 1024L); // 20GB
1733+
when(rootVol.getDiskOfferingId()).thenReturn(100L);
1734+
when(rootVol.isDisplay()).thenReturn(true);
1735+
rootVols.add(rootVol);
1736+
DiskOfferingVO diskOffering = mock(DiskOfferingVO.class);
1737+
when(diskOfferingDao.findById(100L)).thenReturn(diskOffering);
1738+
1739+
StoragePoolVO storagePool = mock(StoragePoolVO.class);
1740+
when(storagePool.isManaged()).thenReturn(true);
1741+
when(primaryDataStoreDao.findById(5L)).thenReturn(storagePool);
1742+
when(vmSnapshotDaoMock.findByVm(vmId)).thenReturn(new ArrayList<>());
1743+
when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.ROOT)).thenReturn(rootVols);
1744+
when(volumeDaoMock.findById(anyLong())).thenReturn(rootVol);
1745+
when(userVmDao.findById(vmId)).thenReturn(vm);
17381746

1739-
StoragePoolVO storagePool = mock(StoragePoolVO.class);
1740-
when(storagePool.isManaged()).thenReturn(true);
1741-
when(primaryDataStoreDao.findById(5L)).thenReturn(storagePool);
1742-
when(vmSnapshotDaoMock.findByVm(vmId)).thenReturn(new ArrayList<>());
1743-
when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.ROOT)).thenReturn(rootVols);
1744-
when(userVmDao.findById(vmId)).thenReturn(vm);
1747+
HostVO host = mock(HostVO.class);
1748+
when(host.getStatus()).thenReturn(Status.Removed);
1749+
when(hostDao.findByIdIncludingRemoved(lastHostId)).thenReturn(host);
1750+
VolumeInfo volumeInfo = mock(VolumeInfo.class);
17451751

1746-
HostVO host = mock(HostVO.class);
1747-
when(host.getStatus()).thenReturn(Status.Removed);
1748-
when(hostDao.findByIdIncludingRemoved(lastHostId)).thenReturn(host);
1749-
VolumeInfo volumeInfo = mock(VolumeInfo.class);
1750-
when(volFactory.getVolume(10L)).thenReturn(volumeInfo);
1751-
doNothing().when(resourceLimitMgr).checkVmResourceLimitsForTemplateChange(
1752-
any(Account.class), Mockito.anyBoolean(), any(ServiceOffering.class),
1753-
any(VMTemplateVO.class), any(VMTemplateVO.class), any(List.class));
1754-
doNothing().when(resourceLimitMgr).checkVolumeResourceLimitForDiskOfferingChange(
1755-
any(Account.class), Mockito.anyBoolean(), anyLong(), anyLong(),
1756-
any(DiskOffering.class), any(DiskOffering.class), any(List.class));
1757-
1758-
VolumeVO newVolume = mock(VolumeVO.class);
1759-
when(volumeMgr.allocateDuplicateVolume(any(VolumeVO.class), any(), anyLong()))
1760-
.thenReturn(newVolume);
1761-
when(newVolume.getId()).thenReturn(11L);
1762-
when(newVolume.getState()).thenReturn(Volume.State.Ready);
1763-
doReturn(20L * 1024 * 1024 * 1024L).when(userVmManagerImpl).getRootVolumeSizeForVmRestore(
1764-
any(Volume.class), any(VMTemplateVO.class), any(UserVmVO.class),
1765-
any(DiskOffering.class), anyMap(), Mockito.anyBoolean());
1766-
1767-
try (MockedStatic<CallContext> ignored = Mockito.mockStatic(CallContext.class)) {
1768-
when(CallContext.current()).thenReturn(mockCallContext);
1752+
VolumeVO newVolume = mock(VolumeVO.class);
1753+
when(volumeMgr.allocateDuplicateVolume(any(VolumeVO.class), any(), anyLong()))
1754+
.thenReturn(newVolume);
1755+
when(newVolume.getId()).thenReturn(11L);
17691756

17701757
UserVm result = userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId, null, expunge, details);
17711758
assertNotNull(result);
1772-
}
17731759

1774-
Mockito.verify(userVmDao).findById(vmId);
1775-
Mockito.verify(hostDao).findByIdIncludingRemoved(lastHostId);
1760+
Mockito.verify(userVmDao).findById(vmId);
1761+
Mockito.verify(hostDao).findByIdIncludingRemoved(lastHostId);
1762+
}
17761763
}
17771764

17781765
@Test

0 commit comments

Comments
 (0)