"Fossies" - the Fresh Open Source Software Archive 
Member "nova-22.0.1/nova/tests/unit/virt/libvirt/test_config.py" (19 Nov 2020, 140371 Bytes) of package /linux/misc/openstack/nova-22.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "test_config.py":
22.0.0_vs_22.0.1.
1 # Copyright (C) 2012 Red Hat, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from lxml import etree
16 from oslo_utils.fixture import uuidsentinel as uuids
17 from oslo_utils import units
18
19 from nova.objects import fields as obj_fields
20 from nova import test
21 from nova.tests.unit.virt.libvirt import fake_libvirt_data
22 from nova.virt import hardware
23 from nova.virt.libvirt import config
24
25
26 class LibvirtConfigBaseTest(test.NoDBTestCase):
27 pass
28
29
30 class LibvirtConfigTest(LibvirtConfigBaseTest):
31
32 def test_config_plain(self):
33 obj = config.LibvirtConfigObject(root_name="demo")
34 xml = obj.to_xml()
35
36 self.assertXmlEqual(xml, "<demo/>")
37
38 def test_config_ns(self):
39 obj = config.LibvirtConfigObject(root_name="demo", ns_prefix="foo",
40 ns_uri="http://example.com/foo")
41 xml = obj.to_xml()
42
43 self.assertXmlEqual(xml, """
44 <foo:demo xmlns:foo="http://example.com/foo"/>""")
45
46 def test_config_text(self):
47 obj = config.LibvirtConfigObject(root_name="demo")
48 root = obj.format_dom()
49 root.append(obj._text_node("foo", "bar"))
50
51 xml = etree.tostring(root, encoding='unicode')
52 self.assertXmlEqual(xml, "<demo><foo>bar</foo></demo>")
53
54 def test_config_text_unicode(self):
55 obj = config.LibvirtConfigObject(root_name='demo')
56 root = obj.format_dom()
57 root.append(obj._text_node('foo', u'\xF0\x9F\x92\xA9'))
58 self.assertXmlEqual('<demo><foo>💩</foo></demo>',
59 etree.tostring(root, encoding='unicode'))
60
61 def test_config_text_node_name_attr(self):
62 obj = config.LibvirtConfigObject(root_name='demo')
63 root = obj.format_dom()
64 root.append(obj._text_node('foo', 'bar', name='foobar'))
65 self.assertXmlEqual('<demo><foo name="foobar">bar</foo></demo>',
66 etree.tostring(root, encoding='unicode'))
67
68 def test_config_parse(self):
69 inxml = "<demo><foo/></demo>"
70 obj = config.LibvirtConfigObject(root_name="demo")
71 obj.parse_str(inxml)
72
73
74 class LibvirtConfigCapsTest(LibvirtConfigBaseTest):
75
76 def test_config_host(self):
77 xmlin = """
78 <capabilities>
79 <host>
80 <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
81 <cpu>
82 <arch>x86_64</arch>
83 <model>Opteron_G3</model>
84 <vendor>AMD</vendor>
85 <topology sockets='1' cores='4' threads='1'/>
86 <feature name='ibs'/>
87 <feature name='osvw'/>
88 </cpu>
89 <topology>
90 <cells num='2'>
91 <cell id='0'>
92 <memory unit='KiB'>4048280</memory>
93 <pages unit='KiB' size='4'>1011941</pages>
94 <pages unit='KiB' size='2048'>0</pages>
95 <cpus num='4'>
96 <cpu id='0' socket_id='0' core_id='0' siblings='0'/>
97 <cpu id='1' socket_id='0' core_id='1' siblings='1'/>
98 <cpu id='2' socket_id='0' core_id='2' siblings='2'/>
99 <cpu id='3' socket_id='0' core_id='3' siblings='3'/>
100 </cpus>
101 </cell>
102 <cell id='1'>
103 <memory unit='KiB'>4127684</memory>
104 <pages unit='KiB' size='4'>1031921</pages>
105 <pages unit='KiB' size='2048'>0</pages>
106 <cpus num='4'>
107 <cpu id='4' socket_id='1' core_id='0' siblings='4'/>
108 <cpu id='5' socket_id='1' core_id='1' siblings='5'/>
109 <cpu id='6' socket_id='1' core_id='2' siblings='6'/>
110 <cpu id='7' socket_id='1' core_id='3' siblings='7'/>
111 </cpus>
112 </cell>
113 </cells>
114 </topology>
115 </host>
116 </capabilities>"""
117
118 obj = config.LibvirtConfigCaps()
119 obj.parse_str(xmlin)
120
121 self.assertIsInstance(obj.host, config.LibvirtConfigCapsHost)
122 self.assertEqual(obj.host.uuid, "c7a5fdbd-edaf-9455-926a-d65c16db1809")
123
124 xmlout = obj.to_xml()
125
126 self.assertXmlEqual(xmlin, xmlout)
127
128 def test_config_host_numa_cell_no_memory_caps(self):
129 xmlin = """
130 <cell id='0'>
131 <cpus num='1'>
132 <cpu id='0' socket_id='0' core_id='0' siblings='0'/>
133 </cpus>
134 </cell>"""
135 obj = config.LibvirtConfigCapsNUMACell()
136 obj.parse_str(xmlin)
137 self.assertEqual(0, obj.memory)
138 self.assertEqual(1, len(obj.cpus))
139
140 def test_config_host_numa_cell_no_cpus_caps(self):
141 xmlin = """
142 <cell id='0'>
143 <memory unit='KiB'>128</memory>
144 </cell>"""
145 obj = config.LibvirtConfigCapsNUMACell()
146 obj.parse_str(xmlin)
147 self.assertEqual(128, obj.memory)
148 self.assertEqual(0, len(obj.cpus))
149
150 def test_config_guest(self):
151 xmlin = """
152 <capabilities>
153 <guest>
154 <os_type>hvm</os_type>
155 <arch name='x86_64'>
156 <emulator>/usr/bin/qemu-system-x86_64</emulator>
157 <machine maxCpus='255'>pc-i440fx-2.11</machine>
158 <machine canonical='pc-i440fx-2.11' maxCpus='255'>pc</machine>
159 <machine maxCpus='1'>isapc</machine>
160 <machine maxCpus='255'>pc-1.1</machine>
161 <machine maxCpus='255'>pc-i440fx-2.0</machine>
162 <machine maxCpus='288'>pc-q35-2.11</machine>
163 <machine canonical='pc-q35-2.11' maxCpus='288'>q35</machine>
164 <machine maxCpus='1'>xenpv</machine>
165 <machine maxCpus='288'>pc-q35-2.10</machine>
166 <domain type="qemu" />
167 <domain type="kvm">
168 <emulator>/usr/bin/qemu-kvm</emulator>
169 <machine maxCpus='255'>pc-i440fx-2.11</machine>
170 <machine canonical='pc-i440fx-2.11' maxCpus='255'>pc</machine>
171 <machine maxCpus='1'>isapc</machine>
172 <machine maxCpus='255'>pc-1.1</machine>
173 <machine maxCpus='255'>pc-i440fx-2.0</machine>
174 <machine maxCpus='288'>pc-q35-2.11</machine>
175 <machine canonical='pc-q35-2.11' maxCpus='288'>q35</machine>
176 <machine maxCpus='1'>xenpv</machine>
177 <machine maxCpus='288'>pc-q35-2.10</machine>
178 </domain>
179 </arch>
180 </guest>
181 <guest>
182 <os_type>hvm</os_type>
183 <arch name='i686'>
184 <emulator>/usr/bin/qemu-system-i386</emulator>
185 <machine maxCpus='255'>pc-i440fx-2.11</machine>
186 <machine canonical='pc-i440fx-2.11' maxCpus='255'>pc</machine>
187 <machine maxCpus='1'>isapc</machine>
188 <machine maxCpus='255'>pc-1.1</machine>
189 <machine maxCpus='255'>pc-i440fx-2.0</machine>
190 <machine maxCpus='288'>pc-q35-2.11</machine>
191 <machine canonical='pc-q35-2.11' maxCpus='288'>q35</machine>
192 <machine maxCpus='1'>xenpv</machine>
193 <machine maxCpus='288'>pc-q35-2.10</machine>
194 <domain type="qemu" />
195 <domain type="kvm">
196 <emulator>/usr/bin/qemu-kvm</emulator>
197 <machine maxCpus='255'>pc-i440fx-2.11</machine>
198 <machine canonical='pc-i440fx-2.11' maxCpus='255'>pc</machine>
199 <machine maxCpus='1'>isapc</machine>
200 <machine maxCpus='255'>pc-1.1</machine>
201 <machine maxCpus='255'>pc-i440fx-2.0</machine>
202 <machine maxCpus='288'>pc-q35-2.11</machine>
203 <machine canonical='pc-q35-2.11' maxCpus='288'>q35</machine>
204 <machine maxCpus='1'>xenpv</machine>
205 <machine maxCpus='288'>pc-q35-2.10</machine>
206 </domain>
207 </arch>
208 </guest>
209 </capabilities>"""
210 obj = config.LibvirtConfigCaps()
211 obj.parse_str(xmlin)
212
213 self.assertEqual(2, len(obj.guests))
214 for guest in obj.guests:
215 self.assertIsInstance(guest, config.LibvirtConfigCapsGuest)
216 self.assertEqual('hvm', guest.ostype)
217
218 self.assertEqual('x86_64', obj.guests[0].arch)
219 self.assertEqual('i686', obj.guests[1].arch)
220
221 guest = obj.guests[0]
222 self.assertIn('qemu', guest.domains)
223 self.assertIn('kvm', guest.domains)
224 self.assertEqual('qemu', guest.default_domain.domtype)
225 self.assertEqual('/usr/bin/qemu-system-x86_64',
226 guest.default_domain.emulator)
227 self.assertEqual(guest.default_domain, guest.domains['qemu'])
228 for domtype, domain in guest.domains.items():
229 self.assertEqual(7, len(domain.machines))
230 self.assertIn('pc-i440fx-2.0', domain.machines)
231 self.assertIn('xenpv', domain.machines)
232 self.assertEqual(2, len(domain.aliases))
233 self.assertIn('pc', domain.aliases)
234 self.assertIn('q35', domain.aliases)
235
236 xmlout = obj.to_xml()
237 self.assertXmlEqual(xmlin, xmlout, allow_mixed_nodes=True)
238
239
240 class LibvirtConfigGuestTimerTest(LibvirtConfigBaseTest):
241 def test_config_platform(self):
242 obj = config.LibvirtConfigGuestTimer()
243 obj.track = "host"
244
245 xml = obj.to_xml()
246 self.assertXmlEqual(xml, """
247 <timer name="platform" track="host"/>
248 """)
249
250 def test_config_pit(self):
251 obj = config.LibvirtConfigGuestTimer()
252 obj.name = "pit"
253 obj.tickpolicy = "discard"
254
255 xml = obj.to_xml()
256 self.assertXmlEqual(xml, """
257 <timer name="pit" tickpolicy="discard"/>
258 """)
259
260 def test_config_hpet(self):
261 obj = config.LibvirtConfigGuestTimer()
262 obj.name = "hpet"
263 obj.present = False
264
265 xml = obj.to_xml()
266 self.assertXmlEqual(xml, """
267 <timer name="hpet" present="no"/>
268 """)
269
270
271 class LibvirtConfigGuestClockTest(LibvirtConfigBaseTest):
272 def test_config_utc(self):
273 obj = config.LibvirtConfigGuestClock()
274
275 xml = obj.to_xml()
276 self.assertXmlEqual(xml, """
277 <clock offset="utc"/>
278 """)
279
280 def test_config_localtime(self):
281 obj = config.LibvirtConfigGuestClock()
282 obj.offset = "localtime"
283
284 xml = obj.to_xml()
285 self.assertXmlEqual(xml, """
286 <clock offset="localtime"/>
287 """)
288
289 def test_config_timezone(self):
290 obj = config.LibvirtConfigGuestClock()
291 obj.offset = "timezone"
292 obj.timezone = "EDT"
293
294 xml = obj.to_xml()
295 self.assertXmlEqual(xml, """
296 <clock offset="timezone" timezone="EDT"/>
297 """)
298
299 def test_config_variable(self):
300 obj = config.LibvirtConfigGuestClock()
301 obj.offset = "variable"
302 obj.adjustment = "123456"
303
304 xml = obj.to_xml()
305 self.assertXmlEqual(xml, """
306 <clock offset="variable" adjustment="123456"/>
307 """)
308
309 def test_config_timers(self):
310 obj = config.LibvirtConfigGuestClock()
311
312 tmpit = config.LibvirtConfigGuestTimer()
313 tmpit.name = "pit"
314 tmpit.tickpolicy = "discard"
315
316 tmrtc = config.LibvirtConfigGuestTimer()
317 tmrtc.name = "rtc"
318 tmrtc.tickpolicy = "merge"
319
320 obj.add_timer(tmpit)
321 obj.add_timer(tmrtc)
322
323 xml = obj.to_xml()
324 self.assertXmlEqual(xml, """
325 <clock offset="utc">
326 <timer name="pit" tickpolicy="discard"/>
327 <timer name="rtc" tickpolicy="merge"/>
328 </clock>
329 """)
330
331
332 class LibvirtConfigCPUFeatureTest(LibvirtConfigBaseTest):
333
334 def test_config_simple(self):
335 obj = config.LibvirtConfigCPUFeature("mtrr")
336
337 xml = obj.to_xml()
338 self.assertXmlEqual(xml, """
339 <feature name="mtrr"/>
340 """)
341
342 def test_config_parse_require(self):
343 xml = """
344 <feature name="mtrr" policy="require"/>
345 """
346 xmldoc = etree.fromstring(xml)
347 obj = config.LibvirtConfigCPUFeature()
348 obj.parse_dom(xmldoc)
349
350 self.assertEqual(obj.policy, "require")
351
352 def test_config_parse_disable(self):
353 xml = """
354 <feature name="mtrr" policy="disable"/>
355 """
356 xmldoc = etree.fromstring(xml)
357 obj = config.LibvirtConfigCPUFeature()
358 obj.parse_dom(xmldoc)
359
360 self.assertEqual(obj.policy, "disable")
361
362
363 class LibvirtConfigGuestCPUFeatureTest(LibvirtConfigBaseTest):
364
365 def test_config_simple(self):
366 obj = config.LibvirtConfigGuestCPUFeature("mtrr")
367 obj.policy = "force"
368
369 xml = obj.to_xml()
370 self.assertXmlEqual(xml, """
371 <feature name="mtrr" policy="force"/>
372 """)
373
374 def test_config_simple_pcid(self):
375 obj = config.LibvirtConfigGuestCPUFeature("pcid")
376 obj.policy = "require"
377
378 xml = obj.to_xml()
379 self.assertXmlEqual(xml, """
380 <feature name="pcid" policy="require"/>
381 """)
382
383
384 class LibvirtConfigGuestCPUNUMATest(LibvirtConfigBaseTest):
385
386 def test_parse_dom(self):
387 xml = """
388 <numa>
389 <cell id="0" cpus="0-1" memory="1000000"/>
390 <cell id="1" cpus="2-3" memory="1500000"/>
391 </numa>
392 """
393 xmldoc = etree.fromstring(xml)
394 obj = config.LibvirtConfigGuestCPUNUMA()
395 obj.parse_dom(xmldoc)
396
397 self.assertEqual(2, len(obj.cells))
398
399 def test_config_simple(self):
400 obj = config.LibvirtConfigGuestCPUNUMA()
401
402 cell = config.LibvirtConfigGuestCPUNUMACell()
403 cell.id = 0
404 cell.cpus = set([0, 1])
405 cell.memory = 1000000
406 cell.memAccess = "shared"
407
408 obj.cells.append(cell)
409
410 cell = config.LibvirtConfigGuestCPUNUMACell()
411 cell.id = 1
412 cell.cpus = set([2, 3])
413 cell.memory = 1500000
414 cell.memAccess = "private"
415
416 obj.cells.append(cell)
417
418 xml = obj.to_xml()
419 self.assertXmlEqual(xml, """
420 <numa>
421 <cell id="0" cpus="0-1" memory="1000000" memAccess="shared"/>
422 <cell id="1" cpus="2-3" memory="1500000" memAccess="private"/>
423 </numa>
424 """)
425
426
427 class LibvirtConfigCPUTest(LibvirtConfigBaseTest):
428
429 def test_config_simple(self):
430 obj = config.LibvirtConfigCPU()
431 obj.model = "Penryn"
432
433 xml = obj.to_xml()
434 self.assertXmlEqual(xml, """
435 <cpu>
436 <model>Penryn</model>
437 </cpu>
438 """)
439
440 def test_config_complex(self):
441 obj = config.LibvirtConfigCPU()
442 obj.model = "Penryn"
443 obj.vendor = "Intel"
444 obj.arch = obj_fields.Architecture.X86_64
445
446 obj.add_feature(config.LibvirtConfigCPUFeature("mtrr"))
447 obj.add_feature(config.LibvirtConfigCPUFeature("apic"))
448
449 xml = obj.to_xml()
450 self.assertXmlEqual(xml, """
451 <cpu>
452 <arch>x86_64</arch>
453 <model>Penryn</model>
454 <vendor>Intel</vendor>
455 <feature name="apic"/>
456 <feature name="mtrr"/>
457 </cpu>
458 """)
459
460 def test_config_disabled_features(self):
461 obj = config.LibvirtConfigCPU()
462 obj.model = "Penryn"
463 obj.vendor = "Intel"
464 obj.arch = obj_fields.Architecture.X86_64
465
466 disabled_feature = config.LibvirtConfigCPUFeature("mtrr")
467 disabled_feature.policy = "disable"
468 obj.add_feature(disabled_feature)
469 obj.add_feature(config.LibvirtConfigCPUFeature("apic"))
470
471 xml = obj.to_xml()
472 self.assertXmlEqual(xml, """
473 <cpu>
474 <arch>x86_64</arch>
475 <model>Penryn</model>
476 <vendor>Intel</vendor>
477 <feature name="apic"/>
478 </cpu>
479 """)
480
481 def test_only_uniq_cpu_featues(self):
482 obj = config.LibvirtConfigCPU()
483 obj.model = "Penryn"
484 obj.vendor = "Intel"
485 obj.arch = obj_fields.Architecture.X86_64
486
487 obj.add_feature(config.LibvirtConfigCPUFeature("mtrr"))
488 obj.add_feature(config.LibvirtConfigCPUFeature("apic"))
489 obj.add_feature(config.LibvirtConfigCPUFeature("apic"))
490 obj.add_feature(config.LibvirtConfigCPUFeature("mtrr"))
491
492 xml = obj.to_xml()
493 self.assertXmlEqual(xml, """
494 <cpu>
495 <arch>x86_64</arch>
496 <model>Penryn</model>
497 <vendor>Intel</vendor>
498 <feature name="apic"/>
499 <feature name="mtrr"/>
500 </cpu>
501 """)
502
503 def test_config_topology(self):
504 obj = config.LibvirtConfigCPU()
505 obj.model = "Penryn"
506 obj.sockets = 4
507 obj.cores = 4
508 obj.threads = 2
509
510 xml = obj.to_xml()
511 self.assertXmlEqual(xml, """
512 <cpu>
513 <model>Penryn</model>
514 <topology sockets="4" cores="4" threads="2"/>
515 </cpu>
516 """)
517
518
519 class LibvirtConfigGuestCPUTest(LibvirtConfigBaseTest):
520
521 def test_config_simple(self):
522 obj = config.LibvirtConfigGuestCPU()
523 obj.model = "Penryn"
524
525 xml = obj.to_xml()
526 self.assertXmlEqual(xml, """
527 <cpu match="exact">
528 <model>Penryn</model>
529 </cpu>
530 """)
531
532 def test_config_complex(self):
533 obj = config.LibvirtConfigGuestCPU()
534 obj.model = "Penryn"
535 obj.vendor = "Intel"
536 obj.arch = obj_fields.Architecture.X86_64
537 obj.mode = "custom"
538
539 obj.add_feature(config.LibvirtConfigGuestCPUFeature("mtrr"))
540 obj.add_feature(config.LibvirtConfigGuestCPUFeature("apic"))
541
542 xml = obj.to_xml()
543 self.assertXmlEqual(xml, """
544 <cpu mode="custom" match="exact">
545 <arch>x86_64</arch>
546 <model>Penryn</model>
547 <vendor>Intel</vendor>
548 <feature name="apic" policy="require"/>
549 <feature name="mtrr" policy="require"/>
550 </cpu>
551 """)
552
553 def test_config_host(self):
554 obj = config.LibvirtConfigGuestCPU()
555 obj.mode = "host-model"
556 obj.match = "exact"
557
558 xml = obj.to_xml()
559 self.assertXmlEqual(xml, """
560 <cpu mode="host-model" match="exact"/>
561 """)
562
563 def test_config_host_with_numa(self):
564 obj = config.LibvirtConfigGuestCPU()
565 obj.mode = "host-model"
566 obj.match = "exact"
567
568 numa = config.LibvirtConfigGuestCPUNUMA()
569
570 cell = config.LibvirtConfigGuestCPUNUMACell()
571 cell.id = 0
572 cell.cpus = set([0, 1])
573 cell.memory = 1000000
574 cell.memAccess = "private"
575
576 numa.cells.append(cell)
577
578 cell = config.LibvirtConfigGuestCPUNUMACell()
579 cell.id = 1
580 cell.cpus = set([2, 3])
581 cell.memory = 1500000
582
583 numa.cells.append(cell)
584
585 obj.numa = numa
586
587 xml = obj.to_xml()
588 self.assertXmlEqual(xml, """
589 <cpu mode="host-model" match="exact">
590 <numa>
591 <cell id="0" cpus="0-1" memory="1000000" memAccess="private"/>
592 <cell id="1" cpus="2-3" memory="1500000"/>
593 </numa>
594 </cpu>
595 """)
596
597
598 class LibvirtConfigGuestSMBIOSTest(LibvirtConfigBaseTest):
599
600 def test_config_simple(self):
601 obj = config.LibvirtConfigGuestSMBIOS()
602
603 xml = obj.to_xml()
604 self.assertXmlEqual(xml, """
605 <smbios mode="sysinfo"/>
606 """)
607
608
609 class LibvirtConfigGuestSysinfoTest(LibvirtConfigBaseTest):
610
611 def test_config_simple(self):
612 obj = config.LibvirtConfigGuestSysinfo()
613
614 xml = obj.to_xml()
615 self.assertXmlEqual(xml, """
616 <sysinfo type="smbios"/>
617 """)
618
619 def test_config_bios(self):
620 obj = config.LibvirtConfigGuestSysinfo()
621 obj.bios_vendor = "Acme"
622 obj.bios_version = "6.6.6"
623
624 xml = obj.to_xml()
625 self.assertXmlEqual(xml, """
626 <sysinfo type="smbios">
627 <bios>
628 <entry name="vendor">Acme</entry>
629 <entry name="version">6.6.6</entry>
630 </bios>
631 </sysinfo>
632 """)
633
634 def test_config_system(self):
635 obj = config.LibvirtConfigGuestSysinfo()
636 obj.system_manufacturer = "Acme"
637 obj.system_product = "Wile Coyote"
638 obj.system_version = "6.6.6"
639 obj.system_serial = "123456"
640 obj.system_uuid = "c7a5fdbd-edaf-9455-926a-d65c16db1809"
641 obj.system_family = "Anvils"
642
643 xml = obj.to_xml()
644 self.assertXmlEqual(xml, """
645 <sysinfo type="smbios">
646 <system>
647 <entry name="manufacturer">Acme</entry>
648 <entry name="product">Wile Coyote</entry>
649 <entry name="version">6.6.6</entry>
650 <entry name="serial">123456</entry>
651 <entry name="uuid">c7a5fdbd-edaf-9455-926a-d65c16db1809</entry>
652 <entry name="family">Anvils</entry>
653 </system>
654 </sysinfo>
655 """)
656
657 def test_config_mixed(self):
658 obj = config.LibvirtConfigGuestSysinfo()
659 obj.bios_vendor = "Acme"
660 obj.system_manufacturer = "Acme"
661 obj.system_product = "Wile Coyote"
662 obj.system_uuid = "c7a5fdbd-edaf-9455-926a-d65c16db1809"
663 obj.system_family = "Anvils"
664
665 xml = obj.to_xml()
666 self.assertXmlEqual(xml, """
667 <sysinfo type="smbios">
668 <bios>
669 <entry name="vendor">Acme</entry>
670 </bios>
671 <system>
672 <entry name="manufacturer">Acme</entry>
673 <entry name="product">Wile Coyote</entry>
674 <entry name="uuid">c7a5fdbd-edaf-9455-926a-d65c16db1809</entry>
675 <entry name="family">Anvils</entry>
676 </system>
677 </sysinfo>
678 """)
679
680
681 class LibvirtConfigGuestDiskTest(LibvirtConfigBaseTest):
682
683 def test_config_file(self):
684 obj = config.LibvirtConfigGuestDisk()
685 obj.source_type = "file"
686 obj.source_path = "/tmp/hello"
687 obj.target_dev = "/dev/hda"
688 obj.target_bus = "ide"
689
690 xml = obj.to_xml()
691 self.assertXmlEqual(xml, """
692 <disk type="file" device="disk">
693 <source file="/tmp/hello"/>
694 <target bus="ide" dev="/dev/hda"/>
695 </disk>""")
696
697 def test_config_file_parse(self):
698 xml = """<disk type="file" device="disk">
699 <source file="/tmp/hello"/>
700 <target bus="ide" dev="/dev/hda"/>
701 </disk>"""
702 xmldoc = etree.fromstring(xml)
703
704 obj = config.LibvirtConfigGuestDisk()
705 obj.parse_dom(xmldoc)
706
707 self.assertEqual(obj.source_type, 'file')
708 self.assertEqual(obj.source_path, '/tmp/hello')
709 self.assertEqual(obj.target_dev, '/dev/hda')
710 self.assertEqual(obj.target_bus, 'ide')
711 self.assertFalse(obj.readonly)
712 self.assertFalse(obj.shareable)
713
714 def test_config_file_readonly(self):
715 obj = config.LibvirtConfigGuestDisk()
716 obj.source_type = "file"
717 obj.source_path = "/tmp/hello"
718 obj.target_dev = "/dev/hda"
719 obj.target_bus = "ide"
720 obj.readonly = True
721
722 xml = obj.to_xml()
723 self.assertXmlEqual(xml, """
724 <disk type="file" device="disk">
725 <source file="/tmp/hello"/>
726 <target bus="ide" dev="/dev/hda"/>
727 <readonly/>
728 </disk>""")
729
730 def test_config_file_parse_readonly(self):
731 xml = """<disk type="file" device="disk">
732 <source file="/tmp/hello"/>
733 <target bus="ide" dev="/dev/hda"/>
734 <readonly/>
735 </disk>"""
736 xmldoc = etree.fromstring(xml)
737
738 obj = config.LibvirtConfigGuestDisk()
739 obj.parse_dom(xmldoc)
740
741 self.assertEqual(obj.source_type, 'file')
742 self.assertEqual(obj.source_path, '/tmp/hello')
743 self.assertEqual(obj.target_dev, '/dev/hda')
744 self.assertEqual(obj.target_bus, 'ide')
745 self.assertTrue(obj.readonly)
746 self.assertFalse(obj.shareable)
747
748 def test_config_file_shareable(self):
749 obj = config.LibvirtConfigGuestDisk()
750 obj.source_type = "file"
751 obj.source_path = "/tmp/hello"
752 obj.target_dev = "/dev/hda"
753 obj.target_bus = "ide"
754 obj.shareable = True
755
756 xml = obj.to_xml()
757 self.assertXmlEqual(xml, """
758 <disk type="file" device="disk">
759 <source file="/tmp/hello"/>
760 <target bus="ide" dev="/dev/hda"/>
761 <shareable/>
762 </disk>""")
763
764 def test_config_file_parse_shareable(self):
765 xml = """<disk type="file" device="disk">
766 <source file="/tmp/hello"/>
767 <target bus="ide" dev="/dev/hda"/>
768 <shareable/>
769 </disk>"""
770 xmldoc = etree.fromstring(xml)
771
772 obj = config.LibvirtConfigGuestDisk()
773 obj.parse_dom(xmldoc)
774
775 self.assertEqual(obj.source_type, 'file')
776 self.assertEqual(obj.source_path, '/tmp/hello')
777 self.assertEqual(obj.target_dev, '/dev/hda')
778 self.assertEqual(obj.target_bus, 'ide')
779 self.assertFalse(obj.readonly)
780 self.assertTrue(obj.shareable)
781
782 def test_config_file_serial(self):
783 obj = config.LibvirtConfigGuestDisk()
784 obj.source_type = "file"
785 obj.source_path = "/tmp/hello"
786 obj.target_dev = "/dev/hda"
787 obj.target_bus = "ide"
788 obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9"
789
790 xml = obj.to_xml()
791 self.assertXmlEqual(xml, """
792 <disk type="file" device="disk">
793 <source file="/tmp/hello"/>
794 <target bus="ide" dev="/dev/hda"/>
795 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
796 </disk>""")
797
798 def test_config_file_serial_parse(self):
799 xml = """<disk type="file" device="disk">
800 <source file="/tmp/hello"/>
801 <target bus="ide" dev="/dev/hda"/>
802 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
803 </disk>"""
804 xmldoc = etree.fromstring(xml)
805
806 obj = config.LibvirtConfigGuestDisk()
807 obj.parse_dom(xmldoc)
808
809 self.assertEqual(obj.source_type, 'file')
810 self.assertEqual(obj.serial, '7a97c4a3-6f59-41d4-bf47-191d7f97f8e9')
811
812 def test_config_file_discard(self):
813 obj = config.LibvirtConfigGuestDisk()
814 obj.driver_name = "qemu"
815 obj.driver_format = "qcow2"
816 obj.driver_cache = "none"
817 obj.driver_discard = "unmap"
818 obj.source_type = "file"
819 obj.source_path = "/tmp/hello.qcow2"
820 obj.target_dev = "/dev/hda"
821 obj.target_bus = "ide"
822 obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9"
823
824 xml = obj.to_xml()
825 self.assertXmlEqual("""
826 <disk type="file" device="disk">
827 <driver name="qemu" type="qcow2" cache="none" discard="unmap"/>
828 <source file="/tmp/hello.qcow2"/>
829 <target bus="ide" dev="/dev/hda"/>
830 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
831 </disk>""", xml)
832
833 def test_config_file_discard_parse(self):
834 xml = """
835 <disk type="file" device="disk">
836 <driver name="qemu" type="qcow2" cache="none" discard="unmap"/>
837 <source file="/tmp/hello.qcow2"/>
838 <target bus="ide" dev="/dev/hda"/>
839 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
840 </disk>"""
841 xmldoc = etree.fromstring(xml)
842
843 obj = config.LibvirtConfigGuestDisk()
844 obj.parse_dom(xmldoc)
845
846 self.assertEqual('unmap', obj.driver_discard)
847
848 def test_config_file_io(self):
849 obj = config.LibvirtConfigGuestDisk()
850 obj.driver_name = "qemu"
851 obj.driver_format = "qcow2"
852 obj.driver_cache = "none"
853 obj.driver_io = "native"
854 obj.source_type = "file"
855 obj.source_path = "/tmp/hello.qcow2"
856 obj.target_dev = "/dev/hda"
857 obj.target_bus = "ide"
858 obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9"
859
860 xml = obj.to_xml()
861 self.assertXmlEqual("""
862 <disk type="file" device="disk">
863 <driver name="qemu" type="qcow2" cache="none" io="native"/>
864 <source file="/tmp/hello.qcow2"/>
865 <target bus="ide" dev="/dev/hda"/>
866 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
867 </disk>""", xml)
868
869 def test_config_file_io_parse(self):
870 xml = """
871 <disk type="file" device="disk">
872 <driver name="qemu" type="qcow2" cache="none" io="native"/>
873 <source file="/tmp/hello.qcow2"/>
874 <target bus="ide" dev="/dev/hda"/>
875 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
876 </disk>"""
877 xmldoc = etree.fromstring(xml)
878
879 obj = config.LibvirtConfigGuestDisk()
880 obj.parse_dom(xmldoc)
881
882 self.assertEqual('native', obj.driver_io)
883
884 def test_config_boot_order(self):
885 obj = config.LibvirtConfigGuestDisk()
886 obj.driver_name = "qemu"
887 obj.driver_format = "qcow2"
888 obj.driver_cache = "none"
889 obj.driver_io = "native"
890 obj.source_type = "file"
891 obj.source_path = "/tmp/hello.qcow2"
892 obj.target_dev = "/dev/hda"
893 obj.target_bus = "ide"
894 obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9"
895 obj.boot_order = "1"
896
897 xml = obj.to_xml()
898 self.assertXmlEqual("""
899 <disk type="file" device="disk">
900 <driver name="qemu" type="qcow2" cache="none" io="native"/>
901 <source file="/tmp/hello.qcow2"/>
902 <target bus="ide" dev="/dev/hda"/>
903 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
904 <boot order="1"/>
905 </disk>""", xml)
906
907 def test_config_mirror_parse(self):
908 xml = """
909 <disk type="file" device="disk">
910 <driver name="qemu" type="qcow2" cache="none" discard="unmap"/>
911 <source file="/tmp/hello.qcow2"/>
912 <target bus="ide" dev="/dev/hda"/>
913 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
914 <mirror type='file' file='/tmp/new.img' format='raw' job='copy' ready='yes'>
915 <format type='raw'/>
916 <source file='/tmp/new.img'/>
917 </mirror>
918 <boot order="1"/>
919 </disk>"""
920 xmldoc = etree.fromstring(xml)
921 obj = config.LibvirtConfigGuestDisk()
922 obj.parse_dom(xmldoc)
923 self.assertEqual(obj.mirror.ready, "yes")
924
925 def test_config_disk_encryption_format(self):
926 d = config.LibvirtConfigGuestDisk()
927 e = config.LibvirtConfigGuestDiskEncryption()
928 s = config.LibvirtConfigGuestDiskEncryptionSecret()
929
930 d.driver_name = "qemu"
931 d.driver_format = "qcow2"
932 d.driver_cache = "none"
933 d.driver_io = "native"
934 d.source_type = "file"
935 d.source_path = "/tmp/hello.qcow2"
936 d.target_dev = "/dev/hda"
937 d.target_bus = "ide"
938 d.serial = uuids.serial
939 d.boot_order = "1"
940 e.format = "luks"
941 s.type = "passphrase"
942 s.uuid = uuids.secret
943 e.secret = s
944 d.encryption = e
945
946 xml = d.to_xml()
947 expected_xml = """
948 <disk type="file" device="disk">
949 <driver name="qemu" type="qcow2" cache="none" io="native"/>
950 <source file="/tmp/hello.qcow2"/>
951 <target bus="ide" dev="/dev/hda"/>
952 <serial>%s</serial>
953 <boot order="1"/>
954 <encryption format='luks'>
955 <secret type='passphrase' uuid='%s'/>
956 </encryption>
957 </disk>""" % (uuids.serial, uuids.secret)
958 self.assertXmlEqual(expected_xml, xml)
959
960 def test_config_disk_encryption_parse(self):
961 xml = """
962 <disk type="file" device="disk">
963 <driver name="qemu" type="qcow2" cache="none" io="native"/>
964 <source file="/tmp/hello.qcow2"/>
965 <target bus="ide" dev="/dev/hda"/>
966 <serial>%s</serial>
967 <boot order="1"/>
968 <encryption format='luks'>
969 <secret type='passphrase' uuid='%s'/>
970 </encryption>
971 </disk>""" % (uuids.serial, uuids.secret)
972
973 xmldoc = etree.fromstring(xml)
974 d = config.LibvirtConfigGuestDisk()
975 d.parse_dom(xmldoc)
976
977 self.assertEqual(d.encryption.format, "luks")
978 self.assertEqual(d.encryption.secret.type, "passphrase")
979 self.assertEqual(d.encryption.secret.uuid, uuids.secret)
980
981 def test_config_boot_order_parse(self):
982 xml = """
983 <disk type="file" device="disk">
984 <driver name="qemu" type="qcow2" cache="none" discard="unmap"/>
985 <source file="/tmp/hello.qcow2"/>
986 <target bus="ide" dev="/dev/hda"/>
987 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
988 <boot order="1"/>
989 </disk>"""
990 xmldoc = etree.fromstring(xml)
991 obj = config.LibvirtConfigGuestDisk()
992 obj.parse_dom(xmldoc)
993 self.assertEqual(obj.boot_order, "1")
994
995 def test_config_block(self):
996 obj = config.LibvirtConfigGuestDisk()
997 obj.source_type = "block"
998 obj.source_path = "/tmp/hello"
999 obj.source_device = "cdrom"
1000 obj.driver_name = "qemu"
1001 obj.target_dev = "/dev/hdc"
1002 obj.target_bus = "ide"
1003
1004 xml = obj.to_xml()
1005 self.assertXmlEqual(xml, """
1006 <disk type="block" device="cdrom">
1007 <driver name="qemu"/>
1008 <source dev="/tmp/hello"/>
1009 <target bus="ide" dev="/dev/hdc"/>
1010 </disk>""")
1011
1012 def test_config_block_parse(self):
1013 xml = """<disk type="block" device="cdrom">
1014 <driver name="qemu"/>
1015 <source dev="/tmp/hello"/>
1016 <target bus="ide" dev="/dev/hdc"/>
1017 </disk>"""
1018 xmldoc = etree.fromstring(xml)
1019
1020 obj = config.LibvirtConfigGuestDisk()
1021 obj.parse_dom(xmldoc)
1022
1023 self.assertEqual(obj.source_type, 'block')
1024 self.assertEqual(obj.source_path, '/tmp/hello')
1025 self.assertEqual(obj.target_dev, '/dev/hdc')
1026 self.assertEqual(obj.target_bus, 'ide')
1027
1028 def test_config_network(self):
1029 obj = config.LibvirtConfigGuestDisk()
1030 obj.source_type = "network"
1031 obj.source_protocol = "iscsi"
1032 obj.source_name = "foo.bar.com"
1033 obj.driver_name = "qemu"
1034 obj.driver_format = "qcow2"
1035 obj.target_dev = "/dev/hda"
1036 obj.target_bus = "ide"
1037
1038 xml = obj.to_xml()
1039 self.assertXmlEqual(xml, """
1040 <disk type="network" device="disk">
1041 <driver name="qemu" type="qcow2"/>
1042 <source name="foo.bar.com" protocol="iscsi"/>
1043 <target bus="ide" dev="/dev/hda"/>
1044 </disk>""")
1045
1046 def test_config_network_parse(self):
1047 xml = """<disk type="network" device="disk">
1048 <driver name="qemu" type="qcow2"/>
1049 <source name="foo.bar.com" protocol="iscsi"/>
1050 <target bus="ide" dev="/dev/hda"/>
1051 </disk>"""
1052 xmldoc = etree.fromstring(xml)
1053
1054 obj = config.LibvirtConfigGuestDisk()
1055 obj.parse_dom(xmldoc)
1056
1057 self.assertEqual(obj.source_type, 'network')
1058 self.assertEqual(obj.source_protocol, 'iscsi')
1059 self.assertEqual(obj.source_name, 'foo.bar.com')
1060 self.assertEqual(obj.driver_name, 'qemu')
1061 self.assertEqual(obj.driver_format, 'qcow2')
1062 self.assertEqual(obj.target_dev, '/dev/hda')
1063 self.assertEqual(obj.target_bus, 'ide')
1064
1065 def test_config_network_no_name(self):
1066 obj = config.LibvirtConfigGuestDisk()
1067 obj.source_type = 'network'
1068 obj.source_protocol = 'nbd'
1069 obj.source_hosts = ['foo.bar.com']
1070 obj.source_ports = [None]
1071 obj.driver_name = 'qemu'
1072 obj.driver_format = 'raw'
1073 obj.target_dev = '/dev/vda'
1074 obj.target_bus = 'virtio'
1075
1076 xml = obj.to_xml()
1077 self.assertXmlEqual(xml, """
1078 <disk type="network" device="disk">
1079 <driver name="qemu" type="raw"/>
1080 <source protocol="nbd">
1081 <host name="foo.bar.com"/>
1082 </source>
1083 <target bus="virtio" dev="/dev/vda"/>
1084 </disk>""")
1085
1086 def test_config_network_multihost(self):
1087 obj = config.LibvirtConfigGuestDisk()
1088 obj.source_type = 'network'
1089 obj.source_protocol = 'rbd'
1090 obj.source_name = 'pool/image'
1091 obj.source_hosts = ['foo.bar.com', '::1', '1.2.3.4']
1092 obj.source_ports = [None, '123', '456']
1093 obj.driver_name = 'qemu'
1094 obj.driver_format = 'raw'
1095 obj.target_dev = '/dev/vda'
1096 obj.target_bus = 'virtio'
1097
1098 xml = obj.to_xml()
1099 self.assertXmlEqual(xml, """
1100 <disk type="network" device="disk">
1101 <driver name="qemu" type="raw"/>
1102 <source name="pool/image" protocol="rbd">
1103 <host name="foo.bar.com"/>
1104 <host name="::1" port="123"/>
1105 <host name="1.2.3.4" port="456"/>
1106 </source>
1107 <target bus="virtio" dev="/dev/vda"/>
1108 </disk>""")
1109
1110 def test_config_network_auth(self):
1111 obj = config.LibvirtConfigGuestDisk()
1112 obj.source_type = "network"
1113 obj.source_protocol = "rbd"
1114 obj.source_name = "pool/image"
1115 obj.driver_name = "qemu"
1116 obj.driver_format = "raw"
1117 obj.target_dev = "/dev/vda"
1118 obj.target_bus = "virtio"
1119 obj.auth_username = "foo"
1120 obj.auth_secret_type = "ceph"
1121 obj.auth_secret_uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
1122
1123 xml = obj.to_xml()
1124 self.assertXmlEqual(xml, """
1125 <disk type="network" device="disk">
1126 <driver name="qemu" type="raw"/>
1127 <source name="pool/image" protocol="rbd"/>
1128 <auth username="foo">
1129 <secret type="ceph"
1130 uuid="b38a3f43-4be2-4046-897f-b67c2f5e0147"/>
1131 </auth>
1132 <target bus="virtio" dev="/dev/vda"/>
1133 </disk>""")
1134
1135 def test_config_iotune(self):
1136 obj = config.LibvirtConfigGuestDisk()
1137 obj.source_type = "file"
1138 obj.source_path = "/tmp/hello"
1139 obj.target_dev = "/dev/hda"
1140 obj.target_bus = "ide"
1141 # Note that read/write iops/bytes values cannot be used with
1142 # total values. These are only here for illustrative purposes.
1143 obj.disk_read_bytes_sec = 1024000
1144 obj.disk_read_iops_sec = 1000
1145 obj.disk_total_bytes_sec = 2048000
1146 obj.disk_write_bytes_sec = 1024000
1147 obj.disk_write_iops_sec = 1000
1148 obj.disk_total_iops_sec = 2000
1149 obj.disk_write_bytes_sec_max = 1536000
1150 obj.disk_write_iops_sec_max = 1500
1151 obj.disk_total_iops_sec_max = 3072000
1152 obj.disk_read_bytes_sec_max = 1536000
1153 obj.disk_read_iops_sec_max = 1500
1154 obj.disk_total_iops_sec_max = 2000
1155 obj.disk_total_bytes_sec_max = 3072000
1156 obj.disk_size_iops_sec = 16
1157
1158 xml = obj.to_xml()
1159 self.assertXmlEqual(xml, """
1160 <disk type="file" device="disk">
1161 <source file="/tmp/hello"/>
1162 <target bus="ide" dev="/dev/hda"/>
1163 <iotune>
1164 <read_bytes_sec>1024000</read_bytes_sec>
1165 <read_iops_sec>1000</read_iops_sec>
1166 <write_bytes_sec>1024000</write_bytes_sec>
1167 <write_iops_sec>1000</write_iops_sec>
1168 <total_bytes_sec>2048000</total_bytes_sec>
1169 <total_iops_sec>2000</total_iops_sec>
1170 <read_bytes_sec_max>1536000</read_bytes_sec_max>
1171 <write_bytes_sec_max>1536000</write_bytes_sec_max>
1172 <total_bytes_sec_max>3072000</total_bytes_sec_max>
1173 <read_iops_sec_max>1500</read_iops_sec_max>
1174 <write_iops_sec_max>1500</write_iops_sec_max>
1175 <total_iops_sec_max>2000</total_iops_sec_max>
1176 <size_iops_sec>16</size_iops_sec>
1177 </iotune>
1178 </disk>""")
1179
1180 def test_config_blockio(self):
1181 obj = config.LibvirtConfigGuestDisk()
1182 obj.source_type = "file"
1183 obj.source_path = "/tmp/hello"
1184 obj.target_dev = "/dev/hda"
1185 obj.target_bus = "ide"
1186 obj.logical_block_size = "4096"
1187 obj.physical_block_size = "4096"
1188
1189 xml = obj.to_xml()
1190 self.assertXmlEqual("""
1191 <disk type="file" device="disk">
1192 <source file="/tmp/hello"/>
1193 <target bus="ide" dev="/dev/hda"/>
1194 <blockio logical_block_size="4096" physical_block_size="4096"/>
1195 </disk>""", xml)
1196
1197 def test_config_disk_device_address(self):
1198 xml = """
1199 <disk type='file' device='disk'>
1200 <driver name='qemu' type='qcow2'/>
1201 <source file='/var/lib/libvirt/images/centos.qcow2'/>
1202 <target dev='vda' bus='virtio'/>
1203 <address type='pci' domain='0x0000' bus='0x00' slot='0x09'
1204 function='0x0'/>
1205 </disk>"""
1206
1207 obj = config.LibvirtConfigGuestDisk()
1208 obj.parse_str(xml)
1209
1210 self.assertIsInstance(obj.device_addr,
1211 config.LibvirtConfigGuestDeviceAddressPCI)
1212 self.assertEqual('0000:00:09.0', obj.device_addr.format_address())
1213
1214 def test_config_disk_device_address_no_format(self):
1215 xml = """
1216 <disk type='file' device='disk'>
1217 <driver name='qemu' type='qcow2'/>
1218 <source file='/var/lib/libvirt/images/generic.qcow2'/>
1219 <target dev='sda' bus='scsi'/>
1220 <address type='drive' controller='0' bus='0'
1221 target='0' unit='1'/>
1222 </disk>"""
1223
1224 obj = config.LibvirtConfigGuestDisk()
1225 obj.parse_str(xml)
1226
1227 self.assertIsInstance(obj.device_addr,
1228 config.LibvirtConfigGuestDeviceAddressDrive)
1229 self.assertEqual(('0', '0', '0', '1'), (obj.device_addr.controller,
1230 obj.device_addr.bus,
1231 obj.device_addr.target,
1232 obj.device_addr.unit))
1233 self.assertIsNone(obj.device_addr.format_address())
1234
1235 def test_config_disk_device_address_drive(self):
1236 obj = config.LibvirtConfigGuestDeviceAddressDrive()
1237 obj.controller = 1
1238 obj.bus = 2
1239 obj.target = 3
1240 obj.unit = 4
1241
1242 xml = """
1243 <address type="drive" controller="1" bus="2" target="3" unit="4"/>
1244 """
1245 self.assertXmlEqual(xml, obj.to_xml())
1246
1247 def test_config_disk_device_address_drive_added(self):
1248 obj = config.LibvirtConfigGuestDisk()
1249 obj.source_type = "file"
1250 obj.source_path = "/tmp/hello"
1251 obj.target_dev = "/dev/hda"
1252 obj.target_bus = "scsi"
1253 obj.device_addr = config.LibvirtConfigGuestDeviceAddressDrive()
1254 obj.device_addr.controller = 1
1255 obj.device_addr.bus = 2
1256 obj.device_addr.target = 3
1257 obj.device_addr.unit = 4
1258
1259 self.assertXmlEqual("""
1260 <disk type="file" device="disk">
1261 <source file="/tmp/hello"/>
1262 <target bus="scsi" dev="/dev/hda"/>
1263 <address type="drive" controller="1" bus="2" target="3" unit="4"/>
1264 </disk>""", obj.to_xml())
1265
1266 def test_config_disk_device_address_pci(self):
1267 obj = config.LibvirtConfigGuestDeviceAddressPCI()
1268 obj.domain = 1
1269 obj.bus = 2
1270 obj.slot = 3
1271 obj.function = 4
1272
1273 xml = """
1274 <address type="pci" domain="1" bus="2" slot="3" function="4"/>
1275 """
1276 self.assertXmlEqual(xml, obj.to_xml())
1277
1278 def test_config_disk_device_address_pci_added(self):
1279 obj = config.LibvirtConfigGuestDisk()
1280 obj.source_type = "network"
1281 obj.source_name = "volumes/volume-0"
1282 obj.source_protocol = "rbd"
1283 obj.source_hosts = ["192.168.1.1"]
1284 obj.source_ports = ["1234"]
1285 obj.target_dev = "hdb"
1286 obj.target_bus = "virtio"
1287 obj.device_addr = config.LibvirtConfigGuestDeviceAddressPCI()
1288 obj.device_addr.domain = 1
1289 obj.device_addr.bus = 2
1290 obj.device_addr.slot = 3
1291 obj.device_addr.function = 4
1292
1293 self.assertXmlEqual("""
1294 <disk type="network" device="disk">
1295 <source protocol="rbd" name="volumes/volume-0">
1296 <host name="192.168.1.1" port="1234"/>
1297 </source>
1298 <target bus="virtio" dev="hdb"/>
1299 <address type="pci" domain="1" bus="2" slot="3" function="4"/>
1300 </disk>""", obj.to_xml())
1301
1302 def test_config_disk_device_address_type_virtio_mmio(self):
1303 xml = """
1304 <disk type='file' device='disk'>
1305 <driver name='qemu' type='qcow2' cache='none'/>
1306 <source file='/var/lib/libvirt/images/generic.qcow2'/>
1307 <target dev='vda' bus='virtio'/>
1308 <address type='virtio-mmio'/>
1309 </disk>"""
1310
1311 obj = config.LibvirtConfigGuestDisk()
1312 obj.parse_str(xml)
1313
1314 self.assertNotIsInstance(obj.device_addr,
1315 config.LibvirtConfigGuestDeviceAddressPCI)
1316 self.assertNotIsInstance(obj.device_addr,
1317 config.LibvirtConfigGuestDeviceAddressDrive)
1318
1319 def test_config_disk_device_address_type_ccw(self):
1320 xml = """
1321 <disk type='file' device='disk'>
1322 <driver name='qemu' type='qcow2'/>
1323 <source file='/var/lib/libvirt/images/test.qcow2'/>
1324 <backingStore/>
1325 <target dev='vda' bus='virtio'/>
1326 <alias name='virtio-disk0'/>
1327 <address type='ccw' cssid='0xfe' ssid='0x0' devno='0x0000'/>
1328 </disk>"""
1329
1330 obj = config.LibvirtConfigGuestDisk()
1331 obj.parse_str(xml)
1332
1333 self.assertNotIsInstance(obj.device_addr,
1334 config.LibvirtConfigGuestDeviceAddressPCI)
1335 self.assertNotIsInstance(obj.device_addr,
1336 config.LibvirtConfigGuestDeviceAddressDrive)
1337
1338
1339 class LibvirtConfigGuestSnapshotDiskTest(LibvirtConfigBaseTest):
1340
1341 def test_config_file(self):
1342 obj = config.LibvirtConfigGuestDisk()
1343 obj.source_type = "file"
1344 obj.source_path = "/tmp/hello"
1345 obj.target_dev = "/dev/hda"
1346 obj.target_bus = "ide"
1347
1348 xml = obj.to_xml()
1349 self.assertXmlEqual(xml, """
1350 <disk type="file" device="disk">
1351 <source file="/tmp/hello"/>
1352 <target bus="ide" dev="/dev/hda"/>
1353 </disk>""")
1354
1355 def test_config_file_parse(self):
1356 xml = """<disk type="file" device="disk">
1357 <source file="/tmp/hello"/>
1358 <target bus="ide" dev="/dev/hda"/>
1359 </disk>"""
1360 xmldoc = etree.fromstring(xml)
1361
1362 obj = config.LibvirtConfigGuestDisk()
1363 obj.parse_dom(xmldoc)
1364
1365 self.assertEqual(obj.source_type, 'file')
1366 self.assertEqual(obj.source_path, '/tmp/hello')
1367 self.assertEqual(obj.target_dev, '/dev/hda')
1368 self.assertEqual(obj.target_bus, 'ide')
1369
1370
1371 class LibvirtConfigGuestDiskBackingStoreTest(LibvirtConfigBaseTest):
1372
1373 def test_config_file_parse(self):
1374 xml = """<backingStore type='file'>
1375 <driver name='qemu' type='qcow2'/>
1376 <source file='/var/lib/libvirt/images/mid.qcow2'/>
1377 <backingStore type='file'>
1378 <driver name='qemu' type='qcow2'/>
1379 <source file='/var/lib/libvirt/images/base.qcow2'/>
1380 <backingStore/>
1381 </backingStore>
1382 </backingStore>
1383 """
1384 xmldoc = etree.fromstring(xml)
1385
1386 obj = config.LibvirtConfigGuestDiskBackingStore()
1387 obj.parse_dom(xmldoc)
1388
1389 self.assertEqual(obj.driver_name, 'qemu')
1390 self.assertEqual(obj.driver_format, 'qcow2')
1391 self.assertEqual(obj.source_type, 'file')
1392 self.assertEqual(obj.source_file, '/var/lib/libvirt/images/mid.qcow2')
1393 self.assertEqual(obj.backing_store.driver_name, 'qemu')
1394 self.assertEqual(obj.backing_store.source_type, 'file')
1395 self.assertEqual(obj.backing_store.source_file,
1396 '/var/lib/libvirt/images/base.qcow2')
1397 self.assertIsNone(obj.backing_store.backing_store)
1398
1399 def test_config_network_parse(self):
1400 xml = """<backingStore type='network' index='1'>
1401 <format type='qcow2'/>
1402 <source protocol='netfs' name='volume1/img1'>
1403 <host name='host1' port='24007'/>
1404 </source>
1405 <backingStore type='network' index='2'>
1406 <format type='qcow2'/>
1407 <source protocol='netfs' name='volume1/img2'>
1408 <host name='host1' port='24007'/>
1409 </source>
1410 <backingStore/>
1411 </backingStore>
1412 </backingStore>
1413 """
1414 xmldoc = etree.fromstring(xml)
1415
1416 obj = config.LibvirtConfigGuestDiskBackingStore()
1417 obj.parse_dom(xmldoc)
1418
1419 self.assertEqual(obj.source_type, 'network')
1420 self.assertEqual(obj.source_protocol, 'netfs')
1421 self.assertEqual(obj.source_name, 'volume1/img1')
1422 self.assertEqual(obj.source_hosts[0], 'host1')
1423 self.assertEqual(obj.source_ports[0], '24007')
1424 self.assertEqual(obj.index, '1')
1425 self.assertEqual(obj.backing_store.source_name, 'volume1/img2')
1426 self.assertEqual(obj.backing_store.index, '2')
1427 self.assertEqual(obj.backing_store.source_hosts[0], 'host1')
1428 self.assertEqual(obj.backing_store.source_ports[0], '24007')
1429 self.assertIsNone(obj.backing_store.backing_store)
1430
1431
1432 class LibvirtConfigGuestFilesysTest(LibvirtConfigBaseTest):
1433
1434 def test_config_mount(self):
1435 obj = config.LibvirtConfigGuestFilesys()
1436 obj.source_type = "mount"
1437 obj.source_dir = "/tmp/hello"
1438 obj.target_dir = "/mnt"
1439
1440 xml = obj.to_xml()
1441 self.assertXmlEqual(xml, """
1442 <filesystem type="mount">
1443 <source dir="/tmp/hello"/>
1444 <target dir="/mnt"/>
1445 </filesystem>""")
1446
1447 def test_config_block(self):
1448 obj = config.LibvirtConfigGuestFilesys()
1449 obj.source_type = "block"
1450 obj.source_dev = "/dev/sdb"
1451 obj.target_dir = "/mnt"
1452
1453 xml = obj.to_xml()
1454 self.assertXmlEqual(xml, """
1455 <filesystem type="block">
1456 <source dev="/dev/sdb"/>
1457 <target dir="/mnt"/>
1458 </filesystem>""")
1459
1460 def test_config_file(self):
1461 obj = config.LibvirtConfigGuestFilesys()
1462 obj.source_type = "file"
1463 obj.source_file = "/data/myimage.qcow2"
1464 obj.driver_type = "nbd"
1465 obj.driver_format = "qcow2"
1466 obj.target_dir = "/mnt"
1467
1468 xml = obj.to_xml()
1469 self.assertXmlEqual(xml, """
1470 <filesystem type="file">
1471 <driver format="qcow2" type="nbd"/>
1472 <source file="/data/myimage.qcow2"/>
1473 <target dir="/mnt"/>
1474 </filesystem>""")
1475
1476 def test_parse_mount(self):
1477 xmldoc = """
1478 <filesystem type="mount">
1479 <source dir="/tmp/hello"/>
1480 <target dir="/mnt"/>
1481 </filesystem>
1482 """
1483 obj = config.LibvirtConfigGuestFilesys()
1484 obj.parse_str(xmldoc)
1485 self.assertEqual('mount', obj.source_type)
1486 self.assertEqual('/tmp/hello', obj.source_dir)
1487 self.assertEqual('/mnt', obj.target_dir)
1488
1489 def test_parse_block(self):
1490 xmldoc = """
1491 <filesystem type="block">
1492 <source dev="/dev/sdb"/>
1493 <target dir="/mnt"/>
1494 </filesystem>
1495 """
1496 obj = config.LibvirtConfigGuestFilesys()
1497 obj.parse_str(xmldoc)
1498 self.assertEqual('block', obj.source_type)
1499 self.assertEqual('/dev/sdb', obj.source_dev)
1500 self.assertEqual('/mnt', obj.target_dir)
1501
1502 def test_parse_file(self):
1503 xmldoc = """
1504 <filesystem type="file">
1505 <driver format="qcow2" type="nbd"/>
1506 <source file="/data/myimage.qcow2"/>
1507 <target dir="/mnt"/>
1508 </filesystem>
1509 """
1510 obj = config.LibvirtConfigGuestFilesys()
1511 obj.parse_str(xmldoc)
1512 self.assertEqual('file', obj.source_type)
1513 self.assertEqual('qcow2', obj.driver_format)
1514 self.assertEqual('nbd', obj.driver_type)
1515 self.assertEqual('/data/myimage.qcow2', obj.source_file)
1516 self.assertEqual('/mnt', obj.target_dir)
1517
1518
1519 class LibvirtConfigGuestInputTest(LibvirtConfigBaseTest):
1520
1521 def test_config_tablet(self):
1522 obj = config.LibvirtConfigGuestInput()
1523
1524 xml = obj.to_xml()
1525 self.assertXmlEqual(xml, """
1526 <input type="tablet" bus="usb"/>""")
1527
1528 def test_config_input(self):
1529 obj = config.LibvirtConfigGuestInput()
1530 obj.type = "mouse"
1531 obj.bus = "virtio"
1532 obj.driver_iommu = True
1533
1534 xml = obj.to_xml()
1535 self.assertXmlEqual("""
1536 <input type="mouse" bus="virtio">
1537 <driver iommu="on" />
1538 </input>""", xml)
1539
1540
1541 class LibvirtConfigGuestGraphicsTest(LibvirtConfigBaseTest):
1542
1543 def test_config_graphics(self):
1544 obj = config.LibvirtConfigGuestGraphics()
1545 obj.type = "vnc"
1546 obj.autoport = True
1547 obj.keymap = "en_US"
1548 obj.listen = "127.0.0.1"
1549
1550 xml = obj.to_xml()
1551 self.assertXmlEqual(xml, """
1552 <graphics type="vnc" autoport="yes" keymap="en_US" listen="127.0.0.1"/>
1553 """)
1554
1555
1556 class LibvirtConfigGuestHostdev(LibvirtConfigBaseTest):
1557
1558 def test_config_pci_guest_host_dev(self):
1559 obj = config.LibvirtConfigGuestHostdev(mode='subsystem', type='pci')
1560 xml = obj.to_xml()
1561 expected = """
1562 <hostdev mode="subsystem" type="pci" managed="yes"/>
1563 """
1564 self.assertXmlEqual(xml, expected)
1565
1566 def test_parse_GuestHostdev(self):
1567 xmldoc = """<hostdev mode="subsystem" type="pci" managed="yes"/>"""
1568 obj = config.LibvirtConfigGuestHostdev()
1569 obj.parse_str(xmldoc)
1570 self.assertEqual(obj.mode, 'subsystem')
1571 self.assertEqual(obj.type, 'pci')
1572 self.assertEqual(obj.managed, 'yes')
1573
1574 def test_parse_GuestHostdev_non_pci(self):
1575 xmldoc = """<hostdev mode="subsystem" type="usb" managed="no"/>"""
1576 obj = config.LibvirtConfigGuestHostdev()
1577 obj.parse_str(xmldoc)
1578 self.assertEqual(obj.mode, 'subsystem')
1579 self.assertEqual(obj.type, 'usb')
1580 self.assertEqual(obj.managed, 'no')
1581
1582
1583 class LibvirtConfigGuestHostdevPCI(LibvirtConfigBaseTest):
1584
1585 expected = """
1586 <hostdev mode="subsystem" type="pci" managed="yes">
1587 <source>
1588 <address bus="0x11" domain="0x1234" function="0x3"
1589 slot="0x22" />
1590 </source>
1591 </hostdev>
1592 """
1593
1594 def test_config_guest_hosdev_pci(self):
1595 hostdev = config.LibvirtConfigGuestHostdevPCI()
1596 hostdev.domain = "1234"
1597 hostdev.bus = "11"
1598 hostdev.slot = "22"
1599 hostdev.function = "3"
1600 xml = hostdev.to_xml()
1601 self.assertXmlEqual(self.expected, xml)
1602
1603 def test_parse_guest_hosdev_pci(self):
1604 xmldoc = self.expected
1605 obj = config.LibvirtConfigGuestHostdevPCI()
1606 obj.parse_str(xmldoc)
1607 self.assertEqual(obj.mode, 'subsystem')
1608 self.assertEqual(obj.type, 'pci')
1609 self.assertEqual(obj.managed, 'yes')
1610 self.assertEqual(obj.domain, '0x1234')
1611 self.assertEqual(obj.bus, '0x11')
1612 self.assertEqual(obj.slot, '0x22')
1613 self.assertEqual(obj.function, '0x3')
1614
1615 def test_parse_guest_hosdev_usb(self):
1616 xmldoc = """<hostdev mode='subsystem' type='usb'>
1617 <source startupPolicy='optional'>
1618 <vendor id='0x1234'/>
1619 <product id='0xbeef'/>
1620 </source>
1621 <boot order='2'/>
1622 </hostdev>"""
1623 obj = config.LibvirtConfigGuestHostdevPCI()
1624 obj.parse_str(xmldoc)
1625 self.assertEqual(obj.mode, 'subsystem')
1626 self.assertEqual(obj.type, 'usb')
1627
1628
1629 class LibvirtConfigGuestHostdevMDEV(LibvirtConfigBaseTest):
1630
1631 expected = """
1632 <hostdev mode='subsystem' type='mdev' model='vfio-pci'
1633 managed='no'>
1634 <source>
1635 <address uuid="b38a3f43-4be2-4046-897f-b67c2f5e0140" />
1636 </source>
1637 </hostdev>
1638 """
1639
1640 def test_config_guest_hostdev_mdev(self):
1641 hostdev = config.LibvirtConfigGuestHostdevMDEV()
1642 hostdev.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0140"
1643 xml = hostdev.to_xml()
1644 self.assertXmlEqual(self.expected, xml)
1645
1646 def test_parse_guest_hostdev_mdev(self):
1647 xmldoc = self.expected
1648 obj = config.LibvirtConfigGuestHostdevMDEV()
1649 obj.parse_str(xmldoc)
1650 self.assertEqual(obj.mode, 'subsystem')
1651 self.assertEqual(obj.type, 'mdev')
1652 self.assertEqual(obj.managed, 'no')
1653 self.assertEqual(obj.model, 'vfio-pci')
1654 self.assertEqual(obj.uuid, 'b38a3f43-4be2-4046-897f-b67c2f5e0140')
1655
1656
1657 class LibvirtConfigGuestCharDeviceLog(LibvirtConfigBaseTest):
1658
1659 def test_config_log(self):
1660 obj = config.LibvirtConfigGuestCharDeviceLog()
1661 obj.file = "/tmp/guestname-logd.log"
1662 obj.append = "on"
1663
1664 xml = obj.to_xml()
1665 self.assertXmlEqual(xml, """
1666 <log file="/tmp/guestname-logd.log" append="on"/>""")
1667
1668 # create a new object from the XML and check it again
1669 obj2 = config.LibvirtConfigGuestCharDeviceLog()
1670 obj2.parse_str(xml)
1671 self.assertXmlEqual(xml, obj2.to_xml())
1672
1673
1674 class LibvirtConfigGuestSerialTest(LibvirtConfigBaseTest):
1675
1676 def test_config_file(self):
1677 obj = config.LibvirtConfigGuestSerial()
1678 obj.type = "file"
1679 obj.source_path = "/tmp/vm.log"
1680
1681 xml = obj.to_xml()
1682 self.assertXmlEqual(xml, """
1683 <serial type="file">
1684 <source path="/tmp/vm.log"/>
1685 </serial>""")
1686
1687 def test_config_serial_port(self):
1688 obj = config.LibvirtConfigGuestSerial()
1689 obj.type = "tcp"
1690 obj.listen_port = 11111
1691 obj.listen_host = "0.0.0.0"
1692
1693 xml = obj.to_xml()
1694 self.assertXmlEqual(xml, """
1695 <serial type="tcp">
1696 <source host="0.0.0.0" service="11111" mode="bind"/>
1697 </serial>""")
1698
1699 def test_config_log(self):
1700 log = config.LibvirtConfigGuestCharDeviceLog()
1701 log.file = "/tmp/guestname-logd.log"
1702 log.append = "off"
1703
1704 device = config.LibvirtConfigGuestSerial()
1705 device.type = "tcp"
1706 device.listen_port = 11111
1707 device.listen_host = "0.0.0.0"
1708 device.log = log
1709
1710 xml = device.to_xml()
1711 self.assertXmlEqual(xml, """
1712 <serial type="tcp">
1713 <source host="0.0.0.0" service="11111" mode="bind"/>
1714 <log file="/tmp/guestname-logd.log" append="off"/>
1715 </serial>""")
1716
1717
1718 class LibvirtConfigGuestConsoleTest(LibvirtConfigBaseTest):
1719 def test_config_pty(self):
1720 obj = config.LibvirtConfigGuestConsole()
1721 obj.type = "pty"
1722
1723 xml = obj.to_xml()
1724 self.assertXmlEqual(xml, """
1725 <console type="pty"/>""")
1726
1727 def test_config_target_type(self):
1728 obj = config.LibvirtConfigGuestConsole()
1729 obj.type = "pty"
1730 obj.target_type = "sclp"
1731
1732 xml = obj.to_xml()
1733 self.assertXmlEqual(xml, """
1734 <console type="pty">
1735 <target type="sclp"/>
1736 </console>
1737 """)
1738
1739 def test_config_target_port(self):
1740 obj = config.LibvirtConfigGuestConsole()
1741 obj.target_port = 0
1742
1743 xml = obj.to_xml()
1744 self.assertXmlEqual(xml, """
1745 <console type="pty">
1746 <target port="0"/>
1747 </console>
1748 """)
1749
1750 def test_config_log(self):
1751 log = config.LibvirtConfigGuestCharDeviceLog()
1752 log.file = "/tmp/guestname-logd.log"
1753 log.append = "off"
1754
1755 device = config.LibvirtConfigGuestConsole()
1756 device.type = "tcp"
1757 device.listen_port = 11111
1758 device.listen_host = "0.0.0.0"
1759 device.log = log
1760
1761 xml = device.to_xml()
1762 self.assertXmlEqual(xml, """
1763 <console type="tcp">
1764 <source host="0.0.0.0" service="11111" mode="bind"/>
1765 <log file="/tmp/guestname-logd.log" append="off"/>
1766 </console>""")
1767
1768
1769 class LibvirtConfigGuestChannelTest(LibvirtConfigBaseTest):
1770 def test_config_spice_minimal(self):
1771 obj = config.LibvirtConfigGuestChannel()
1772 obj.type = "spicevmc"
1773
1774 xml = obj.to_xml()
1775 self.assertXmlEqual(xml, """
1776 <channel type="spicevmc">
1777 <target type='virtio'/>
1778 </channel>""")
1779
1780 def test_config_spice_full(self):
1781 obj = config.LibvirtConfigGuestChannel()
1782 obj.type = "spicevmc"
1783 obj.target_name = "com.redhat.spice.0"
1784
1785 xml = obj.to_xml()
1786 self.assertXmlEqual(xml, """
1787 <channel type="spicevmc">
1788 <target type='virtio' name='com.redhat.spice.0'/>
1789 </channel>""")
1790
1791 def test_config_qga_full(self):
1792 obj = config.LibvirtConfigGuestChannel()
1793 obj.type = "unix"
1794 obj.target_name = "org.qemu.guest_agent.0"
1795 obj.source_path = "/var/lib/libvirt/qemu/%s.%s.sock" % (
1796 obj.target_name, "instance-name")
1797
1798 xml = obj.to_xml()
1799 self.assertXmlEqual(xml, """
1800 <channel type="unix">
1801 <source path="%s" mode="bind"/>
1802 <target type="virtio" name="org.qemu.guest_agent.0"/>
1803 </channel>""" % obj.source_path)
1804
1805
1806 class LibvirtConfigGuestInterfaceTest(LibvirtConfigBaseTest):
1807 def test_config_ethernet(self):
1808 obj = config.LibvirtConfigGuestInterface()
1809 obj.net_type = "ethernet"
1810 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
1811 obj.model = "virtio"
1812 obj.target_dev = "vnet0"
1813 obj.driver_name = "vhost"
1814 obj.vif_inbound_average = 16384
1815 obj.vif_inbound_peak = 32768
1816 obj.vif_inbound_burst = 3276
1817 obj.vif_outbound_average = 32768
1818 obj.vif_outbound_peak = 65536
1819 obj.vif_outbound_burst = 6553
1820
1821 xml = obj.to_xml()
1822 self.assertXmlEqual(xml, """
1823 <interface type="ethernet">
1824 <mac address="DE:AD:BE:EF:CA:FE"/>
1825 <model type="virtio"/>
1826 <driver name="vhost"/>
1827 <target dev="vnet0"/>
1828 <bandwidth>
1829 <inbound average="16384" peak="32768" burst="3276"/>
1830 <outbound average="32768" peak="65536" burst="6553"/>
1831 </bandwidth>
1832 </interface>""")
1833
1834 # parse the xml from the first object into a new object and make sure
1835 # they are the same
1836 obj2 = config.LibvirtConfigGuestInterface()
1837 obj2.parse_str(xml)
1838 self.assertXmlEqual(xml, obj2.to_xml())
1839
1840 def test_config_ethernet_with_mtu(self):
1841 obj = config.LibvirtConfigGuestInterface()
1842 obj.net_type = "ethernet"
1843 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
1844 obj.model = "virtio"
1845 obj.target_dev = "vnet0"
1846 obj.driver_name = "vhost"
1847 obj.vif_inbound_average = 16384
1848 obj.vif_inbound_peak = 32768
1849 obj.vif_inbound_burst = 3276
1850 obj.vif_outbound_average = 32768
1851 obj.vif_outbound_peak = 65536
1852 obj.vif_outbound_burst = 6553
1853 obj.mtu = 9000
1854
1855 xml = obj.to_xml()
1856 self.assertXmlEqual(xml, """
1857 <interface type="ethernet">
1858 <mac address="DE:AD:BE:EF:CA:FE"/>
1859 <model type="virtio"/>
1860 <driver name="vhost"/>
1861 <mtu size="9000"/>
1862 <target dev="vnet0"/>
1863 <bandwidth>
1864 <inbound average="16384" peak="32768" burst="3276"/>
1865 <outbound average="32768" peak="65536" burst="6553"/>
1866 </bandwidth>
1867 </interface>""")
1868
1869 # parse the xml from the first object into a new object and make sure
1870 # they are the same
1871 obj2 = config.LibvirtConfigGuestInterface()
1872 obj2.parse_str(xml)
1873 self.assertXmlEqual(xml, obj2.to_xml())
1874
1875 def _get_virtio_interface(self):
1876 obj = config.LibvirtConfigGuestInterface()
1877 obj.net_type = "ethernet"
1878 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
1879 obj.model = "virtio"
1880 obj.target_dev = "vnet0"
1881 self.assertTrue(obj.uses_virtio)
1882 return obj
1883
1884 def test_config_driver_options(self):
1885 obj = self._get_virtio_interface()
1886 obj.driver_name = "vhost"
1887 obj.vhost_queues = 4
1888 obj.driver_iommu = True
1889
1890 xml = obj.to_xml()
1891 self.assertXmlEqual(xml, """
1892 <interface type="ethernet">
1893 <mac address="DE:AD:BE:EF:CA:FE"/>
1894 <model type="virtio"/>
1895 <driver name="vhost" queues="4" iommu="on"/>
1896 <target dev="vnet0"/>
1897 </interface>""")
1898
1899 # parse the xml from the first object into a new object and make sure
1900 # they are the same
1901 obj2 = config.LibvirtConfigGuestInterface()
1902 obj2.parse_str(xml)
1903 self.assertXmlEqual(xml, obj2.to_xml())
1904
1905 def test_config_driver_iommu_option(self):
1906 obj = self._get_virtio_interface()
1907 # Check that the <driver> element is included even when there is
1908 # no driver name or queues
1909 obj.driver_iommu = True
1910
1911 xml = obj.to_xml()
1912 self.assertXmlEqual(xml, """
1913 <interface type="ethernet">
1914 <mac address="DE:AD:BE:EF:CA:FE"/>
1915 <model type="virtio"/>
1916 <driver iommu="on"/>
1917 <target dev="vnet0"/>
1918 </interface>""")
1919
1920 # parse the xml from the first object into a new object and make sure
1921 # they are the same
1922 obj2 = config.LibvirtConfigGuestInterface()
1923 obj2.parse_str(xml)
1924 self.assertXmlEqual(xml, obj2.to_xml())
1925
1926 def test_config_bridge(self):
1927 obj = config.LibvirtConfigGuestInterface()
1928 obj.net_type = "bridge"
1929 obj.source_dev = "br0"
1930 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
1931 obj.model = "virtio"
1932 obj.target_dev = "tap12345678"
1933 obj.filtername = "clean-traffic"
1934 obj.filterparams.append({"key": "IP", "value": "192.168.122.1"})
1935 obj.vif_inbound_average = 16384
1936 obj.vif_inbound_peak = 32768
1937 obj.vif_inbound_burst = 3276
1938 obj.vif_outbound_average = 32768
1939 obj.vif_outbound_peak = 65536
1940 obj.vif_outbound_burst = 6553
1941
1942 xml = obj.to_xml()
1943 self.assertXmlEqual(xml, """
1944 <interface type="bridge">
1945 <mac address="DE:AD:BE:EF:CA:FE"/>
1946 <model type="virtio"/>
1947 <source bridge="br0"/>
1948 <target dev="tap12345678"/>
1949 <filterref filter="clean-traffic">
1950 <parameter name="IP" value="192.168.122.1"/>
1951 </filterref>
1952 <bandwidth>
1953 <inbound average="16384" peak="32768" burst="3276"/>
1954 <outbound average="32768" peak="65536" burst="6553"/>
1955 </bandwidth>
1956 </interface>""")
1957
1958 # parse the xml from the first object into a new object and make sure
1959 # they are the same
1960 obj2 = config.LibvirtConfigGuestInterface()
1961 obj2.parse_str(xml)
1962 self.assertXmlEqual(xml, obj2.to_xml())
1963
1964 def test_config_bridge_with_mtu(self):
1965 obj = config.LibvirtConfigGuestInterface()
1966 obj.net_type = "bridge"
1967 obj.source_dev = "br0"
1968 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
1969 obj.model = "virtio"
1970 obj.target_dev = "tap12345678"
1971 obj.filtername = "clean-traffic"
1972 obj.filterparams.append({"key": "IP", "value": "192.168.122.1"})
1973 obj.vif_inbound_average = 16384
1974 obj.vif_inbound_peak = 32768
1975 obj.vif_inbound_burst = 3276
1976 obj.vif_outbound_average = 32768
1977 obj.vif_outbound_peak = 65536
1978 obj.vif_outbound_burst = 6553
1979 obj.mtu = 9000
1980
1981 xml = obj.to_xml()
1982 self.assertXmlEqual(xml, """
1983 <interface type="bridge">
1984 <mac address="DE:AD:BE:EF:CA:FE"/>
1985 <model type="virtio"/>
1986 <source bridge="br0"/>
1987 <mtu size="9000"/>
1988 <target dev="tap12345678"/>
1989 <filterref filter="clean-traffic">
1990 <parameter name="IP" value="192.168.122.1"/>
1991 </filterref>
1992 <bandwidth>
1993 <inbound average="16384" peak="32768" burst="3276"/>
1994 <outbound average="32768" peak="65536" burst="6553"/>
1995 </bandwidth>
1996 </interface>""")
1997
1998 # parse the xml from the first object into a new object and make sure
1999 # they are the same
2000 obj2 = config.LibvirtConfigGuestInterface()
2001 obj2.parse_str(xml)
2002 self.assertXmlEqual(xml, obj2.to_xml())
2003
2004 def test_config_bridge_ovs(self):
2005 obj = config.LibvirtConfigGuestInterface()
2006 obj.net_type = "bridge"
2007 obj.source_dev = "br0"
2008 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2009 obj.model = "virtio"
2010 obj.target_dev = "tap12345678"
2011 obj.vporttype = "openvswitch"
2012 obj.vportparams.append({"key": "instanceid", "value": "foobar"})
2013
2014 xml = obj.to_xml()
2015 self.assertXmlEqual(xml, """
2016 <interface type="bridge">
2017 <mac address="DE:AD:BE:EF:CA:FE"/>
2018 <model type="virtio"/>
2019 <source bridge="br0"/>
2020 <target dev="tap12345678"/>
2021 <virtualport type="openvswitch">
2022 <parameters instanceid="foobar"/>
2023 </virtualport>
2024 </interface>""")
2025
2026 # parse the xml from the first object into a new object and make sure
2027 # they are the same
2028 obj2 = config.LibvirtConfigGuestInterface()
2029 obj2.parse_str(xml)
2030 self.assertXmlEqual(xml, obj2.to_xml())
2031
2032 def test_config_bridge_ovs_with_mtu(self):
2033 obj = config.LibvirtConfigGuestInterface()
2034 obj.net_type = "bridge"
2035 obj.source_dev = "br0"
2036 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2037 obj.model = "virtio"
2038 obj.target_dev = "tap12345678"
2039 obj.vporttype = "openvswitch"
2040 obj.vportparams.append({"key": "instanceid", "value": "foobar"})
2041 obj.mtu = 9000
2042
2043 xml = obj.to_xml()
2044 self.assertXmlEqual(xml, """
2045 <interface type="bridge">
2046 <mac address="DE:AD:BE:EF:CA:FE"/>
2047 <model type="virtio"/>
2048 <source bridge="br0"/>
2049 <mtu size="9000"/>
2050 <target dev="tap12345678"/>
2051 <virtualport type="openvswitch">
2052 <parameters instanceid="foobar"/>
2053 </virtualport>
2054 </interface>""")
2055
2056 # parse the xml from the first object into a new object and make sure
2057 # they are the same
2058 obj2 = config.LibvirtConfigGuestInterface()
2059 obj2.parse_str(xml)
2060 self.assertXmlEqual(xml, obj2.to_xml())
2061
2062 def test_config_bridge_xen(self):
2063 obj = config.LibvirtConfigGuestInterface()
2064 obj.net_type = "bridge"
2065 obj.source_dev = "br0"
2066 obj.mac_addr = "CA:FE:BE:EF:CA:FE"
2067 obj.script = "/path/to/test-vif-openstack"
2068
2069 xml = obj.to_xml()
2070 self.assertXmlEqual(xml, """
2071 <interface type="bridge">
2072 <mac address="CA:FE:BE:EF:CA:FE"/>
2073 <source bridge="br0"/>
2074 <script path="/path/to/test-vif-openstack"/>
2075 </interface>""")
2076
2077 # parse the xml from the first object into a new object and make sure
2078 # they are the same
2079 obj2 = config.LibvirtConfigGuestInterface()
2080 obj2.parse_str(xml)
2081 self.assertXmlEqual(xml, obj2.to_xml())
2082
2083 def test_config_8021Qbh(self):
2084 obj = config.LibvirtConfigGuestInterface()
2085 obj.net_type = "direct"
2086 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2087 obj.model = "virtio"
2088 obj.target_dev = "tap12345678"
2089 obj.source_dev = "eth0"
2090 obj.vporttype = "802.1Qbh"
2091
2092 xml = obj.to_xml()
2093 self.assertXmlEqual(xml, """
2094 <interface type="direct">
2095 <mac address="DE:AD:BE:EF:CA:FE"/>
2096 <model type="virtio"/>
2097 <source dev="eth0" mode="private"/>
2098 <target dev="tap12345678"/>
2099 <virtualport type="802.1Qbh"/>
2100 </interface>""")
2101
2102 # parse the xml from the first object into a new object and make sure
2103 # they are the same
2104 obj2 = config.LibvirtConfigGuestInterface()
2105 obj2.parse_str(xml)
2106 self.assertXmlEqual(xml, obj2.to_xml())
2107
2108 def test_config_direct(self):
2109 obj = config.LibvirtConfigGuestInterface()
2110 obj.net_type = "direct"
2111 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2112 obj.model = "virtio"
2113 obj.source_dev = "eth0"
2114 obj.source_mode = "passthrough"
2115
2116 xml = obj.to_xml()
2117 self.assertXmlEqual(xml, """
2118 <interface type="direct">
2119 <mac address="DE:AD:BE:EF:CA:FE"/>
2120 <model type="virtio"/>
2121 <source dev="eth0" mode="passthrough"/>
2122 </interface>""")
2123
2124 # parse the xml from the first object into a new object and make sure
2125 # they are the same
2126 obj2 = config.LibvirtConfigGuestInterface()
2127 obj2.parse_str(xml)
2128 self.assertXmlEqual(xml, obj2.to_xml())
2129
2130 def test_config_8021Qbh_hostdev(self):
2131 obj = config.LibvirtConfigGuestInterface()
2132 obj.net_type = "hostdev"
2133 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2134 obj.source_dev = "0000:0a:00.1"
2135 obj.vporttype = "802.1Qbh"
2136 obj.add_vport_param("profileid", "MyPortProfile")
2137
2138 xml = obj.to_xml()
2139 self.assertXmlEqual(xml, """
2140 <interface type="hostdev" managed="yes">
2141 <mac address="DE:AD:BE:EF:CA:FE"/>
2142 <source>
2143 <address type="pci" domain="0x0000"
2144 bus="0x0a" slot="0x00" function="0x1"/>
2145 </source>
2146 <virtualport type="802.1Qbh">
2147 <parameters profileid="MyPortProfile"/>
2148 </virtualport>
2149 </interface>""")
2150
2151 # parse the xml from the first object into a new object and make sure
2152 # they are the same
2153 obj2 = config.LibvirtConfigGuestInterface()
2154 obj2.parse_str(xml)
2155 self.assertXmlEqual(xml, obj2.to_xml())
2156
2157 def test_config_hw_veb_hostdev(self):
2158 obj = config.LibvirtConfigGuestInterface()
2159 obj.net_type = "hostdev"
2160 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2161 obj.source_dev = "0000:0a:00.1"
2162 obj.vlan = "100"
2163
2164 xml = obj.to_xml()
2165 self.assertXmlEqual(xml, """
2166 <interface type="hostdev" managed="yes">
2167 <mac address="DE:AD:BE:EF:CA:FE"/>
2168 <source>
2169 <address type="pci" domain="0x0000"
2170 bus="0x0a" slot="0x00" function="0x1"/>
2171 </source>
2172 <vlan>
2173 <tag id="100"/>
2174 </vlan>
2175 </interface>""")
2176
2177 # parse the xml from the first object into a new object and make sure
2178 # they are the same
2179 obj2 = config.LibvirtConfigGuestInterface()
2180 obj2.parse_str(xml)
2181 self.assertXmlEqual(xml, obj2.to_xml())
2182
2183 def test_config_vhostuser(self):
2184 obj = config.LibvirtConfigGuestInterface()
2185 obj.net_type = "vhostuser"
2186 obj.vhostuser_type = "unix"
2187 obj.vhostuser_mode = "server"
2188 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2189 obj.vhostuser_path = "/vhost-user/test.sock"
2190 obj.model = "virtio"
2191 xml = obj.to_xml()
2192 self.assertXmlEqual(xml, """
2193 <interface type="vhostuser">
2194 <mac address="DE:AD:BE:EF:CA:FE"/>
2195 <model type="virtio"/>
2196 <source type="unix" mode="server" path="/vhost-user/test.sock"/>
2197 </interface>""")
2198
2199 # parse the xml from the first object into a new object and make sure
2200 # they are the same
2201 obj2 = config.LibvirtConfigGuestInterface()
2202 obj2.parse_str(xml)
2203 self.assertXmlEqual(xml, obj2.to_xml())
2204
2205 def test_config_vhostuser_ensure_driver_never_set(self):
2206 obj = config.LibvirtConfigGuestInterface()
2207 # Even if 'driver_name' attribute is set we should never set
2208 # it in the domain XML for vhostuser interface.
2209 obj.driver_name = "vhost-user"
2210
2211 obj.net_type = "vhostuser"
2212 obj.vhostuser_type = "unix"
2213 obj.vhostuser_mode = "server"
2214 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2215 obj.vhostuser_path = "/vhost-user/test.sock"
2216 obj.model = "virtio"
2217 xml = obj.to_xml()
2218 self.assertXmlEqual(xml, """
2219 <interface type="vhostuser">
2220 <mac address="DE:AD:BE:EF:CA:FE"/>
2221 <model type="virtio"/>
2222 <source type="unix" mode="server" path="/vhost-user/test.sock"/>
2223 </interface>""")
2224
2225 # parse the xml from the first object into a new object and make sure
2226 # they are the same
2227 obj2 = config.LibvirtConfigGuestInterface()
2228 obj2.parse_str(xml)
2229 self.assertXmlEqual(xml, obj2.to_xml())
2230
2231 def test_config_vhostuser_queue_size(self):
2232 obj = config.LibvirtConfigGuestInterface()
2233 obj.net_type = "vhostuser"
2234 obj.vhostuser_type = "unix"
2235 obj.vhostuser_mode = "server"
2236 obj.mac_addr = "DE:AD:BE:EF:CA:FE"
2237 obj.vhostuser_path = "/vhost-user/test.sock"
2238 obj.vhost_rx_queue_size = 512
2239 obj.vhost_tx_queue_size = 1024
2240 obj.model = "virtio"
2241 xml = obj.to_xml()
2242 self.assertXmlEqual(xml, """
2243 <interface type="vhostuser">
2244 <mac address="DE:AD:BE:EF:CA:FE"/>
2245 <model type="virtio"/>
2246 <driver rx_queue_size="512" tx_queue_size="1024"/>
2247 <source type="unix" mode="server" path="/vhost-user/test.sock"/>
2248 </interface>""")
2249
2250 # parse the xml from the first object into a new object and make sure
2251 # they are the same
2252 obj2 = config.LibvirtConfigGuestInterface()
2253 obj2.parse_str(xml)
2254 self.assertXmlEqual(xml, obj2.to_xml())
2255
2256 def test_config_interface_address(self):
2257 xml = """
2258 <interface type='network'>
2259 <mac address='52:54:00:f6:35:8f'/>
2260 <source network='default'/>
2261 <model type='virtio'/>
2262 <address type='pci' domain='0x0000' bus='0x00'
2263 slot='0x03' function='0x0'/>
2264 </interface>"""
2265
2266 obj = config.LibvirtConfigGuestInterface()
2267 obj.parse_str(xml)
2268
2269 self.assertIsInstance(obj.device_addr,
2270 config.LibvirtConfigGuestDeviceAddressPCI)
2271 self.assertEqual('0000:00:03.0', obj.device_addr.format_address())
2272
2273 def test_config_interface_address_type_virtio_mmio(self):
2274 xml = """
2275 <interface type='network'>
2276 <mac address='fa:16:3e:d1:28:e4'/>
2277 <source network='default'/>
2278 <model type='virtio'/>
2279 <address type='virtio-mmio'/>
2280 </interface>"""
2281
2282 obj = config.LibvirtConfigGuestInterface()
2283 obj.parse_str(xml)
2284
2285 self.assertNotIsInstance(obj.device_addr,
2286 config.LibvirtConfigGuestDeviceAddressPCI)
2287 self.assertNotIsInstance(obj.device_addr,
2288 config.LibvirtConfigGuestDeviceAddressDrive)
2289
2290 def test_config_interface_address_type_ccw(self):
2291 xml = """
2292 <interface type='network'>
2293 <mac address='52:54:00:14:6f:50'/>
2294 <source network='default' bridge='virbr0'/>
2295 <target dev='vnet0'/>
2296 <model type='virtio'/>
2297 <alias name='net0'/>
2298 <address type='ccw' cssid='0xfe' ssid='0x0' devno='0x0001'/>
2299 </interface>"""
2300
2301 obj = config.LibvirtConfigGuestInterface()
2302 obj.parse_str(xml)
2303
2304 self.assertNotIsInstance(obj.device_addr,
2305 config.LibvirtConfigGuestDeviceAddressPCI)
2306 self.assertNotIsInstance(obj.device_addr,
2307 config.LibvirtConfigGuestDeviceAddressDrive)
2308
2309
2310 class LibvirtConfigGuestFeatureTest(LibvirtConfigBaseTest):
2311
2312 def test_feature_hyperv_relaxed(self):
2313
2314 obj = config.LibvirtConfigGuestFeatureHyperV()
2315 obj.relaxed = True
2316
2317 xml = obj.to_xml()
2318 self.assertXmlEqual(xml, """
2319 <hyperv>
2320 <relaxed state="on"/>
2321 </hyperv>""")
2322
2323 def test_feature_hyperv_all(self):
2324
2325 obj = config.LibvirtConfigGuestFeatureHyperV()
2326 obj.relaxed = True
2327 obj.vapic = True
2328 obj.spinlocks = True
2329 obj.vendorid_spoof = True
2330
2331 xml = obj.to_xml()
2332 self.assertXmlEqual(xml, """
2333 <hyperv>
2334 <relaxed state="on"/>
2335 <vapic state="on"/>
2336 <spinlocks state="on" retries="4095"/>
2337 <vendor_id state="on" value="1234567890ab"/>
2338 </hyperv>""")
2339
2340 def test_feature_pmu(self):
2341 # NOTE(sean-k-moonmey): LibvirtConfigGuestFeaturePMU uses
2342 # bool_from_string internally so assert that boolean and
2343 # string inputs work. This does not need to be exhaustive
2344 # as bool_from_string is tested in oslo so we just try
2345 # some common values.
2346
2347 for val in ("true", "on", "1", "yes", True):
2348 obj = config.LibvirtConfigGuestFeaturePMU(val)
2349 xml = obj.to_xml()
2350 self.assertXmlEqual(xml, "<pmu state='on'/>")
2351 for val in ("false", "off", "0", "no", False):
2352 obj = config.LibvirtConfigGuestFeaturePMU(val)
2353 xml = obj.to_xml()
2354 self.assertXmlEqual(xml, "<pmu state='off'/>")
2355
2356
2357 class LibvirtConfigGuestTest(LibvirtConfigBaseTest):
2358
2359 def test_launch_security(self):
2360 # test that sev-specific bits are added to the xml
2361
2362 obj = config.LibvirtConfigGuestSEVLaunchSecurity()
2363 obj.cbitpos = 47
2364 obj.reduced_phys_bits = 1
2365
2366 xml = obj.to_xml()
2367 launch_security_expected = """
2368 <launchSecurity type="sev">
2369 <policy>0x0033</policy>
2370 <cbitpos>47</cbitpos>
2371 <reducedPhysBits>1</reducedPhysBits>
2372 </launchSecurity>"""
2373
2374 self.assertXmlEqual(launch_security_expected, xml)
2375
2376 def test_config_lxc(self):
2377 obj = config.LibvirtConfigGuest()
2378 obj.virt_type = "lxc"
2379 obj.memory = 100 * units.Mi
2380 obj.vcpus = 2
2381 obj.cpuset = set([0, 1, 3, 4, 5])
2382 obj.name = "demo"
2383 obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
2384 obj.os_type = "exe"
2385 obj.os_init_path = "/sbin/init"
2386 obj.os_init_env["foo"] = "bar"
2387
2388 fs = config.LibvirtConfigGuestFilesys()
2389 fs.source_dir = "/root/lxc"
2390 fs.target_dir = "/"
2391
2392 obj.add_device(fs)
2393
2394 xml = obj.to_xml()
2395 self.assertXmlEqual(xml, """
2396 <domain type="lxc">
2397 <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2398 <name>demo</name>
2399 <memory>104857600</memory>
2400 <vcpu cpuset="0-1,3-5">2</vcpu>
2401 <os>
2402 <type>exe</type>
2403 <init>/sbin/init</init>
2404 <initenv name="foo">bar</initenv>
2405 </os>
2406 <devices>
2407 <filesystem type="mount">
2408 <source dir="/root/lxc"/>
2409 <target dir="/"/>
2410 </filesystem>
2411 </devices>
2412 </domain>""")
2413
2414 def test_config_lxc_with_idmap(self):
2415 obj = config.LibvirtConfigGuest()
2416 obj.virt_type = "lxc"
2417 obj.memory = 100 * units.Mi
2418 obj.vcpus = 2
2419 obj.cpuset = set([0, 1, 3, 4, 5])
2420 obj.name = "demo"
2421 obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
2422 obj.os_type = "exe"
2423 obj.os_init_path = "/sbin/init"
2424
2425 uidmap = config.LibvirtConfigGuestUIDMap()
2426 uidmap.target = "10000"
2427 uidmap.count = "1"
2428 obj.idmaps.append(uidmap)
2429 gidmap = config.LibvirtConfigGuestGIDMap()
2430 gidmap.target = "10000"
2431 gidmap.count = "1"
2432 obj.idmaps.append(gidmap)
2433
2434 fs = config.LibvirtConfigGuestFilesys()
2435 fs.source_dir = "/root/lxc"
2436 fs.target_dir = "/"
2437
2438 obj.add_device(fs)
2439
2440 xml = obj.to_xml()
2441 self.assertXmlEqual("""
2442 <domain type="lxc">
2443 <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2444 <name>demo</name>
2445 <memory>104857600</memory>
2446 <vcpu cpuset="0-1,3-5">2</vcpu>
2447 <os>
2448 <type>exe</type>
2449 <init>/sbin/init</init>
2450 </os>
2451 <devices>
2452 <filesystem type="mount">
2453 <source dir="/root/lxc"/>
2454 <target dir="/"/>
2455 </filesystem>
2456 </devices>
2457 <idmap>
2458 <uid start="0" target="10000" count="1"/>
2459 <gid start="0" target="10000" count="1"/>
2460 </idmap>
2461 </domain>""", xml)
2462
2463 def test_config_xen_pv(self):
2464 obj = config.LibvirtConfigGuest()
2465 obj.virt_type = "xen"
2466 obj.memory = 100 * units.Mi
2467 obj.vcpus = 2
2468 obj.cpuset = set([0, 1, 3, 4, 5])
2469 obj.name = "demo"
2470 obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
2471 obj.os_type = "linux"
2472 obj.os_kernel = "/tmp/vmlinuz"
2473 obj.os_initrd = "/tmp/ramdisk"
2474 obj.os_cmdline = "console=xvc0"
2475
2476 disk = config.LibvirtConfigGuestDisk()
2477 disk.source_type = "file"
2478 disk.source_path = "/tmp/img"
2479 disk.target_dev = "/dev/xvda"
2480 disk.target_bus = "xen"
2481
2482 obj.add_device(disk)
2483
2484 xml = obj.to_xml()
2485 self.assertXmlEqual(xml, """
2486 <domain type="xen">
2487 <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2488 <name>demo</name>
2489 <memory>104857600</memory>
2490 <vcpu cpuset="0-1,3-5">2</vcpu>
2491 <os>
2492 <type>linux</type>
2493 <kernel>/tmp/vmlinuz</kernel>
2494 <initrd>/tmp/ramdisk</initrd>
2495 <cmdline>console=xvc0</cmdline>
2496 </os>
2497 <devices>
2498 <disk type="file" device="disk">
2499 <source file="/tmp/img"/>
2500 <target bus="xen" dev="/dev/xvda"/>
2501 </disk>
2502 </devices>
2503 </domain>""")
2504
2505 def test_config_xen_hvm(self):
2506 obj = config.LibvirtConfigGuest()
2507 obj.virt_type = "xen"
2508 obj.memory = 100 * units.Mi
2509 obj.vcpus = 2
2510 obj.cpuset = set([0, 1, 3, 4, 5])
2511 obj.name = "demo"
2512 obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
2513 obj.os_type = "hvm"
2514 obj.os_loader = '/usr/lib/xen/boot/hvmloader'
2515 obj.os_root = "root=xvda"
2516 obj.os_cmdline = "console=xvc0"
2517 obj.features = [
2518 config.LibvirtConfigGuestFeatureACPI(),
2519 config.LibvirtConfigGuestFeatureAPIC(),
2520 config.LibvirtConfigGuestFeaturePAE(),
2521 ]
2522
2523 disk = config.LibvirtConfigGuestDisk()
2524 disk.source_type = "file"
2525 disk.source_path = "/tmp/img"
2526 disk.target_dev = "/dev/xvda"
2527 disk.target_bus = "xen"
2528
2529 obj.add_device(disk)
2530
2531 xml = obj.to_xml()
2532 self.assertXmlEqual(xml, """
2533 <domain type="xen">
2534 <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2535 <name>demo</name>
2536 <memory>104857600</memory>
2537 <vcpu cpuset="0-1,3-5">2</vcpu>
2538 <os>
2539 <type>hvm</type>
2540 <loader>/usr/lib/xen/boot/hvmloader</loader>
2541 <cmdline>console=xvc0</cmdline>
2542 <root>root=xvda</root>
2543 </os>
2544 <features>
2545 <acpi/>
2546 <apic/>
2547 <pae/>
2548 </features>
2549 <devices>
2550 <disk type="file" device="disk">
2551 <source file="/tmp/img"/>
2552 <target bus="xen" dev="/dev/xvda"/>
2553 </disk>
2554 </devices>
2555 </domain>""")
2556
2557 def test_config_kvm(self):
2558 obj = fake_libvirt_data.fake_kvm_guest()
2559
2560 launch_security = config.LibvirtConfigGuestSEVLaunchSecurity()
2561 launch_security.cbitpos = 47
2562 launch_security.reduced_phys_bits = 1
2563 obj.launch_security = launch_security
2564
2565 xml = obj.to_xml()
2566 self.assertXmlEqual(fake_libvirt_data.FAKE_KVM_GUEST, xml)
2567
2568 def test_config_uefi(self):
2569 obj = config.LibvirtConfigGuest()
2570 obj.virt_type = "kvm"
2571 obj.memory = 100 * units.Mi
2572 obj.vcpus = 1
2573 obj.name = "uefi"
2574 obj.uuid = "f01cf68d-515c-4daf-b85f-ef1424d93bfc"
2575 obj.os_type = "x86_64"
2576 obj.os_loader = '/tmp/OVMF_CODE.fd'
2577 obj.os_loader_type = 'pflash'
2578 xml = obj.to_xml()
2579
2580 self.assertXmlEqual(xml, """
2581 <domain type="kvm">
2582 <uuid>f01cf68d-515c-4daf-b85f-ef1424d93bfc</uuid>
2583 <name>uefi</name>
2584 <memory>104857600</memory>
2585 <vcpu>1</vcpu>
2586 <os>
2587 <type>x86_64</type>
2588 <loader readonly='yes' type='pflash'>/tmp/OVMF_CODE.fd</loader>
2589 </os>
2590 </domain>""")
2591
2592 def test_config_boot_menu(self):
2593 obj = config.LibvirtConfigGuest()
2594 obj.virt_type = "kvm"
2595 obj.memory = 100 * units.Mi
2596 obj.vcpus = 2
2597 obj.name = "bootmenu"
2598 obj.uuid = "f01cf68d-515c-4daf-b85f-ef1424d93bfc"
2599 obj.os_type = "fake"
2600 obj.os_bootmenu = True
2601 xml = obj.to_xml()
2602
2603 self.assertXmlEqual(xml, """
2604 <domain type="kvm">
2605 <uuid>f01cf68d-515c-4daf-b85f-ef1424d93bfc</uuid>
2606 <name>bootmenu</name>
2607 <memory>104857600</memory>
2608 <vcpu>2</vcpu>
2609 <os>
2610 <type>fake</type>
2611 <bootmenu enable="yes"/>
2612 </os>
2613 </domain>""")
2614
2615 def test_config_perf(self):
2616 obj = config.LibvirtConfigGuest()
2617 obj.virt_type = "kvm"
2618 obj.memory = 100 * units.Mi
2619 obj.vcpus = 2
2620 obj.name = "perf"
2621 obj.uuid = "f01cf68d-515c-4daf-b85f-ef1424d93bfc"
2622 obj.os_type = "fake"
2623 obj.perf_events = ['cmt', 'mbml']
2624 xml = obj.to_xml()
2625
2626 self.assertXmlEqual(xml, """
2627 <domain type="kvm">
2628 <uuid>f01cf68d-515c-4daf-b85f-ef1424d93bfc</uuid>
2629 <name>perf</name>
2630 <memory>104857600</memory>
2631 <vcpu>2</vcpu>
2632 <os>
2633 <type>fake</type>
2634 </os>
2635 <perf>
2636 <event enabled="yes" name="cmt"/>
2637 <event enabled="yes" name="mbml"/>
2638 </perf>
2639 </domain>""")
2640
2641 def test_config_machine_type(self):
2642 obj = config.LibvirtConfigGuest()
2643 obj.virt_type = "kvm"
2644 obj.memory = 100 * units.Mi
2645 obj.vcpus = 2
2646 obj.name = "demo"
2647 obj.uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147"
2648 obj.os_type = "hvm"
2649 obj.os_mach_type = "fake_machine_type"
2650 xml = obj.to_xml()
2651
2652 self.assertXmlEqual(xml, """
2653 <domain type="kvm">
2654 <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2655 <name>demo</name>
2656 <memory>104857600</memory>
2657 <vcpu>2</vcpu>
2658 <os>
2659 <type machine="fake_machine_type">hvm</type>
2660 </os>
2661 </domain>""")
2662
2663 def test_ConfigGuest_parse_devices(self):
2664 xmldoc = """ <domain type="kvm">
2665 <devices>
2666 <hostdev mode="subsystem" type="pci" managed="no">
2667 </hostdev>
2668 <filesystem type="mount">
2669 </filesystem>
2670 </devices>
2671 </domain>
2672 """
2673 obj = config.LibvirtConfigGuest()
2674 obj.parse_str(xmldoc)
2675 self.assertEqual('kvm', obj.virt_type)
2676 self.assertEqual(len(obj.devices), 2)
2677 self.assertIsInstance(obj.devices[0],
2678 config.LibvirtConfigGuestHostdevPCI)
2679 self.assertEqual(obj.devices[0].mode, 'subsystem')
2680 self.assertEqual(obj.devices[0].managed, 'no')
2681 self.assertIsInstance(obj.devices[1],
2682 config.LibvirtConfigGuestFilesys)
2683 self.assertEqual('mount', obj.devices[1].source_type)
2684
2685 def test_ConfigGuest_parse_devices_wrong_type(self):
2686 xmldoc = """ <domain type="kvm">
2687 <devices>
2688 <hostdev mode="subsystem" type="xxxx" managed="no">
2689 </hostdev>
2690 </devices>
2691 </domain>
2692 """
2693 obj = config.LibvirtConfigGuest()
2694 obj.parse_str(xmldoc)
2695 self.assertEqual(len(obj.devices), 0)
2696
2697 def test_ConfigGuest_parse_cpu(self):
2698 xmldoc = """ <domain>
2699 <cpu mode='custom' match='exact'>
2700 <model>kvm64</model>
2701 </cpu>
2702 </domain>
2703 """
2704 obj = config.LibvirtConfigGuest()
2705 obj.parse_str(xmldoc)
2706
2707 self.assertEqual(obj.cpu.mode, 'custom')
2708 self.assertEqual(obj.cpu.match, 'exact')
2709 self.assertEqual(obj.cpu.model, 'kvm64')
2710
2711 def test_ConfigGuest_parse_perf(self):
2712 xmldoc = """ <domain>
2713 <perf>
2714 <event enabled="yes" name="cmt"/>
2715 <event enabled="no" name="mbml"/>
2716 </perf>
2717 </domain>
2718 """
2719 obj = config.LibvirtConfigGuest()
2720 obj.parse_str(xmldoc)
2721
2722 self.assertEqual(['cmt'], obj.perf_events)
2723
2724 def test_ConfigGuest_parse_os(self):
2725 xmldoc = """
2726 <domain type="kvm">
2727 <os>
2728 <type machine="fake_machine_type">hvm</type>
2729 <kernel>/tmp/vmlinuz</kernel>
2730 <loader>/usr/lib/xen/boot/hvmloader</loader>
2731 <initrd>/tmp/ramdisk</initrd>
2732 <cmdline>console=xvc0</cmdline>
2733 <root>root=xvda</root>
2734 <init>/sbin/init</init>
2735 <boot dev="hd"/>
2736 <boot dev="cdrom"/>
2737 <boot dev="fd"/>
2738 <bootmenu enable="yes"/>
2739 <smbios mode="sysinfo"/>
2740 <initenv name="foo">bar</initenv>
2741 </os>
2742 </domain>
2743 """
2744 obj = config.LibvirtConfigGuest()
2745 obj.parse_str(xmldoc)
2746
2747 self.assertEqual('kvm', obj.virt_type)
2748 self.assertEqual('hvm', obj.os_type)
2749 self.assertEqual('fake_machine_type', obj.os_mach_type)
2750 self.assertEqual('/tmp/vmlinuz', obj.os_kernel)
2751 self.assertEqual('/usr/lib/xen/boot/hvmloader', obj.os_loader)
2752 self.assertIsNone(obj.os_loader_type)
2753 self.assertEqual('/tmp/ramdisk', obj.os_initrd)
2754 self.assertEqual('console=xvc0', obj.os_cmdline)
2755 self.assertEqual('root=xvda', obj.os_root)
2756 self.assertEqual('/sbin/init', obj.os_init_path)
2757 self.assertEqual('bar', obj.os_init_env['foo'])
2758 self.assertEqual(['hd', 'cdrom', 'fd'], obj.os_boot_dev)
2759 self.assertTrue(obj.os_bootmenu)
2760 self.assertIsNone(obj.os_smbios)
2761
2762 xmldoc = """
2763 <domain>
2764 <os>
2765 <type>x86_64</type>
2766 <loader readonly='yes' type='pflash'>/tmp/OVMF_CODE.fd</loader>
2767 </os>
2768 </domain>
2769 """
2770 obj = config.LibvirtConfigGuest()
2771 obj.parse_str(xmldoc)
2772
2773 self.assertIsNone(obj.virt_type)
2774 self.assertEqual('x86_64', obj.os_type)
2775 self.assertIsNone(obj.os_mach_type)
2776 self.assertIsNone(obj.os_kernel)
2777 self.assertEqual('/tmp/OVMF_CODE.fd', obj.os_loader)
2778 self.assertEqual('pflash', obj.os_loader_type)
2779 self.assertIsNone(obj.os_initrd)
2780 self.assertIsNone(obj.os_cmdline)
2781 self.assertIsNone(obj.os_root)
2782 self.assertIsNone(obj.os_init_path)
2783 self.assertEqual([], obj.os_boot_dev)
2784 self.assertFalse(obj.os_bootmenu)
2785 self.assertIsNone(obj.os_smbios)
2786
2787 def test_ConfigGuest_parse_basic_props(self):
2788 xmldoc = """
2789 <domain>
2790 <uuid>b38a3f43-4be2-4046-897f-b67c2f5e0147</uuid>
2791 <name>demo</name>
2792 <memory>104857600</memory>
2793 <vcpu cpuset="0-1,3-5">2</vcpu>
2794 </domain>
2795 """
2796 obj = config.LibvirtConfigGuest()
2797 obj.parse_str(xmldoc)
2798
2799 self.assertEqual('b38a3f43-4be2-4046-897f-b67c2f5e0147', obj.uuid)
2800 self.assertEqual('demo', obj.name)
2801 self.assertEqual(100 * units.Mi, obj.memory)
2802 self.assertEqual(2, obj.vcpus)
2803 self.assertEqual(set([0, 1, 3, 4, 5]), obj.cpuset)
2804
2805 xmldoc = """
2806 <domain>
2807 <vcpu>3</vcpu>
2808 </domain>
2809 """
2810 obj = config.LibvirtConfigGuest()
2811 obj.parse_str(xmldoc)
2812
2813 self.assertIsNone(obj.uuid)
2814 self.assertIsNone(obj.name)
2815 self.assertEqual(500 * units.Mi, obj.memory) # default value
2816 self.assertEqual(3, obj.vcpus)
2817 self.assertIsNone(obj.cpuset)
2818
2819
2820 class LibvirtConfigGuestSnapshotTest(LibvirtConfigBaseTest):
2821
2822 def test_config_snapshot(self):
2823 obj = config.LibvirtConfigGuestSnapshot()
2824 obj.name = "Demo"
2825
2826 xml = obj.to_xml()
2827 self.assertXmlEqual(xml, """
2828 <domainsnapshot>
2829 <name>Demo</name>
2830 <disks/>
2831 </domainsnapshot>""")
2832
2833 def test_config_snapshot_with_disks(self):
2834 obj = config.LibvirtConfigGuestSnapshot()
2835 obj.name = "Demo"
2836
2837 disk = config.LibvirtConfigGuestSnapshotDisk()
2838 disk.name = 'vda'
2839 disk.source_path = 'source-path'
2840 disk.source_type = 'file'
2841 disk.snapshot = 'external'
2842 disk.driver_name = 'qcow2'
2843 obj.add_disk(disk)
2844
2845 disk2 = config.LibvirtConfigGuestSnapshotDisk()
2846 disk2.name = 'vdb'
2847 disk2.snapshot = 'no'
2848 obj.add_disk(disk2)
2849
2850 xml = obj.to_xml()
2851 self.assertXmlEqual(xml, """
2852 <domainsnapshot>
2853 <name>Demo</name>
2854 <disks>
2855 <disk name='vda' snapshot='external' type='file'>
2856 <source file='source-path'/>
2857 </disk>
2858 <disk name='vdb' snapshot='no'/>
2859 </disks>
2860 </domainsnapshot>""")
2861
2862 def test_config_snapshot_with_network_disks(self):
2863 obj = config.LibvirtConfigGuestSnapshot()
2864 obj.name = "Demo"
2865
2866 disk = config.LibvirtConfigGuestSnapshotDisk()
2867 disk.name = 'vda'
2868 disk.source_name = 'source-file'
2869 disk.source_type = 'network'
2870 disk.source_hosts = ['host1']
2871 disk.source_ports = ['12345']
2872 disk.source_protocol = 'netfs'
2873 disk.snapshot = 'external'
2874 disk.driver_name = 'qcow2'
2875 obj.add_disk(disk)
2876
2877 disk2 = config.LibvirtConfigGuestSnapshotDisk()
2878 disk2.name = 'vdb'
2879 disk2.snapshot = 'no'
2880 obj.add_disk(disk2)
2881
2882 xml = obj.to_xml()
2883 self.assertXmlEqual(xml, """
2884 <domainsnapshot>
2885 <name>Demo</name>
2886 <disks>
2887 <disk name='vda' snapshot='external' type='network'>
2888 <source protocol='netfs' name='source-file'>
2889 <host name='host1' port='12345'/>
2890 </source>
2891 </disk>
2892 <disk name='vdb' snapshot='no'/>
2893 </disks>
2894 </domainsnapshot>""")
2895
2896 def test_config_file_iommu(self):
2897 obj = config.LibvirtConfigGuestDisk()
2898 obj.driver_iommu = True
2899 obj.source_type = "file"
2900 obj.source_path = "/tmp/hello.qcow2"
2901 obj.target_dev = "/dev/sda"
2902 obj.target_bus = "virtio"
2903 obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9"
2904
2905 self.assertTrue(obj.uses_virtio)
2906
2907 xml = obj.to_xml()
2908 self.assertXmlEqual("""
2909 <disk type="file" device="disk">
2910 <driver iommu="on"/>
2911 <source file="/tmp/hello.qcow2"/>
2912 <target bus="virtio" dev="/dev/sda"/>
2913 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
2914 </disk>""", xml)
2915
2916 def test_config_file_iommu_parse(self):
2917 xml = """
2918 <disk type="file" device="disk">
2919 <driver iommu="on"/>
2920 <source file="/tmp/hello.qcow2"/>
2921 <target bus="virtio" dev="/dev/sda"/>
2922 <serial>7a97c4a3-6f59-41d4-bf47-191d7f97f8e9</serial>
2923 </disk>"""
2924 xmldoc = etree.fromstring(xml)
2925
2926 obj = config.LibvirtConfigGuestDisk()
2927 obj.parse_dom(xmldoc)
2928
2929 self.assertTrue(obj.driver_iommu)
2930
2931
2932 class LibvirtConfigNodeDeviceTest(LibvirtConfigBaseTest):
2933
2934 def test_config_virt_usb_device(self):
2935 xmlin = """
2936 <device>
2937 <name>usb_0000_09_00_0</name>
2938 <parent>pci_0000_00_1c_0</parent>
2939 <driver>
2940 <name>vxge</name>
2941 </driver>
2942 <capability type="usb">
2943 <domain>0</domain>
2944 <capability type="fake_usb">
2945 <address fake_usb="fake"/>
2946 </capability>
2947 </capability>
2948 </device>"""
2949
2950 obj = config.LibvirtConfigNodeDevice()
2951 obj.parse_str(xmlin)
2952
2953 self.assertIsNone(obj.pci_capability)
2954
2955 def test_config_virt_device(self):
2956 xmlin = """
2957 <device>
2958 <name>pci_0000_09_00_0</name>
2959 <parent>pci_0000_00_1c_0</parent>
2960 <driver>
2961 <name>vxge</name>
2962 </driver>
2963 <capability type="pci">
2964 <domain>0</domain>
2965 <bus>9</bus>
2966 <slot>0</slot>
2967 <function>0</function>
2968 <product id="0x5833">X3100 Series 10 Gigabit Ethernet PCIe</product>
2969 <vendor id="0x17d5">Neterion Inc.</vendor>
2970 <capability type="virt_functions">
2971 <address domain="0x0000" bus="0x0a" slot="0x00" function="0x1"/>
2972 <address domain="0x0000" bus="0x0a" slot="0x00" function="0x2"/>
2973 <address domain="0x0000" bus="0x0a" slot="0x00" function="0x3"/>
2974 </capability>
2975 </capability>
2976 </device>"""
2977
2978 obj = config.LibvirtConfigNodeDevice()
2979 obj.parse_str(xmlin)
2980
2981 self.assertIsInstance(obj.pci_capability,
2982 config.LibvirtConfigNodeDevicePciCap)
2983 self.assertIsInstance(obj.pci_capability.fun_capability[0],
2984 config.LibvirtConfigNodeDevicePciSubFunctionCap)
2985 self.assertEqual(obj.pci_capability.fun_capability[0].type,
2986 "virt_functions")
2987 self.assertEqual(len(obj.pci_capability.fun_capability[0].
2988 device_addrs),
2989 3)
2990 self.assertEqual(obj.pci_capability.bus, 9)
2991
2992 def test_config_phy_device(self):
2993 xmlin = """
2994 <device>
2995 <name>pci_0000_33_00_0</name>
2996 <parent>pci_0000_22_1c_0</parent>
2997 <driver>
2998 <name>vxx</name>
2999 </driver>
3000 <capability type="pci">
3001 <domain>0</domain>
3002 <bus>9</bus>
3003 <slot>0</slot>
3004 <function>0</function>
3005 <product id="0x5833">X3100 Series 10 Gigabit Ethernet PCIe</product>
3006 <vendor id="0x17d5">Neterion Inc.</vendor>
3007 <capability type="phys_function">
3008 <address domain='0x0000' bus='0x09' slot='0x00' function='0x0'/>
3009 </capability>
3010 </capability>
3011 </device>"""
3012
3013 obj = config.LibvirtConfigNodeDevice()
3014 obj.parse_str(xmlin)
3015
3016 self.assertIsInstance(obj.pci_capability,
3017 config.LibvirtConfigNodeDevicePciCap)
3018 self.assertIsInstance(obj.pci_capability.fun_capability[0],
3019 config.LibvirtConfigNodeDevicePciSubFunctionCap)
3020 self.assertEqual(obj.pci_capability.fun_capability[0].type,
3021 "phys_function")
3022 self.assertEqual(len(obj.pci_capability.fun_capability[0].
3023 device_addrs),
3024 1)
3025
3026 def test_config_non_device(self):
3027 xmlin = """
3028 <device>
3029 <name>pci_0000_33_00_0</name>
3030 <parent>pci_0000_22_1c_0</parent>
3031 <driver>
3032 <name>vxx</name>
3033 </driver>
3034 <capability type="pci">
3035 <domain>0</domain>
3036 <bus>9</bus>
3037 <slot>0</slot>
3038 <function>0</function>
3039 <product id="0x5833">X3100 Series 10 Gigabit Ethernet PCIe</product>
3040 <vendor id="0x17d5">Neterion Inc.</vendor>
3041 <capability type="virt_functions"/>
3042 </capability>
3043 </device>"""
3044
3045 obj = config.LibvirtConfigNodeDevice()
3046 obj.parse_str(xmlin)
3047
3048 self.assertIsInstance(obj.pci_capability,
3049 config.LibvirtConfigNodeDevicePciCap)
3050 self.assertIsInstance(obj.pci_capability.fun_capability[0],
3051 config.LibvirtConfigNodeDevicePciSubFunctionCap)
3052 self.assertEqual(obj.pci_capability.fun_capability[0].type,
3053 "virt_functions")
3054
3055 def test_config_fail_device(self):
3056 xmlin = """
3057 <device>
3058 <name>pci_0000_33_00_0</name>
3059 <parent>pci_0000_22_1c_0</parent>
3060 <driver>
3061 <name>vxx</name>
3062 </driver>
3063 <capability type="pci">
3064 <domain>0</domain>
3065 <bus>9</bus>
3066 <slot>0</slot>
3067 <function>0</function>
3068 <product id="0x5833">X3100 Series 10 Gigabit Ethernet PCIe</product>
3069 <vendor id="0x17d5">Neterion Inc.</vendor>
3070 <capability type="virt_functions">
3071 </capability>
3072 </capability>
3073 </device>"""
3074
3075 obj = config.LibvirtConfigNodeDevice()
3076 obj.parse_str(xmlin)
3077
3078 self.assertIsInstance(obj.pci_capability,
3079 config.LibvirtConfigNodeDevicePciCap)
3080 self.assertIsInstance(obj.pci_capability.fun_capability[0],
3081 config.LibvirtConfigNodeDevicePciSubFunctionCap)
3082 self.assertEqual(obj.pci_capability.fun_capability[0].type,
3083 "virt_functions")
3084
3085 def test_config_2cap_device(self):
3086 xmlin = """
3087 <device>
3088 <name>pci_0000_04_10_7</name>
3089 <parent>pci_0000_00_01_1</parent>
3090 <driver>
3091 <name>igbvf</name>
3092 </driver>
3093 <capability type='pci'>
3094 <domain>0</domain>
3095 <bus>4</bus>
3096 <slot>16</slot>
3097 <function>7</function>
3098 <product id='0x1520'>I350 Ethernet Controller Virtual</product>
3099 <vendor id='0x8086'>Intel Corporation</vendor>
3100 <capability type='phys_function'>
3101 <address domain='0x0000' bus='0x04' slot='0x00' function='0x3'/>
3102 </capability>
3103 <capability type='virt_functions'>
3104 <address domain='0x0000' bus='0x04' slot='0x00' function='0x3'/>
3105 </capability>
3106 </capability>
3107 </device>"""
3108
3109 obj = config.LibvirtConfigNodeDevice()
3110 obj.parse_str(xmlin)
3111
3112 self.assertIsInstance(obj.pci_capability,
3113 config.LibvirtConfigNodeDevicePciCap)
3114 self.assertIsInstance(obj.pci_capability.fun_capability[0],
3115 config.LibvirtConfigNodeDevicePciSubFunctionCap)
3116 self.assertEqual(obj.pci_capability.fun_capability[0].type,
3117 "phys_function")
3118 self.assertEqual(obj.pci_capability.fun_capability[1].type,
3119 "virt_functions")
3120
3121 def test_config_net_device(self):
3122 xmlin = """
3123 <device>
3124 <name>net_enp2s2_02_9a_a1_37_be_54</name>
3125 <path>/sys/devices/pci0000:00/0000:00:02.0/net/enp2s2</path>
3126 <parent>pci_0000_00_02_0</parent>
3127 <capability type='net'>
3128 <interface>enp2s2</interface>
3129 <address>02:9a:a1:37:be:54</address>
3130 <link state='down'/>
3131 <feature name='rx'/>
3132 <feature name='tx'/>
3133 <feature name='sg'/>
3134 <feature name='tso'/>
3135 <feature name='gso'/>
3136 <feature name='gro'/>
3137 <feature name='rxvlan'/>
3138 <feature name='txvlan'/>
3139 <capability type='80203'/>
3140 </capability>
3141 </device>"""
3142
3143 obj = config.LibvirtConfigNodeDevice()
3144 obj.parse_str(xmlin)
3145
3146 self.assertIsInstance(obj.pci_capability,
3147 config.LibvirtConfigNodeDevicePciCap)
3148 self.assertEqual(obj.pci_capability.interface, "enp2s2")
3149 self.assertEqual(obj.pci_capability.address, "02:9a:a1:37:be:54")
3150 self.assertEqual(obj.pci_capability.link_state, "down")
3151 self.assertEqual(obj.pci_capability.features,
3152 ['rx', 'tx', 'sg', 'tso', 'gso', 'gro', 'rxvlan',
3153 'txvlan'])
3154
3155 def test_config_mdev_device(self):
3156 xmlin = """
3157 <device>
3158 <name>mdev_4b20d080_1b54_4048_85b3_a6a62d165c01</name>
3159 <parent>pci_0000_06_00_0</parent>
3160 <capability type='mdev'>
3161 <type id='nvidia-11'/>
3162 <iommuGroup number='12'/>
3163 </capability>
3164 </device>"""
3165
3166 obj = config.LibvirtConfigNodeDevice()
3167 obj.parse_str(xmlin)
3168 self.assertIsInstance(obj.mdev_information,
3169 config.LibvirtConfigNodeDeviceMdevInformation)
3170 self.assertEqual("nvidia-11", obj.mdev_information.type)
3171 self.assertEqual(12, obj.mdev_information.iommu_group)
3172
3173
3174 class LibvirtConfigNodeDevicePciCapTest(LibvirtConfigBaseTest):
3175
3176 def test_config_device_pci_cap(self):
3177 xmlin = """
3178 <capability type="pci">
3179 <domain>0</domain>
3180 <bus>10</bus>
3181 <slot>1</slot>
3182 <function>5</function>
3183 <product id="0x10bd">Intel 10 Gigabit Ethernet</product>
3184 <vendor id="0x8086">Intel Inc.</vendor>
3185 <capability type="virt_functions">
3186 <address domain="0000" bus="0x0a" slot="0x1" function="0x1"/>
3187 <address domain="0001" bus="0x0a" slot="0x02" function="0x03"/>
3188 </capability>
3189 </capability>"""
3190 obj = config.LibvirtConfigNodeDevicePciCap()
3191 obj.parse_str(xmlin)
3192
3193 self.assertEqual(obj.domain, 0)
3194 self.assertEqual(obj.bus, 10)
3195 self.assertEqual(obj.slot, 1)
3196 self.assertEqual(obj.function, 5)
3197 self.assertEqual(obj.product, "Intel 10 Gigabit Ethernet")
3198 self.assertEqual(obj.product_id, 0x10bd)
3199 self.assertEqual(obj.vendor, "Intel Inc.")
3200 self.assertEqual(obj.vendor_id, 0x8086)
3201 self.assertIsNone(obj.numa_node)
3202 self.assertIsInstance(obj.fun_capability[0],
3203 config.LibvirtConfigNodeDevicePciSubFunctionCap)
3204
3205 self.assertEqual(obj.fun_capability[0].type, 'virt_functions')
3206 self.assertEqual(obj.fun_capability[0].device_addrs,
3207 [(0, 10, 1, 1),
3208 (1, 10, 2, 3), ])
3209
3210 def test_config_device_pci_2cap(self):
3211 xmlin = """
3212 <capability type="pci">
3213 <domain>0</domain>
3214 <bus>10</bus>
3215 <slot>1</slot>
3216 <function>5</function>
3217 <product id="0x10bd">Intel 10 Gigabit Ethernet</product>
3218 <vendor id="0x8086">Intel Inc.</vendor>
3219 <numa node='0'/>
3220 <capability type="virt_functions">
3221 <address domain="0000" bus="0x0a" slot="0x1" function="0x1"/>
3222 <address domain="0001" bus="0x0a" slot="0x02" function="0x03"/>
3223 </capability>
3224 <capability type="phys_function">
3225 <address domain="0000" bus="0x0a" slot="0x1" function="0x1"/>
3226 </capability>
3227 </capability>"""
3228 obj = config.LibvirtConfigNodeDevicePciCap()
3229 obj.parse_str(xmlin)
3230
3231 self.assertEqual(obj.domain, 0)
3232 self.assertEqual(obj.bus, 10)
3233 self.assertEqual(obj.slot, 1)
3234 self.assertEqual(obj.function, 5)
3235 self.assertEqual(obj.product, "Intel 10 Gigabit Ethernet")
3236 self.assertEqual(obj.product_id, 0x10bd)
3237 self.assertEqual(obj.vendor, "Intel Inc.")
3238 self.assertEqual(obj.vendor_id, 0x8086)
3239 self.assertEqual(0, obj.numa_node)
3240 self.assertIsInstance(obj.fun_capability[0],
3241 config.LibvirtConfigNodeDevicePciSubFunctionCap)
3242
3243 self.assertEqual(obj.fun_capability[0].type, 'virt_functions')
3244 self.assertEqual(obj.fun_capability[0].device_addrs,
3245 [(0, 10, 1, 1),
3246 (1, 10, 2, 3), ])
3247 self.assertEqual(obj.fun_capability[1].type, 'phys_function')
3248 self.assertEqual(obj.fun_capability[1].device_addrs,
3249 [(0, 10, 1, 1), ])
3250
3251 def test_config_device_pci_mdev_capable(self):
3252 xmlin = """
3253 <capability type="pci">
3254 <domain>0</domain>
3255 <bus>10</bus>
3256 <slot>1</slot>
3257 <function>5</function>
3258 <product id="0x0FFE">GRID M60-0B</product>
3259 <vendor id="0x10DE">Nvidia</vendor>
3260 <capability type='mdev_types'>
3261 <type id='nvidia-11'>
3262 <name>GRID M60-0B</name>
3263 <deviceAPI>vfio-pci</deviceAPI>
3264 <availableInstances>16</availableInstances>
3265 </type>
3266 </capability>
3267 </capability>"""
3268 obj = config.LibvirtConfigNodeDevicePciCap()
3269 obj.parse_str(xmlin)
3270
3271 self.assertEqual(0, obj.domain)
3272 self.assertEqual(10, obj.bus)
3273 self.assertEqual(1, obj.slot)
3274 self.assertEqual(5, obj.function)
3275 self.assertEqual("GRID M60-0B", obj.product)
3276 self.assertEqual(0x0FFE, obj.product_id)
3277 self.assertEqual("Nvidia", obj.vendor)
3278 self.assertEqual(0x10DE, obj.vendor_id)
3279 self.assertIsNone(obj.numa_node)
3280 self.assertIsInstance(
3281 obj.mdev_capability[0],
3282 config.LibvirtConfigNodeDeviceMdevCapableSubFunctionCap)
3283
3284 self.assertEqual([{
3285 'availableInstances': 16,
3286 'deviceAPI': 'vfio-pci',
3287 'name': 'GRID M60-0B',
3288 'type': 'nvidia-11'}], obj.mdev_capability[0].mdev_types)
3289
3290
3291 class LibvirtConfigNodeDevicePciSubFunctionCap(LibvirtConfigBaseTest):
3292
3293 def test_config_device_pci_subfunction(self):
3294 xmlin = """
3295 <capability type="virt_functions">
3296 <address domain="0000" bus="0x0a" slot="0x1" function="0x1"/>
3297 <address domain="0001" bus="0x0a" slot="0x02" function="0x03"/>
3298 </capability>"""
3299 fun_capability = config.LibvirtConfigNodeDevicePciSubFunctionCap()
3300 fun_capability.parse_str(xmlin)
3301 self.assertEqual('virt_functions', fun_capability.type)
3302 self.assertEqual([(0, 10, 1, 1),
3303 (1, 10, 2, 3)],
3304 fun_capability.device_addrs)
3305
3306
3307 class LibvirtConfigGuestVideoTest(LibvirtConfigBaseTest):
3308
3309 def test_config_video_driver(self):
3310 obj = config.LibvirtConfigGuestVideo()
3311 obj.type = 'qxl'
3312
3313 self.assertFalse(obj.uses_virtio)
3314
3315 xml = obj.to_xml()
3316 self.assertXmlEqual(xml, """
3317 <video>
3318 <model type='qxl'/>
3319 </video>""")
3320
3321 def test_config_video_driver_virtio(self):
3322 obj = config.LibvirtConfigGuestVideo()
3323 obj.type = 'virtio'
3324
3325 self.assertTrue(obj.uses_virtio)
3326
3327 xml = obj.to_xml()
3328 self.assertXmlEqual(xml, """
3329 <video>
3330 <model type='virtio'/>
3331 </video>""")
3332
3333 def test_config_video_driver_vram_heads(self):
3334 obj = config.LibvirtConfigGuestVideo()
3335 obj.type = 'qxl'
3336 obj.vram = '9216'
3337 obj.heads = '1'
3338
3339 xml = obj.to_xml()
3340 self.assertXmlEqual(xml, """
3341 <video>
3342 <model type='qxl' vram='9216' heads='1'/>
3343 </video>""")
3344
3345
3346 class LibvirtConfigGuestSeclabel(LibvirtConfigBaseTest):
3347
3348 def test_config_seclabel_config(self):
3349 obj = config.LibvirtConfigSeclabel()
3350
3351 xml = obj.to_xml()
3352 self.assertXmlEqual(xml, """
3353 <seclabel type='dynamic'/>""")
3354
3355 def test_config_seclabel_baselabel(self):
3356 obj = config.LibvirtConfigSeclabel()
3357 obj.type = 'dynamic'
3358 obj.baselabel = 'system_u:system_r:my_svirt_t:s0'
3359
3360 xml = obj.to_xml()
3361 self.assertXmlEqual(xml, """
3362 <seclabel type='dynamic'>
3363 <baselabel>system_u:system_r:my_svirt_t:s0</baselabel>
3364 </seclabel>""")
3365
3366
3367 class LibvirtConfigGuestRngTest(LibvirtConfigBaseTest):
3368
3369 def test_config_rng_driver(self):
3370 obj = config.LibvirtConfigGuestRng()
3371
3372 xml = obj.to_xml()
3373 self.assertXmlEqual(xml, """
3374 <rng model='virtio'>
3375 <backend model='random'/>
3376 </rng>""")
3377
3378 def test_config_rng_driver_with_rate(self):
3379 obj = config.LibvirtConfigGuestRng()
3380 obj.backend = '/dev/urandom'
3381 obj.rate_period = '12'
3382 obj.rate_bytes = '34'
3383
3384 xml = obj.to_xml()
3385 self.assertXmlEqual(xml, """
3386 <rng model='virtio'>
3387 <rate period='12' bytes='34'/>
3388 <backend model='random'>/dev/urandom</backend>
3389 </rng>""")
3390
3391 def test_config_rng_driver_iommu(self):
3392 obj = config.LibvirtConfigGuestRng()
3393 obj.driver_iommu = True
3394
3395 self.assertTrue(obj.uses_virtio)
3396
3397 xml = obj.to_xml()
3398 self.assertXmlEqual(xml, """
3399 <rng model='virtio'>
3400 <backend model='random'/>
3401 <driver iommu="on"/>
3402 </rng>""")
3403
3404
3405 class LibvirtConfigGuestControllerTest(LibvirtConfigBaseTest):
3406
3407 def test_config_guest_contoller(self):
3408 obj = config.LibvirtConfigGuestController()
3409 obj.type = 'scsi'
3410 obj.index = 0
3411 obj.model = 'virtio-scsi'
3412 obj.driver_iommu = True
3413
3414 self.assertTrue(obj.uses_virtio)
3415
3416 xml = obj.to_xml()
3417 self.assertXmlEqual(xml, """
3418 <controller type='scsi' index='0' model='virtio-scsi'>
3419 <driver iommu="on" />
3420 </controller>""")
3421
3422 def test_config_guest_usb_host_controller(self):
3423 obj = config.LibvirtConfigGuestUSBHostController()
3424 obj.type = 'usb'
3425 obj.index = 0
3426
3427 xml = obj.to_xml()
3428 self.assertXmlEqual(xml, "<controller type='usb' index='0'/>")
3429
3430
3431 class LibvirtConfigGuestWatchdogTest(LibvirtConfigBaseTest):
3432 def test_config_watchdog(self):
3433 obj = config.LibvirtConfigGuestWatchdog()
3434 obj.action = 'none'
3435
3436 xml = obj.to_xml()
3437 self.assertXmlEqual(xml, "<watchdog model='i6300esb' action='none'/>")
3438
3439 def test_config_watchdog_default_action(self):
3440 obj = config.LibvirtConfigGuestWatchdog()
3441
3442 xml = obj.to_xml()
3443 self.assertXmlEqual(xml, "<watchdog model='i6300esb' action='reset'/>")
3444
3445
3446 class LibvirtConfigGuestCPUTuneTest(LibvirtConfigBaseTest):
3447
3448 def test_config_cputune_timeslice(self):
3449 cputune = config.LibvirtConfigGuestCPUTune()
3450 cputune.shares = 100
3451 cputune.quota = 50000
3452 cputune.period = 25000
3453
3454 xml = cputune.to_xml()
3455 self.assertXmlEqual(xml, """
3456 <cputune>
3457 <shares>100</shares>
3458 <quota>50000</quota>
3459 <period>25000</period>
3460 </cputune>""")
3461
3462 def test_config_cputune_vcpus(self):
3463 cputune = config.LibvirtConfigGuestCPUTune()
3464
3465 vcpu0 = config.LibvirtConfigGuestCPUTuneVCPUPin()
3466 vcpu0.id = 0
3467 vcpu0.cpuset = set([0, 1])
3468 vcpu1 = config.LibvirtConfigGuestCPUTuneVCPUPin()
3469 vcpu1.id = 1
3470 vcpu1.cpuset = set([2, 3])
3471 vcpu2 = config.LibvirtConfigGuestCPUTuneVCPUPin()
3472 vcpu2.id = 2
3473 vcpu2.cpuset = set([4, 5])
3474 vcpu3 = config.LibvirtConfigGuestCPUTuneVCPUPin()
3475 vcpu3.id = 3
3476 vcpu3.cpuset = set([6, 7])
3477 cputune.vcpupin.extend([vcpu0, vcpu1, vcpu2, vcpu3])
3478
3479 emu = config.LibvirtConfigGuestCPUTuneEmulatorPin()
3480 emu.cpuset = set([0, 1, 2, 3, 4, 5, 6, 7])
3481 cputune.emulatorpin = emu
3482
3483 sch0 = config.LibvirtConfigGuestCPUTuneVCPUSched()
3484 sch0.vcpus = set([0, 1, 2, 3])
3485 sch0.scheduler = "fifo"
3486 sch0.priority = 1
3487 sch1 = config.LibvirtConfigGuestCPUTuneVCPUSched()
3488 sch1.vcpus = set([4, 5, 6, 7])
3489 sch1.scheduler = "fifo"
3490 sch1.priority = 99
3491 cputune.vcpusched.extend([sch0, sch1])
3492
3493 xml = cputune.to_xml()
3494 self.assertXmlEqual(xml, """
3495 <cputune>
3496 <emulatorpin cpuset="0-7"/>
3497 <vcpupin vcpu="0" cpuset="0-1"/>
3498 <vcpupin vcpu="1" cpuset="2-3"/>
3499 <vcpupin vcpu="2" cpuset="4-5"/>
3500 <vcpupin vcpu="3" cpuset="6-7"/>
3501 <vcpusched vcpus="0-3" scheduler="fifo" priority="1"/>
3502 <vcpusched vcpus="4-7" scheduler="fifo" priority="99"/>
3503 </cputune>""")
3504
3505
3506 class LibvirtConfigGuestMemoryBackingTest(LibvirtConfigBaseTest):
3507 def test_config_memory_backing_none(self):
3508 obj = config.LibvirtConfigGuestMemoryBacking()
3509
3510 xml = obj.to_xml()
3511 self.assertXmlEqual(xml, "<memoryBacking/>")
3512
3513 def test_config_memory_backing_all(self):
3514 obj = config.LibvirtConfigGuestMemoryBacking()
3515 obj.locked = True
3516 obj.sharedpages = False
3517 page = config.LibvirtConfigGuestMemoryBackingPage()
3518 page.size_kb = 2048
3519 page.nodeset = [2, 3]
3520 obj.hugepages.append(page)
3521
3522 xml = obj.to_xml()
3523 self.assertXmlEqual(xml, """
3524 <memoryBacking>
3525 <hugepages>
3526 <page size="2048" unit="KiB" nodeset="2-3"/>
3527 </hugepages>
3528 <nosharepages/>
3529 <locked/>
3530 </memoryBacking>""")
3531
3532 def test_config_memory_backing_source_all(self):
3533 obj = config.LibvirtConfigGuestMemoryBacking()
3534 obj.sharedaccess = True
3535 obj.allocateimmediate = True
3536 obj.filesource = True
3537 obj.discard = True
3538 xml = obj.to_xml()
3539 self.assertXmlEqual(xml, """
3540 <memoryBacking>
3541 <source type="file"/>
3542 <access mode="shared"/>
3543 <allocation mode="immediate"/>
3544 <discard />
3545 </memoryBacking>""")
3546
3547
3548 class LibvirtConfigGuestMemoryTuneTest(LibvirtConfigBaseTest):
3549 def test_config_memory_backing_none(self):
3550 obj = config.LibvirtConfigGuestMemoryTune()
3551
3552 xml = obj.to_xml()
3553 self.assertXmlEqual(xml, "<memtune/>")
3554
3555 def test_config_memory_backing_all(self):
3556 obj = config.LibvirtConfigGuestMemoryTune()
3557 obj.soft_limit = 6
3558 obj.hard_limit = 28
3559 obj.swap_hard_limit = 140
3560 obj.min_guarantee = 270
3561
3562 xml = obj.to_xml()
3563 self.assertXmlEqual(xml, """
3564 <memtune>
3565 <hard_limit unit="KiB">28</hard_limit>
3566 <soft_limit unit="KiB">6</soft_limit>
3567 <swap_hard_limit unit="KiB">140</swap_hard_limit>
3568 <min_guarantee unit="KiB">270</min_guarantee>
3569 </memtune>""")
3570
3571
3572 class LibvirtConfigGuestNUMATuneTest(LibvirtConfigBaseTest):
3573 def test_config_numa_tune_none(self):
3574 obj = config.LibvirtConfigGuestNUMATune()
3575
3576 xml = obj.to_xml()
3577 self.assertXmlEqual("<numatune/>", xml)
3578
3579 def test_config_numa_tune_memory(self):
3580 obj = config.LibvirtConfigGuestNUMATune()
3581
3582 numamemory = config.LibvirtConfigGuestNUMATuneMemory()
3583 numamemory.nodeset = [0, 1, 2, 3, 8]
3584
3585 obj.memory = numamemory
3586
3587 xml = obj.to_xml()
3588 self.assertXmlEqual("""
3589 <numatune>
3590 <memory mode="strict" nodeset="0-3,8"/>
3591 </numatune>""", xml)
3592
3593 def test_config_numa_tune_memnodes(self):
3594 obj = config.LibvirtConfigGuestNUMATune()
3595
3596 numamemnode0 = config.LibvirtConfigGuestNUMATuneMemNode()
3597 numamemnode0.cellid = 0
3598 numamemnode0.nodeset = [0, 1]
3599
3600 numamemnode1 = config.LibvirtConfigGuestNUMATuneMemNode()
3601 numamemnode1.cellid = 1
3602 numamemnode1.nodeset = [2, 3]
3603
3604 numamemnode2 = config.LibvirtConfigGuestNUMATuneMemNode()
3605 numamemnode2.cellid = 2
3606 numamemnode2.nodeset = [8]
3607
3608 obj.memnodes.extend([numamemnode0,
3609 numamemnode1,
3610 numamemnode2])
3611
3612 xml = obj.to_xml()
3613 self.assertXmlEqual("""
3614 <numatune>
3615 <memnode cellid="0" mode="strict" nodeset="0-1"/>
3616 <memnode cellid="1" mode="strict" nodeset="2-3"/>
3617 <memnode cellid="2" mode="strict" nodeset="8"/>
3618 </numatune>""", xml)
3619
3620
3621 class LibvirtConfigGuestMetadataNovaTest(LibvirtConfigBaseTest):
3622
3623 def test_config_metadata(self):
3624 meta = config.LibvirtConfigGuestMetaNovaInstance()
3625 meta.package = "2014.2.3"
3626 meta.name = "moonbuggy"
3627 meta.creationTime = 1234567890
3628 meta.roottype = "image"
3629 meta.rootid = "fe55c69a-8b2e-4bbc-811a-9ad2023a0426"
3630
3631 owner = config.LibvirtConfigGuestMetaNovaOwner()
3632 owner.userid = "3472c2a6-de91-4fb5-b618-42bc781ef670"
3633 owner.username = "buzz"
3634 owner.projectid = "f241e906-010e-4917-ae81-53f4fb8aa021"
3635 owner.projectname = "moonshot"
3636
3637 meta.owner = owner
3638
3639 flavor = config.LibvirtConfigGuestMetaNovaFlavor()
3640 flavor.name = "m1.lowgravity"
3641 flavor.vcpus = 8
3642 flavor.memory = 2048
3643 flavor.swap = 10
3644 flavor.disk = 50
3645 flavor.ephemeral = 10
3646
3647 meta.flavor = flavor
3648
3649 xml = meta.to_xml()
3650 self.assertXmlEqual(xml, """
3651 <nova:instance xmlns:nova='http://openstack.org/xmlns/libvirt/nova/1.0'>
3652 <nova:package version="2014.2.3"/>
3653 <nova:name>moonbuggy</nova:name>
3654 <nova:creationTime>2009-02-13 23:31:30</nova:creationTime>
3655 <nova:flavor name="m1.lowgravity">
3656 <nova:memory>2048</nova:memory>
3657 <nova:disk>50</nova:disk>
3658 <nova:swap>10</nova:swap>
3659 <nova:ephemeral>10</nova:ephemeral>
3660 <nova:vcpus>8</nova:vcpus>
3661 </nova:flavor>
3662 <nova:owner>
3663 <nova:user
3664 uuid="3472c2a6-de91-4fb5-b618-42bc781ef670">buzz</nova:user>
3665 <nova:project
3666 uuid="f241e906-010e-4917-ae81-53f4fb8aa021">moonshot</nova:project>
3667 </nova:owner>
3668 <nova:root type="image" uuid="fe55c69a-8b2e-4bbc-811a-9ad2023a0426"/>
3669 </nova:instance>
3670 """)
3671
3672
3673 class LibvirtConfigGuestIDMap(LibvirtConfigBaseTest):
3674 def test_config_id_map_parse_start_not_int(self):
3675 xmlin = "<uid start='a' target='20000' count='5'/>"
3676 obj = config.LibvirtConfigGuestIDMap()
3677
3678 self.assertRaises(ValueError, obj.parse_str, xmlin)
3679
3680 def test_config_id_map_parse_target_not_int(self):
3681 xmlin = "<uid start='2' target='a' count='5'/>"
3682 obj = config.LibvirtConfigGuestIDMap()
3683
3684 self.assertRaises(ValueError, obj.parse_str, xmlin)
3685
3686 def test_config_id_map_parse_count_not_int(self):
3687 xmlin = "<uid start='2' target='20000' count='a'/>"
3688 obj = config.LibvirtConfigGuestIDMap()
3689
3690 self.assertRaises(ValueError, obj.parse_str, xmlin)
3691
3692 def test_config_uid_map(self):
3693 obj = config.LibvirtConfigGuestUIDMap()
3694 obj.start = 1
3695 obj.target = 10000
3696 obj.count = 2
3697
3698 xml = obj.to_xml()
3699 self.assertXmlEqual("<uid start='1' target='10000' count='2'/>", xml)
3700
3701 def test_config_uid_map_parse(self):
3702 xmlin = "<uid start='2' target='20000' count='5'/>"
3703 obj = config.LibvirtConfigGuestUIDMap()
3704 obj.parse_str(xmlin)
3705
3706 self.assertEqual(2, obj.start)
3707 self.assertEqual(20000, obj.target)
3708 self.assertEqual(5, obj.count)
3709
3710 def test_config_gid_map(self):
3711 obj = config.LibvirtConfigGuestGIDMap()
3712 obj.start = 1
3713 obj.target = 10000
3714 obj.count = 2
3715
3716 xml = obj.to_xml()
3717 self.assertXmlEqual("<gid start='1' target='10000' count='2'/>", xml)
3718
3719 def test_config_gid_map_parse(self):
3720 xmlin = "<gid start='2' target='20000' count='5'/>"
3721 obj = config.LibvirtConfigGuestGIDMap()
3722 obj.parse_str(xmlin)
3723
3724 self.assertEqual(2, obj.start)
3725 self.assertEqual(20000, obj.target)
3726 self.assertEqual(5, obj.count)
3727
3728
3729 class LibvirtConfigMemoryBalloonTest(LibvirtConfigBaseTest):
3730
3731 def test_config_memory_balloon_period(self):
3732 balloon = config.LibvirtConfigMemoryBalloon()
3733 balloon.model = 'virtio'
3734 balloon.period = 11
3735
3736 xml = balloon.to_xml()
3737 expected_xml = """
3738 <memballoon model='virtio'>
3739 <stats period='11'/>
3740 </memballoon>"""
3741
3742 self.assertXmlEqual(expected_xml, xml)
3743
3744 def test_config_memory_balloon_no_period(self):
3745 balloon = config.LibvirtConfigMemoryBalloon()
3746 balloon.model = 'virtio'
3747
3748 xml = balloon.to_xml()
3749 expected_xml = """
3750 <memballoon model='virtio' />"""
3751
3752 self.assertXmlEqual(expected_xml, xml)
3753
3754 def test_config_memory_balloon_driver_iommu(self):
3755 balloon = config.LibvirtConfigMemoryBalloon()
3756 balloon.model = 'virtio'
3757 balloon.driver_iommu = True
3758
3759 self.assertTrue(balloon.uses_virtio)
3760
3761 xml = balloon.to_xml()
3762 expected_xml = """
3763 <memballoon model='virtio'>
3764 <driver iommu="on" />
3765 </memballoon>"""
3766
3767 self.assertXmlEqual(expected_xml, xml)
3768
3769
3770 class LibvirtConfigSecretTest(LibvirtConfigBaseTest):
3771
3772 def test_config_secret_volume(self):
3773 secret = config.LibvirtConfigSecret()
3774 secret.ephemeral = False
3775 secret.private = False
3776 secret.description = 'sample desc'
3777 secret.uuid = 'c7a5fdbd-edaf-9455-926a-d65c16db1809'
3778 secret.usage_type = 'volume'
3779 secret.usage_id = 'sample_volume'
3780
3781 xml = secret.to_xml()
3782 expected_xml = """
3783 <secret ephemeral="no" private="no">
3784 <description>sample desc</description>
3785 <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
3786 <usage type="volume">
3787 <volume>sample_volume</volume>
3788 </usage>
3789 </secret>"""
3790
3791 self.assertXmlEqual(expected_xml, xml)
3792
3793 def test_config_secret_ceph(self):
3794 secret = config.LibvirtConfigSecret()
3795 secret.ephemeral = False
3796 secret.private = False
3797 secret.description = 'sample desc'
3798 secret.usage_type = 'ceph'
3799 secret.usage_id = 'sample_name'
3800
3801 xml = secret.to_xml()
3802 expected_xml = """
3803 <secret ephemeral="no" private="no">
3804 <description>sample desc</description>
3805 <usage type="ceph">
3806 <name>sample_name</name>
3807 </usage>
3808 </secret>"""
3809
3810 self.assertXmlEqual(expected_xml, xml)
3811
3812 def test_config_secret_vtpm(self):
3813 secret = config.LibvirtConfigSecret()
3814 secret.ephemeral = True
3815 secret.private = True
3816 secret.usage_type = 'vtpm'
3817 secret.usage_id = 'sample_name'
3818 secret.uuid = uuids.vtpm
3819
3820 xml = secret.to_xml()
3821 expected_xml = f"""
3822 <secret ephemeral="yes" private="yes">
3823 <uuid>{uuids.vtpm}</uuid>
3824 <usage type="vtpm">
3825 <name>sample_name</name>
3826 </usage>
3827 </secret>"""
3828
3829 self.assertXmlEqual(expected_xml, xml)
3830
3831 def test_config_secret_iscsi(self):
3832 secret = config.LibvirtConfigSecret()
3833 secret.ephemeral = False
3834 secret.private = False
3835 secret.description = 'sample desc'
3836 secret.usage_type = 'iscsi'
3837 secret.usage_id = 'sample_target'
3838
3839 xml = secret.to_xml()
3840 expected_xml = """
3841 <secret ephemeral="no" private="no">
3842 <description>sample desc</description>
3843 <usage type="iscsi">
3844 <target>sample_target</target>
3845 </usage>
3846 </secret>"""
3847
3848 self.assertXmlEqual(expected_xml, xml)
3849
3850
3851 class LibvirtConfigGuestVPMEMTest(LibvirtConfigBaseTest):
3852 def test_config_vpmem(self):
3853 obj = config.LibvirtConfigGuestVPMEM(
3854 devpath='/dev/dax0.0', size_kb=4096 * units.Ki, align_kb=2048)
3855
3856 xml = obj.to_xml()
3857 self.assertXmlEqual(xml, """
3858 <memory model='nvdimm' access="shared">
3859 <source>
3860 <path>/dev/dax0.0</path>
3861 <alignsize>2048</alignsize>
3862 <pmem/>
3863 </source>
3864 <target>
3865 <size>4194304</size>
3866 <node>0</node>
3867 <label>
3868 <size>2048</size>
3869 </label>
3870 </target>
3871 </memory>""")
3872
3873
3874 class LibvirtConfigVideoModelsTests(LibvirtConfigBaseTest):
3875
3876 def test_parse_video_model(self):
3877
3878 xml = """
3879 <video supported='yes'>
3880 <enum name='modelType'>
3881 <value>vga</value>
3882 <value>cirrus</value>
3883 <value>vmvga</value>
3884 <value>qxl</value>
3885 <value>virtio</value>
3886 </enum>
3887 </video>
3888 """
3889 obj = config.LibvirtConfigDomainCapsVideoModels()
3890 obj.parse_str(xml)
3891 expected_models = ('vga', 'cirrus', 'vmvga', 'qxl', 'virtio')
3892 self.assertTrue(obj.supported)
3893 for model in expected_models:
3894 self.assertIn(model, obj.models)
3895 self.assertNotIn('gop', obj.models)
3896
3897
3898 class LibvirtConfigDiskBusesTests(LibvirtConfigBaseTest):
3899
3900 def test_parse_disk_buses(self):
3901
3902 xml = """
3903 <disk supported='yes'>
3904 <enum name='diskDevice'>
3905 <value>disk</value>
3906 <value>cdrom</value>
3907 <value>floppy</value>
3908 <value>lun</value>
3909 </enum>
3910 <enum name='bus'>
3911 <value>ide</value>
3912 <value>scsi</value>
3913 <value>virtio</value>
3914 <value>usb</value>
3915 <value>sata</value>
3916 </enum>
3917 </disk>
3918 """
3919 obj = config.LibvirtConfigDomainCapsDiskBuses()
3920 obj.parse_str(xml)
3921 expected_buses = ('ide', 'scsi', 'virtio', 'usb', 'sata')
3922 self.assertTrue(obj.supported)
3923 for bus in expected_buses:
3924 self.assertIn(bus, obj.buses)
3925 self.assertNotIn('fdc', obj.buses)
3926
3927
3928 class LibvirtConfigDomainCapsDevicesTests(LibvirtConfigBaseTest):
3929
3930 def test_parse_domain_caps_devices(self):
3931
3932 xml = """
3933 <devices>
3934 <disk supported='yes'>
3935 <enum name='diskDevice'>
3936 <value>disk</value>
3937 <value>cdrom</value>
3938 <value>floppy</value>
3939 <value>lun</value>
3940 </enum>
3941 <enum name='bus'>
3942 <value>ide</value>
3943 <value>fdc</value>
3944 <value>scsi</value>
3945 <value>virtio</value>
3946 <value>usb</value>
3947 <value>sata</value>
3948 </enum>
3949 </disk>
3950 <graphics supported='yes'>
3951 <enum name='type'>
3952 <value>sdl</value>
3953 <value>vnc</value>
3954 <value>spice</value>
3955 </enum>
3956 </graphics>
3957 <video supported='yes'>
3958 <enum name='modelType'>
3959 <value>vga</value>
3960 <value>cirrus</value>
3961 <value>vmvga</value>
3962 <value>qxl</value>
3963 <value>virtio</value>
3964 </enum>
3965 </video>
3966 <hostdev supported='yes'>
3967 <enum name='mode'>
3968 <value>subsystem</value>
3969 </enum>
3970 <enum name='startupPolicy'>
3971 <value>default</value>
3972 <value>mandatory</value>
3973 <value>requisite</value>
3974 <value>optional</value>
3975 </enum>
3976 <enum name='subsysType'>
3977 <value>usb</value>
3978 <value>pci</value>
3979 <value>scsi</value>
3980 </enum>
3981 <enum name='capsType'/>
3982 <enum name='pciBackend'/>
3983 </hostdev>
3984 </devices>
3985 """
3986 obj = config.LibvirtConfigDomainCapsDevices()
3987 obj.parse_str(xml)
3988 # we only use the video and disk devices today.
3989 device_types = [config.LibvirtConfigDomainCapsDiskBuses,
3990 config.LibvirtConfigDomainCapsVideoModels]
3991 # so we assert there are only two device types parsed
3992 self.assertEqual(2, len(obj.devices))
3993 # we then assert that the parsed devices are of the correct type
3994 for dev in obj.devices:
3995 self.assertIn(type(dev), device_types)
3996 # and that the sub-devices are accessible directly via properties.
3997 self.assertIsInstance(
3998 obj.disk, config.LibvirtConfigDomainCapsDiskBuses)
3999 self.assertIsInstance(
4000 obj.video, config.LibvirtConfigDomainCapsVideoModels)
4001
4002
4003 class LibvirtConfigTPMTest(LibvirtConfigBaseTest):
4004
4005 def test_config_tpm_tis_1_2(self):
4006 vtpm_config = hardware.VTPMConfig('1.2', 'tpm-tis')
4007 vtpm_secret_uuid = 'b028130c-bdcb-4d5f-9bca-b9175ca6c28c'
4008 expected_xml = """
4009 <tpm model='tpm-tis'>
4010 <backend type='emulator' version='1.2'>
4011 <encryption secret='b028130c-bdcb-4d5f-9bca-b9175ca6c28c'/>
4012 </backend>
4013 </tpm>"""
4014
4015 tpm = config.LibvirtConfigGuestVTPM(vtpm_config, vtpm_secret_uuid)
4016 xml = tpm.to_xml()
4017
4018 self.assertXmlEqual(expected_xml, xml)
4019
4020 def test_config_tpm_tis_2_0(self):
4021 vtpm_config = hardware.VTPMConfig('2.0', 'tpm-tis')
4022 vtpm_secret_uuid = 'b028130c-bdcb-4d5f-9bca-b9175ca6c28c'
4023 expected_xml = """
4024 <tpm model='tpm-tis'>
4025 <backend type='emulator' version='2.0'>
4026 <encryption secret='b028130c-bdcb-4d5f-9bca-b9175ca6c28c'/>
4027 </backend>
4028 </tpm>"""
4029
4030 tpm = config.LibvirtConfigGuestVTPM(vtpm_config, vtpm_secret_uuid)
4031 xml = tpm.to_xml()
4032
4033 self.assertXmlEqual(expected_xml, xml)
4034
4035 def test_config_tpm_crb_2_0(self):
4036 vtpm_config = hardware.VTPMConfig('2.0', 'tpm-crb')
4037 vtpm_secret_uuid = 'b028130c-bdcb-4d5f-9bca-b9175ca6c28c'
4038 expected_xml = """
4039 <tpm model='tpm-crb'>
4040 <backend type='emulator' version='2.0'>
4041 <encryption secret='b028130c-bdcb-4d5f-9bca-b9175ca6c28c'/>
4042 </backend>
4043 </tpm>"""
4044
4045 tpm = config.LibvirtConfigGuestVTPM(vtpm_config, vtpm_secret_uuid)
4046 xml = tpm.to_xml()
4047
4048 self.assertXmlEqual(expected_xml, xml)