diff options
| -rw-r--r-- | nova/tests/test_libvirt_config.py | 37 | ||||
| -rw-r--r-- | nova/virt/libvirt/config.py | 40 |
2 files changed, 77 insertions, 0 deletions
diff --git a/nova/tests/test_libvirt_config.py b/nova/tests/test_libvirt_config.py index 186fb5a18..8d6ede1be 100644 --- a/nova/tests/test_libvirt_config.py +++ b/nova/tests/test_libvirt_config.py @@ -361,3 +361,40 @@ class LibvirtConfigGuestTest(LibvirtConfigBaseTest): </disk> </devices> </domain>""") + + +class LibvirtConfigCPUTest(LibvirtConfigBaseTest): + + def test_config_cpu(self): + obj = config.LibvirtConfigCPU() + obj.vendor = "AMD" + obj.model = "Quad-Core AMD Opteron(tm) Processor 2350" + obj.arch = "x86_64" + obj.add_feature("svm") + obj.add_feature("extapic") + obj.add_feature("constant_tsc") + + xml = obj.to_xml() + self.assertXmlEqual(xml, """ + <cpu> + <arch>x86_64</arch> + <model>Quad-Core AMD Opteron(tm) Processor 2350</model> + <vendor>AMD</vendor> + <feature name="svm"/> + <feature name="extapic"/> + <feature name="constant_tsc"/> + </cpu>""") + + def test_config_topology(self): + obj = config.LibvirtConfigCPU() + obj.vendor = "AMD" + obj.sockets = 2 + obj.cores = 4 + obj.threads = 2 + + xml = obj.to_xml() + self.assertXmlEqual(xml, """ + <cpu> + <vendor>AMD</vendor> + <topology cores="4" threads="2" sockets="2"/> + </cpu>""") diff --git a/nova/virt/libvirt/config.py b/nova/virt/libvirt/config.py index 44790a068..e4f99a7ba 100644 --- a/nova/virt/libvirt/config.py +++ b/nova/virt/libvirt/config.py @@ -356,3 +356,43 @@ class LibvirtConfigGuest(LibvirtConfigObject): def add_device(self, dev): self.devices.append(dev) + + +class LibvirtConfigCPU(LibvirtConfigObject): + + def __init__(self, **kwargs): + super(LibvirtConfigCPU, self).__init__(root_name="cpu", + **kwargs) + + self.arch = None + self.model = None + self.vendor = None + self.sockets = None + self.cores = None + self.threads = None + self.features = [] + + def add_feature(self, name): + self.features.append(name) + + def format_dom(self): + cpu = super(LibvirtConfigCPU, self).format_dom() + if self.arch: + cpu.append(self._text_node("arch", self.arch)) + if self.model: + cpu.append(self._text_node("model", self.model)) + if self.vendor: + cpu.append(self._text_node("vendor", self.vendor)) + if (self.sockets is not None and + self.cores is not None and + self.threads is not None): + cpu.append(etree.Element("topology", + sockets=str(self.sockets), + cores=str(self.cores), + threads=str(self.threads))) + + for f in self.features: + cpu.append(etree.Element("feature", + name=f)) + + return cpu |
