Exemplo n.º 1
0
 def test_define_and_retrieve(self):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     self.assertEquals(conn.listDomainsID(), [])
     conn.defineXML(get_vm_xml())
     dom = conn.lookupByName('testname')
     xml = dom.XMLDesc(0)
     xml_to_tree(xml)
Exemplo n.º 2
0
 def test_define_and_retrieve(self):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     self.assertEquals(conn.listDomainsID(), [])
     conn.defineXML(get_vm_xml())
     dom = conn.lookupByName('testname')
     xml = dom.XMLDesc(0)
     xml_to_tree(xml)
Exemplo n.º 3
0
 def _test_accepts_source_type(self, source_type):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     self.assertEquals(conn.listDomainsID(), [])
     conn.defineXML(get_vm_xml(source_type=source_type))
     dom = conn.lookupByName('testname')
     xml = dom.XMLDesc(0)
     tree = xml_to_tree(xml)
     elem = tree.find('./devices/disk/source')
     self.assertEquals(elem.get('file'), '/somefile')
Exemplo n.º 4
0
 def _test_accepts_source_type(self, source_type):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     self.assertEquals(conn.listDomainsID(), [])
     conn.defineXML(get_vm_xml(source_type=source_type))
     dom = conn.lookupByName('testname')
     xml = dom.XMLDesc(0)
     tree = xml_to_tree(xml)
     elem = tree.find('./devices/disk/source')
     self.assertEquals(elem.get('file'), '/somefile')
Exemplo n.º 5
0
 def _test_network_type_sticks(self, network_type):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     self.assertEquals(conn.listDomainsID(), [])
     conn.defineXML(get_vm_xml(interface_type=network_type))
     dom = conn.lookupByName('testname')
     xml = dom.XMLDesc(0)
     tree = xml_to_tree(xml)
     elem = tree.find('./devices/interface')
     self.assertEquals(elem.get('type'), network_type)
     elem = elem.find('./source')
     self.assertEquals(elem.get(network_type), 'br100')
Exemplo n.º 6
0
 def _test_network_type_sticks(self, network_type):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     self.assertEquals(conn.listDomainsID(), [])
     conn.defineXML(get_vm_xml(interface_type=network_type))
     dom = conn.lookupByName('testname')
     xml = dom.XMLDesc(0)
     tree = xml_to_tree(xml)
     elem = tree.find('./devices/interface')
     self.assertEquals(elem.get('type'), network_type)
     elem = elem.find('./source')
     self.assertEquals(elem.get(network_type), 'br100')
Exemplo n.º 7
0
 def test_multi_nic(self):
     instance_data = dict(self.test_instance)
     network_info = _create_network_info(2)
     conn = libvirt_conn.LibvirtConnection(True)
     instance_ref = db.instance_create(self.context, instance_data)
     xml = conn.to_xml(instance_ref, False, network_info)
     tree = xml_to_tree(xml)
     interfaces = tree.findall("./devices/interface")
     self.assertEquals(len(interfaces), 2)
     parameters = interfaces[0].findall('./filterref/parameter')
     self.assertEquals(interfaces[0].get('type'), 'bridge')
     self.assertEquals(parameters[0].get('name'), 'IP')
     self.assertEquals(parameters[0].get('value'), '0.0.0.0/0')
     self.assertEquals(parameters[1].get('name'), 'DHCPSERVER')
     self.assertEquals(parameters[1].get('value'), 'fake')
Exemplo n.º 8
0
 def test_multi_nic(self):
     instance_data = dict(self.test_instance)
     network_info = _create_network_info(2)
     conn = libvirt_conn.LibvirtConnection(True)
     instance_ref = db.instance_create(self.context, instance_data)
     xml = conn.to_xml(instance_ref, False, network_info)
     tree = xml_to_tree(xml)
     interfaces = tree.findall("./devices/interface")
     self.assertEquals(len(interfaces), 2)
     parameters = interfaces[0].findall('./filterref/parameter')
     self.assertEquals(interfaces[0].get('type'), 'bridge')
     self.assertEquals(parameters[0].get('name'), 'IP')
     self.assertEquals(parameters[0].get('value'), '0.0.0.0/0')
     self.assertEquals(parameters[1].get('name'), 'DHCPSERVER')
     self.assertEquals(parameters[1].get('value'), 'fake')
