Search in sources :

Example 86 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserVmManagerImplTest method prepareAndExecuteMethodDealingWithDetails.

private void prepareAndExecuteMethodDealingWithDetails(boolean cleanUpDetails, boolean isDetailsEmpty) throws ResourceUnavailableException, InsufficientCapacityException {
    configureDoNothingForMethodsThatWeDoNotWantToTest();
    ServiceOffering offering = getSvcoffering(512);
    Mockito.when(_serviceOfferingDao.findById(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering);
    Mockito.when(_serviceOfferingDao.findByIdIncludingRemoved(Mockito.anyLong(), Mockito.anyLong())).thenReturn((ServiceOfferingVO) offering);
    ServiceOfferingVO currentServiceOffering = Mockito.mock(ServiceOfferingVO.class);
    Mockito.lenient().when(currentServiceOffering.getCpu()).thenReturn(1);
    Mockito.lenient().when(currentServiceOffering.getRamSize()).thenReturn(512);
    List<NicVO> nics = new ArrayList<>();
    NicVO nic1 = mock(NicVO.class);
    NicVO nic2 = mock(NicVO.class);
    nics.add(nic1);
    nics.add(nic2);
    when(this.nicDao.listByVmId(Mockito.anyLong())).thenReturn(nics);
    when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
    lenient().doNothing().when(_networkMgr).saveExtraDhcpOptions(anyString(), anyLong(), anyMap());
    HashMap<String, String> details = new HashMap<>();
    if (!isDetailsEmpty) {
        details.put("", "");
    }
    Mockito.when(updateVmCommand.getDetails()).thenReturn(details);
    Mockito.when(updateVmCommand.isCleanupDetails()).thenReturn(cleanUpDetails);
    configureDoNothingForDetailsMethod();
    userVmManagerImpl.updateVirtualMachine(updateVmCommand);
    verifyMethodsThatAreAlwaysExecuted();
    Mockito.verify(userVmVoMock, Mockito.times(cleanUpDetails || isDetailsEmpty ? 0 : 1)).setDetails(details);
    Mockito.verify(userVmDetailVO, Mockito.times(cleanUpDetails ? 1 : 0)).removeDetails(vmId);
    Mockito.verify(userVmDao, Mockito.times(cleanUpDetails || isDetailsEmpty ? 0 : 1)).saveDetails(userVmVoMock);
    Mockito.verify(userVmManagerImpl, Mockito.times(0)).updateDisplayVmFlag(false, vmId, userVmVoMock);
}
Also used : ServiceOffering(com.cloud.offering.ServiceOffering) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO)

Example 87 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class ManagementServerMock method createVM.

public UserVm createVM(String name, Network network) {
    VMTemplateVO tmpl = getVMTemplate();
    assertNotNull(tmpl);
    ServiceOffering small = getServiceByName("Small Instance");
    assertNotNull(small);
    Answer<?> callback = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            Commands cmds = (Commands) args[1];
            if (cmds == null) {
                return null;
            }
            PlugNicAnswer reply = new PlugNicAnswer(null, true, "PlugNic");
            com.cloud.agent.api.Answer[] answers = { reply };
            cmds.setAnswers(answers);
            return null;
        }
    };
    try {
        Mockito.when(_agentMgr.send(Matchers.anyLong(), Matchers.any(Commands.class))).thenAnswer(callback);
    } catch (AgentUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OperationTimedoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    long id = _userVmDao.getNextInSequence(Long.class, "id");
    UserVmVO vm = new UserVmVO(id, name, name, tmpl.getId(), HypervisorType.XenServer, tmpl.getGuestOSId(), false, false, _zone.getDomainId(), Account.ACCOUNT_ID_SYSTEM, 1, small.getId(), null, name);
    vm.setState(com.cloud.vm.VirtualMachine.State.Running);
    vm.setHostId(_hostId);
    vm.setDataCenterId(network.getDataCenterId());
    _userVmDao.persist(vm);
    NicProfile profile = new NicProfile();
    try {
        _vmMgr.addVmToNetwork(vm, network, profile);
    } catch (Exception ex) {
    // TODO Auto-generated catch block
    // ex.printStackTrace();
    }
    return vm;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) UserVmVO(com.cloud.vm.UserVmVO) ServiceOffering(com.cloud.offering.ServiceOffering) VMTemplateVO(com.cloud.storage.VMTemplateVO) NicProfile(com.cloud.vm.NicProfile) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) Answer(org.mockito.stubbing.Answer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer)

Example 88 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class AutoScaleManagerImpl method createAutoScaleVmProfile.

