Skip to content

Commit 8803c78

Browse files
committed
Fix for UT failures
1 parent 6b098ce commit 8803c78

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

engine/schema/src/test/java/org/apache/cloudstack/backup/dao/BackupDaoImplTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
// under the License.
1717
package org.apache.cloudstack.backup.dao;
1818

19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
1922
import java.util.HashMap;
2023
import java.util.Map;
2124

@@ -40,6 +43,9 @@
4043
import com.cloud.domain.DomainVO;
4144
import com.cloud.domain.dao.DomainDao;
4245
import com.cloud.hypervisor.Hypervisor;
46+
import com.cloud.storage.Storage;
47+
import com.cloud.storage.VMTemplateVO;
48+
import com.cloud.storage.dao.VMTemplateDao;
4349
import com.cloud.user.AccountVO;
4450
import com.cloud.user.dao.AccountDao;
4551
import com.cloud.utils.db.SearchBuilder;
@@ -74,6 +80,9 @@ public class BackupDaoImplTest {
7480
@Mock
7581
BackupOfferingDao backupOfferingDao;
7682

83+
@Mock
84+
private VMTemplateDao templateDao;
85+
7786
@Test
7887
public void testLoadDetails() {
7988
Long backupId = 1L;
@@ -115,6 +124,9 @@ public void testNewBackupResponse() {
115124
Long vmOfferingId = 5L;
116125
Long backupOfferingId = 6L;
117126
Long backupId = 7L;
127+
Long templateId = 8L;
128+
String templateUuid = "template-uuid1";
129+
118130
BackupVO backup = new BackupVO();
119131
ReflectionTestUtils.setField(backup, "id", backupId);
120132
ReflectionTestUtils.setField(backup, "uuid", "backup-uuid");
@@ -130,6 +142,7 @@ public void testNewBackupResponse() {
130142
0L, Hypervisor.HypervisorType.Simulator, 0L, domainId, accountId, 0L, false);
131143
vm.setDataCenterId(zoneId);
132144
vm.setBackupOfferingId(vmOfferingId);
145+
vm.setTemplateId(templateId);
133146

134147
AccountVO account = new AccountVO();
135148
account.setUuid("account-uuid");
@@ -152,8 +165,12 @@ public void testNewBackupResponse() {
152165
Mockito.when(dataCenterDao.findByIdIncludingRemoved(zoneId)).thenReturn(zone);
153166
Mockito.when(backupOfferingDao.findByIdIncludingRemoved(backupOfferingId)).thenReturn(offering);
154167

168+
VMTemplateVO template = mock(VMTemplateVO.class);
169+
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
170+
when(template.getUuid()).thenReturn(templateUuid);
171+
when(templateDao.findById(templateId)).thenReturn(template);
155172
Map<String, String> details = new HashMap<>();
156-
details.put(ApiConstants.HYPERVISOR, String.valueOf(Hypervisor.HypervisorType.KVM));
173+
157174
Mockito.when(backupDetailsDao.listDetailsKeyPairs(backup.getId(), true)).thenReturn(details);
158175

159176
BackupResponse response = backupDao.newBackupResponse(backup, true);
@@ -169,7 +186,7 @@ public void testNewBackupResponse() {
169186
Assert.assertEquals("offering-uuid", response.getBackupOfferingId());
170187
Assert.assertEquals("test-offering", response.getBackupOffering());
171188
Assert.assertEquals("MANUAL", response.getIntervalType());
172-
Assert.assertEquals("{hypervisor=KVM}", response.getVmDetails().toString());
189+
Assert.assertEquals("{isiso=false, hypervisor=Simulator, templateid=template-uuid1}", response.getVmDetails().toString());
173190
Assert.assertEquals(true, response.getVmOfferingRemoved());
174191
}
175192
}

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9058,7 +9058,7 @@ public UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws Insufficien
90589058

90599059
verifyDetails(cmd.getDetails());
90609060

9061-
VMInstanceVO backupVm = _vmInstanceDao.findByIdIncludingRemoved(backup.getVmId());
9061+
UserVmVO backupVm = _vmDao.findByIdIncludingRemoved(backup.getVmId());
90629062
HypervisorType hypervisorType = backupVm.getHypervisorType();
90639063

90649064
Long templateId;

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,13 +3183,19 @@ public void testAllocateVMFromBackupUsingCmdValues() throws InsufficientCapacity
31833183
when(diskOffering.isCustomizedIops()).thenReturn(true);
31843184
when(entityManager.findByUuid(DiskOffering.class, "disk-offering-uuid")).thenReturn(diskOffering);
31853185

3186-
VMTemplateVO template = mock(VMTemplateVO.class);
3187-
when(templateDao.findById(templateId)).thenReturn(template);
3188-
31893186
BackupVO backup = mock(BackupVO.class);
31903187
when(backup.getZoneId()).thenReturn(zoneId);
3188+
when(backup.getVmId()).thenReturn(vmId);
31913189
when(backupDao.findById(backupId)).thenReturn(backup);
31923190
when(backup.getDetail(ApiConstants.NETWORK_IDS)).thenReturn("network-uuid");
3191+
3192+
UserVmVO userVmVO = new UserVmVO();
3193+
userVmVO.setTemplateId(templateId);
3194+
when(userVmDao.findByIdIncludingRemoved(vmId)).thenReturn(userVmVO);
3195+
VMTemplateVO template = mock(VMTemplateVO.class);
3196+
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
3197+
when(templateDao.findById(templateId)).thenReturn(template);
3198+
31933199
NetworkVO network = mock(NetworkVO.class);
31943200
when(network.getId()).thenReturn(networkId);
31953201
when(_networkDao.findByUuid("network-uuid")).thenReturn(network);
@@ -3233,18 +3239,22 @@ public void testAllocateVMFromBackupUsingBackupValues() throws InsufficientCapac
32333239

32343240
BackupVO backup = mock(BackupVO.class);
32353241
when(backup.getZoneId()).thenReturn(zoneId);
3242+
when(backup.getVmId()).thenReturn(vmId);
32363243
when(backupDao.findById(backupId)).thenReturn(backup);
32373244

3245+
UserVmVO userVmVO = new UserVmVO();
3246+
userVmVO.setTemplateId(templateId);
3247+
when(userVmDao.findByIdIncludingRemoved(vmId)).thenReturn(userVmVO);
3248+
VMTemplateVO template = mock(VMTemplateVO.class);
3249+
when(template.getFormat()).thenReturn(Storage.ImageFormat.QCOW2);
3250+
when(templateDao.findById(templateId)).thenReturn(template);
3251+
32383252
DiskOfferingVO diskOffering = mock(DiskOfferingVO.class);
32393253
when(backup.getDetail(ApiConstants.SERVICE_OFFERING_ID)).thenReturn("service-offering-uuid");
32403254
when(_serviceOfferingDao.findByUuid("service-offering-uuid")).thenReturn(serviceOffering);
32413255
when(serviceOffering.getDiskOfferingId()).thenReturn(rootDiskOfferingId);
32423256
when(diskOfferingDao.findById(rootDiskOfferingId)).thenReturn(diskOffering);
32433257

3244-
when(backup.getDetail(ApiConstants.TEMPLATE_ID)).thenReturn("template-uuid");
3245-
VMTemplateVO template = mock(VMTemplateVO.class);
3246-
when(templateDao.findByUuid("template-uuid")).thenReturn(template);
3247-
32483258
when(backup.getDetail(ApiConstants.NETWORK_IDS)).thenReturn("net1-uuid,net2-uuid");
32493259
NetworkVO network1 = mock(NetworkVO.class);
32503260
NetworkVO network2 = mock(NetworkVO.class);

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -770,29 +770,21 @@ public void testBackupSyncTask() {
770770
public void testGetVmDetailsForBackup() {
771771
Long vmId = 1L;
772772
VirtualMachine vm = mock(VirtualMachine.class);
773-
when(vm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
774773
when(vm.getServiceOfferingId()).thenReturn(1L);
775-
when(vm.getTemplateId()).thenReturn(1L);
776774
when(vm.getId()).thenReturn(vmId);
777775

778776
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
779777
when(serviceOffering.getUuid()).thenReturn("service-offering-uuid");
780778
when(serviceOfferingDao.findById(1L)).thenReturn(serviceOffering);
781779

782-
VMTemplateVO template = mock(VMTemplateVO.class);
783-
when(template.getUuid()).thenReturn("template-uuid");
784-
when(vmTemplateDao.findById(1L)).thenReturn(template);
785-
786780
UserVmJoinVO userVmJoinVO = mock(UserVmJoinVO.class);
787781
when(userVmJoinVO.getNetworkUuid()).thenReturn("mocked-network-uuid");
788782
List<UserVmJoinVO> userVmJoinVOs = Collections.singletonList(userVmJoinVO);
789783
when(userVmJoinDao.searchByIds(vmId)).thenReturn(userVmJoinVOs);
790784

791785
Map<String, String> details = backupManager.getVmDetailsForBackup(vm);
792786

793-
assertEquals("KVM", details.get(ApiConstants.HYPERVISOR));
794787
assertEquals("service-offering-uuid", details.get(ApiConstants.SERVICE_OFFERING_ID));
795-
assertEquals("template-uuid", details.get(ApiConstants.TEMPLATE_ID));
796788
assertEquals("mocked-network-uuid", details.get(ApiConstants.NETWORK_IDS));
797789
}
798790

0 commit comments

Comments
 (0)