Exemplo n.º 9
0
    def compareCPU(self, xml, flags):
        tree = xml_to_tree(xml)

        arch_node = tree.find('./arch')
        if arch_node is not None:
            if arch_node.text not in ['x86_64', 'i686']:
                return VIR_CPU_COMPARE_INCOMPATIBLE

        model_node = tree.find('./model')
        if model_node is not None:
            if model_node.text != node_cpu_model:
                return VIR_CPU_COMPARE_INCOMPATIBLE

        vendor_node = tree.find('./vendor')
        if vendor_node is not None:
            if vendor_node.text != node_cpu_vendor:
                return VIR_CPU_COMPARE_INCOMPATIBLE

        # The rest of the stuff libvirt implements is rather complicated
        # and I don't think it adds much value to replicate it here.

        return VIR_CPU_COMPARE_IDENTICAL
Exemplo n.º 10
0
    def compareCPU(self, xml, flags):
        tree = xml_to_tree(xml)

        arch_node = tree.find('./arch')
        if arch_node is not None:
            if arch_node.text not in ['x86_64', 'i686']:
                return VIR_CPU_COMPARE_INCOMPATIBLE

        model_node = tree.find('./model')
        if model_node is not None:
            if model_node.text != node_cpu_model:
                return VIR_CPU_COMPARE_INCOMPATIBLE

        vendor_node = tree.find('./vendor')
        if vendor_node is not None:
            if vendor_node.text != node_cpu_vendor:
                return VIR_CPU_COMPARE_INCOMPATIBLE

        # The rest of the stuff libvirt implements is rather complicated
        # and I don't think it adds much value to replicate it here.

        return VIR_CPU_COMPARE_IDENTICAL
Exemplo n.º 11
0
    def _check_xml_and_container(self, instance):
        user_context = context.RequestContext(project=self.project,
                                              user=self.user)
        instance_ref = db.instance_create(user_context, instance)
        host = self.network.get_network_host(user_context.elevated())
        network_ref = db.project_get_network(context.get_admin_context(),
                                             self.project.id)

        fixed_ip = {'address': self.test_ip,
                    'network_id': network_ref['id']}

        ctxt = context.get_admin_context()
        fixed_ip_ref = db.fixed_ip_create(ctxt, fixed_ip)
        db.fixed_ip_update(ctxt, self.test_ip,
                                 {'allocated': True,
                                  'instance_id': instance_ref['id']})

        self.flags(libvirt_type='lxc')
        conn = libvirt_conn.LibvirtConnection(True)

        uri = conn.get_uri()
        self.assertEquals(uri, 'lxc:///')

        xml = conn.to_xml(instance_ref)
        tree = xml_to_tree(xml)

        check = [
        (lambda t: t.find('.').get('type'), 'lxc'),
        (lambda t: t.find('./os/type').text, 'exe'),
        (lambda t: t.find('./devices/filesystem/target').get('dir'), '/')]

        for i, (check, expected_result) in enumerate(check):
            self.assertEqual(check(tree),
                             expected_result,
                             '%s failed common check %d' % (xml, i))

        target = tree.find('./devices/filesystem/source').get('dir')
        self.assertTrue(len(target) > 0)
