Search in sources :

Example 31 with UserVO

use of com.cloud.user.UserVO in project cloudstack by apache.

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 {
    AssignVMCmd cmd = new AssignVMCmd();
    Class<?> _class = cmd.getClass();
    Field virtualmachineIdField = _class.getDeclaredField("virtualMachineId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    Field accountNameField = _class.getDeclaredField("accountName");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "account");
    Field domainIdField = _class.getDeclaredField("domainId");
    domainIdField.setAccessible(true);
    domainIdField.set(cmd, 1L);
    // caller is of type 0
    Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 1, UUID.randomUUID().toString());
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    Account oldAccount = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    Account newAccount = new AccountVO("testaccount", 1, "networkdomain", (short) 1, UUID.randomUUID().toString());
    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(_accountMgr.finalizeOwner(any(Account.class), anyString(), anyLong(), 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(org.apache.cloudstack.acl.ControlledEntity) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) AssignVMCmd(org.apache.cloudstack.api.command.admin.vm.AssignVMCmd) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) AccountVO(com.cloud.user.AccountVO) AccessType(org.apache.cloudstack.acl.SecurityChecker.AccessType) Test(org.junit.Test)

Example 32 with UserVO

use of com.cloud.user.UserVO in project cloudstack by apache.

the class UserVmManagerTest method testUpdateVmNicIpFailure3.

// vm is stopped in shared network in advanced zone
@Test(expected = InvalidParameterValueException.class)
public void testUpdateVmNicIpFailure3() throws Exception {
    UpdateVmNicIpCmd cmd = new UpdateVmNicIpCmd();
    Class<?> _class = cmd.getClass();
    Field virtualmachineIdField = _class.getDeclaredField("nicId");
    virtualmachineIdField.setAccessible(true);
    virtualmachineIdField.set(cmd, 1L);
    Field accountNameField = _class.getDeclaredField("ipAddr");
    accountNameField.setAccessible(true);
    accountNameField.set(cmd, "10.10.10.10");
    NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
    when(_nicDao.findById(anyLong())).thenReturn(nic);
    when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
    doReturn(9L).when(_networkMock).getNetworkOfferingId();
    when(_networkOfferingDao.findByIdIncludingRemoved(anyLong())).thenReturn(_networkOfferingMock);
    doReturn(10L).when(_networkOfferingMock).getId();
    List<Service> services = new ArrayList<Service>();
    services.add(Service.Dhcp);
    when(_networkModel.listNetworkOfferingServices(anyLong())).thenReturn(services);
    when(_vmMock.getState()).thenReturn(State.Stopped);
    doNothing().when(_accountMgr).checkAccess(_account, null, true, _vmMock);
    when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(_accountMock);
    when(_networkMock.getState()).thenReturn(Network.State.Implemented);
    when(_networkMock.getDataCenterId()).thenReturn(3L);
    when(_networkMock.getGuestType()).thenReturn(GuestType.Shared);
    when(_dcDao.findById(anyLong())).thenReturn(_dcMock);
    when(_dcMock.getNetworkType()).thenReturn(NetworkType.Advanced);
    when(_ipAddrMgr.allocatePublicIpForGuestNic(Mockito.eq(_networkMock), anyLong(), Mockito.eq(_accountMock), anyString())).thenReturn(null);
    Account caller = new AccountVO("testaccount", 1, "networkdomain", (short) 0, UUID.randomUUID().toString());
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, caller);
    try {
        _userVmMgr.updateNicIpForVirtualMachine(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) ArrayList(java.util.ArrayList) AccountService(com.cloud.user.AccountService) NetworkOrchestrationService(org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService) Service(com.cloud.network.Network.Service) VolumeOrchestrationService(org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService) ResourceLimitService(com.cloud.user.ResourceLimitService) UpdateVmNicIpCmd(org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 33 with UserVO

use of com.cloud.user.UserVO in project cloudstack by apache.

the class UserVmManagerTest method testScaleVMF1.

// Test scaleVm on incompatible HV.
@Test(expected = InvalidParameterValueException.class)
public void testScaleVMF1() throws Exception {
    ScaleVMCmd cmd = new ScaleVMCmd();
    Class<?> _class = cmd.getClass();
    Field idField = _class.getDeclaredField("id");
    idField.setAccessible(true);
    idField.set(cmd, 1L);
    Field serviceOfferingIdField = _class.getDeclaredField("serviceOfferingId");
    serviceOfferingIdField.setAccessible(true);
    serviceOfferingIdField.set(cmd, 1L);
    when(_vmInstanceDao.findById(anyLong())).thenReturn(_vmInstance);
    // UserContext.current().setEventDetails("Vm Id: "+getId());
    Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
    UserVO user = new UserVO(1, "testuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    //AccountVO(String accountName, long domainId, String networkDomain, short type, int regionId)
    doReturn(VirtualMachine.State.Running).when(_vmInstance).getState();
    CallContext.register(user, account);
    try {
        _userVmMgr.upgradeVirtualMachine(cmd);
    } finally {
        CallContext.unregister();
    }
}
Also used : Field(java.lang.reflect.Field) Account(com.cloud.user.Account) UserVO(com.cloud.user.UserVO) ScaleVMCmd(org.apache.cloudstack.api.command.user.vm.ScaleVMCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 34 with UserVO

use of com.cloud.user.UserVO in project cloudstack by apache.

the class CreateNetworkOfferingTest method setUp.

@Override
@Before
public void setUp() {
    ComponentContext.initComponentsLifeCycle();
    ConfigurationVO configVO = new ConfigurationVO("200", "200", "200", "200", "200", "200");
    Mockito.when(configDao.findByName(Matchers.anyString())).thenReturn(configVO);
    Mockito.when(offDao.persist(Matchers.any(NetworkOfferingVO.class))).thenReturn(new NetworkOfferingVO());
    Mockito.when(offDao.persist(Matchers.any(NetworkOfferingVO.class), Matchers.anyMap())).thenReturn(new NetworkOfferingVO());
    Mockito.when(mapDao.persist(Matchers.any(NetworkOfferingServiceMapVO.class))).thenReturn(new NetworkOfferingServiceMapVO());
    Mockito.when(accountMgr.getSystemUser()).thenReturn(new UserVO(1));
    Mockito.when(accountMgr.getSystemAccount()).thenReturn(new AccountVO(2));
    CallContext.register(accountMgr.getSystemUser(), accountMgr.getSystemAccount());
}
Also used : NetworkOfferingServiceMapVO(com.cloud.offerings.NetworkOfferingServiceMapVO) ConfigurationVO(org.apache.cloudstack.framework.config.impl.ConfigurationVO) UserVO(com.cloud.user.UserVO) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) AccountVO(com.cloud.user.AccountVO) Before(org.junit.Before)

Example 35 with UserVO

use of com.cloud.user.UserVO in project cloudstack by apache.

the class ManagementServerImpl method enableAdminUser.

private void enableAdminUser(final String password) {
    String encodedPassword = null;
    final UserVO adminUser = _userDao.getUser(2);
    if (adminUser == null) {
        final String msg = "CANNOT find admin user";
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    }
    if (adminUser.getState() == Account.State.disabled) {
        for (final UserAuthenticator authenticator : _userPasswordEncoders) {
            encodedPassword = authenticator.encode(password);
            if (encodedPassword != null) {
                break;
            }
        }
        adminUser.setPassword(encodedPassword);
        adminUser.setState(Account.State.enabled);
        _userDao.persist(adminUser);
        s_logger.info("Admin user enabled");
    }
}
Also used : UserVO(com.cloud.user.UserVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UserAuthenticator(com.cloud.server.auth.UserAuthenticator)

Aggregations

UserVO (com.cloud.user.UserVO)72 AccountVO (com.cloud.user.AccountVO)44 Account (com.cloud.user.Account)42 Test (org.junit.Test)23 Before (org.junit.Before)21 ArrayList (java.util.ArrayList)19 Field (java.lang.reflect.Field)15 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 HashMap (java.util.HashMap)11 DomainVO (com.cloud.domain.DomainVO)10 VMTemplateVO (com.cloud.storage.VMTemplateVO)8 DomainRouterVO (com.cloud.vm.DomainRouterVO)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)7 Service (com.cloud.network.Network.Service)7 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)6 DataCenterVO (com.cloud.dc.DataCenterVO)5 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 LinkedHashMap (java.util.LinkedHashMap)5 NetworkOrchestrationService (org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService)5