|
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; |
37 | 38 | import java.util.List; |
38 | 39 | import java.util.Map; |
39 | 40 | import java.util.UUID; |
40 | 41 |
|
| 42 | +import com.cloud.kubernetes.cluster.KubernetesServiceHelper; |
41 | 43 | import com.cloud.offering.DiskOffering; |
| 44 | +import com.cloud.storage.Snapshot; |
| 45 | +import com.cloud.storage.SnapshotVO; |
| 46 | +import com.cloud.storage.dao.SnapshotDao; |
42 | 47 | import com.cloud.vm.ImportVMTaskVO; |
| 48 | +import org.apache.cloudstack.acl.ControlledEntity; |
43 | 49 | import org.apache.cloudstack.api.ApiConstants; |
44 | 50 | import org.apache.cloudstack.api.ResponseGenerator; |
45 | 51 | import org.apache.cloudstack.api.ResponseObject; |
@@ -241,6 +247,10 @@ public class UnmanagedVMsManagerImplTest { |
241 | 247 | private StoragePoolHostDao storagePoolHostDao; |
242 | 248 | @Mock |
243 | 249 | private ImportVmTasksManager importVmTasksManager; |
| 250 | + @Mock |
| 251 | + private KubernetesServiceHelper kubernetesServiceHelper; |
| 252 | + @Mock |
| 253 | + private SnapshotDao snapshotDao; |
244 | 254 |
|
245 | 255 | @Mock |
246 | 256 | private VMInstanceVO virtualMachine; |
@@ -568,6 +578,53 @@ public void unmanageVMInstanceStoppedInstanceTest() { |
568 | 578 | unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
569 | 579 | } |
570 | 580 |
|
| 581 | + @Test(expected = UnsupportedServiceException.class) |
| 582 | + public void testUnmanageVMInstanceWithVolumeSnapshotsFail() { |
| 583 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 584 | + when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 585 | + when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped); |
| 586 | + when(virtualMachine.getId()).thenReturn(virtualMachineId); |
| 587 | + UserVmVO userVmVO = mock(UserVmVO.class); |
| 588 | + when(userVmDao.findById(anyLong())).thenReturn(userVmVO); |
| 589 | + when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine); |
| 590 | + VolumeVO volumeVO = mock(VolumeVO.class); |
| 591 | + long volumeId = 20L; |
| 592 | + when(volumeVO.getId()).thenReturn(volumeId); |
| 593 | + SnapshotVO snapshotVO = mock(SnapshotVO.class); |
| 594 | + when(snapshotVO.getState()).thenReturn(Snapshot.State.BackedUp); |
| 595 | + when(snapshotDao.listByVolumeId(volumeId)).thenReturn(Collections.singletonList(snapshotVO)); |
| 596 | + when(volumeDao.findByInstance(virtualMachineId)).thenReturn(Collections.singletonList(volumeVO)); |
| 597 | + unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
| 598 | + } |
| 599 | + |
| 600 | + @Test(expected = UnsupportedServiceException.class) |
| 601 | + public void testUnmanageVMInstanceWithAssociatedIsoFail() { |
| 602 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 603 | + when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 604 | + when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped); |
| 605 | + when(virtualMachine.getId()).thenReturn(virtualMachineId); |
| 606 | + UserVmVO userVmVO = mock(UserVmVO.class); |
| 607 | + when(userVmVO.getIsoId()).thenReturn(null); |
| 608 | + when(userVmDao.findById(anyLong())).thenReturn(userVmVO); |
| 609 | + when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine); |
| 610 | + when(userVmVO.getIsoId()).thenReturn(1L); |
| 611 | + unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
| 612 | + } |
| 613 | + |
| 614 | + @Test(expected = UnsupportedServiceException.class) |
| 615 | + public void testUnmanageVMInstanceBelongingToCksClusterFail() { |
| 616 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 617 | + when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 618 | + when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped); |
| 619 | + when(virtualMachine.getId()).thenReturn(virtualMachineId); |
| 620 | + UserVmVO userVmVO = mock(UserVmVO.class); |
| 621 | + when(userVmVO.getIsoId()).thenReturn(null); |
| 622 | + when(userVmDao.findById(anyLong())).thenReturn(userVmVO); |
| 623 | + when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine); |
| 624 | + when(kubernetesServiceHelper.findByVmId(virtualMachineId)).thenReturn(mock(ControlledEntity.class)); |
| 625 | + unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false); |
| 626 | + } |
| 627 | + |
571 | 628 | @Test |
572 | 629 | public void testListRemoteInstancesTest() { |
573 | 630 | ListVmsForImportCmd cmd = Mockito.mock(ListVmsForImportCmd.class); |
|
0 commit comments