Search in sources :

Example 1 with AssignVMCmd

use of com.cloud.api.command.admin.vm.AssignVMCmd in project cosmic by MissionCriticalCloud.

the class UserVmManagerTest method testMoveVmToUser1.

// Test Move VM b/w accounts where caller is not ROOT/Domain admin
@Test(expected = InvalidParameterValueException.class)
public void testMoveVmToUser1() throws Exception {
    final AssignVMCmd cmd = new AssignVMCmd();
    final Class<?> _class = cmd.getClass();
    final Field virtualmachineIdField = _class.getDeclaredField("virtualMachineId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    final Field accountNameField = _class.getDeclaredField("accountName");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "account");
    final Field domainIdField = _class.getDeclaredField("domainId");
    domainIdField.setAccessible(true);
    domainIdField.set(cmd, 1L);
    // caller is of type 0
    final Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, caller);
    try {
        _userVmMgr.moveVMToUser(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) AssignVMCmd(com.cloud.api.command.admin.vm.AssignVMCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 2 with AssignVMCmd

use of com.cloud.api.command.admin.vm.AssignVMCmd in project cosmic by MissionCriticalCloud.

the class UserVmManagerTest method testMoveVmToUser2.

// Test Move VM b/w accounts where caller doesn't have access to the old or new account
@Test(expected = PermissionDeniedException.class)
public void testMoveVmToUser2() throws Exception {
    final AssignVMCmd cmd = new AssignVMCmd();
    final Class<?> _class = cmd.getClass();
    final Field virtualmachineIdField = _class.getDeclaredField("virtualMachineId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    final Field accountNameField = _class.getDeclaredField("accountName");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "account");
    final Field domainIdField = _class.getDeclaredField("domainId");
    domainIdField.setAccessible(true);
    domainIdField.set(cmd, 1L);
    // caller is of type 0
    final Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 1, UUID.randomUUID().toString());
    final UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    final Account oldAccount = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    final Account newAccount = new AccountVO("testaccount", 1, "networkdomain", (short) 1, UUID.randomUUID().toString());
    final UserVmVO vm = new UserVmVO(10L, "test", "test", 1L, HypervisorType.Any, 1L, false, false, 1L, 1L, 1, 5L, "test", "test", 1L);
    vm.setState(VirtualMachine.State.Stopped);
    when(_vmDao.findById(anyLong())).thenReturn(vm);
    when(_accountService.getActiveAccountById(anyLong())).thenReturn(oldAccount);
    when(_accountService.getActiveAccountByName(anyString(), anyLong())).thenReturn(newAccount);
    doThrow(new PermissionDeniedException("Access check failed")).when(_accountMgr).checkAccess(any(Account.class), any(AccessType.class), any(Boolean.class), any(ControlledEntity.class));
    CallContext.register(user, caller);
    when(_accountMgr.isRootAdmin(anyLong())).thenReturn(true);
    try {
        _userVmMgr.moveVMToUser(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) ControlledEntity(com.cloud.acl.ControlledEntity) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) AssignVMCmd(com.cloud.api.command.admin.vm.AssignVMCmd) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) AccountVO(com.cloud.user.AccountVO) AccessType(com.cloud.acl.SecurityChecker.AccessType) Test(org.junit.Test)

Aggregations

AssignVMCmd (com.cloud.api.command.admin.vm.AssignVMCmd)2 Account (com.cloud.user.Account)2 AccountVO (com.cloud.user.AccountVO)2 UserVO (com.cloud.user.UserVO)2 Field (java.lang.reflect.Field)2 Test (org.junit.Test)2 ControlledEntity (com.cloud.acl.ControlledEntity)1 AccessType (com.cloud.acl.SecurityChecker.AccessType)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)1