CLOUDSTACK-658 - Adding scale vm support for vmware with unit tests Signed off by : Nitin Mehta <nitin.me...@citrix.com>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/75a2457a Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/75a2457a Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/75a2457a Branch: refs/heads/ui-vpc-redesign Commit: 75a2457af639e6bcf751c2e97d1360df1a0265e4 Parents: 19f0145 Author: Hari Patanala <hari.patan...@citrix.com> Authored: Fri May 10 09:29:41 2013 +0530 Committer: Nitin Mehta <nitin.me...@citrix.com> Committed: Fri May 10 09:32:08 2013 +0530 ---------------------------------------------------------------------- plugins/hypervisors/vmware/pom.xml | 10 ++ .../hypervisor/vmware/resource/VmwareResource.java | 31 ++++++- .../vmware/resource/VmwareResourceTest.java | 82 +++++++++++++++ server/src/com/cloud/vm/UserVmManagerImpl.java | 2 +- .../cloud/hypervisor/vmware/util/VmwareHelper.java | 26 +++++- 5 files changed, 148 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/75a2457a/plugins/hypervisors/vmware/pom.xml ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/pom.xml b/plugins/hypervisors/vmware/pom.xml index d65ef64..79779de 100644 --- a/plugins/hypervisors/vmware/pom.xml +++ b/plugins/hypervisors/vmware/pom.xml @@ -58,5 +58,15 @@ <artifactId>wsdl4j</artifactId> <version>1.4</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.10</version> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>1.9.5</version> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/75a2457a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index d87da64..6d7e0e7 100755 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -111,6 +111,8 @@ import com.cloud.agent.api.RebootCommand; import com.cloud.agent.api.RebootRouterCommand; import com.cloud.agent.api.RevertToVMSnapshotAnswer; import com.cloud.agent.api.RevertToVMSnapshotCommand; +import com.cloud.agent.api.ScaleVmCommand; +import com.cloud.agent.api.ScaleVmAnswer; import com.cloud.agent.api.SetupAnswer; import com.cloud.agent.api.SetupCommand; import com.cloud.agent.api.SetupGuestNetworkAnswer; @@ -485,6 +487,8 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return execute((ResizeVolumeCommand) cmd); } else if (clz == UnregisterVMCommand.class) { return execute((UnregisterVMCommand) cmd); + } else if (clz == ScaleVmCommand.class) { + return execute((ScaleVmCommand) cmd); } else { answer = Answer.createUnsupportedCommandAnswer(cmd); } @@ -2088,6 +2092,28 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa return validatedDisks.toArray(new VolumeTO[0]); } + protected ScaleVmAnswer execute(ScaleVmCommand cmd) { + + VmwareContext context = getServiceContext(); + VirtualMachineTO vmSpec = cmd.getVirtualMachine(); + try{ + VmwareHypervisorHost hyperHost = getHyperHost(context); + VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName()); + VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); + int ramMb = (int) (vmSpec.getMinRam()); + + VmwareHelper.setVmScaleUpConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getSpeed(), vmSpec.getSpeed(),(int) (vmSpec.getMaxRam()), ramMb, vmSpec.getLimitCpuUse()); + + if(!vmMo.configureVm(vmConfigSpec)) { + throw new Exception("Unable to execute ScaleVmCommand"); + } + }catch(Exception e) { + s_logger.error("Unexpected exception: ", e); + return new ScaleVmAnswer(cmd, false, "Unable to execute ScaleVmCommand due to " + e.toString()); + } + return new ScaleVmAnswer(cmd, true, null); + } + protected StartAnswer execute(StartCommand cmd) { if (s_logger.isInfoEnabled()) { @@ -2191,7 +2217,10 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa VmwareHelper.setBasicVmConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), vmSpec.getMinSpeed(),(int) (vmSpec.getMaxRam()/(1024*1024)), ramMb, translateGuestOsIdentifier(vmSpec.getArch(), vmSpec.getOs()).value(), vmSpec.getLimitCpuUse()); - + + vmConfigSpec.setMemoryHotAddEnabled(true); + vmConfigSpec.setCpuHotAddEnabled(true); + if ("true".equals(vmSpec.getDetails().get(VmDetailConstants.NESTED_VIRTUALIZATION_FLAG))) { s_logger.debug("Nested Virtualization enabled in configuration, checking hypervisor capability"); ManagedObjectReference hostMor = vmMo.getRunningHost().getMor(); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/75a2457a/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/resource/VmwareResourceTest.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/resource/VmwareResourceTest.java b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/resource/VmwareResourceTest.java new file mode 100644 index 0000000..3ca0b60 --- /dev/null +++ b/plugins/hypervisors/vmware/test/com/cloud/hypervisor/vmware/resource/VmwareResourceTest.java @@ -0,0 +1,82 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.hypervisor.vmware.resource; + +import com.cloud.agent.api.Command; +import com.cloud.agent.api.ScaleVmAnswer; +import com.cloud.agent.api.ScaleVmCommand; +import com.cloud.agent.api.to.VirtualMachineTO; +import com.cloud.hypervisor.vmware.mo.VirtualMachineMO; +import com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost; +import com.cloud.hypervisor.vmware.util.VmwareContext; +import com.cloud.hypervisor.vmware.util.VmwareHelper; +import com.vmware.vim25.VirtualMachineConfigSpec; +import org.junit.Test; +import org.junit.Before; + +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.MockitoAnnotations; +import static org.mockito.Mockito.*; + + +public class VmwareResourceTest { + + @Spy VmwareResource _resource = new VmwareResource() { + + @Override + public ScaleVmAnswer execute(ScaleVmCommand cmd) { + return super.execute(cmd); + } + @Override + public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) { + return hyperHost; + } + }; + + @Mock VmwareContext context; + @Mock ScaleVmCommand cmd; + @Mock VirtualMachineTO vmSpec; + @Mock + VmwareHypervisorHost hyperHost; + @Mock VirtualMachineMO vmMo; + @Mock VirtualMachineConfigSpec vmConfigSpec; + + @Before + public void setup(){ + MockitoAnnotations.initMocks(this); + doReturn(context).when(_resource).getServiceContext(null); + when(cmd.getVirtualMachine()).thenReturn(vmSpec); + } + //Test successful scaling up the vm + @Test + public void testScaleVMF1() throws Exception { + when(_resource.getHyperHost(context, null)).thenReturn(hyperHost); + doReturn("i-2-3-VM").when(cmd).getVmName(); + when(hyperHost.findVmOnHyperHost("i-2-3-VM")).thenReturn(vmMo); + doReturn(1024L).when(vmSpec).getMinRam(); + doReturn(1).when(vmSpec).getCpus(); + doReturn(1000).when(vmSpec).getSpeed(); + doReturn(1024L).when(vmSpec).getMaxRam(); + doReturn(false).when(vmSpec).getLimitCpuUse(); + when(vmMo.configureVm(vmConfigSpec)).thenReturn(true); + + ScaleVmAnswer answer = _resource.execute(cmd); + verify(_resource).execute(cmd); + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/75a2457a/server/src/com/cloud/vm/UserVmManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index eee8503..9fbc509 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1064,7 +1064,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use // Verify input parameters VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId); - if(vmInstance.getHypervisorType() != HypervisorType.XenServer){ + if(vmInstance.getHypervisorType() != HypervisorType.XenServer && vmInstance.getHypervisorType() != HypervisorType.VMware){ throw new InvalidParameterValueException("This operation not permitted for this hypervisor of the vm"); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/75a2457a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java ---------------------------------------------------------------------- diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java index 9c467dc..4a6a135 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java @@ -524,7 +524,31 @@ public class VmwareHelper { return options; } - public static void setBasicVmConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, + public static void setVmScaleUpConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, + int memoryMB, int memoryReserveMB, boolean limitCpuUse) { + + // VM config for scaling up + vmConfig.setMemoryMB((long)memoryMB); + vmConfig.setNumCPUs(cpuCount); + + ResourceAllocationInfo cpuInfo = new ResourceAllocationInfo(); + if (limitCpuUse) { + cpuInfo.setLimit((long)(cpuSpeedMHz * cpuCount)); + } else { + cpuInfo.setLimit(-1L); + } + + cpuInfo.setReservation((long)cpuReservedMhz); + vmConfig.setCpuAllocation(cpuInfo); + + ResourceAllocationInfo memInfo = new ResourceAllocationInfo(); + memInfo.setLimit((long)memoryMB); + memInfo.setReservation((long)memoryReserveMB); + vmConfig.setMemoryAllocation(memInfo); + + } + + public static void setBasicVmConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, int memoryMB, int memoryReserveMB, String guestOsIdentifier, boolean limitCpuUse) { // VM config basics