Skip to content

Commit 41c7e22

Browse files
committed
test: Add test for LibvirtDomainXMLParser
A couple of changes where made to other classes as well to add some features to allow more extensive testing.
1 parent cecb216 commit 41c7e22

3 files changed

Lines changed: 198 additions & 2 deletions

File tree

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtDomainXMLParser.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public boolean parseDomainXML(String domXML) {
7676
def.defNetworkBasedDisk(diskPath, host, port, authUserName, poolUuid, diskLabel,
7777
DiskDef.diskBus.valueOf(bus.toUpperCase()),
7878
DiskDef.diskProtocol.valueOf(protocol.toUpperCase()));
79-
def.setCacheMode(DiskDef.diskCacheMode.valueOf(diskCacheMode));
79+
def.setCacheMode(DiskDef.diskCacheMode.valueOf(diskCacheMode.toUpperCase()));
8080
} else {
8181
String diskFmtType = getAttrValue("driver", "type", disk);
8282
String diskCacheMode = getAttrValue("driver", "cache", disk);
@@ -100,8 +100,8 @@ public boolean parseDomainXML(String domXML) {
100100
} else if (type.equalsIgnoreCase("block")) {
101101
def.defBlockBasedDisk(diskDev, diskLabel,
102102
DiskDef.diskBus.valueOf(bus.toUpperCase()));
103-
def.setCacheMode(DiskDef.diskCacheMode.valueOf(diskCacheMode));
104103
}
104+
def.setCacheMode(DiskDef.diskCacheMode.valueOf(diskCacheMode.toUpperCase()));
105105
}
106106

107107
NodeList iotune = disk.getElementsByTagName("iotune");

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ public String getDiskLabel() {
585585
return _diskLabel;
586586
}
587587

588+
public diskType getDiskType() {
589+
return _diskType;
590+
}
591+
588592
public deviceType getDeviceType() {
589593
return _deviceType;
590594
}
@@ -597,6 +601,10 @@ public diskBus getBusType() {
597601
return _bus;
598602
}
599603

