CLOUDSTACK-1410: Add a Unit test to verify if it actually works. This is also the first Unit Test for LibvirtComputingResource
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/0b7f2eb1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/0b7f2eb1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/0b7f2eb1 Branch: refs/heads/ui-regions Commit: 0b7f2eb1785be5a4f3715d563446535506cafe67 Parents: 2f135be Author: Wido den Hollander <[email protected]> Authored: Wed Feb 27 17:33:20 2013 +0100 Committer: Wido den Hollander <[email protected]> Committed: Wed Feb 27 17:34:06 2013 +0100 ---------------------------------------------------------------------- .../kvm/resource/LibvirtComputingResourceTest.java | 184 +++++++++++++++ 1 files changed, 184 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/0b7f2eb1/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java new file mode 100644 index 0000000..018f2f5 --- /dev/null +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -0,0 +1,184 @@ +/* + * 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.kvm.resource; + +import org.junit.Test; +import com.cloud.agent.api.to.VirtualMachineTO; +import com.cloud.hypervisor.kvm.resource.LibvirtVMDef; +import com.cloud.template.VirtualMachineTemplate.BootloaderType; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachine.Type; +import java.util.Random; +import static org.junit.Assert.assertEquals; + +public class LibvirtComputingResourceTest { + + String _hyperVisorType = "kvm"; + Random _random = new Random(); + + /** + This test tests if the Agent can handle a vmSpec coming + from a <=4.1 management server. + + The overcommit feature has not been merged in there and thus + only 'speed' is set. + */ + @Test + public void testCreateVMFromSpecLegacy() { + int id = _random.nextInt(65534); + String name = "test-instance-1"; + + int cpus = _random.nextInt(7) + 1; + int speed = 1024; + int minRam = 256 * 1024; + int maxRam = 512 * 1024; + + String os = "Ubuntu"; + boolean haEnabled = false; + boolean limitCpuUse = false; + + String vncAddr = "1.2.3.4"; + String vncPassword = "mySuperSecretPassword"; + + LibvirtComputingResource lcr = new LibvirtComputingResource(); + VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, speed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword); + to.setVncAddr(vncAddr); + + LibvirtVMDef vm = lcr.createVMFromSpec(to); + vm.setHvsType(_hyperVisorType); + + String vmStr = "<domain type='" + _hyperVisorType + "'>\n"; + vmStr += "<name>" + name + "</name>\n"; + vmStr += "<uuid>b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9</uuid>\n"; + vmStr += "<description>" + os + "</description>\n"; + vmStr += "<clock offset='utc'>\n"; + vmStr += "</clock>\n"; + vmStr += "<features>\n"; + vmStr += "<pae/>\n"; + vmStr += "<apic/>\n"; + vmStr += "<acpi/>\n"; + vmStr += "</features>\n"; + vmStr += "<devices>\n"; + vmStr += "<serial type='pty'>\n"; + vmStr += "<target port='0'/>\n"; + vmStr += "</serial>\n"; + vmStr += "<graphics type='vnc' autoport='yes' listen='" + vncAddr + "'/>\n"; + vmStr += "<console type='pty'>\n"; + vmStr += "<target port='0'/>\n"; + vmStr += "</console>\n"; + vmStr += "<input type='tablet' bus='usb'/>\n"; + vmStr += "</devices>\n"; + vmStr += "<memory>" + maxRam / 1024 + "</memory>\n"; + vmStr += "<currentMemory>" + minRam / 1024 + "</currentMemory>\n"; + vmStr += "<devices>\n"; + vmStr += "<memballoon model='virtio'/>\n"; + vmStr += "</devices>\n"; + vmStr += "<vcpu>" + cpus + "</vcpu>\n"; + vmStr += "<os>\n"; + vmStr += "<type machine='pc'>hvm</type>\n"; + vmStr += "<boot dev='cdrom'/>\n"; + vmStr += "<boot dev='hd'/>\n"; + vmStr += "</os>\n"; + vmStr += "<cputune>\n"; + vmStr += "<shares>" + (cpus * speed) + "</shares>\n"; + vmStr += "</cputune>\n"; + vmStr += "<on_reboot>restart</on_reboot>\n"; + vmStr += "<on_poweroff>destroy</on_poweroff>\n"; + vmStr += "<on_crash>destroy</on_crash>\n"; + vmStr += "</domain>\n"; + + assertEquals(vmStr, vm.toString()); + } + + /** + This test tests if the Agent can handle a vmSpec coming + from a >4.1 management server. + + It tests if the Agent can handle a vmSpec with overcommit + data like minSpeed and maxSpeed in there + */ + @Test + public void testCreateVMFromSpec() { + int id = _random.nextInt(65534); + String name = "test-instance-1"; + + int cpus = _random.nextInt(7) + 1; + int minSpeed = 1024; + int maxSpeed = 2048; + int minRam = 256 * 1024; + int maxRam = 512 * 1024; + + String os = "Ubuntu"; + boolean haEnabled = false; + boolean limitCpuUse = false; + + String vncAddr = "1.2.3.4"; + String vncPassword = "mySuperSecretPassword"; + + LibvirtComputingResource lcr = new LibvirtComputingResource(); + VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, minSpeed, maxSpeed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword); + to.setVncAddr(vncAddr); + + LibvirtVMDef vm = lcr.createVMFromSpec(to); + vm.setHvsType(_hyperVisorType); + + String vmStr = "<domain type='" + _hyperVisorType + "'>\n"; + vmStr += "<name>" + name + "</name>\n"; + vmStr += "<uuid>b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9</uuid>\n"; + vmStr += "<description>" + os + "</description>\n"; + vmStr += "<clock offset='utc'>\n"; + vmStr += "</clock>\n"; + vmStr += "<features>\n"; + vmStr += "<pae/>\n"; + vmStr += "<apic/>\n"; + vmStr += "<acpi/>\n"; + vmStr += "</features>\n"; + vmStr += "<devices>\n"; + vmStr += "<serial type='pty'>\n"; + vmStr += "<target port='0'/>\n"; + vmStr += "</serial>\n"; + vmStr += "<graphics type='vnc' autoport='yes' listen='" + vncAddr + "'/>\n"; + vmStr += "<console type='pty'>\n"; + vmStr += "<target port='0'/>\n"; + vmStr += "</console>\n"; + vmStr += "<input type='tablet' bus='usb'/>\n"; + vmStr += "</devices>\n"; + vmStr += "<memory>" + maxRam / 1024 + "</memory>\n"; + vmStr += "<currentMemory>" + minRam / 1024 + "</currentMemory>\n"; + vmStr += "<devices>\n"; + vmStr += "<memballoon model='virtio'/>\n"; + vmStr += "</devices>\n"; + vmStr += "<vcpu>" + cpus + "</vcpu>\n"; + vmStr += "<os>\n"; + vmStr += "<type machine='pc'>hvm</type>\n"; + vmStr += "<boot dev='cdrom'/>\n"; + vmStr += "<boot dev='hd'/>\n"; + vmStr += "</os>\n"; + vmStr += "<cputune>\n"; + vmStr += "<shares>" + (cpus * minSpeed) + "</shares>\n"; + vmStr += "</cputune>\n"; + vmStr += "<on_reboot>restart</on_reboot>\n"; + vmStr += "<on_poweroff>destroy</on_poweroff>\n"; + vmStr += "<on_crash>destroy</on_crash>\n"; + vmStr += "</domain>\n"; + + assertEquals(vmStr, vm.toString()); + } +} \ No newline at end of file