Exemplo n.º 12
0
    def _check_xml_and_container(self, instance):
        user_context = context.RequestContext(project=self.project,
                                              user=self.user)
        instance_ref = db.instance_create(user_context, instance)
        host = self.network.get_network_host(user_context.elevated())
        network_ref = db.project_get_network(context.get_admin_context(),
                                             self.project.id)

        fixed_ip = {'address': self.test_ip, 'network_id': network_ref['id']}

        ctxt = context.get_admin_context()
        fixed_ip_ref = db.fixed_ip_create(ctxt, fixed_ip)
        db.fixed_ip_update(ctxt, self.test_ip, {
            'allocated': True,
            'instance_id': instance_ref['id']
        })

        self.flags(libvirt_type='lxc')
        conn = libvirt_conn.LibvirtConnection(True)

        uri = conn.get_uri()
        self.assertEquals(uri, 'lxc:///')

        xml = conn.to_xml(instance_ref)
        tree = xml_to_tree(xml)

        check = [(lambda t: t.find('.').get('type'), 'lxc'),
                 (lambda t: t.find('./os/type').text, 'exe'),
                 (lambda t: t.find('./devices/filesystem/target').get('dir'),
                  '/')]

        for i, (check, expected_result) in enumerate(check):
            self.assertEqual(check(tree), expected_result,
                             '%s failed common check %d' % (xml, i))

        target = tree.find('./devices/filesystem/source').get('dir')
        self.assertTrue(len(target) > 0)
Exemplo n.º 13
0
 def snapshotCreateXML(self, xml, flags):
     tree = xml_to_tree(xml)
     name = tree.find('./name').text
     snapshot = DomainSnapshot(name, self)
     self._snapshots[name] = snapshot
     return snapshot
Exemplo n.º 14
0
 def detachDevice(self, xml):
     disk_info = _parse_disk_info(xml_to_tree(xml))
     disk_info['_attached'] = True
     return disk_info in self._def['devices']['disks']
Exemplo n.º 15
0
 def attachDevice(self, xml):
     disk_info = _parse_disk_info(xml_to_tree(xml))
     disk_info['_attached'] = True
     self._def['devices']['disks'] += [disk_info]
     return True
Exemplo n.º 16
0
    def _parse_definition(self, xml):
        try:
            tree = xml_to_tree(xml)
        except ParseError:
            raise libvirtError(VIR_ERR_XML_DETAIL, VIR_FROM_DOMAIN,
                               "Invalid XML.")

        definition = {}

        name = tree.find('./name')
        if name is not None:
            definition['name'] = name.text

        uuid_elem = tree.find('./uuid')
        if uuid_elem is not None:
            definition['uuid'] = uuid_elem.text
        else:
            definition['uuid'] = str(uuid.uuid4())

        vcpu = tree.find('./vcpu')
        if vcpu is not None:
            definition['vcpu'] = int(vcpu.text)

        memory = tree.find('./memory')
        if memory is not None:
            definition['memory'] = int(memory.text)

        os = {}
        os_type = tree.find('./os/type')
        if os_type is not None:
            os['type'] = os_type.text
            os['arch'] = os_type.get('arch', node_arch)

        os_kernel = tree.find('./os/kernel')
        if os_kernel is not None:
            os['kernel'] = os_kernel.text

        os_initrd = tree.find('./os/initrd')
        if os_initrd is not None:
            os['initrd'] = os_initrd.text

        os_cmdline = tree.find('./os/cmdline')
        if os_cmdline is not None:
            os['cmdline'] = os_cmdline.text

        os_boot = tree.find('./os/boot')
        if os_boot is not None:
            os['boot_dev'] = os_boot.get('dev')

        definition['os'] = os

        features = {}

        acpi = tree.find('./features/acpi')
        if acpi is not None:
            features['acpi'] = True

        definition['features'] = features

        devices = {}

        device_nodes = tree.find('./devices')
        if device_nodes is not None:
            disks_info = []
            disks = device_nodes.findall('./disk')
            for disk in disks:
                disks_info += [_parse_disk_info(disk)]
            devices['disks'] = disks_info

            nics_info = []
            nics = device_nodes.findall('./interface')
            for nic in nics:
                nic_info = {}
                nic_info['type'] = nic.get('type')

                mac = nic.find('./mac')
                if mac is not None:
                    nic_info['mac'] = mac.get('address')

                source = nic.find('./source')
                if source is not None:
                    if nic_info['type'] == 'network':
                        nic_info['source'] = source.get('network')
                    elif nic_info['type'] == 'bridge':
                        nic_info['source'] = source.get('bridge')

                nics_info += [nic_info]

            devices['nics'] = nics_info

        definition['devices'] = devices

        return definition
