Skip to content

Commit a298c33

Browse files
committed
Fix errors in testDestroyVM in UserVmManagerImplTest
1 parent a34c78a commit a298c33

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import org.apache.cloudstack.acl.ControlledEntity;
4848
import org.apache.cloudstack.acl.SecurityChecker;
49+
import org.apache.cloudstack.api.ApiCommandResourceType;
4950
import org.apache.cloudstack.api.ApiConstants;
5051
import org.apache.cloudstack.api.BaseCmd;
5152
import org.apache.cloudstack.api.BaseCmd.HTTPMethod;
@@ -103,6 +104,7 @@
103104
import com.cloud.host.dao.HostDao;
104105
import com.cloud.hypervisor.Hypervisor;
105106
import com.cloud.network.NetworkModel;
107+
import com.cloud.network.as.AutoScaleManager;
106108
import com.cloud.network.dao.NetworkDao;
107109
import com.cloud.network.dao.NetworkVO;
108110
import com.cloud.network.element.UserDataServiceProvider;
@@ -144,6 +146,7 @@
144146
import com.cloud.uservm.UserVm;
145147
import com.cloud.utils.Pair;
146148
import com.cloud.utils.db.EntityManager;
149+
import com.cloud.utils.db.UUIDManager;
147150
import com.cloud.utils.exception.CloudRuntimeException;
148151
import com.cloud.utils.exception.ExceptionProxyObject;
149152
import com.cloud.vm.dao.NicDao;
@@ -391,6 +394,12 @@ public class UserVmManagerImplTest {
391394
@Mock
392395
private VMInstanceVO vmInstanceMock;
393396

397+
@Mock
398+
private AutoScaleManager autoScaleManager;
399+
400+
@Mock
401+
private UUIDManager uuidMgr;
402+
394403
private static final long vmId = 1l;
395404
private static final long zoneId = 2L;
396405
private static final long accountId = 3L;
@@ -3485,15 +3494,16 @@ public void testDestroyVm() throws ResourceUnavailableException {
34853494
Long userId = 6L;
34863495
boolean expunge = true;
34873496

3497+
ReflectionTestUtils.setField(userVmManagerImpl, "_uuidMgr", uuidMgr);
34883498
CallContext callContext = mock(CallContext.class);
34893499
Account callingAccount = mock(Account.class);
34903500
when(callingAccount.getId()).thenReturn(accountId);
34913501
when(callContext.getCallingAccount()).thenReturn(callingAccount);
3492-
when(callContext.getCallingUserId()).thenReturn(userId);
34933502
when(accountManager.isAdmin(callingAccount.getId())).thenReturn(true);
34943503
doNothing().when(accountManager).checkApiAccess(callingAccount, BaseCmd.getCommandNameByClass(ExpungeVMCmd.class));
34953504
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
34963505
mockedCallContext.when(CallContext::current).thenReturn(callContext);
3506+
mockedCallContext.when(() -> CallContext.register(callContext, ApiCommandResourceType.Volume)).thenReturn(callContext);
34973507

34983508
DestroyVMCmd cmd = mock(DestroyVMCmd.class);
34993509
when(cmd.getId()).thenReturn(vmId);
@@ -3504,32 +3514,21 @@ public void testDestroyVm() throws ResourceUnavailableException {
35043514
UserVmVO vm = mock(UserVmVO.class);
35053515
when(vm.getId()).thenReturn(vmId);
35063516
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);
35113517
when(vm.getUuid()).thenReturn("vm-uuid");
35123518
when(vm.getUserVmType()).thenReturn("User");
35133519
when(userVmDao.findById(vmId)).thenReturn(vm);
35143520

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-
35253521
VolumeVO vol = Mockito.mock(VolumeVO.class);
35263522
when(vol.getInstanceId()).thenReturn(vmId);
3523+
when(vol.getId()).thenReturn(volumeId);
35273524
when(vol.getVolumeType()).thenReturn(Volume.Type.DATADISK);
35283525
when(volumeDaoMock.findById(volumeId)).thenReturn(vol);
35293526

35303527
List<VolumeVO> dataVolumes = new ArrayList<>();
35313528
when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.DATADISK)).thenReturn(dataVolumes);
35323529

3530+
when(volumeApiService.destroyVolume(volumeId, CallContext.current().getCallingAccount(), expunge, false)).thenReturn(vol);
3531+
35333532
doReturn(vm).when(userVmManagerImpl).stopVirtualMachine(anyLong(), anyBoolean());
35343533
doReturn(vm).when(userVmManagerImpl).destroyVm(vmId, expunge);
35353534
doReturn(true).when(userVmManagerImpl).expunge(vm);

0 commit comments

Comments
 (0)