4141import com .cloud .service .dao .ServiceOfferingDao ;
4242import com .cloud .storage .DiskOfferingVO ;
4343import com .cloud .exception .ResourceAllocationException ;
44+ import com .cloud .storage .VMTemplateVO ;
4445import com .cloud .storage .Volume ;
4546import com .cloud .storage .VolumeApiService ;
4647import com .cloud .storage .VolumeVO ;
4748import com .cloud .storage .dao .DiskOfferingDao ;
4849import com .cloud .storage .dao .VMTemplateDao ;
4950import com .cloud .storage .dao .VolumeDao ;
51+ import com .cloud .template .VirtualMachineTemplate ;
5052import com .cloud .user .Account ;
5153import com .cloud .user .AccountManager ;
5254import com .cloud .user .AccountVO ;
5860import com .cloud .utils .Pair ;
5961import com .cloud .utils .exception .CloudRuntimeException ;
6062import com .cloud .utils .fsm .NoTransitionException ;
63+ import com .cloud .vm .UserVmDetailVO ;
6164import com .cloud .vm .VMInstanceVO ;
6265import com .cloud .vm .VirtualMachine ;
6366import com .cloud .vm .VirtualMachineManager ;
67+ import com .cloud .vm .dao .UserVmDetailsDao ;
6468import com .cloud .vm .dao .VMInstanceDao ;
6569
6670import org .apache .cloudstack .api .ApiConstants ;
112116import static org .mockito .Mockito .when ;
113117import static org .mockito .Mockito .atLeastOnce ;
114118
119+ import javax .inject .Inject ;
120+
115121@ RunWith (MockitoJUnitRunner .class )
116122public 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