Exemplo n.º 17
0
 def _parse_xml(self, xml):
     tree = xml_to_tree(xml)
     root = tree.find('.')
     self._name = root.get('name')
Exemplo n.º 18
0
 def _extract_message(self, result):
     tree = xml_to_tree(result.body)
     return tree.findall('./Errors')[0].find('Error/Message').text
Exemplo n.º 19
0
 def detachDevice(self, xml):
     disk_info = _parse_disk_info(xml_to_tree(xml))
     disk_info['_attached'] = True
     return disk_info in self._def['devices']['disks']
Exemplo n.º 20
0
 def test_getCapabilities(self):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     xml_to_tree(conn.getCapabilities())
Exemplo n.º 21
0
 def _parse_xml(self, xml):
     tree = xml_to_tree(xml)
     root = tree.find('.')
     self._name = root.get('name')
Exemplo n.º 22
0
 def _extract_message(self, result):
     tree = xml_to_tree(result.body)
     return tree.findall('./Errors')[0].find('Error/Message').text
Exemplo n.º 23
0
 def test_getCapabilities(self):
     conn = self.get_openAuth_curry_func()('qemu:///system')
     xml_to_tree(conn.getCapabilities())
Exemplo n.º 24
0
    def _check_xml_and_uri(self,
                           instance,
                           expect_ramdisk,
                           expect_kernel,
                           rescue=False):
        user_context = context.RequestContext(project=self.project,
                                              user=self.user)
        instance_ref = db.instance_create(user_context, instance)
        host = self.network.get_network_host(user_context.elevated())
        network_ref = db.project_get_network(context.get_admin_context(),
                                             self.project.id)

        fixed_ip = {'address': self.test_ip, 'network_id': network_ref['id']}

        ctxt = context.get_admin_context()
        fixed_ip_ref = db.fixed_ip_create(ctxt, fixed_ip)
        db.fixed_ip_update(ctxt, self.test_ip, {
            'allocated': True,
            'instance_id': instance_ref['id']
        })

        type_uri_map = {
            'qemu': ('qemu:///system',
                     [(lambda t: t.find('.').get('type'), 'qemu'),
                      (lambda t: t.find('./os/type').text, 'hvm'),
                      (lambda t: t.find('./devices/emulator'), None)]),
            'kvm': ('qemu:///system',
                    [(lambda t: t.find('.').get('type'), 'kvm'),
                     (lambda t: t.find('./os/type').text, 'hvm'),
                     (lambda t: t.find('./devices/emulator'), None)]),
            'uml':
            ('uml:///system', [(lambda t: t.find('.').get('type'), 'uml'),
                               (lambda t: t.find('./os/type').text, 'uml')]),
            'xen':
            ('xen:///', [(lambda t: t.find('.').get('type'), 'xen'),
                         (lambda t: t.find('./os/type').text, 'linux')]),
        }

        for hypervisor_type in ['qemu', 'kvm', 'xen']:
            check_list = type_uri_map[hypervisor_type][1]

            if rescue:
                check = (lambda t: t.find('./os/kernel').text.split('/')[1],
                         'kernel.rescue')
                check_list.append(check)
                check = (lambda t: t.find('./os/initrd').text.split('/')[1],
                         'ramdisk.rescue')
                check_list.append(check)
            else:
                if expect_kernel:
                    check = (
                        lambda t: t.find('./os/kernel').text.split('/')[1],
                        'kernel')
                else:
                    check = (lambda t: t.find('./os/kernel'), None)
                check_list.append(check)

                if expect_ramdisk:
                    check = (
                        lambda t: t.find('./os/initrd').text.split('/')[1],
                        'ramdisk')
                else:
                    check = (lambda t: t.find('./os/initrd'), None)
                check_list.append(check)

        common_checks = [
            (lambda t: t.find('.').tag, 'domain'),
            (lambda t: t.find('./devices/interface/filterref/parameter').get(
                'name'), 'IP'),
            (lambda t: t.find('./devices/interface/filterref/parameter').get(
                'value'), '10.11.12.13'),
            (lambda t: t.findall('./devices/interface/filterref/parameter')[1].
             get('name'), 'DHCPSERVER'),
            (lambda t: t.findall('./devices/interface/filterref/parameter')[1].
             get('value'), '10.0.0.1'),
            (lambda t: t.find('./devices/serial/source').get('path').split(
                '/')[1], 'console.log'),
            (lambda t: t.find('./memory').text, '2097152')
        ]
        if rescue:
            common_checks += [(lambda t: t.findall('./devices/disk/source')[0].
                               get('file').split('/')[1], 'disk.rescue'),
                              (lambda t: t.findall('./devices/disk/source')[1].
                               get('file').split('/')[1], 'disk')]
        else:
            common_checks += [(lambda t: t.findall('./devices/disk/source')[0].
                               get('file').split('/')[1], 'disk')]
            common_checks += [(lambda t: t.findall('./devices/disk/source')[1].
                               get('file').split('/')[1], 'disk.local')]

        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
            FLAGS.libvirt_type = libvirt_type
            conn = libvirt_conn.LibvirtConnection(True)

            uri = conn.get_uri()
            self.assertEquals(uri, expected_uri)

            xml = conn.to_xml(instance_ref, rescue)
            tree = xml_to_tree(xml)
            for i, (check, expected_result) in enumerate(checks):
                self.assertEqual(check(tree), expected_result,
                                 '%s failed check %d' % (xml, i))

            for i, (check, expected_result) in enumerate(common_checks):
                self.assertEqual(check(tree), expected_result,
                                 '%s failed common check %d' % (xml, i))

        # This test is supposed to make sure we don't
        # override a specifically set uri
        #
        # Deliberately not just assigning this string to FLAGS.libvirt_uri and
        # checking against that later on. This way we make sure the
        # implementation doesn't fiddle around with the FLAGS.
        testuri = 'something completely different'
        FLAGS.libvirt_uri = testuri
        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
            FLAGS.libvirt_type = libvirt_type
            conn = libvirt_conn.LibvirtConnection(True)
            uri = conn.get_uri()
            self.assertEquals(uri, testuri)
        db.instance_destroy(user_context, instance_ref['id'])
