Skip to content

Commit b5830c6

Browse files
committed
Fix UT
1 parent 4bacb50 commit b5830c6

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,11 +3367,11 @@ public void testAllocateVMFromBackupUsingBackupValues() throws InsufficientCapac
33673367
when(backupDao.findById(backupId)).thenReturn(backup);
33683368

33693369
UserVmVO userVmVO = new UserVmVO();
3370-
userVmVO.setTemplateId(templateId);
33713370
when(userVmDao.findByIdIncludingRemoved(vmId)).thenReturn(userVmVO);
33723371
VMTemplateVO template = mock(VMTemplateVO.class);
33733372
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
3374-
when(templateDao.findById(templateId)).thenReturn(template);
3373+
when(backup.getDetail(ApiConstants.TEMPLATE_ID)).thenReturn("template-uuid");
3374+
when(templateDao.findByUuid("template-uuid")).thenReturn(template);
33753375

33763376
DiskOfferingVO diskOffering = mock(DiskOfferingVO.class);
33773377
when(backup.getDetail(ApiConstants.SERVICE_OFFERING_ID)).thenReturn("service-offering-uuid");
@@ -3542,11 +3542,11 @@ public void testAllocateVMFromBackupUsingBackupValuesWithISO() throws Insufficie
35423542
when(backupDao.findById(backupId)).thenReturn(backup);
35433543

35443544
UserVmVO userVmVO = new UserVmVO();
3545-
userVmVO.setTemplateId(isoId);
35463545
when(userVmDao.findByIdIncludingRemoved(vmId)).thenReturn(userVmVO);
35473546
VMTemplateVO iso = mock(VMTemplateVO.class);
35483547
when(iso.getFormat()).thenReturn(Storage.ImageFormat.ISO);
3549-
when(templateDao.findById(isoId)).thenReturn(iso);
3548+
when(backup.getDetail(ApiConstants.TEMPLATE_ID)).thenReturn("iso-uuid");
3549+
when(templateDao.findByUuid("iso-uuid")).thenReturn(iso);
35503550

35513551
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
35523552
DiskOfferingVO diskOffering = mock(DiskOfferingVO.class);

server/src/test/java/org/apache/cloudstack/backup/BackupManagerTest.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
import com.cloud.service.dao.ServiceOfferingDao;
4242
import com.cloud.storage.DiskOfferingVO;
4343
import com.cloud.exception.ResourceAllocationException;
44+
import com.cloud.storage.VMTemplateVO;
4445
import com.cloud.storage.Volume;
4546
import com.cloud.storage.VolumeApiService;
4647
import com.cloud.storage.VolumeVO;
4748
import com.cloud.storage.dao.DiskOfferingDao;
4849
import com.cloud.storage.dao.VMTemplateDao;
4950
import com.cloud.storage.dao.VolumeDao;
51+
import com.cloud.template.VirtualMachineTemplate;
5052
import com.cloud.user.Account;
5153
import com.cloud.user.AccountManager;
5254
import com.cloud.user.AccountVO;
@@ -58,9 +60,11 @@
5860
import com.cloud.utils.Pair;
5961
import com.cloud.utils.exception.CloudRuntimeException;
6062
import com.cloud.utils.fsm.NoTransitionException;
63+
import com.cloud.vm.UserVmDetailVO;
6164
import com.cloud.vm.VMInstanceVO;
6265
import com.cloud.vm.VirtualMachine;
6366
import com.cloud.vm.VirtualMachineManager;
67+
import com.cloud.vm.dao.UserVmDetailsDao;
6468
import com.cloud.vm.dao.VMInstanceDao;
6569

6670
import org.apache.cloudstack.api.ApiConstants;
@@ -112,6 +116,8 @@
112116
import static org.mockito.Mockito.when;
113117
import static org.mockito.Mockito.atLeastOnce;
114118

