# Copyright (C) 2012 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from lxml import etree from oslo_utils import units from nova.compute import arch from nova import test from nova.tests.unit import matchers from nova.virt.libvirt import config class LibvirtConfigBaseTest(test.NoDBTestCase): def assertXmlEqual(self, expectedXmlstr, actualXmlstr): self.assertThat(actualXmlstr, matchers.XMLMatches(expectedXmlstr)) class LibvirtConfigTest(LibvirtConfigBaseTest): def test_config_plain(self): obj = config.LibvirtConfigObject(root_name="demo") xml = obj.to_xml() self.assertXmlEqual(xml, "") def test_config_ns(self): obj = config.LibvirtConfigObject(root_name="demo", ns_prefix="foo", ns_uri="http://example.com/foo") xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_text(self): obj = config.LibvirtConfigObject(root_name="demo") root = obj.format_dom() root.append(obj._text_node("foo", "bar")) xml = etree.tostring(root) self.assertXmlEqual(xml, "bar") def test_config_text_unicode(self): obj = config.LibvirtConfigObject(root_name='demo') root = obj.format_dom() root.append(obj._text_node('foo', u'\xF0\x9F\x92\xA9')) self.assertXmlEqual('💩', etree.tostring(root)) def test_config_parse(self): inxml = "" obj = config.LibvirtConfigObject(root_name="demo") obj.parse_str(inxml) class LibvirtConfigCapsTest(LibvirtConfigBaseTest): def test_config_host(self): xmlin = """ c7a5fdbd-edaf-9455-926a-d65c16db1809 x86_64 Opteron_G3 AMD 4048280 1011941 0 4127684 1031921 0 hvm hvm """ obj = config.LibvirtConfigCaps() obj.parse_str(xmlin) self.assertIsInstance(obj.host, config.LibvirtConfigCapsHost) self.assertEqual(obj.host.uuid, "c7a5fdbd-edaf-9455-926a-d65c16db1809") xmlout = obj.to_xml() self.assertXmlEqual(xmlin, xmlout) def test_config_host_numa_cell_no_memory_caps(self): xmlin = """ """ obj = config.LibvirtConfigCapsNUMACell() obj.parse_str(xmlin) self.assertEqual(0, obj.memory) self.assertEqual(1, len(obj.cpus)) def test_config_host_numa_cell_no_cpus_caps(self): xmlin = """ 128 """ obj = config.LibvirtConfigCapsNUMACell() obj.parse_str(xmlin) self.assertEqual(128, obj.memory) self.assertEqual(0, len(obj.cpus)) class LibvirtConfigGuestTimerTest(LibvirtConfigBaseTest): def test_config_platform(self): obj = config.LibvirtConfigGuestTimer() obj.track = "host" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_pit(self): obj = config.LibvirtConfigGuestTimer() obj.name = "pit" obj.tickpolicy = "discard" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_hpet(self): obj = config.LibvirtConfigGuestTimer() obj.name = "hpet" obj.present = False xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestClockTest(LibvirtConfigBaseTest): def test_config_utc(self): obj = config.LibvirtConfigGuestClock() xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_localtime(self): obj = config.LibvirtConfigGuestClock() obj.offset = "localtime" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_timezone(self): obj = config.LibvirtConfigGuestClock() obj.offset = "timezone" obj.timezone = "EDT" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_variable(self): obj = config.LibvirtConfigGuestClock() obj.offset = "variable" obj.adjustment = "123456" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_timers(self): obj = config.LibvirtConfigGuestClock() tmpit = config.LibvirtConfigGuestTimer() tmpit.name = "pit" tmpit.tickpolicy = "discard" tmrtc = config.LibvirtConfigGuestTimer() tmrtc.name = "rtc" tmrtc.tickpolicy = "merge" obj.add_timer(tmpit) obj.add_timer(tmrtc) xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigCPUFeatureTest(LibvirtConfigBaseTest): def test_config_simple(self): obj = config.LibvirtConfigCPUFeature("mtrr") xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestCPUFeatureTest(LibvirtConfigBaseTest): def test_config_simple(self): obj = config.LibvirtConfigGuestCPUFeature("mtrr") obj.policy = "force" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestCPUNUMATest(LibvirtConfigBaseTest): def test_parse_dom(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestCPUNUMA() obj.parse_dom(xmldoc) self.assertEqual(2, len(obj.cells)) def test_config_simple(self): obj = config.LibvirtConfigGuestCPUNUMA() cell = config.LibvirtConfigGuestCPUNUMACell() cell.id = 0 cell.cpus = set([0, 1]) cell.memory = 1000000 cell.memAccess = "shared" obj.cells.append(cell) cell = config.LibvirtConfigGuestCPUNUMACell() cell.id = 1 cell.cpus = set([2, 3]) cell.memory = 1500000 cell.memAccess = "private" obj.cells.append(cell) xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigCPUTest(LibvirtConfigBaseTest): def test_config_simple(self): obj = config.LibvirtConfigCPU() obj.model = "Penryn" xml = obj.to_xml() self.assertXmlEqual(xml, """ Penryn """) def test_config_complex(self): obj = config.LibvirtConfigCPU() obj.model = "Penryn" obj.vendor = "Intel" obj.arch = arch.X86_64 obj.add_feature(config.LibvirtConfigCPUFeature("mtrr")) obj.add_feature(config.LibvirtConfigCPUFeature("apic")) xml = obj.to_xml() self.assertXmlEqual(xml, """ x86_64 Penryn Intel """) def test_only_uniq_cpu_featues(self): obj = config.LibvirtConfigCPU() obj.model = "Penryn" obj.vendor = "Intel" obj.arch = arch.X86_64 obj.add_feature(config.LibvirtConfigCPUFeature("mtrr")) obj.add_feature(config.LibvirtConfigCPUFeature("apic")) obj.add_feature(config.LibvirtConfigCPUFeature("apic")) obj.add_feature(config.LibvirtConfigCPUFeature("mtrr")) xml = obj.to_xml() self.assertXmlEqual(xml, """ x86_64 Penryn Intel """) def test_config_topology(self): obj = config.LibvirtConfigCPU() obj.model = "Penryn" obj.sockets = 4 obj.cores = 4 obj.threads = 2 xml = obj.to_xml() self.assertXmlEqual(xml, """ Penryn """) class LibvirtConfigGuestCPUTest(LibvirtConfigBaseTest): def test_config_simple(self): obj = config.LibvirtConfigGuestCPU() obj.model = "Penryn" xml = obj.to_xml() self.assertXmlEqual(xml, """ Penryn """) def test_config_complex(self): obj = config.LibvirtConfigGuestCPU() obj.model = "Penryn" obj.vendor = "Intel" obj.arch = arch.X86_64 obj.mode = "custom" obj.add_feature(config.LibvirtConfigGuestCPUFeature("mtrr")) obj.add_feature(config.LibvirtConfigGuestCPUFeature("apic")) xml = obj.to_xml() self.assertXmlEqual(xml, """ x86_64 Penryn Intel """) def test_config_host(self): obj = config.LibvirtConfigGuestCPU() obj.mode = "host-model" obj.match = "exact" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_host_with_numa(self): obj = config.LibvirtConfigGuestCPU() obj.mode = "host-model" obj.match = "exact" numa = config.LibvirtConfigGuestCPUNUMA() cell = config.LibvirtConfigGuestCPUNUMACell() cell.id = 0 cell.cpus = set([0, 1]) cell.memory = 1000000 cell.memAccess = "private" numa.cells.append(cell) cell = config.LibvirtConfigGuestCPUNUMACell() cell.id = 1 cell.cpus = set([2, 3]) cell.memory = 1500000 numa.cells.append(cell) obj.numa = numa xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestSMBIOSTest(LibvirtConfigBaseTest): def test_config_simple(self): obj = config.LibvirtConfigGuestSMBIOS() xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestSysinfoTest(LibvirtConfigBaseTest): def test_config_simple(self): obj = config.LibvirtConfigGuestSysinfo() xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_bios(self): obj = config.LibvirtConfigGuestSysinfo() obj.bios_vendor = "Acme" obj.bios_version = "6.6.6" xml = obj.to_xml() self.assertXmlEqual(xml, """ Acme 6.6.6 """) def test_config_system(self): obj = config.LibvirtConfigGuestSysinfo() obj.system_manufacturer = "Acme" obj.system_product = "Wile Coyote" obj.system_version = "6.6.6" obj.system_serial = "123456" obj.system_uuid = "c7a5fdbd-edaf-9455-926a-d65c16db1809" obj.system_family = "Anvils" xml = obj.to_xml() self.assertXmlEqual(xml, """ Acme Wile Coyote 6.6.6 123456 c7a5fdbd-edaf-9455-926a-d65c16db1809 Anvils """) def test_config_mixed(self): obj = config.LibvirtConfigGuestSysinfo() obj.bios_vendor = "Acme" obj.system_manufacturer = "Acme" obj.system_product = "Wile Coyote" obj.system_uuid = "c7a5fdbd-edaf-9455-926a-d65c16db1809" obj.system_family = "Anvils" xml = obj.to_xml() self.assertXmlEqual(xml, """ Acme Acme Wile Coyote c7a5fdbd-edaf-9455-926a-d65c16db1809 Anvils """) class LibvirtConfigGuestDiskTest(LibvirtConfigBaseTest): def test_config_file(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_file_parse(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'file') self.assertEqual(obj.source_path, '/tmp/hello') self.assertEqual(obj.target_dev, '/dev/hda') self.assertEqual(obj.target_bus, 'ide') self.assertFalse(obj.readonly) self.assertFalse(obj.shareable) def test_config_file_readonly(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.readonly = True xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_file_parse_readonly(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'file') self.assertEqual(obj.source_path, '/tmp/hello') self.assertEqual(obj.target_dev, '/dev/hda') self.assertEqual(obj.target_bus, 'ide') self.assertTrue(obj.readonly) self.assertFalse(obj.shareable) def test_config_file_shareable(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.shareable = True xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_file_parse_shareable(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'file') self.assertEqual(obj.source_path, '/tmp/hello') self.assertEqual(obj.target_dev, '/dev/hda') self.assertEqual(obj.target_bus, 'ide') self.assertFalse(obj.readonly) self.assertTrue(obj.shareable) def test_config_file_serial(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9" xml = obj.to_xml() self.assertXmlEqual(xml, """ 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """) def test_config_file_serial_parse(self): xml = """ 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'file') self.assertEqual(obj.serial, '7a97c4a3-6f59-41d4-bf47-191d7f97f8e9') def test_config_file_discard(self): obj = config.LibvirtConfigGuestDisk() obj.driver_name = "qemu" obj.driver_format = "qcow2" obj.driver_cache = "none" obj.driver_discard = "unmap" obj.source_type = "file" obj.source_path = "/tmp/hello.qcow2" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9" xml = obj.to_xml() self.assertXmlEqual(""" 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """, xml) def test_config_file_discard_parse(self): xml = """ 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual('unmap', obj.driver_discard) def test_config_file_io(self): obj = config.LibvirtConfigGuestDisk() obj.driver_name = "qemu" obj.driver_format = "qcow2" obj.driver_cache = "none" obj.driver_io = "native" obj.source_type = "file" obj.source_path = "/tmp/hello.qcow2" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9" xml = obj.to_xml() self.assertXmlEqual(""" 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """, xml) def test_config_file_io_parse(self): xml = """ 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual('native', obj.driver_io) def test_config_boot_order(self): obj = config.LibvirtConfigGuestDisk() obj.driver_name = "qemu" obj.driver_format = "qcow2" obj.driver_cache = "none" obj.driver_io = "native" obj.source_type = "file" obj.source_path = "/tmp/hello.qcow2" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.serial = "7a97c4a3-6f59-41d4-bf47-191d7f97f8e9" obj.boot_order = "1" xml = obj.to_xml() self.assertXmlEqual(""" 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """, xml) def test_config_boot_order_parse(self): xml = """ 7a97c4a3-6f59-41d4-bf47-191d7f97f8e9 """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.boot_order, "1") def test_config_block(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "block" obj.source_path = "/tmp/hello" obj.source_device = "cdrom" obj.driver_name = "qemu" obj.target_dev = "/dev/hdc" obj.target_bus = "ide" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_block_parse(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'block') self.assertEqual(obj.source_path, '/tmp/hello') self.assertEqual(obj.target_dev, '/dev/hdc') self.assertEqual(obj.target_bus, 'ide') def test_config_network(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "network" obj.source_protocol = "iscsi" obj.source_name = "foo.bar.com" obj.driver_name = "qemu" obj.driver_format = "qcow2" obj.target_dev = "/dev/hda" obj.target_bus = "ide" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_network_parse(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'network') self.assertEqual(obj.source_protocol, 'iscsi') self.assertEqual(obj.source_name, 'foo.bar.com') self.assertEqual(obj.driver_name, 'qemu') self.assertEqual(obj.driver_format, 'qcow2') self.assertEqual(obj.target_dev, '/dev/hda') self.assertEqual(obj.target_bus, 'ide') def test_config_network_no_name(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = 'network' obj.source_protocol = 'nbd' obj.source_hosts = ['foo.bar.com'] obj.source_ports = [None] obj.driver_name = 'qemu' obj.driver_format = 'raw' obj.target_dev = '/dev/vda' obj.target_bus = 'virtio' xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_network_multihost(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = 'network' obj.source_protocol = 'rbd' obj.source_name = 'pool/image' obj.source_hosts = ['foo.bar.com', '::1', '1.2.3.4'] obj.source_ports = [None, '123', '456'] obj.driver_name = 'qemu' obj.driver_format = 'raw' obj.target_dev = '/dev/vda' obj.target_bus = 'virtio' xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_network_auth(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "network" obj.source_protocol = "rbd" obj.source_name = "pool/image" obj.driver_name = "qemu" obj.driver_format = "raw" obj.target_dev = "/dev/vda" obj.target_bus = "virtio" obj.auth_username = "foo" obj.auth_secret_type = "ceph" obj.auth_secret_uuid = "b38a3f43-4be2-4046-897f-b67c2f5e0147" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_iotune(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.disk_read_bytes_sec = 1024000 obj.disk_read_iops_sec = 1000 obj.disk_total_bytes_sec = 2048000 obj.disk_write_bytes_sec = 1024000 obj.disk_write_iops_sec = 1000 obj.disk_total_iops_sec = 2000 xml = obj.to_xml() self.assertXmlEqual(xml, """ 1024000 1000 1024000 1000 2048000 2000 """) def test_config_blockio(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" obj.logical_block_size = "4096" obj.physical_block_size = "4096" xml = obj.to_xml() self.assertXmlEqual(""" """, xml) def test_config_disk_device_address(self): xml = """
""" obj = config.LibvirtConfigGuestDisk() obj.parse_str(xml) self.assertIsInstance(obj.device_addr, config.LibvirtConfigGuestDeviceAddressPCI) self.assertEqual('0000:00:09.0', obj.device_addr.format_address()) def test_config_disk_device_address_no_format(self): xml = """
""" obj = config.LibvirtConfigGuestDisk() obj.parse_str(xml) self.assertIsInstance(obj.device_addr, config.LibvirtConfigGuestDeviceAddressDrive) self.assertEqual(('0', '0', '0', '1'), (obj.device_addr.controller, obj.device_addr.bus, obj.device_addr.target, obj.device_addr.unit)) self.assertIsNone(obj.device_addr.format_address()) def test_config_disk_device_address_type_virtio_mmio(self): xml = """
""" obj = config.LibvirtConfigGuestDisk() obj.parse_str(xml) self.assertNotIsInstance(obj.device_addr, config.LibvirtConfigGuestDeviceAddressPCI) self.assertNotIsInstance(obj.device_addr, config.LibvirtConfigGuestDeviceAddressDrive) def test_config_disk_device_address_type_ccw(self): xml = """
""" obj = config.LibvirtConfigGuestDisk() obj.parse_str(xml) self.assertNotIsInstance(obj.device_addr, config.LibvirtConfigGuestDeviceAddressPCI) self.assertNotIsInstance(obj.device_addr, config.LibvirtConfigGuestDeviceAddressDrive) class LibvirtConfigGuestSnapshotDiskTest(LibvirtConfigBaseTest): def test_config_file(self): obj = config.LibvirtConfigGuestDisk() obj.source_type = "file" obj.source_path = "/tmp/hello" obj.target_dev = "/dev/hda" obj.target_bus = "ide" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_file_parse(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDisk() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'file') self.assertEqual(obj.source_path, '/tmp/hello') self.assertEqual(obj.target_dev, '/dev/hda') self.assertEqual(obj.target_bus, 'ide') class LibvirtConfigGuestDiskBackingStoreTest(LibvirtConfigBaseTest): def test_config_file_parse(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDiskBackingStore() obj.parse_dom(xmldoc) self.assertEqual(obj.driver_name, 'qemu') self.assertEqual(obj.driver_format, 'qcow2') self.assertEqual(obj.source_type, 'file') self.assertEqual(obj.source_file, '/var/lib/libvirt/images/mid.qcow2') self.assertEqual(obj.backing_store.driver_name, 'qemu') self.assertEqual(obj.backing_store.source_type, 'file') self.assertEqual(obj.backing_store.source_file, '/var/lib/libvirt/images/base.qcow2') self.assertIsNone(obj.backing_store.backing_store) def test_config_network_parse(self): xml = """ """ xmldoc = etree.fromstring(xml) obj = config.LibvirtConfigGuestDiskBackingStore() obj.parse_dom(xmldoc) self.assertEqual(obj.source_type, 'network') self.assertEqual(obj.source_protocol, 'gluster') self.assertEqual(obj.source_name, 'volume1/img1') self.assertEqual(obj.source_hosts[0], 'host1') self.assertEqual(obj.source_ports[0], '24007') self.assertEqual(obj.index, '1') self.assertEqual(obj.backing_store.source_name, 'volume1/img2') self.assertEqual(obj.backing_store.index, '2') self.assertEqual(obj.backing_store.source_hosts[0], 'host1') self.assertEqual(obj.backing_store.source_ports[0], '24007') self.assertIsNone(obj.backing_store.backing_store) class LibvirtConfigGuestFilesysTest(LibvirtConfigBaseTest): def test_config_mount(self): obj = config.LibvirtConfigGuestFilesys() obj.source_type = "mount" obj.source_dir = "/tmp/hello" obj.target_dir = "/mnt" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_block(self): obj = config.LibvirtConfigGuestFilesys() obj.source_type = "block" obj.source_dev = "/dev/sdb" obj.target_dir = "/mnt" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_file(self): obj = config.LibvirtConfigGuestFilesys() obj.source_type = "file" obj.source_file = "/data/myimage.qcow2" obj.driver_type = "nbd" obj.driver_format = "qcow2" obj.target_dir = "/mnt" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestInputTest(LibvirtConfigBaseTest): def test_config_tablet(self): obj = config.LibvirtConfigGuestInput() xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestGraphicsTest(LibvirtConfigBaseTest): def test_config_graphics(self): obj = config.LibvirtConfigGuestGraphics() obj.type = "vnc" obj.autoport = True obj.keymap = "en_US" obj.listen = "127.0.0.1" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestHostdev(LibvirtConfigBaseTest): def test_config_pci_guest_host_dev(self): obj = config.LibvirtConfigGuestHostdev(mode='subsystem', type='pci') xml = obj.to_xml() expected = """ """ self.assertXmlEqual(xml, expected) def test_parse_GuestHostdev(self): xmldoc = """""" obj = config.LibvirtConfigGuestHostdev() obj.parse_str(xmldoc) self.assertEqual(obj.mode, 'subsystem') self.assertEqual(obj.type, 'pci') self.assertEqual(obj.managed, 'yes') def test_parse_GuestHostdev_non_pci(self): xmldoc = """""" obj = config.LibvirtConfigGuestHostdev() obj.parse_str(xmldoc) self.assertEqual(obj.mode, 'subsystem') self.assertEqual(obj.type, 'usb') self.assertEqual(obj.managed, 'no') class LibvirtConfigGuestHostdevPCI(LibvirtConfigBaseTest): expected = """
""" def test_config_guest_hosdev_pci(self): hostdev = config.LibvirtConfigGuestHostdevPCI() hostdev.domain = "1234" hostdev.bus = "11" hostdev.slot = "22" hostdev.function = "3" xml = hostdev.to_xml() self.assertXmlEqual(self.expected, xml) def test_parse_guest_hosdev_pci(self): xmldoc = self.expected obj = config.LibvirtConfigGuestHostdevPCI() obj.parse_str(xmldoc) self.assertEqual(obj.mode, 'subsystem') self.assertEqual(obj.type, 'pci') self.assertEqual(obj.managed, 'yes') self.assertEqual(obj.domain, '0x1234') self.assertEqual(obj.bus, '0x11') self.assertEqual(obj.slot, '0x22') self.assertEqual(obj.function, '0x3') def test_parse_guest_hosdev_usb(self): xmldoc = """ """ obj = config.LibvirtConfigGuestHostdevPCI() obj.parse_str(xmldoc) self.assertEqual(obj.mode, 'subsystem') self.assertEqual(obj.type, 'usb') class LibvirtConfigGuestCharDeviceLog(LibvirtConfigBaseTest): def test_config_log(self): obj = config.LibvirtConfigGuestCharDeviceLog() obj.file = "/tmp/guestname-logd.log" obj.append = "on" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) # create a new object from the XML and check it again obj2 = config.LibvirtConfigGuestCharDeviceLog() obj2.parse_str(xml) self.assertXmlEqual(xml, obj2.to_xml()) class LibvirtConfigGuestSerialTest(LibvirtConfigBaseTest): def test_config_file(self): obj = config.LibvirtConfigGuestSerial() obj.type = "file" obj.source_path = "/tmp/vm.log" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_serial_port(self): obj = config.LibvirtConfigGuestSerial() obj.type = "tcp" obj.listen_port = 11111 obj.listen_host = "0.0.0.0" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_log(self): log = config.LibvirtConfigGuestCharDeviceLog() log.file = "/tmp/guestname-logd.log" log.append = "off" device = config.LibvirtConfigGuestSerial() device.type = "tcp" device.listen_port = 11111 device.listen_host = "0.0.0.0" device.log = log xml = device.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestConsoleTest(LibvirtConfigBaseTest): def test_config_pty(self): obj = config.LibvirtConfigGuestConsole() obj.type = "pty" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_target_type(self): obj = config.LibvirtConfigGuestConsole() obj.type = "pty" obj.target_type = "sclp" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_type_file_with_target_type(self): obj = config.LibvirtConfigGuestConsole() obj.type = "file" obj.target_type = "sclplm" obj.source_path = "/var/lib/nova/instances/uuid/console.log" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_target_port(self): obj = config.LibvirtConfigGuestConsole() obj.target_port = 0 xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_log(self): log = config.LibvirtConfigGuestCharDeviceLog() log.file = "/tmp/guestname-logd.log" log.append = "off" device = config.LibvirtConfigGuestConsole() device.type = "tcp" device.listen_port = 11111 device.listen_host = "0.0.0.0" device.log = log xml = device.to_xml() self.assertXmlEqual(xml, """ """) class LibvirtConfigGuestChannelTest(LibvirtConfigBaseTest): def test_config_spice_minimal(self): obj = config.LibvirtConfigGuestChannel() obj.type = "spicevmc" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_spice_full(self): obj = config.LibvirtConfigGuestChannel() obj.type = "spicevmc" obj.target_name = "com.redhat.spice.0" xml = obj.to_xml() self.assertXmlEqual(xml, """ """) def test_config_qga_full(self): obj = config.LibvirtConfigGuestChannel() obj.type = "unix" obj.target_name = "org.qemu.guest_agent.0" obj.source_path = "/var/lib/libvirt/qemu/%s.%s.sock" % ( obj.target_name, "instance-name") xml = obj.to_xml() self.assertXmlEqual(xml, """ """ % obj.source_path) class LibvirtConfigGuestInterfaceTest(LibvirtConfigBaseTest): def test_config_ethernet(self): obj = config.LibvirtConfigGuestInterface() obj.net_type = "ethernet" obj.mac_addr = "DE:AD:BE:EF:CA:FE" obj.model = "virtio" obj.target_dev = "vnet0" obj.driver_name = "vhost" obj.vif_inbound_average = 16384 obj.vif_inbound_peak = 32768 obj.vif_inbound_burst = 3276 obj.vif_outbound_average = 32768 obj.vif_outbound_peak = 65536 obj.vif_outbound_burst = 6553 xml = obj.to_xml() self.assertXmlEqual(xml, """ """) # parse the xml from the first object into a new object and make sure # they are the same obj2 = config.LibvirtConfigGuestInterface() obj2.parse_str(xml) self.assertXmlEqual(xml, obj2.to_xml()) def test_config_driver_options(self): obj = config.LibvirtConfigGuestInterface() obj.net_type = "ethernet" obj.mac_addr = "DE:AD:BE:EF:CA:FE" obj.model = "virtio" obj.target_dev = "vnet0" obj.driver_name = "vhost" obj.vhost_queues = 4 xml = obj.to_xml() self.assertXmlEqual(xml, """ """) # parse the xml from the first object into a new object and make sure # they are the same obj2 = config.LibvirtConfigGuestInterface() obj2.parse_str(xml) self.assertXmlEqual(xml, obj2.to_xml()) def test_config_bridge(self): obj = config.LibvirtConfigGuestInterface() obj.net_type = "bridge" obj.source_dev = "br0" obj.mac_addr = "DE:AD:BE:EF:CA:FE" obj.model = "virtio" obj.target_dev = "tap12345678" obj.filtername = "clean-traffic" obj.filterparams.append({"key": "IP", "value": "192.168.122.1"}) obj.vif_inbound_average = 16384 obj.vif_inbound_peak = 32768 obj.vif_inbound_burst = 3276 obj.vif_outbound_average = 32768 obj.vif_outbound_peak = 65536 obj.vif_outbound_burst = 6553 xml = obj.to_xml() self.assertXmlEqual(xml, """ """) # parse the xml from the first object into a new object and make sure # they are the same obj2 = config.LibvirtConfigGuestInterface() obj2.parse_str(xml) self.assertXmlEqual(xml, obj2.to_xml()) def test_config_bridge_ovs(self): obj = config.LibvirtConfigGuestInterface() obj.net_type = "bridge" obj.source_dev = "br0" obj.mac_addr = "DE:AD:BE:EF:CA:FE" obj.model = "virtio" obj.target_dev = "tap12345678" obj.vporttype = "openvswitch" obj.vportparams.append({"key": "instanceid", "value": "foobar"}) xml = obj.to_xml() self.assertXmlEqual(xml, """ """) # parse the xml from the first object into a new object and make sure # they are the same obj2 = config.LibvirtConfigGuestInterface() obj2.parse_str(xml) self.assertXmlEqual(xml, obj2.to_xml()) def test_config_bridge_xen(self): obj = config.LibvirtConfigGuestInterface() obj.net_type = "bridge" obj.source_dev = "br0" obj.mac_addr = "CA:FE:BE:EF:CA:FE" obj.script = "/path/to/test-vif-openstack" xml = obj.to_xml() self.assertXmlEqual(xml, """