Exemplo n.º 25
0
 def snapshotCreateXML(self, xml, flags):
     tree = xml_to_tree(xml)
     name = tree.find('./name').text
     snapshot = DomainSnapshot(name, self)
     self._snapshots[name] = snapshot
     return snapshot
Exemplo n.º 26
0
    def _parse_definition(self, xml):
        try:
            tree = xml_to_tree(xml)
        except ParseError:
            raise libvirtError(VIR_ERR_XML_DETAIL, VIR_FROM_DOMAIN,
                               "Invalid XML.")

        definition = {}

        name = tree.find('./name')
        if name is not None:
            definition['name'] = name.text

        uuid_elem = tree.find('./uuid')
        if uuid_elem is not None:
            definition['uuid'] = uuid_elem.text
        else:
            definition['uuid'] = str(uuid.uuid4())

        vcpu = tree.find('./vcpu')
        if vcpu is not None:
            definition['vcpu'] = int(vcpu.text)

        memory = tree.find('./memory')
        if memory is not None:
            definition['memory'] = int(memory.text)

        os = {}
        os_type = tree.find('./os/type')
        if os_type is not None:
            os['type'] = os_type.text
            os['arch'] = os_type.get('arch', node_arch)

        os_kernel = tree.find('./os/kernel')
        if os_kernel is not None:
            os['kernel'] = os_kernel.text

        os_initrd = tree.find('./os/initrd')
        if os_initrd is not None:
            os['initrd'] = os_initrd.text

        os_cmdline = tree.find('./os/cmdline')
        if os_cmdline is not None:
            os['cmdline'] = os_cmdline.text

        os_boot = tree.find('./os/boot')
        if os_boot is not None:
            os['boot_dev'] = os_boot.get('dev')

        definition['os'] = os

        features = {}

        acpi = tree.find('./features/acpi')
        if acpi is not None:
            features['acpi'] = True

        definition['features'] = features

        devices = {}

        device_nodes = tree.find('./devices')
        if device_nodes is not None:
            disks_info = []
            disks = device_nodes.findall('./disk')
            for disk in disks:
                disks_info += [_parse_disk_info(disk)]
            devices['disks'] = disks_info

            nics_info = []
            nics = device_nodes.findall('./interface')
            for nic in nics:
                nic_info = {}
                nic_info['type'] = nic.get('type')

                mac = nic.find('./mac')
                if mac is not None:
                    nic_info['mac'] = mac.get('address')

                source = nic.find('./source')
                if source is not None:
                    if nic_info['type'] == 'network':
                        nic_info['source'] = source.get('network')
                    elif nic_info['type'] == 'bridge':
                        nic_info['source'] = source.get('bridge')

                nics_info += [nic_info]

            devices['nics'] = nics_info

        definition['devices'] = devices

        return definition