119+
import javax.inject.Inject;
120+
115121
@RunWith(MockitoJUnitRunner.class)
116122
public class BackupManagerTest {
117123
@Spy
@@ -181,6 +187,9 @@ public class BackupManagerTest {
181187
@Mock
182188
private NetworkService networkService;
183189

190+
@Mock
191+
private UserVmDetailsDao userVmDetailsDao;
192+
184193
private AccountVO account;
185194
private UserVO user;
186195

@@ -808,11 +817,21 @@ public void testGetBackupDetailsFromVM() {
808817
Long vmId = 1L;
809818
VirtualMachine vm = mock(VirtualMachine.class);
810819
when(vm.getServiceOfferingId()).thenReturn(1L);
820+
when(vm.getTemplateId()).thenReturn(2L);
811821
when(vm.getId()).thenReturn(vmId);
812822

813823
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
814824
when(serviceOffering.getUuid()).thenReturn("service-offering-uuid");
815825
when(serviceOfferingDao.findById(1L)).thenReturn(serviceOffering);
826+
VMTemplateVO template = mock(VMTemplateVO.class);
827+
when(template.getUuid()).thenReturn("template-uuid");
828+
when(vmTemplateDao.findById(2L)).thenReturn(template);
829+
830+
UserVmDetailVO userVmDetail = mock(UserVmDetailVO.class);
831+
when(userVmDetail.getName()).thenReturn("mocked-detail-name");
832+
when(userVmDetail.getValue()).thenReturn("mocked-detail-value");
833+
List<UserVmDetailVO> vmDetails = Collections.singletonList(userVmDetail);
834+
when(userVmDetailsDao.listDetails(vmId)).thenReturn(vmDetails);
816835

817836
UserVmJoinVO userVmJoinVO = mock(UserVmJoinVO.class);
818837
when(userVmJoinVO.getNetworkUuid()).thenReturn("mocked-network-uuid");
@@ -822,7 +841,8 @@ public void testGetBackupDetailsFromVM() {
822841
Map<String, String> details = backupManager.getBackupDetailsFromVM(vm);
823842

824843
assertEquals("service-offering-uuid", details.get(ApiConstants.SERVICE_OFFERING_ID));
825-
assertEquals("mocked-network-uuid", details.get(ApiConstants.NETWORK_IDS));
844+
assertEquals("[{\"networkid\":\"mocked-network-uuid\"}]", details.get(ApiConstants.NICS));
845+
assertEquals("{\"mocked-detail-name\":\"mocked-detail-value\"}", details.get(ApiConstants.VM_SETTINGS));
826846
}
827847

828848
@Test
@@ -1284,7 +1304,6 @@ public void testGetIpToNetworkMapFromBackup() {
12841304

12851305
// Test case 1: Missing network information
12861306
Backup backup1 = mock(Backup.class);
1287-
when(backup1.getDetail(ApiConstants.NETWORK_IDS)).thenReturn(null);
12881307
List<Long> networkIds1 = new ArrayList<>();
12891308
try {
12901309
backupManager.getIpToNetworkMapFromBackup(backup1, true, networkIds1);
@@ -1295,10 +1314,10 @@ public void testGetIpToNetworkMapFromBackup() {
12951314

12961315
// Test case 2: IP preservation enabled with IP information
12971316
Backup backup2 = mock(Backup.class);
1298-
when(backup2.getDetail(ApiConstants.NETWORK_IDS)).thenReturn(networkUuid1 + "," + networkUuid2);
1299-
when(backup2.getDetail(ApiConstants.IP_ADDRESSES)).thenReturn(ip1 + "," + ip2);
1300-
when(backup2.getDetail(ApiConstants.IP6_ADDRESSES)).thenReturn(ipv61 + "," + ipv62);
1301-
when(backup2.getDetail(ApiConstants.MAC_ADDRESSES)).thenReturn(mac1 + "," + mac2);
1317+
String nicsJson = String.format("[{\"networkid\":\"%s\",\"ipaddress\":\"%s\",\"ip6address\":\"%s\",\"macaddress\":\"%s\"}," +
1318+
"{\"networkid\":\"%s\",\"ipaddress\":\"%s\",\"ip6address\":\"%s\",\"macaddress\":\"%s\"}]",
1319+
networkUuid1, ip1, ipv61, mac1, networkUuid2, ip2, ipv62, mac2);
1320+
when(backup2.getDetail(ApiConstants.NICS)).thenReturn(nicsJson);
13021321

13031322
NetworkVO network1 = mock(NetworkVO.class);
13041323
NetworkVO network2 = mock(NetworkVO.class);
@@ -1324,10 +1343,8 @@ public void testGetIpToNetworkMapFromBackup() {
13241343

13251344
// Test case 3: IP preservation enabled but missing IP information
13261345
Backup backup3 = mock(Backup.class);
1327-
when(backup3.getDetail(ApiConstants.NETWORK_IDS)).thenReturn(networkUuid1);
1328-
when(backup3.getDetail(ApiConstants.IP_ADDRESSES)).thenReturn(null);
1329-
when(backup3.getDetail(ApiConstants.IP6_ADDRESSES)).thenReturn(null);
1330-
when(backup3.getDetail(ApiConstants.MAC_ADDRESSES)).thenReturn(null);
1346+
nicsJson = String.format("[{\"networkid\":\"%s\"}]", networkUuid1);
1347+
when(backup3.getDetail(ApiConstants.NICS)).thenReturn(nicsJson);
13311348

13321349
List<Long> networkIds3 = new ArrayList<>();
13331350
Map<Long, Network.IpAddresses> result3 = backupManager.getIpToNetworkMapFromBackup(backup3, true, networkIds3);
@@ -1339,7 +1356,8 @@ public void testGetIpToNetworkMapFromBackup() {
13391356

13401357
// Test case 4: IP preservation disabled
13411358
Backup backup4 = mock(Backup.class);
1342-
when(backup4.getDetail(ApiConstants.NETWORK_IDS)).thenReturn(networkUuid1);
1359+
nicsJson = String.format("[{\"networkid\":\"%s\"}]", networkUuid1);
1360+
when(backup4.getDetail(ApiConstants.NICS)).thenReturn(nicsJson);
13431361

13441362
List<Long> networkIds4 = new ArrayList<>();
13451363
Map<Long, Network.IpAddresses> result4 = backupManager.getIpToNetworkMapFromBackup(backup4, false, networkIds4);

0 commit comments

Comments
 (0)