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);
}
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;
}
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;
}
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();
}
}
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();
}
}
Aggregations