Exemplo n.º 27
0
    def _check_xml_and_uri(self, instance, expect_ramdisk, expect_kernel, rescue=False):
        user_context = context.RequestContext(project=self.project, user=self.user)
        instance_ref = db.instance_create(user_context, instance)
        host = self.network.get_network_host(user_context.elevated())
        network_ref = db.project_get_network(context.get_admin_context(), self.project.id)

        fixed_ip = {"address": self.test_ip, "network_id": network_ref["id"]}

        ctxt = context.get_admin_context()
        fixed_ip_ref = db.fixed_ip_create(ctxt, fixed_ip)
        db.fixed_ip_update(ctxt, self.test_ip, {"allocated": True, "instance_id": instance_ref["id"]})

        type_uri_map = {
            "qemu": (
                "qemu:///system",
                [
                    (lambda t: t.find(".").get("type"), "qemu"),
                    (lambda t: t.find("./os/type").text, "hvm"),
                    (lambda t: t.find("./devices/emulator"), None),
                ],
            ),
            "kvm": (
                "qemu:///system",
                [
                    (lambda t: t.find(".").get("type"), "kvm"),
                    (lambda t: t.find("./os/type").text, "hvm"),
                    (lambda t: t.find("./devices/emulator"), None),
                ],
            ),
            "uml": (
                "uml:///system",
                [(lambda t: t.find(".").get("type"), "uml"), (lambda t: t.find("./os/type").text, "uml")],
            ),
            "xen": (
                "xen:///",
                [(lambda t: t.find(".").get("type"), "xen"), (lambda t: t.find("./os/type").text, "linux")],
            ),
        }

        for hypervisor_type in ["qemu", "kvm", "xen"]:
            check_list = type_uri_map[hypervisor_type][1]

            if rescue:
                check = (lambda t: t.find("./os/kernel").text.split("/")[1], "kernel.rescue")
                check_list.append(check)
                check = (lambda t: t.find("./os/initrd").text.split("/")[1], "ramdisk.rescue")
                check_list.append(check)
            else:
                if expect_kernel:
                    check = (lambda t: t.find("./os/kernel").text.split("/")[1], "kernel")
                else:
                    check = (lambda t: t.find("./os/kernel"), None)
                check_list.append(check)

                if expect_ramdisk:
                    check = (lambda t: t.find("./os/initrd").text.split("/")[1], "ramdisk")
                else:
                    check = (lambda t: t.find("./os/initrd"), None)
                check_list.append(check)

        common_checks = [
            (lambda t: t.find(".").tag, "domain"),
            (lambda t: t.find("./devices/interface/filterref/parameter").get("name"), "IP"),
            (lambda t: t.find("./devices/interface/filterref/parameter").get("value"), "10.11.12.13"),
            (lambda t: t.findall("./devices/interface/filterref/parameter")[1].get("name"), "DHCPSERVER"),
            (lambda t: t.findall("./devices/interface/filterref/parameter")[1].get("value"), "10.0.0.1"),
            (lambda t: t.find("./devices/serial/source").get("path").split("/")[1], "console.log"),
            (lambda t: t.find("./memory").text, "2097152"),
        ]
        if rescue:
            common_checks += [
                (lambda t: t.findall("./devices/disk/source")[0].get("file").split("/")[1], "disk.rescue"),
                (lambda t: t.findall("./devices/disk/source")[1].get("file").split("/")[1], "disk"),
            ]
        else:
            common_checks += [(lambda t: t.findall("./devices/disk/source")[0].get("file").split("/")[1], "disk")]
            common_checks += [(lambda t: t.findall("./devices/disk/source")[1].get("file").split("/")[1], "disk.local")]

        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
            FLAGS.libvirt_type = libvirt_type
            conn = libvirt_conn.LibvirtConnection(True)

            uri = conn.get_uri()
            self.assertEquals(uri, expected_uri)

            xml = conn.to_xml(instance_ref, rescue)
            tree = xml_to_tree(xml)
            for i, (check, expected_result) in enumerate(checks):
                self.assertEqual(check(tree), expected_result, "%s failed check %d" % (xml, i))

            for i, (check, expected_result) in enumerate(common_checks):
                self.assertEqual(check(tree), expected_result, "%s failed common check %d" % (xml, i))

        # This test is supposed to make sure we don't override a specifically
        # set uri
        #
        # Deliberately not just assigning this string to FLAGS.libvirt_uri and
        # checking against that later on. This way we make sure the
        # implementation doesn't fiddle around with the FLAGS.
        testuri = "something completely different"
        FLAGS.libvirt_uri = testuri
        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
            FLAGS.libvirt_type = libvirt_type
            conn = libvirt_conn.LibvirtConnection(True)
            uri = conn.get_uri()
            self.assertEquals(uri, testuri)
        db.instance_destroy(user_context, instance_ref["id"])
