Skip to content

Commit e5b3c9d

Browse files
committed
Add unit tests
1 parent ffb96e8 commit e5b3c9d

File tree

2 files changed

+249
-13
lines changed

2 files changed

+249
-13
lines changed

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ private Map<String, String> createParamsForRemoveClonedInstance(String vcenter,
17881788
return params;
17891789
}
17901790

1791-
private HostVO selectKVMHostForImportingInCluster(Cluster destinationCluster, Long importInstanceHostId) {
1791+
HostVO selectKVMHostForImportingInCluster(Cluster destinationCluster, Long importInstanceHostId) {
17921792
if (importInstanceHostId != null) {
17931793
String err = null;
17941794
HostVO selectedHost = hostDao.findById(importInstanceHostId);
@@ -1797,7 +1797,7 @@ private HostVO selectKVMHostForImportingInCluster(Cluster destinationCluster, Lo
17971797
importInstanceHostId);
17981798
} else if (selectedHost.getResourceState() != ResourceState.Enabled) {
17991799
err = String.format(
1800-
"Cannot import the converted instance on the host %s as it is not a running and Enabled host",
1800+
"Cannot import the converted instance on the host %s as it is not in Enabled state",
18011801
selectedHost.getName());
18021802
} else if (selectedHost.getStatus() != Status.Up) {
18031803
err = String.format(
@@ -1832,7 +1832,7 @@ private HostVO selectKVMHostForImportingInCluster(Cluster destinationCluster, Lo
18321832
throw new CloudRuntimeException(err);
18331833
}
18341834

1835-
private HostVO selectKVMHostForConversionInCluster(Cluster destinationCluster, Long convertInstanceHostId) {
1835+
HostVO selectKVMHostForConversionInCluster(Cluster destinationCluster, Long convertInstanceHostId) {
18361836
if (convertInstanceHostId != null) {
18371837
HostVO selectedHost = hostDao.findById(convertInstanceHostId);
18381838
String err = null;

server/src/test/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImplTest.java

Lines changed: 246 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public void setUp() throws Exception {
372372
doNothing().when(networkModel).checkNetworkPermissions(Mockito.any(Account.class), Mockito.any(Network.class));
373373
NicProfile profile = Mockito.mock(NicProfile.class);
374374
Integer deviceId = 100;
375-
Pair<NicProfile, Integer> pair = new Pair<NicProfile, Integer>(profile, deviceId);
375+
Pair<NicProfile, Integer> pair = new Pair<>(profile, deviceId);
376376
when(networkOrchestrationService.importNic(nullable(String.class), nullable(Integer.class), nullable(Network.class), nullable(Boolean.class), nullable(VirtualMachine.class), nullable(Network.IpAddresses.class), nullable(DataCenter.class), Mockito.anyBoolean())).thenReturn(pair);
377377
when(volumeDao.findByInstance(Mockito.anyLong())).thenReturn(volumes);
378378
List<UserVmResponse> userVmResponses = new ArrayList<>();
@@ -611,6 +611,7 @@ private void baseTestImportVmFromVmwareToKvm(VcenterParameter vcenterParameter,
611611
when(importVmCmd.getHostIp()).thenReturn(host);
612612
when(importVmCmd.getNicNetworkList()).thenReturn(Map.of("NIC 1", networkId));
613613
when(importVmCmd.getConvertInstanceHostId()).thenReturn(null);
614+
when(importVmCmd.getImportInstanceHostId()).thenReturn(null);
614615
when(importVmCmd.getConvertStoragePoolId()).thenReturn(null);
615616

616617
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
@@ -638,6 +639,8 @@ private void baseTestImportVmFromVmwareToKvm(VcenterParameter vcenterParameter,
638639
when(importVmCmd.getConvertInstanceHostId()).thenReturn(convertHostId);
639640
when(importVmCmd.getImportInstanceHostId()).thenReturn(convertHostId);
640641
when(hostDao.findById(convertHostId)).thenReturn(convertHost);
642+
} else {
643+
when(hostDao.listByClusterAndHypervisorType(clusterId, Hypervisor.HypervisorType.KVM)).thenReturn(List.of(convertHost));
641644
}
642645

643646
DataStoreTO dataStoreTO = mock(DataStoreTO.class);
@@ -768,6 +771,7 @@ private void testImportFromDisk(String source) throws InsufficientServerCapacity
768771
}
769772
}
770773

774+
@Test
771775
public void testImportVmFromVmwareToKvmExistingVcenter() throws OperationTimedoutException, AgentUnavailableException {
772776
baseTestImportVmFromVmwareToKvm(VcenterParameter.EXISTING, false, false);
773777
}
@@ -828,8 +832,8 @@ public void testSelectInstanceConversionTemporaryLocationPoolInvalidScope() {
828832
ClusterVO cluster = getClusterForTests();
829833
long poolId = 1L;
830834
StoragePoolVO pool = mock(StoragePoolVO.class);
831-
Mockito.when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
832-
Mockito.when(pool.getClusterId()).thenReturn(100L);
835+
when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
836+
when(pool.getClusterId()).thenReturn(100L);
833837
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
834838
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
835839
}
@@ -840,8 +844,8 @@ public void testSelectInstanceConversionTemporaryLocationPoolConvertHostDifferen
840844
ClusterVO cluster = getClusterForTests();
841845
long poolId = 1L;
842846
StoragePoolVO pool = mock(StoragePoolVO.class);
843-
Mockito.when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
844-
Mockito.when(pool.getClusterId()).thenReturn(1L);
847+
when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
848+
when(pool.getClusterId()).thenReturn(1L);
845849
HostVO host = mock(HostVO.class);
846850
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
847851
when(host.getClusterId()).thenReturn(2L);
@@ -854,7 +858,7 @@ public void testSelectInstanceConversionTemporaryLocationLocalStoragePoolInvalid
854858
ClusterVO cluster = getClusterForTests();
855859
long poolId = 1L;
856860
StoragePoolVO pool = mock(StoragePoolVO.class);
857-
Mockito.when(pool.getScope()).thenReturn(ScopeType.HOST);
861+
when(pool.getScope()).thenReturn(ScopeType.HOST);
858862
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
859863
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
860864
}
@@ -864,17 +868,249 @@ public void testSelectInstanceConversionTemporaryLocationStoragePoolInvalidType(
864868
ClusterVO cluster = getClusterForTests();
865869
long poolId = 1L;
866870
StoragePoolVO pool = mock(StoragePoolVO.class);
867-
Mockito.when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
868-
Mockito.when(pool.getClusterId()).thenReturn(1L);
871+
when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
872+
when(pool.getClusterId()).thenReturn(1L);
869873
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
870-
Mockito.when(pool.getPoolType()).thenReturn(Storage.StoragePoolType.RBD);
874+
when(pool.getPoolType()).thenReturn(Storage.StoragePoolType.RBD);
871875
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
872876
}
873877

874878
@Test(expected = CloudRuntimeException.class)
875879
public void testSelectInstanceConversionTemporaryLocationNoPoolAvailable() {
876880
ClusterVO cluster = getClusterForTests();
877-
Mockito.when(imageStoreDao.findOneByZoneAndProtocol(anyLong(), anyString())).thenReturn(null);
881+
when(imageStoreDao.findOneByZoneAndProtocol(anyLong(), anyString())).thenReturn(null);
878882
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, null);
879883
}
884+
885+
@Test
886+
public void testSelectKVMHostForImportingInClusterWithImportInstanceIdSuccess() {
887+
Long hostId = 1L;
888+
ClusterVO cluster = getClusterForTests();
889+
HostVO host = Mockito.mock(HostVO.class);
890+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
891+
when(host.getStatus()).thenReturn(Status.Up);
892+
when(host.getType()).thenReturn(Host.Type.Routing);
893+
when(host.getClusterId()).thenReturn(1L);
894+
895+
when(hostDao.findById(hostId)).thenReturn(host);
896+
897+
HostVO returnedHost = unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, hostId);
898+
Assert.assertEquals(host, returnedHost);
899+
}
900+
901+
@Test
902+
public void testSelectKVMHostForImportingInClusterWithNullImportInstanceIdSuccess() {
903+
ClusterVO cluster = getClusterForTests();
904+
HostVO host = Mockito.mock(HostVO.class);
905+
when(hostDao.listByClusterAndHypervisorType(cluster.getId(), cluster.getHypervisorType())).thenReturn(List.of(host));
906+
907+
HostVO returnedHost = unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, null);
908+
Assert.assertEquals(host, returnedHost);
909+
}
910+
911+
@Test(expected = CloudRuntimeException.class)
912+
public void testSelectKVMHostForImportingInClusterFailure() {
913+
ClusterVO cluster = getClusterForTests();
914+
when(hostDao.listByClusterAndHypervisorType(cluster.getId(), cluster.getHypervisorType())).thenReturn(List.of());
915+
916+
unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, null);
917+
}
918+
919+
@Test(expected = CloudRuntimeException.class)
920+
public void testSelectKVMHostForImportingInClusterWithImportInstanceIdInvalidCluster() {
921+
Long hostId = 1L;
922+
ClusterVO cluster = getClusterForTests();
923+
HostVO host = Mockito.mock(HostVO.class);
924+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
925+
when(host.getStatus()).thenReturn(Status.Up);
926+
when(host.getType()).thenReturn(Host.Type.Routing);
927+
when(host.getClusterId()).thenReturn(2L);
928+
929+
when(hostDao.findById(hostId)).thenReturn(host);
930+
931+
unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, hostId);
932+
}
933+
934+
@Test(expected = CloudRuntimeException.class)
935+
public void testSelectKVMHostForImportingInClusterWithImportInstanceIdInvalidType() {
936+
Long hostId = 1L;
937+
ClusterVO cluster = getClusterForTests();
938+
HostVO host = Mockito.mock(HostVO.class);
939+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
940+
when(host.getStatus()).thenReturn(Status.Up);
941+
when(host.getType()).thenReturn(Host.Type.Storage);
942+
943+
when(hostDao.findById(hostId)).thenReturn(host);
944+
945+
unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, hostId);
946+
}
947+
948+
@Test(expected = CloudRuntimeException.class)
949+
public void testSelectKVMHostForImportingInClusterWithImportInstanceIdInvalidStatus() {
950+
Long hostId = 1L;
951+
ClusterVO cluster = getClusterForTests();
952+
HostVO host = Mockito.mock(HostVO.class);
953+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
954+
when(host.getStatus()).thenReturn(Status.Alert);
955+
956+
when(hostDao.findById(hostId)).thenReturn(host);
957+
958+
unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, hostId);
959+
}
960+
961+
@Test(expected = CloudRuntimeException.class)
962+
public void testSelectKVMHostForImportingInClusterWithImportInstanceIdInvalidResourceState() {
963+
Long hostId = 1L;
964+
ClusterVO cluster = getClusterForTests();
965+
HostVO host = Mockito.mock(HostVO.class);
966+
when(host.getResourceState()).thenReturn(ResourceState.Disabled);
967+
968+
when(hostDao.findById(hostId)).thenReturn(host);
969+
970+
unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, hostId);
971+
}
972+
973+
@Test(expected = CloudRuntimeException.class)
974+
public void testSelectKVMHostForImportingInClusterWithImportInstanceIdInvalidHostId() {
975+
Long hostId = 1L;
976+
ClusterVO cluster = getClusterForTests();
977+
978+
when(hostDao.findById(hostId)).thenReturn(null);
979+
980+
unmanagedVMsManager.selectKVMHostForImportingInCluster(cluster, hostId);
981+
}
982+
983+
@Test
984+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdEnabledHost() {
985+
Long hostId = 1L;
986+
ClusterVO cluster = getClusterForTests();
987+
HostVO host = Mockito.mock(HostVO.class);
988+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
989+
when(host.getStatus()).thenReturn(Status.Up);
990+
when(host.getType()).thenReturn(Host.Type.Routing);
991+
when(host.getDataCenterId()).thenReturn(1L);
992+
993+
when(hostDao.findById(hostId)).thenReturn(host);
994+
995+
HostVO returnedHost = unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
996+
Assert.assertEquals(host, returnedHost);
997+
}
998+
999+
@Test
1000+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdDisabledHost() {
1001+
Long hostId = 1L;
1002+
ClusterVO cluster = getClusterForTests();
1003+
HostVO host = Mockito.mock(HostVO.class);
1004+
when(host.getResourceState()).thenReturn(ResourceState.Disabled);
1005+
when(host.getStatus()).thenReturn(Status.Up);
1006+
when(host.getType()).thenReturn(Host.Type.Routing);
1007+
when(host.getDataCenterId()).thenReturn(1L);
1008+
1009+
when(hostDao.findById(hostId)).thenReturn(host);
1010+
1011+
HostVO returnedHost = unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
1012+
Assert.assertEquals(host, returnedHost);
1013+
}
1014+
1015+
@Test
1016+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdSuccessCompatible() {
1017+
ClusterVO cluster = getClusterForTests();
1018+
HostVO host = Mockito.mock(HostVO.class);
1019+
1020+
when(hostDao.listByClusterHypervisorTypeAndHostCapability(cluster.getId(),
1021+
cluster.getHypervisorType(), Host.HOST_INSTANCE_CONVERSION)).thenReturn(List.of(host));
1022+
1023+
HostVO returnedHost = unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, null);
1024+
Assert.assertEquals(host, returnedHost);
1025+
}
1026+
1027+
@Test
1028+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdSuccessNonCompatible() {
1029+
ClusterVO cluster = getClusterForTests();
1030+
HostVO host = Mockito.mock(HostVO.class);
1031+
1032+
when(hostDao.listByClusterHypervisorTypeAndHostCapability(cluster.getId(),
1033+
cluster.getHypervisorType(), Host.HOST_INSTANCE_CONVERSION)).thenReturn(List.of());
1034+
1035+
when(hostDao.listByClusterAndHypervisorType(cluster.getId(), cluster.getHypervisorType())).thenReturn(List.of(host));
1036+
1037+
HostVO returnedHost = unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, null);
1038+
Assert.assertEquals(host, returnedHost);
1039+
}
1040+
1041+
@Test(expected = CloudRuntimeException.class)
1042+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdFailure() {
1043+
ClusterVO cluster = getClusterForTests();
1044+
1045+
when(hostDao.listByClusterHypervisorTypeAndHostCapability(cluster.getId(),
1046+
cluster.getHypervisorType(), Host.HOST_INSTANCE_CONVERSION)).thenReturn(List.of());
1047+
1048+
when(hostDao.listByClusterAndHypervisorType(cluster.getId(), cluster.getHypervisorType())).thenReturn(List.of());
1049+
1050+
unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, null);
1051+
}
1052+
1053+
@Test(expected = CloudRuntimeException.class)
1054+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdInvalidZone() {
1055+
Long hostId = 1L;
1056+
ClusterVO cluster = getClusterForTests();
1057+
HostVO host = Mockito.mock(HostVO.class);
1058+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
1059+
when(host.getStatus()).thenReturn(Status.Up);
1060+
when(host.getType()).thenReturn(Host.Type.Routing);
1061+
when(host.getDataCenterId()).thenReturn(2L);
1062+
1063+
when(hostDao.findById(hostId)).thenReturn(host);
1064+
1065+
unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
1066+
}
1067+
1068+
@Test(expected = CloudRuntimeException.class)
1069+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdInvalidType() {
1070+
Long hostId = 1L;
1071+
ClusterVO cluster = getClusterForTests();
1072+
HostVO host = Mockito.mock(HostVO.class);
1073+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
1074+
when(host.getStatus()).thenReturn(Status.Up);
1075+
when(host.getType()).thenReturn(Host.Type.SecondaryStorage);
1076+
1077+
when(hostDao.findById(hostId)).thenReturn(host);
1078+
1079+
unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
1080+
}
1081+
1082+
@Test(expected = CloudRuntimeException.class)
1083+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdInvalidStatus() {
1084+
Long hostId = 1L;
1085+
ClusterVO cluster = getClusterForTests();
1086+
HostVO host = Mockito.mock(HostVO.class);
1087+
when(host.getResourceState()).thenReturn(ResourceState.Enabled);
1088+
when(host.getStatus()).thenReturn(Status.Down);
1089+
1090+
when(hostDao.findById(hostId)).thenReturn(host);
1091+
1092+
unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
1093+
}
1094+
1095+
@Test(expected = CloudRuntimeException.class)
1096+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdInvalidResourceState() {
1097+
Long hostId = 1L;
1098+
ClusterVO cluster = getClusterForTests();
1099+
HostVO host = Mockito.mock(HostVO.class);
1100+
when(host.getResourceState()).thenReturn(ResourceState.Maintenance);
1101+
1102+
when(hostDao.findById(hostId)).thenReturn(host);
1103+
1104+
unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
1105+
}
1106+
1107+
@Test(expected = CloudRuntimeException.class)
1108+
public void testSelectKVMHostForConversionInClusterWithImportInstanceIdInvalidHostId() {
1109+
Long hostId = 1L;
1110+
ClusterVO cluster = getClusterForTests();
1111+
1112+
when(hostDao.findById(hostId)).thenReturn(null);
1113+
1114+
unmanagedVMsManager.selectKVMHostForConversionInCluster(cluster, hostId);
1115+
}
8801116
}

0 commit comments

Comments
 (0)