|
23 | 23 | import static org.junit.Assert.assertTrue; |
24 | 24 | import static org.junit.Assert.fail; |
25 | 25 | import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
26 | 27 | import static org.mockito.ArgumentMatchers.anyList; |
27 | 28 | import static org.mockito.ArgumentMatchers.anyLong; |
28 | 29 | import static org.mockito.ArgumentMatchers.anyMap; |
29 | 30 | import static org.mockito.ArgumentMatchers.anyString; |
30 | 31 | import static org.mockito.ArgumentMatchers.eq; |
| 32 | +import static org.mockito.ArgumentMatchers.isNull; |
31 | 33 | import static org.mockito.ArgumentMatchers.nullable; |
32 | 34 | import static org.mockito.Mockito.doNothing; |
33 | 35 | import static org.mockito.Mockito.doReturn; |
|
45 | 47 | import org.apache.cloudstack.acl.ControlledEntity; |
46 | 48 | import org.apache.cloudstack.acl.SecurityChecker; |
47 | 49 | import org.apache.cloudstack.api.ApiConstants; |
| 50 | +import org.apache.cloudstack.api.BaseCmd; |
48 | 51 | import org.apache.cloudstack.api.BaseCmd.HTTPMethod; |
49 | 52 | import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd; |
| 53 | +import org.apache.cloudstack.api.command.admin.vm.ExpungeVMCmd; |
50 | 54 | import org.apache.cloudstack.api.command.user.vm.CreateVMFromBackupCmd; |
51 | 55 | import org.apache.cloudstack.api.command.user.vm.DeployVMCmd; |
52 | 56 | import org.apache.cloudstack.api.command.user.vm.DeployVnfApplianceCmd; |
| 57 | +import org.apache.cloudstack.api.command.user.vm.DestroyVMCmd; |
53 | 58 | import org.apache.cloudstack.api.command.user.vm.ResetVMSSHKeyCmd; |
54 | 59 | import org.apache.cloudstack.api.command.user.vm.ResetVMUserDataCmd; |
55 | 60 | import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd; |
@@ -3150,7 +3155,6 @@ public void executeStepsToChangeOwnershipOfVmTestResourceCountRunningVmsOnlyEnab |
3150 | 3155 | public void testAllocateVMFromBackupUsingCmdValues() throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException { |
3151 | 3156 | Long backupId = 4L; |
3152 | 3157 | Long rootDiskOfferingId = 5L; |
3153 | | - Long networkId = 6L; |
3154 | 3158 |
|
3155 | 3159 | CreateVMFromBackupCmd cmd = new CreateVMFromBackupCmd(); |
3156 | 3160 | cmd._accountService = accountService; |
@@ -3212,8 +3216,6 @@ public void testAllocateVMFromBackupUsingCmdValues() throws InsufficientCapacity |
3212 | 3216 | public void testAllocateVMFromBackupUsingBackupValues() throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException { |
3213 | 3217 | Long backupId = 5L; |
3214 | 3218 | Long rootDiskOfferingId = 6L; |
3215 | | - Long network1Id = 7L; |
3216 | | - Long network2Id = 8L; |
3217 | 3219 |
|
3218 | 3220 | CreateVMFromBackupCmd cmd = mock(CreateVMFromBackupCmd.class); |
3219 | 3221 | when(cmd.getZoneId()).thenReturn(zoneId); |
@@ -3316,4 +3318,231 @@ public void testResetVMSSHKey() throws ResourceUnavailableException, Insufficien |
3316 | 3318 | "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr..."); |
3317 | 3319 | Assert.assertEquals(details.get(VmDetailConstants.SSH_KEY_PAIR_NAMES), "keypair1,keypair2"); |
3318 | 3320 | } |
| 3321 | + |
| 3322 | + @Test |
| 3323 | + public void testAllocateVMFromBackupUsingCmdValuesWithISO() throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException { |
| 3324 | + Long backupId = 4L; |
| 3325 | + Long rootDiskOfferingId = 5L; |
| 3326 | + Long isoId = 7L; |
| 3327 | + |
| 3328 | + CreateVMFromBackupCmd cmd = new CreateVMFromBackupCmd(); |
| 3329 | + cmd._accountService = accountService; |
| 3330 | + cmd._entityMgr = entityManager; |
| 3331 | + when(accountService.finalyzeAccountId(nullable(String.class), nullable(Long.class), nullable(Long.class), eq(true))).thenReturn(accountId); |
| 3332 | + when(accountService.getActiveAccountById(accountId)).thenReturn(account); |
| 3333 | + |
| 3334 | + ReflectionTestUtils.setField(cmd, "serviceOfferingId", serviceOfferingId); |
| 3335 | + ReflectionTestUtils.setField(cmd, "templateId", isoId); |
| 3336 | + ReflectionTestUtils.setField(cmd, "backupId", backupId); |
| 3337 | + ReflectionTestUtils.setField(cmd, "zoneId", zoneId); |
| 3338 | + ReflectionTestUtils.setField(cmd, "diskOfferingId", rootDiskOfferingId); |
| 3339 | + |
| 3340 | + ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class); |
| 3341 | + when(serviceOffering.getDiskOfferingId()).thenReturn(rootDiskOfferingId); |
| 3342 | + DiskOfferingVO rootDiskOffering = mock(DiskOfferingVO.class); |
| 3343 | + when(_serviceOfferingDao.findById(serviceOfferingId)).thenReturn(serviceOffering); |
| 3344 | + when(diskOfferingDao.findById(rootDiskOfferingId)).thenReturn(rootDiskOffering); |
| 3345 | + when(entityManager.findById(DiskOffering.class, rootDiskOfferingId)).thenReturn(rootDiskOffering); |
| 3346 | + |
| 3347 | + Map<String, String> diskDetails = new HashMap<>(); |
| 3348 | + diskDetails.put(ApiConstants.DISK_OFFERING_ID, "disk-offering-uuid"); |
| 3349 | + diskDetails.put(ApiConstants.DEVICE_ID, "1"); |
| 3350 | + diskDetails.put(ApiConstants.SIZE, "5"); |
| 3351 | + diskDetails.put(ApiConstants.MIN_IOPS, "1000"); |
| 3352 | + diskDetails.put(ApiConstants.MAX_IOPS, "5000"); |
| 3353 | + Map<Integer, Map<String, String>> disksDetails = new HashMap<>(); |
| 3354 | + disksDetails.put(0, diskDetails); |
| 3355 | + ReflectionTestUtils.setField(cmd, "dataDisksDetails", disksDetails); |
| 3356 | + DiskOffering diskOffering = mock(DiskOffering.class); |
| 3357 | + when(diskOffering.isCustomized()).thenReturn(true); |
| 3358 | + when(diskOffering.isCustomizedIops()).thenReturn(true); |
| 3359 | + when(entityManager.findByUuid(DiskOffering.class, "disk-offering-uuid")).thenReturn(diskOffering); |
| 3360 | + |
| 3361 | + BackupVO backup = mock(BackupVO.class); |
| 3362 | + when(backup.getZoneId()).thenReturn(zoneId); |
| 3363 | + when(backup.getVmId()).thenReturn(vmId); |
| 3364 | + when(backupDao.findById(backupId)).thenReturn(backup); |
| 3365 | + |
| 3366 | + UserVmVO userVmVO = new UserVmVO(); |
| 3367 | + userVmVO.setTemplateId(isoId); |
| 3368 | + when(userVmDao.findByIdIncludingRemoved(vmId)).thenReturn(userVmVO); |
| 3369 | + VMTemplateVO iso = mock(VMTemplateVO.class); |
| 3370 | + when(iso.getFormat()).thenReturn(Storage.ImageFormat.ISO); |
| 3371 | + when(templateDao.findById(isoId)).thenReturn(iso); |
| 3372 | + |
| 3373 | + Mockito.doReturn(userVmVoMock).when(userVmManagerImpl).createAdvancedVirtualMachine(any(), any(), any(), any(), any(), any(), any(), |
| 3374 | + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), nullable(Boolean.class), any(), any(), any(), |
| 3375 | + any(), any(), any(), any(), eq(false), any(), any()); |
| 3376 | + |
| 3377 | + UserVm result = userVmManagerImpl.allocateVMFromBackup(cmd); |
| 3378 | + |
| 3379 | + assertNotNull(result); |
| 3380 | + Mockito.verify(backupDao).findById(backupId); |
| 3381 | + Mockito.verify(userVmManagerImpl).createAdvancedVirtualMachine(any(), any(), any(), any(), any(), any(), any(), |
| 3382 | + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), nullable(Boolean.class), any(), any(), any(), |
| 3383 | + any(), any(), any(), any(), eq(false), any(), any()); |
| 3384 | + } |
| 3385 | + |
| 3386 | + @Test |
| 3387 | + public void testAllocateVMFromBackupUsingBackupValuesWithISO() throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException { |
| 3388 | + Long backupId = 5L; |
| 3389 | + Long rootDiskOfferingId = 6L; |
| 3390 | + Long isoId = 9L; |
| 3391 | + |
| 3392 | + CreateVMFromBackupCmd cmd = mock(CreateVMFromBackupCmd.class); |
| 3393 | + when(cmd.getZoneId()).thenReturn(zoneId); |
| 3394 | + when(cmd.getBackupId()).thenReturn(backupId); |
| 3395 | + when(cmd.getEntityOwnerId()).thenReturn(accountId); |
| 3396 | + when(cmd.getServiceOfferingId()).thenReturn(null); |
| 3397 | + when(cmd.getDiskOfferingId()).thenReturn(null); |
| 3398 | + when(cmd.getTemplateId()).thenReturn(null); |
| 3399 | + when(cmd.getNetworkIds()).thenReturn(null); |
| 3400 | + when(cmd.getIpToNetworkMap()).thenReturn(null); |
| 3401 | + when(cmd.getDataDiskOfferingsInfo()).thenReturn(null); |
| 3402 | + |
| 3403 | + Account owner = mock(Account.class); |
| 3404 | + when(accountService.getActiveAccountById(accountId)).thenReturn(owner); |
| 3405 | + |
| 3406 | + DataCenterVO zone = mock(DataCenterVO.class); |
| 3407 | + when(_dcDao.findById(zoneId)).thenReturn(zone); |
| 3408 | + |
| 3409 | + BackupVO backup = mock(BackupVO.class); |
| 3410 | + when(backup.getZoneId()).thenReturn(zoneId); |
| 3411 | + when(backup.getVmId()).thenReturn(vmId); |
| 3412 | + when(backup.getDetail(ApiConstants.SERVICE_OFFERING_ID)).thenReturn("service-offering-uuid"); |
| 3413 | + when(backupDao.findById(backupId)).thenReturn(backup); |
| 3414 | + |
| 3415 | + UserVmVO userVmVO = new UserVmVO(); |
| 3416 | + userVmVO.setTemplateId(isoId); |
| 3417 | + when(userVmDao.findByIdIncludingRemoved(vmId)).thenReturn(userVmVO); |
| 3418 | + VMTemplateVO iso = mock(VMTemplateVO.class); |
| 3419 | + when(iso.getFormat()).thenReturn(Storage.ImageFormat.ISO); |
| 3420 | + when(templateDao.findById(isoId)).thenReturn(iso); |
| 3421 | + |
| 3422 | + ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class); |
| 3423 | + when(serviceOffering.getDiskOfferingId()).thenReturn(rootDiskOfferingId); |
| 3424 | + DiskOfferingVO diskOffering = mock(DiskOfferingVO.class); |
| 3425 | + when(backup.getDetail(ApiConstants.SERVICE_OFFERING_ID)).thenReturn("service-offering-uuid"); |
| 3426 | + when(_serviceOfferingDao.findByUuid("service-offering-uuid")).thenReturn(serviceOffering); |
| 3427 | + when(serviceOffering.getDiskOfferingId()).thenReturn(rootDiskOfferingId); |
| 3428 | + when(diskOfferingDao.findById(rootDiskOfferingId)).thenReturn(diskOffering); |
| 3429 | + |
| 3430 | + DiskOfferingInfo rootDiskOfferingInfo = new DiskOfferingInfo(diskOffering, 10L, 1000L, 2000L); |
| 3431 | + when(backupManager.getRootDiskOfferingInfoFromBackup(backup)).thenReturn(rootDiskOfferingInfo); |
| 3432 | + |
| 3433 | + NetworkVO network1 = mock(NetworkVO.class); |
| 3434 | + NetworkVO network2 = mock(NetworkVO.class); |
| 3435 | + when(backupManager.getDataDiskOfferingListFromBackup(backup)).thenReturn(List.of(new DiskOfferingInfo(diskOffering, 10L, 1000L, 2000L))); |
| 3436 | + |
| 3437 | + Mockito.doReturn(userVmVoMock).when(userVmManagerImpl).createAdvancedVirtualMachine(any(), any(), any(), any(), any(), any(), any(), |
| 3438 | + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), nullable(Boolean.class), any(), any(), any(), |
| 3439 | + any(), any(), any(), any(), eq(false), any(), any()); |
| 3440 | + |
| 3441 | + UserVm result = userVmManagerImpl.allocateVMFromBackup(cmd); |
| 3442 | + |
| 3443 | + assertNotNull(result); |
| 3444 | + Mockito.verify(userVmManagerImpl).createAdvancedVirtualMachine(any(), any(), any(), any(), any(), any(), any(), |
| 3445 | + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), nullable(Boolean.class), any(), any(), any(), |
| 3446 | + any(), any(), any(), any(), eq(false), any(), any()); |
| 3447 | + } |
| 3448 | + |
| 3449 | + @Test |
| 3450 | + public void testRestoreVMFromBackup() throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException { |
| 3451 | + Long backupId = 5L; |
| 3452 | + Long templateId = 6L; |
| 3453 | + |
| 3454 | + CreateVMFromBackupCmd cmd = mock(CreateVMFromBackupCmd.class); |
| 3455 | + when(cmd.getBackupId()).thenReturn(backupId); |
| 3456 | + when(cmd.getStartVm()).thenReturn(true); |
| 3457 | + when(cmd.getEntityId()).thenReturn(vmId); |
| 3458 | + |
| 3459 | + UserVmVO vm = mock(UserVmVO.class); |
| 3460 | + when(vm.getId()).thenReturn(vmId); |
| 3461 | + when(vm.getState()).thenReturn(VirtualMachine.State.Running); |
| 3462 | + when(vm.getTemplateId()).thenReturn(templateId); |
| 3463 | + |
| 3464 | + when(backupManager.restoreBackupToVM(backupId, vmId)).thenReturn(true); |
| 3465 | + |
| 3466 | + Map<VirtualMachineProfile.Param, Object> params = new HashMap<>(); |
| 3467 | + Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> vmPair = new Pair<>(vm, params); |
| 3468 | + doReturn(vmPair).when(userVmManagerImpl).startVirtualMachine(anyLong(), isNull(), isNull(), isNull(), anyMap(), isNull()); |
| 3469 | + doReturn(vmPair).when(userVmManagerImpl).startVirtualMachine(anyLong(), isNull(), isNull(), anyLong(), anyMap(), isNull()); |
| 3470 | + when(userVmDao.findById(vmId)).thenReturn(vm); |
| 3471 | + when(templateDao.findByIdIncludingRemoved(templateId)).thenReturn(mock(VMTemplateVO.class)); |
| 3472 | + when(userVmManagerImpl.stopVirtualMachine(anyLong(), anyLong())).thenReturn(true); |
| 3473 | + |
| 3474 | + UserVm result = userVmManagerImpl.restoreVMFromBackup(cmd); |
| 3475 | + |
| 3476 | + assertNotNull(result); |
| 3477 | + assertEquals(vm, result); |
| 3478 | + Mockito.verify(backupManager).restoreBackupToVM(backupId, vmId); |
| 3479 | + } |
| 3480 | + |
| 3481 | + @Test |
| 3482 | + public void testDestroyVm() throws ResourceUnavailableException { |
| 3483 | + Long volumeId = 4L; |
| 3484 | + Long accountId = 5L; |
| 3485 | + Long userId = 6L; |
| 3486 | + boolean expunge = true; |
| 3487 | + |
| 3488 | + CallContext callContext = mock(CallContext.class); |
| 3489 | + Account callingAccount = mock(Account.class); |
| 3490 | + when(callingAccount.getId()).thenReturn(accountId); |
| 3491 | + when(callContext.getCallingAccount()).thenReturn(callingAccount); |
| 3492 | + when(callContext.getCallingUserId()).thenReturn(userId); |
| 3493 | + when(accountManager.isAdmin(callingAccount.getId())).thenReturn(true); |
| 3494 | + doNothing().when(accountManager).checkApiAccess(callingAccount, BaseCmd.getCommandNameByClass(ExpungeVMCmd.class)); |
| 3495 | + try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) { |
| 3496 | + mockedCallContext.when(CallContext::current).thenReturn(callContext); |
| 3497 | + |
| 3498 | + DestroyVMCmd cmd = mock(DestroyVMCmd.class); |
| 3499 | + when(cmd.getId()).thenReturn(vmId); |
| 3500 | + when(cmd.getExpunge()).thenReturn(expunge); |
| 3501 | + List<Long> volumeIds = List.of(volumeId); |
| 3502 | + when(cmd.getVolumeIds()).thenReturn(volumeIds); |
| 3503 | + |
| 3504 | + UserVmVO vm = mock(UserVmVO.class); |
| 3505 | + when(vm.getId()).thenReturn(vmId); |
| 3506 | + when(vm.getState()).thenReturn(VirtualMachine.State.Running); |
| 3507 | + when(vm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); |
| 3508 | + when(vm.getServiceOfferingId()).thenReturn(serviceOfferingId); |
| 3509 | + when(vm.getTemplateId()).thenReturn(templateId); |
| 3510 | + when(vm.getAccountId()).thenReturn(accountId); |
| 3511 | + when(vm.getUuid()).thenReturn("vm-uuid"); |
| 3512 | + when(vm.getUserVmType()).thenReturn("User"); |
| 3513 | + when(userVmDao.findById(vmId)).thenReturn(vm); |
| 3514 | + |
| 3515 | + when(accountDao.findById(accountId)).thenReturn(account); |
| 3516 | + |
| 3517 | + ServiceOfferingVO offering = mock(ServiceOfferingVO.class); |
| 3518 | + when(offering.getCpu()).thenReturn(2); |
| 3519 | + when(offering.getRamSize()).thenReturn(2048); |
| 3520 | + when(_serviceOfferingDao.findByIdIncludingRemoved(serviceOfferingId)).thenReturn(offering); |
| 3521 | + |
| 3522 | + VMTemplateVO template = mock(VMTemplateVO.class); |
| 3523 | + when(templateDao.findByIdIncludingRemoved(templateId)).thenReturn(template); |
| 3524 | + |
| 3525 | + VolumeVO vol = Mockito.mock(VolumeVO.class); |
| 3526 | + when(vol.getInstanceId()).thenReturn(vmId); |
| 3527 | + when(vol.getVolumeType()).thenReturn(Volume.Type.DATADISK); |
| 3528 | + when(volumeDaoMock.findById(volumeId)).thenReturn(vol); |
| 3529 | + |
| 3530 | + List<VolumeVO> dataVolumes = new ArrayList<>(); |
| 3531 | + when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.DATADISK)).thenReturn(dataVolumes); |
| 3532 | + |
| 3533 | + doReturn(vm).when(userVmManagerImpl).stopVirtualMachine(anyLong(), anyBoolean()); |
| 3534 | + doReturn(vm).when(userVmManagerImpl).destroyVm(vmId, expunge); |
| 3535 | + doReturn(true).when(userVmManagerImpl).expunge(vm); |
| 3536 | + |
| 3537 | + try (MockedStatic<UsageEventUtils> mockedUsageEventUtils = Mockito.mockStatic(UsageEventUtils.class)) { |
| 3538 | + |
| 3539 | + UserVm result = userVmManagerImpl.destroyVm(cmd); |
| 3540 | + |
| 3541 | + assertNotNull(result); |
| 3542 | + assertEquals(vm, result); |
| 3543 | + Mockito.verify(userVmManagerImpl).stopVirtualMachine(vmId, false); |
| 3544 | + Mockito.verify(backupManager).checkAndRemoveBackupOfferingBeforeExpunge(vm); |
| 3545 | + } |
| 3546 | + } |
| 3547 | + } |
3319 | 3548 | } |
0 commit comments