Exemplo n.º 28
0
    def _check_xml_and_uri(self, instance, expect_ramdisk, expect_kernel,
                           rescue=False):
        user_context = context.RequestContext(project=self.project,
                                              user=self.user)
        instance_ref = db.instance_create(user_context, instance)
        host = self.network.get_network_host(user_context.elevated())
        network_ref = db.project_get_network(context.get_admin_context(),
                                             self.project.id)

        fixed_ip = {'address':    self.test_ip,
                    'network_id': network_ref['id']}

        ctxt = context.get_admin_context()
        fixed_ip_ref = db.fixed_ip_create(ctxt, fixed_ip)
        db.fixed_ip_update(ctxt, self.test_ip,
                                 {'allocated':   True,
                                  'instance_id': instance_ref['id']})

        type_uri_map = {'qemu': ('qemu:///system',
                             [(lambda t: t.find('.').get('type'), 'qemu'),
                              (lambda t: t.find('./os/type').text, 'hvm'),
                              (lambda t: t.find('./devices/emulator'), None)]),
                        'kvm': ('qemu:///system',
                             [(lambda t: t.find('.').get('type'), 'kvm'),
                              (lambda t: t.find('./os/type').text, 'hvm'),
                              (lambda t: t.find('./devices/emulator'), None)]),
                        'uml': ('uml:///system',
                             [(lambda t: t.find('.').get('type'), 'uml'),
                              (lambda t: t.find('./os/type').text, 'uml')]),
                        'xen': ('xen:///',
                             [(lambda t: t.find('.').get('type'), 'xen'),
                              (lambda t: t.find('./os/type').text, 'linux')]),
                              }

        for hypervisor_type in ['qemu', 'kvm', 'xen']:
            check_list = type_uri_map[hypervisor_type][1]

            if rescue:
                check = (lambda t: t.find('./os/kernel').text.split('/')[1],
                         'kernel.rescue')
                check_list.append(check)
                check = (lambda t: t.find('./os/initrd').text.split('/')[1],
                         'ramdisk.rescue')
                check_list.append(check)
            else:
                if expect_kernel:
                    check = (lambda t: t.find('./os/kernel').text.split(
                        '/')[1], 'kernel')
                else:
                    check = (lambda t: t.find('./os/kernel'), None)
                check_list.append(check)

                if expect_ramdisk:
                    check = (lambda t: t.find('./os/initrd').text.split(
                        '/')[1], 'ramdisk')
                else:
                    check = (lambda t: t.find('./os/initrd'), None)
                check_list.append(check)

        parameter = './devices/interface/filterref/parameter'
        common_checks = [
            (lambda t: t.find('.').tag, 'domain'),
            (lambda t: t.find(parameter).get('name'), 'IP'),
            (lambda t: t.find(parameter).get('value'), '10.11.12.13'),
            (lambda t: t.findall(parameter)[1].get('name'), 'DHCPSERVER'),
            (lambda t: t.findall(parameter)[1].get('value'), '10.0.0.1'),
            (lambda t: t.find('./devices/serial/source').get(
                'path').split('/')[1], 'console.log'),
            (lambda t: t.find('./memory').text, '2097152')]
        if rescue:
            common_checks += [
                (lambda t: t.findall('./devices/disk/source')[0].get(
                    'file').split('/')[1], 'disk.rescue'),
                (lambda t: t.findall('./devices/disk/source')[1].get(
                    'file').split('/')[1], 'disk')]
        else:
            common_checks += [(lambda t: t.findall(
                './devices/disk/source')[0].get('file').split('/')[1],
                               'disk')]
            common_checks += [(lambda t: t.findall(
                './devices/disk/source')[1].get('file').split('/')[1],
                               'disk.local')]

        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
            FLAGS.libvirt_type = libvirt_type
            conn = libvirt_conn.LibvirtConnection(True)

            uri = conn.get_uri()
            self.assertEquals(uri, expected_uri)

            xml = conn.to_xml(instance_ref, rescue)
            tree = xml_to_tree(xml)
            for i, (check, expected_result) in enumerate(checks):
                self.assertEqual(check(tree),
                                 expected_result,
                                 '%s failed check %d' % (xml, i))

            for i, (check, expected_result) in enumerate(common_checks):
                self.assertEqual(check(tree),
                                 expected_result,
                                 '%s failed common check %d' % (xml, i))

        # This test is supposed to make sure we don't
        # override a specifically set uri
        #
        # Deliberately not just assigning this string to FLAGS.libvirt_uri and
        # checking against that later on. This way we make sure the
        # implementation doesn't fiddle around with the FLAGS.
        testuri = 'something completely different'
        FLAGS.libvirt_uri = testuri
        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
            FLAGS.libvirt_type = libvirt_type
            conn = libvirt_conn.LibvirtConnection(True)
            uri = conn.get_uri()
            self.assertEquals(uri, testuri)
        db.instance_destroy(user_context, instance_ref['id'])
Exemplo n.º 29
0
 def attachDevice(self, xml):
     disk_info = _parse_disk_info(xml_to_tree(xml))
     disk_info['_attached'] = True
     self._def['devices']['disks'] += [disk_info]
     return True