From 452eac63d441c2e5a693d3a661669499e1bc9b94 Mon Sep 17 00:00:00 2001 From: Galit Date: Mon, 13 May 2019 14:40:52 +0300 Subject: [PATCH] Fix issue: no cpu_map.xml file on libvirt fc29 Read the cpu_map/index.xml and create an xml string Signed-off-by: Galit --- lago/providers/libvirt/cpu.py | 50 +++++++++++++------ .../libvirt/fixtures/cpu_map/index.xml | 5 ++ .../libvirt/fixtures/cpu_map/x86_Penryn.xml | 7 +++ tests/unit/lago/providers/libvirt/test_cpu.py | 22 +++++++- 4 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 tests/unit/lago/providers/libvirt/fixtures/cpu_map/index.xml create mode 100644 tests/unit/lago/providers/libvirt/fixtures/cpu_map/x86_Penryn.xml diff --git a/lago/providers/libvirt/cpu.py b/lago/providers/libvirt/cpu.py index 5d660566..cd85cbcc 100644 --- a/lago/providers/libvirt/cpu.py +++ b/lago/providers/libvirt/cpu.py @@ -19,8 +19,7 @@ # import logging -import libvirt - +import os from lxml import etree as ET import lago.providers.libvirt.utils as utils @@ -393,18 +392,6 @@ def get_cpu_props(cls, family, arch='x86'): @classmethod def get_cpus_by_arch(cls, arch): - conn = libvirt.open('qemu:///system') - if conn is None: - raise LagoException('Failed to open connection to qemu:///system') - models = conn.getCPUModelNames('x86_64') - conn.close() - if models: - return models[0] - else: - raise LagoException('No such arch: {0}'.format(arch)) - - @classmethod - def get_cpus_by_arch1(cls, arch): """ Get all CPUs info by arch @@ -418,9 +405,40 @@ def get_cpus_by_arch1(cls, arch): :exc:`~LagoException`: If no such ARCH is found """ - with open('/usr/share/libvirt/cpu_map.xml', 'r') as cpu_map: - cpu_xml = ET.parse(cpu_map) + cpu_map_xml = "/usr/share/libvirt/cpu_map.xml" + cpu_map_dir = "/usr/share/libvirt/cpu_map/" + cpu_map_index_xml = cpu_map_dir + "index.xml" + if not os.path.exists(cpu_map_xml): + cpu_xml = ET.ElementTree( + ET.fromstring(create_xml_map(cpu_map_index_xml, cpu_map_dir)) + ) + else: + with open(cpu_map_xml, 'r') as cpu_map: + cpu_xml = ET.parse(cpu_map) try: return cpu_xml.xpath('/cpus/arch[@name="{0}"]'.format(arch))[0] except IndexError: raise LagoException('No such arch: {0}'.format(arch)) + + +def create_xml_map(cpu_map_index_xml, cpu_map_dir): + xml_list = [] + if os.path.exists(cpu_map_index_xml): + with open(cpu_map_index_xml) as fp: + line = fp.readline() + while line: + if "include" in line: + tree = ET.fromstring(line) + for child in tree.getiterator(): + if child.tag == "include": + filename = child.attrib["filename"] + with open( + cpu_map_dir + filename, 'r' + ) as content_file: + for content_line in content_file: + if "cpus" not in content_line: + xml_list.append(content_line) + else: + xml_list.append(line) + line = fp.readline() + return ''.join(xml_list) diff --git a/tests/unit/lago/providers/libvirt/fixtures/cpu_map/index.xml b/tests/unit/lago/providers/libvirt/fixtures/cpu_map/index.xml new file mode 100644 index 00000000..a83b0acc --- /dev/null +++ b/tests/unit/lago/providers/libvirt/fixtures/cpu_map/index.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/unit/lago/providers/libvirt/fixtures/cpu_map/x86_Penryn.xml b/tests/unit/lago/providers/libvirt/fixtures/cpu_map/x86_Penryn.xml new file mode 100644 index 00000000..a9add03f --- /dev/null +++ b/tests/unit/lago/providers/libvirt/fixtures/cpu_map/x86_Penryn.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/unit/lago/providers/libvirt/test_cpu.py b/tests/unit/lago/providers/libvirt/test_cpu.py index 13cb2eda..9cc0dd69 100644 --- a/tests/unit/lago/providers/libvirt/test_cpu.py +++ b/tests/unit/lago/providers/libvirt/test_cpu.py @@ -23,7 +23,7 @@ import lxml.etree as ET import pytest from xmlunittest import XmlTestCase - +import os from lago.providers.libvirt import cpu from lago.utils import LagoInitException @@ -247,3 +247,23 @@ def test_init_custom_and_model_not_allowed(self): } with pytest.raises(LagoInitException): cpu.CPU(spec=spec, host_cpu=self.get_host_cpu()) + + def test_create_xml_string(self): + _xml = """ + + + + + + + + + + """ + test_dir = os.path.dirname(__file__) + cpu_map_dir = test_dir + "/fixtures/cpu_map/" + cpu_map_index_xml = cpu_map_dir + "index.xml" + cpu_xml = ET.fromstring( + cpu.create_xml_map(cpu_map_index_xml, cpu_map_dir) + ) + self.assertXmlEquivalentOutputs(ET.tostring(cpu_xml), _xml)