[Kimchi-devel] [PATCH] [Kimchi 3/5] CPU Hot plug/unplug: test changes

dhbarboza82 at gmail.com dhbarboza82 at gmail.com
Thu Nov 24 10:57:29 UTC 2016


From: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>

This patch adds new tests in test_model.py to verify
this new feature.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 tests/test_model.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/tests/test_model.py b/tests/test_model.py
index 4bc82ad..f3da835 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -22,6 +22,7 @@ import __builtin__ as builtins
 
 import base64
 import grp
+import libvirt
 import json
 import lxml.etree as ET
 import mock
@@ -1415,6 +1416,117 @@ class ModelTests(unittest.TestCase):
             inst.vm_update(u'пeω-∨м', {"bootmenu": False})
             self.assertEquals("no", inst.vm_lookup(u'пeω-∨м')['bootmenu'])
 
+    def test_get_vm_cpu_cores(self):
+        xml = """<domain type='kvm'>\
+<cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
+</domain>"""
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+        self.assertEqual('2', inst.vm_get_vm_cpu_cores(xml))
+
+    def test_get_vm_cpu_sockets(self):
+        xml = """<domain type='kvm'>\
+<cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
+</domain>"""
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+        self.assertEqual('3', inst.vm_get_vm_cpu_sockets(xml))
+
+    def test_get_vm_cpu_threads(self):
+        xml = """<domain type='kvm'>\
+<cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
+</domain>"""
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+        self.assertEqual('8', inst.vm_get_vm_cpu_threads(xml))
+
+    @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
+    def test_get_vm_cpu_topology(self, mock_has_topology):
+        class FakeDom():
+            def XMLDesc(self, flag):
+                return """<domain type='kvm'>\
+<cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
+</domain>"""
+
+            def name(self):
+                return 'fakedom'
+
+        mock_has_topology.return_value = True
+        expected_topology = {'sockets': 3, 'cores': 2, 'threads': 8}
+
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+        self.assertEqual(expected_topology,
+                         inst.vm_get_vm_cpu_topology(FakeDom()))
+
+    @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
+    def test_get_vm_cpu_topology_blank(self, mock_has_topology):
+        class FakeDom():
+            def XMLDesc(self, flag):
+                return """<domain type='kvm'></domain>"""
+
+            def name(self):
+                return 'fakedom'
+
+        mock_has_topology.return_value = False
+        expected_topology = {}
+
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+        self.assertEqual(expected_topology,
+                         inst.vm_get_vm_cpu_topology(FakeDom()))
+
+    def test_vm_cpu_hotplug_invalidparam_fail(self):
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+
+        with self.assertRaisesRegexp(InvalidParameter, 'KCHCPUHOTP0001E'):
+            params = {"cpu_info": {"vcpus": 1, 'maxvcpus': 4}}
+            inst.vm_cpu_hotplug_precheck('', params)
+
+    @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
+    def test_vm_cpu_hotplug_abovemax_fail(self, mock_has_topology):
+        class FakeDom():
+            def XMLDesc(self, flag):
+                return """<domain type='kvm'>\
+<vcpu placement='static' current='1'>8</vcpu><\
+/domain>"""
+
+            def name(self):
+                return 'fakedom'
+
+        mock_has_topology.return_value = False
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+
+        with self.assertRaisesRegexp(InvalidParameter, 'KCHCPUINF0001E'):
+            params = {"cpu_info": {"vcpus": 16}}
+            inst.vm_cpu_hotplug_precheck(FakeDom(), params)
+
+    @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
+    @mock.patch('wok.plugins.kimchi.model.vms.VMModel.get_vm_cpu_topology')
+    def test_vm_cpu_hotplug_topology_mismatch_fail(self, mock_topology,
+                                                   mock_has_topology):
+        class FakeDom():
+            def XMLDesc(self, flag):
+                return """<domain type='kvm'>\
+<vcpu placement='static' current='8'>48</vcpu><\
+/domain>"""
+
+            def name(self):
+                return 'fakedom'
+
+        mock_has_topology.return_value = True
+        mock_topology.return_value = {'sockets': 3, 'cores': 2, 'threads': 8}
+
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+
+        with self.assertRaisesRegexp(InvalidParameter, 'KCHCPUINF0005E'):
+            params = {"cpu_info": {"vcpus": 10}}
+            inst.vm_cpu_hotplug_precheck(FakeDom(), params)
+
+    def test_vm_cpu_hotplug_error(self):
+        class FakeDom():
+            def setVcpusFlags(self, vcpu, flags):
+                raise libvirt.libvirtError('')
+
+        inst = model.Model(None, objstore_loc=self.tmp_store)
+        with self.assertRaisesRegexp(OperationFailed, 'KCHCPUHOTP0002E'):
+            inst.vm_update_cpu_live(FakeDom(), '')
+
     def test_get_interfaces(self):
         inst = model.Model('test:///default',
                            objstore_loc=self.tmp_store)
-- 
2.7.4




More information about the Kimchi-devel mailing list