@Override
@ActionEvent(eventType = EventTypes.EVENT_AUTOSCALEVMPROFILE_CREATE, eventDescription = "creating autoscale vm profile", create = true)
public AutoScaleVmProfile createAutoScaleVmProfile(CreateAutoScaleVmProfileCmd cmd) {
    Account owner = _accountDao.findById(cmd.getAccountId());
    Account caller = CallContext.current().getCallingAccount();
    _accountMgr.checkAccess(caller, null, true, owner);
    long zoneId = cmd.getZoneId();
    long serviceOfferingId = cmd.getServiceOfferingId();
    long autoscaleUserId = cmd.getAutoscaleUserId();
    DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Unable to find zone by id");
    }
    ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering by id");
    }
    // validations
    HashMap<String, String> deployParams = cmd.getDeployParamMap();
    if (deployParams.containsKey("networks") && deployParams.get("networks").length() > 0) {
        throw new InvalidParameterValueException("'networks' is not a valid parameter, network for an AutoScaled VM is chosen automatically. An autoscaled VM is deployed in the loadbalancer's network");
    }
    /*
         * Just for making sure the values are right in other deploy params.
         * For ex. if projectId is given as a string instead of an long value, this
         * will be throwing an error.
         */
    dispatchChainFactory.getStandardDispatchChain().dispatch(new DispatchTask(ComponentContext.inject(DeployVMCmd.class), deployParams));
    AutoScaleVmProfileVO profileVO = new AutoScaleVmProfileVO(cmd.getZoneId(), cmd.getDomainId(), cmd.getAccountId(), cmd.getServiceOfferingId(), cmd.getTemplateId(), cmd.getOtherDeployParams(), cmd.getCounterParamList(), cmd.getDestroyVmGraceperiod(), autoscaleUserId);
    if (cmd.getDisplay() != null) {
        profileVO.setDisplay(cmd.getDisplay());
    }
    profileVO = checkValidityAndPersist(profileVO);
    s_logger.info("Successfully create AutoScale Vm Profile with Id: " + profileVO.getId());
    return profileVO;
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServiceOffering(com.cloud.offering.ServiceOffering) DispatchTask(com.cloud.api.dispatch.DispatchTask) ActionEvent(com.cloud.event.ActionEvent)

Example 89 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserVmManagerTest method testScaleVMF3.

// Test scaleVm for Stopped vm.
// @Test(expected=InvalidParameterValueException.class)
public void testScaleVMF3() 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);
    doReturn(Hypervisor.HypervisorType.XenServer).when(_vmInstance).getHypervisorType();
    ServiceOffering so1 = getSvcoffering(512);
    ServiceOffering so2 = getSvcoffering(256);
    when(_entityMgr.findById(eq(ServiceOffering.class), anyLong())).thenReturn(so2);
    when(_entityMgr.findById(ServiceOffering.class, 1L)).thenReturn(so1);
    doReturn(VirtualMachine.State.Stopped).when(_vmInstance).getState();
    when(_vmDao.findById(anyLong())).thenReturn(null);
    doReturn(true).when(_itMgr).upgradeVmDb(anyLong(), so1, so2);
    // when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
    Account account = new AccountVO("testaccount", 1L, "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, 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) ServiceOffering(com.cloud.offering.ServiceOffering) ScaleVMCmd(org.apache.cloudstack.api.command.user.vm.ScaleVMCmd) AccountVO(com.cloud.user.AccountVO)

Example 90 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserVmManagerTest method testScaleVMF2.

// Test scaleVm on equal service offerings.
@Test(expected = InvalidParameterValueException.class)
public void testScaleVMF2() 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);
    lenient().when(_vmInstanceDao.findById(anyLong())).thenReturn(_vmInstance);
    lenient().doReturn(Hypervisor.HypervisorType.XenServer).when(_vmInstance).getHypervisorType();
    lenient().doReturn(VirtualMachine.State.Running).when(_vmInstance).getState();
    lenient().doNothing().when(_accountMgr).checkAccess(_account, null, true, _templateMock);
    lenient().doNothing().when(_itMgr).checkIfCanUpgrade(_vmMock, _offeringVo);
    ServiceOffering so1 = getSvcoffering(512);
    lenient().when(_offeringDao.findById(anyLong())).thenReturn((ServiceOfferingVO) so1);
    lenient().when(_offeringDao.findByIdIncludingRemoved(anyLong(), anyLong())).thenReturn((ServiceOfferingVO) so1);
    Account account = new AccountVO("testaccount", 1L, "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, 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) ServiceOffering(com.cloud.offering.ServiceOffering) ScaleVMCmd(org.apache.cloudstack.api.command.user.vm.ScaleVMCmd) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Aggregations

ServiceOffering (com.cloud.offering.ServiceOffering)103 ArrayList (java.util.ArrayList)34 Account (com.cloud.user.Account)30 DataCenter (com.cloud.dc.DataCenter)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 HashMap (java.util.HashMap)18 VirtualMachine (com.cloud.vm.VirtualMachine)17 VMTemplateVO (com.cloud.storage.VMTemplateVO)14 UserVm (com.cloud.uservm.UserVm)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 Map (java.util.Map)14 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)13 ServerApiException (com.cloud.api.ServerApiException)12 HostVO (com.cloud.host.HostVO)12 DiskOffering (com.cloud.offering.DiskOffering)11 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)11 Host (com.cloud.host.Host)10 Network (com.cloud.network.Network)10 List (java.util.List)10 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)9