Skip to content

Commit

Permalink
Fix issue: no cpu_map.xml file on libvirt fc29
Browse files Browse the repository at this point in the history
Read the cpu_map/index.xml and create an
xml string

Signed-off-by: Galit <[email protected]>
  • Loading branch information
Galit committed May 14, 2019
1 parent c56fd8d commit 452eac6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
50 changes: 34 additions & 16 deletions lago/providers/libvirt/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
#

import logging
import libvirt

import os
from lxml import etree as ET

import lago.providers.libvirt.utils as utils
Expand Down Expand Up @@ -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
Expand All @@ -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)
5 changes: 5 additions & 0 deletions tests/unit/lago/providers/libvirt/fixtures/cpu_map/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<cpus>
<arch name="x86">
<include filename="x86_Penryn.xml"/>
</arch>
</cpus>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<cpus>
<model name='Penryn'>
<signature family='6' model='23'/>
<vendor name='Intel'/>
<feature name='apic'/>
</model>
</cpus>
22 changes: 21 additions & 1 deletion tests/unit/lago/providers/libvirt/test_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = """
<cpus>
<arch name="x86">
<model name='Penryn'>
<signature family='6' model='23'/>
<vendor name='Intel'/>
<feature name='apic'/>
</model>
</arch>
</cpus>
"""
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)

0 comments on commit 452eac6

Please sign in to comment.