604+
public diskFmtType getDiskFormatType() {
605+
return _diskFmtType;
606+
}
607+
600608
public int getDiskSeq() {
601609
char suffix = _diskLabel.charAt(_diskLabel.length() - 1);
602610
return suffix - 'a';
@@ -622,6 +630,10 @@ public void setCacheMode(diskCacheMode cacheMode) {
622630
_diskCacheMode = cacheMode;
623631
}
624632

633+
public diskCacheMode getCacheMode() {
634+
return _diskCacheMode;
635+
}
636+
625637
@Override
626638
public String toString() {
627639
StringBuilder diskBuilder = new StringBuilder();
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.cloud.hypervisor.kvm.resource;
21+
22+
import junit.framework.TestCase;
23+
import java.util.List;
24+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef;
25+
26+
public class LibvirtDomainXMLParserTest extends TestCase {
27+
28+
public void testDomainXMLParser() {
29+
int vncPort = 5900;
30+
31+
DiskDef.diskBus diskBus = DiskDef.diskBus.VIRTIO;
32+
DiskDef.diskType diskType = DiskDef.diskType.FILE;
33+
DiskDef.deviceType deviceType = DiskDef.deviceType.DISK;
34+
DiskDef.diskFmtType diskFormat = DiskDef.diskFmtType.QCOW2;
35+
DiskDef.diskCacheMode diskCache = DiskDef.diskCacheMode.NONE;
36+
37+
String diskLabel ="vda";
38+
String diskPath = "/var/lib/libvirt/images/my-test-image.qcow2";
39+
40+
String xml = "<domain type='kvm' id='10'>" +
41+
"<name>s-2970-VM</name>" +
42+
"<uuid>4d2c1526-865d-4fc9-a1ac-dbd1801a22d0</uuid>" +
43+
"<description>Debian GNU/Linux 6(64-bit)</description>" +
44+
"<memory unit='KiB'>262144</memory>" +
45+
"<currentMemory unit='KiB'>262144</currentMemory>" +
46+
"<vcpu placement='static'>1</vcpu>" +
47+
"<cputune>" +
48+
"<shares>250</shares>" +
49+
"</cputune>" +
50+
"<resource>" +
51+
"<partition>/machine</partition>" +
52+
"</resource>" +
53+
"<os>" +
54+
"<type arch='x86_64' machine='pc-i440fx-1.5'>hvm</type>" +
55+
"<boot dev='cdrom'/>" +
56+
"<boot dev='hd'/>" +
57+
"</os>" +
58+
"<features>" +
59+
"<acpi/>" +
60+
"<apic/>" +
61+
"<pae/>" +
62+
"</features>" +
63+
"<clock offset='utc'/>" +
64+
"<on_poweroff>destroy</on_poweroff>" +
65+
"<on_reboot>restart</on_reboot>" +
66+
"<on_crash>destroy</on_crash>" +
67+
"<devices>" +
68+
"<emulator>/usr/bin/kvm-spice</emulator>" +
69+
"<disk type='" + diskType.toString() + "' device='" + deviceType.toString() + "'>" +
70+
"<driver name='qemu' type='" + diskFormat.toString() + "' cache='" + diskCache.toString() + "'/>" +
71+
"<source file='" + diskPath + "'/>" +
72+
"<target dev='" + diskLabel + "' bus='" + diskBus.toString() + "'/>" +
73+
"<alias name='virtio-disk0'/>" +
74+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>" +
75+
"</disk>" +
76+
"<disk type='file' device='cdrom'>" +
77+
"<driver name='qemu' type='raw' cache='none'/>" +
78+
"<source file='/usr/share/cloudstack-common/vms/systemvm.iso'/>" +
79+
"<target dev='hdc' bus='ide'/>" +
80+
"<readonly/>" +
81+
"<alias name='ide0-1-0'/>" +
82+
"<address type='drive' controller='0' bus='1' target='0' unit='0'/>" +
83+
"</disk>" +
84+
"<controller type='usb' index='0'>" +
85+
"<alias name='usb0'/>" +
86+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>" +
87+
"</controller>" +
88+
"<controller type='pci' index='0' model='pci-root'>" +
89+
"<alias name='pci0'/>" +
90+
"</controller>" +
91+
"<controller type='ide' index='0'>" +
92+
"<alias name='ide0'/>" +
93+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>" +
94+
"</controller>" +
95+
"<controller type='virtio-serial' index='0'>" +
96+
"<alias name='virtio-serial0'/>" +
97+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>" +
98+
"</controller>" +
99+
"<interface type='bridge'>" +
100+
"<mac address='0e:00:a9:fe:02:00'/>" +
101+
"<source bridge='cloud0'/>" +
102+
"<target dev='vnet0'/>" +
103+
"<model type='virtio'/>" +
104+
"<alias name='net0'/>" +
105+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>" +
106+
"</interface>" +
107+
"<interface type='bridge'>" +
108+
"<mac address='06:c5:94:00:05:65'/>" +
109+
"<source bridge='cloudbr1'/>" +
110+
"<target dev='vnet1'/>" +
111+
"<model type='virtio'/>" +
112+
"<alias name='net1'/>" +
113+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>" +
114+
"</interface>" +
115+
"<interface type='bridge'>" +
116+
"<mac address='06:c9:f4:00:04:40'/>" +
117+
"<source bridge='cloudbr0'/>" +
118+
"<target dev='vnet2'/>" +
119+
"<model type='virtio'/>" +
120+
"<alias name='net2'/>" +
121+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>" +
122+
"</interface>" +
123+
"<interface type='bridge'>" +
124+
"<mac address='06:7e:c6:00:05:68'/>" +
125+
"<source bridge='cloudbr1'/>" +
126+
"<target dev='vnet3'/>" +
127+
"<model type='virtio'/>" +
128+
"<alias name='net3'/>" +
129+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>" +
130+
"</interface>" +
131+
"<serial type='pty'>" +
132+
"<source path='/dev/pts/3'/>" +
133+
"<target port='0'/>" +
134+
"<alias name='serial0'/>" +
135+
"</serial>" +
136+
"<console type='pty' tty='/dev/pts/3'>" +
137+
"<source path='/dev/pts/3'/>" +
138+
"<target type='serial' port='0'/>" +
139+
"<alias name='serial0'/>" +
140+
"</console>" +
141+
"<channel type='unix'>" +
142+
"<source mode='bind' path='/var/lib/libvirt/qemu/s-2970-VM.agent'/>" +
143+
"<target type='virtio' name='s-2970-VM.vport'/>" +
144+
"<alias name='channel0'/>" +
145+
"<address type='virtio-serial' controller='0' bus='0' port='1'/>" +
146+
"</channel>" +
147+
"<input type='tablet' bus='usb'>" +
148+
"<alias name='input0'/>" +
149+
"</input>" +
150+
"<input type='mouse' bus='ps2'/>" +
151+
"<graphics type='vnc' port='" + vncPort + "' autoport='yes' listen='0.0.0.0'>" +
152+
"<listen type='address' address='0.0.0.0'/>" +
153+
"</graphics>" +
154+
"<video>" +
155+
"<model type='cirrus' vram='9216' heads='1'/>" +
156+
"<alias name='video0'/>" +
157+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>" +
158+
"</video>" +
159+
"<memballoon model='virtio'>" +
160+
"<alias name='balloon0'/>" +
161+
"<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>" +
162+
"</memballoon>" +
163+
"</devices>" +
164+
"<seclabel type='none'/>" +
165+
"</domain>";
166+
167+
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
168+
parser.parseDomainXML(xml);
169+
170+
assertEquals(vncPort - 5900, (int)parser.getVncPort());
171+
172+
List<DiskDef> disks = parser.getDisks();
173+
/* Disk 0 is the first disk, the QCOW2 file backed virto disk */
174+
int diskId = 0;
175+
176+
assertEquals(diskLabel, disks.get(diskId).getDiskLabel());
177+
assertEquals(diskPath, disks.get(diskId).getDiskPath());
178+
assertEquals(diskCache, disks.get(diskId).getCacheMode());
179+
assertEquals(diskBus, disks.get(diskId).getBusType());
180+
assertEquals(diskType, disks.get(diskId).getDiskType());
181+
assertEquals(deviceType, disks.get(diskId).getDeviceType());
182+
assertEquals(diskFormat, disks.get(diskId).getDiskFormatType());
183+
}
184+
}

0 commit comments

Comments
 (0)