|
59 | 59 | import java.util.TimeZone; |
60 | 60 | import java.util.UUID; |
61 | 61 |
|
| 62 | +import com.cloud.event.dao.UsageEventDao; |
| 63 | +import com.cloud.host.Status; |
62 | 64 | import org.apache.cloudstack.acl.ControlledEntity; |
63 | 65 | import org.apache.cloudstack.acl.SecurityChecker; |
64 | 66 | import org.apache.cloudstack.api.ApiCommandResourceType; |
65 | 67 | import org.apache.cloudstack.api.ApiConstants; |
66 | | -import com.cloud.host.Status; |
67 | 68 | import org.apache.cloudstack.api.BaseCmd; |
68 | 69 | import org.apache.cloudstack.api.BaseCmd.HTTPMethod; |
69 | 70 | import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd; |
|
83 | 84 | import org.apache.cloudstack.backup.dao.BackupScheduleDao; |
84 | 85 | import org.apache.cloudstack.context.CallContext; |
85 | 86 | import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; |
| 87 | +import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService; |
86 | 88 | import org.apache.cloudstack.resourcelimit.Reserver; |
87 | 89 | import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore; |
88 | 90 | import org.apache.cloudstack.engine.subsystem.api.storage.Scope; |
89 | 91 | import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory; |
90 | 92 | import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
91 | 93 | import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; |
92 | 94 | 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; |
95 | 95 | import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; |
96 | 96 | import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO; |
| 97 | +import org.apache.cloudstack.storage.template.VnfTemplateManager; |
97 | 98 | import org.apache.cloudstack.userdata.UserDataManager; |
98 | 99 | import org.apache.cloudstack.vm.UnmanagedVMsManager; |
99 | 100 | import org.apache.cloudstack.vm.lease.VMLeaseManager; |
@@ -430,15 +431,15 @@ public class UserVmManagerImplTest { |
430 | 431 | @Mock |
431 | 432 | private VolumeDataFactory volumeDataFactory; |
432 | 433 |
|
433 | | - @Mock |
434 | | - private VolumeDataFactory volFactory; |
435 | | - |
436 | 434 | @Mock |
437 | 435 | private VolumeOrchestrationService volumeMgr; |
438 | 436 |
|
439 | 437 | @Mock |
440 | 438 | private TemplateDataStoreDao templateDataStoreDao; |
441 | 439 |
|
| 440 | + @Mock |
| 441 | + private UsageEventDao usageEventDao; |
| 442 | + |
442 | 443 | @Mock |
443 | 444 | private VolumeInfo volumeInfo; |
444 | 445 |
|
@@ -1686,93 +1687,79 @@ public void testRestoreVirtualMachineWhenHostRemoved() throws ResourceUnavailabl |
1686 | 1687 | boolean expunge = false; |
1687 | 1688 | Map<String, String> details = new HashMap<>(); |
1688 | 1689 |
|
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); |
1706 | 1702 |
|
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); |
1718 | 1705 |
|
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); |
1721 | 1708 |
|
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); |
1738 | 1746 |
|
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); |
1745 | 1751 |
|
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); |
1769 | 1756 |
|
1770 | 1757 | UserVm result = userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId, null, expunge, details); |
1771 | 1758 | assertNotNull(result); |
1772 | | - } |
1773 | 1759 |
|
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 | + } |
1776 | 1763 | } |
1777 | 1764 |
|
1778 | 1765 | @Test |
|
0 commit comments