|
31 | 31 |
|
32 | 32 | import java.net.URI; |
33 | 33 | import java.util.ArrayList; |
| 34 | +import java.util.Collections; |
34 | 35 | import java.util.Date; |
35 | 36 | import java.util.HashMap; |
36 | 37 | import java.util.LinkedHashMap; |
|
39 | 40 | import java.util.UUID; |
40 | 41 |
|
41 | 42 | import com.cloud.offering.DiskOffering; |
| 43 | +import com.cloud.storage.Snapshot; |
| 44 | +import com.cloud.storage.SnapshotVO; |
| 45 | +import com.cloud.storage.dao.SnapshotDao; |
42 | 46 | import com.cloud.vm.ImportVMTaskVO; |
43 | 47 | import org.apache.cloudstack.api.ApiConstants; |
44 | 48 | import org.apache.cloudstack.api.ResponseGenerator; |
@@ -241,6 +245,8 @@ public class UnmanagedVMsManagerImplTest { |
241 | 245 | private StoragePoolHostDao storagePoolHostDao; |
242 | 246 | @Mock |
243 | 247 | private ImportVmTasksManager importVmTasksManager; |
| 248 | + @Mock |
| 249 | + private SnapshotDao snapshotDao; |
244 | 250 |
|
245 | 251 | @Mock |
246 | 252 | private VMInstanceVO virtualMachine; |
@@ -568,6 +574,53 @@ public void unmanageVMInstanceStoppedInstanceTest() { |
568 | 574 | unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
569 | 575 | } |
570 | 576 |
|
| 577 | + @Test(expected = UnsupportedServiceException.class) |
| 578 | + public void testUnmanageVMInstanceWithVolumeSnapshotsFail() { |
| 579 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 580 | + when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 581 | + when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped); |
| 582 | + when(virtualMachine.getId()).thenReturn(virtualMachineId); |
| 583 | + UserVmVO userVmVO = mock(UserVmVO.class); |
| 584 | + when(userVmDao.findById(anyLong())).thenReturn(userVmVO); |
| 585 | + when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine); |
| 586 | + VolumeVO volumeVO = mock(VolumeVO.class); |
| 587 | + long volumeId = 20L; |
| 588 | + when(volumeVO.getId()).thenReturn(volumeId); |
| 589 | + SnapshotVO snapshotVO = mock(SnapshotVO.class); |
| 590 | + when(snapshotVO.getState()).thenReturn(Snapshot.State.BackedUp); |
| 591 | + when(snapshotDao.listByVolumeId(volumeId)).thenReturn(Collections.singletonList(snapshotVO)); |
| 592 | + when(volumeDao.findByInstance(virtualMachineId)).thenReturn(Collections.singletonList(volumeVO)); |
| 593 | + unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
| 594 | + } |
| 595 | + |
| 596 | + @Test(expected = UnsupportedServiceException.class) |
| 597 | + public void testUnmanageVMInstanceWithAssociatedIsoFail() { |
| 598 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 599 | + when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 600 | + when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped); |
| 601 | + when(virtualMachine.getId()).thenReturn(virtualMachineId); |
| 602 | + UserVmVO userVmVO = mock(UserVmVO.class); |
| 603 | + when(userVmVO.getIsoId()).thenReturn(null); |
| 604 | + when(userVmDao.findById(anyLong())).thenReturn(userVmVO); |
| 605 | + when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine); |
| 606 | + when(userVmVO.getIsoId()).thenReturn(1L); |
| 607 | + unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
| 608 | + } |
| 609 | + |
| 610 | + @Test(expected = UnsupportedServiceException.class) |
| 611 | + public void testUnmanageVMInstanceBelongingToCksClusterFail() { |
| 612 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 613 | + when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 614 | + when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped); |
| 615 | + when(virtualMachine.getId()).thenReturn(virtualMachineId); |
| 616 | + UserVmVO userVmVO = mock(UserVmVO.class); |
| 617 | + when(userVmVO.getIsoId()).thenReturn(null); |
| 618 | + when(userVmDao.findById(anyLong())).thenReturn(userVmVO); |
| 619 | + when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine); |
| 620 | + when(userVmManager.isVMPartOfAnyCKSCluster(virtualMachine)).thenReturn(true); |
| 621 | + unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
| 622 | + } |
| 623 | + |
571 | 624 | @Test |
572 | 625 | public void testListRemoteInstancesTest() { |
573 | 626 | ListVmsForImportCmd cmd = Mockito.mock(ListVmsForImportCmd.class); |
|
0 commit comments