[Kimchi-devel] [PATCH] [Kimchi 8/8] Mock user probe functions

Lucio Correia luciojhc at linux.vnet.ibm.com
Fri Mar 10 22:06:11 UTC 2017


Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
 tests/test_model.py | 237 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 208 insertions(+), 29 deletions(-)

diff --git a/tests/test_model.py b/tests/test_model.py
index 4579ded..11c6fab 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -176,7 +176,13 @@ class ModelTests(unittest.TestCase):
         if os.path.isfile(self.tmp_store):
             os.unlink(self.tmp_store)
 
-    def test_vm_info(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_info(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('test:///default', self.tmp_store)
         vms = inst.vms_get_list()
         self.assertEquals(1, len(vms))
@@ -381,8 +387,14 @@ class ModelTests(unittest.TestCase):
             info = inst.vm_lookup('kimchi-vm')
             self.assertEquals('running', info['state'])
 
-    @unittest.skipUnless(os.uname()[4] != "s390x", 'Not applicable to s390x')
-    def test_vm_graphics(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    @unittest.skipUnless(os.uname()[4] != 's390x', "Not applicable to s390x")
+    def test_vm_graphics(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
         params = {'name': 'test', 'domain': 'kvm',
                   'source_media': {'type': 'disk', 'path': UBUNTU_ISO}}
@@ -408,7 +420,14 @@ class ModelTests(unittest.TestCase):
 
         inst.template_delete('test')
 
-    def test_vm_virtviewerfile_vmnotrunning(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_virtviewerfile_vmnotrunning(self, mock_probe_file_perm,
+                                            mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
         params = {'name': 'test', 'domain': 'kvm',
                   'source_media': {'type': 'disk', 'path': UBUNTU_ISO}}
@@ -973,7 +992,14 @@ class ModelTests(unittest.TestCase):
             cur_cdrom_path = re.sub(":80/", '/', cdrom_info['path'])
             self.assertEquals(valid_remote_iso_path, cur_cdrom_path)
 
-    def test_vm_storage_provisioning(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_storage_provisioning(self, mock_probe_file_perm,
+                                     mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         with RollbackContext() as rollback:
@@ -1030,7 +1056,15 @@ class ModelTests(unittest.TestCase):
         xpath = "/domain/devices/disk[@device='disk']/driver/@type"
         return xpath_get_text(xml, xpath)[0]
 
-    def test_template_get_default_vol_format_from_conf(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_template_get_default_vol_format_from_conf(self,
+                                                       mock_probe_file_perm,
+                                                       mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         with RollbackContext() as rollback:
@@ -1055,7 +1089,14 @@ class ModelTests(unittest.TestCase):
             )
             self.assertEqual(created_disk_format, 'vmdk')
 
-    def test_template_creates_user_defined_vol_format_instead_default(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_template_creates_user_defined_vol_format_instead_default(
+            self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         default_vol = 'vmdk'
@@ -1083,7 +1124,14 @@ class ModelTests(unittest.TestCase):
             )
             self.assertEqual(created_disk_format, user_vol)
 
-    def test_template_uses_qcow2_format_if_no_user_or_default_defined(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_template_uses_qcow2_format_if_no_user_or_default_defined(
+            self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         with RollbackContext() as rollback:
@@ -1218,7 +1266,14 @@ class ModelTests(unittest.TestCase):
     msg = "Memory hotplug in non-numa guests only for PowerPC arch."
 
     @unittest.skipUnless(utils.running_as_root() and _host_is_power(), msg)
-    def test_non_numa_vm_memory_hotplug(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_non_numa_vm_memory_hotplug(self, mock_probe_file_perm,
+                                        mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         config.set("authentication", "method", "pam")
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
         conn = inst.conn.get()
@@ -1462,29 +1517,51 @@ 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):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_get_vm_cpu_cores(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         xml = """<domain type='kvm'>\
 <cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
 </domain>"""
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
         self.assertEqual('2', inst.vm_get_vm_cpu_cores(xml))
 
-    def test_get_vm_cpu_sockets(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_get_vm_cpu_sockets(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         xml = """<domain type='kvm'>\
 <cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
 </domain>"""
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
         self.assertEqual('3', inst.vm_get_vm_cpu_sockets(xml))
 
-    def test_get_vm_cpu_threads(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_get_vm_cpu_threads(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         xml = """<domain type='kvm'>\
 <cpu><topology sockets='3' cores='2' threads='8'/></cpu>\
 </domain>"""
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
         self.assertEqual('8', inst.vm_get_vm_cpu_threads(xml))
 
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
     @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
-    def test_get_vm_cpu_topology(self, mock_has_topology):
+    def test_get_vm_cpu_topology(self, mock_has_topology, mock_probe_file_perm,
+                                 mock_probe_user):
         class FakeDom():
             def XMLDesc(self, flag):
                 return """<domain type='kvm'>\
@@ -1494,6 +1571,8 @@ class ModelTests(unittest.TestCase):
             def name(self):
                 return 'fakedom'
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
         mock_has_topology.return_value = True
         expected_topology = {'sockets': 3, 'cores': 2, 'threads': 8}
 
@@ -1501,8 +1580,12 @@ class ModelTests(unittest.TestCase):
         self.assertEqual(expected_topology,
                          inst.vm_get_vm_cpu_topology(FakeDom()))
 
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
     @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
-    def test_get_vm_cpu_topology_blank(self, mock_has_topology):
+    def test_get_vm_cpu_topology_blank(self, mock_has_topology,
+                                       mock_probe_file_perm, mock_probe_user):
         class FakeDom():
             def XMLDesc(self, flag):
                 return """<domain type='kvm'></domain>"""
@@ -1510,6 +1593,8 @@ class ModelTests(unittest.TestCase):
             def name(self):
                 return 'fakedom'
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
         mock_has_topology.return_value = False
         expected_topology = {}
 
@@ -1517,15 +1602,27 @@ class ModelTests(unittest.TestCase):
         self.assertEqual(expected_topology,
                          inst.vm_get_vm_cpu_topology(FakeDom()))
 
-    def test_vm_cpu_hotplug_invalidparam_fail(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_cpu_hotplug_invalidparam_fail(self, mock_probe_file_perm,
+                                              mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model("qemu:///session", 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.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
     @mock.patch('wok.plugins.kimchi.model.vms.VMModel.has_topology')
-    def test_vm_cpu_hotplug_abovemax_fail(self, mock_has_topology):
+    def test_vm_cpu_hotplug_abovemax_fail(self, mock_has_topology,
+                                          mock_probe_file_perm,
+                                          mock_probe_user):
         class FakeDom():
             def XMLDesc(self, flag):
                 return """<domain type='kvm'>\
@@ -1535,6 +1632,8 @@ class ModelTests(unittest.TestCase):
             def name(self):
                 return 'fakedom'
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
         mock_has_topology.return_value = False
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
 
@@ -1542,10 +1641,15 @@ class ModelTests(unittest.TestCase):
             params = {"cpu_info": {"vcpus": 16}}
             inst.vm_cpu_hotplug_precheck(FakeDom(), params)
 
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
     @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):
+                                                   mock_has_topology,
+                                                   mock_probe_file_perm,
+                                                   mock_probe_user):
         class FakeDom():
             def XMLDesc(self, flag):
                 return """<domain type='kvm'>\
@@ -1555,6 +1659,8 @@ class ModelTests(unittest.TestCase):
             def name(self):
                 return 'fakedom'
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
         mock_has_topology.return_value = True
         mock_topology.return_value = {'sockets': 3, 'cores': 2, 'threads': 8}
 
@@ -1564,16 +1670,28 @@ class ModelTests(unittest.TestCase):
             params = {"cpu_info": {"vcpus": 10}}
             inst.vm_cpu_hotplug_precheck(FakeDom(), params)
 
-    def test_vm_cpu_hotplug_error(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_cpu_hotplug_error(self, mock_probe_file_perm, mock_probe_user):
         class FakeDom():
             def setVcpusFlags(self, vcpu, flags):
                 raise libvirt.libvirtError('')
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
         with self.assertRaisesRegexp(OperationFailed, 'KCHCPUHOTP0002E'):
             inst.vm_update_cpu_live(FakeDom(), '')
 
-    def test_get_interfaces(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_get_interfaces(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
         expected_ifaces = netinfo.all_favored_interfaces()
         ifaces = inst.interfaces_get_list()
@@ -1586,7 +1704,10 @@ class ModelTests(unittest.TestCase):
             self.assertIn('ipaddr', iface)
             self.assertIn('netmask', iface)
 
-    def test_async_tasks(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_async_tasks(self, mock_probe_file_perm, mock_probe_user):
         class task_except(Exception):
             pass
 
@@ -1610,6 +1731,9 @@ class ModelTests(unittest.TestCase):
             time.sleep(2)
             cb("step 3 OK", params.get('result', True))
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
         taskid = AsyncTask('', quick_op, 'Hello').id
         inst.task_wait(taskid)
@@ -1664,7 +1788,13 @@ class ModelTests(unittest.TestCase):
             vms = inst.vms_get_list()
             self.assertFalse(u'kīмсhī-∨м' in vms)
 
-    def test_vm_list_sorted(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_list_sorted(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         with RollbackContext() as rollback:
@@ -1683,7 +1813,13 @@ class ModelTests(unittest.TestCase):
 
             self.assertEquals(vms, sorted(vms, key=unicode.lower))
 
-    def test_vm_clone(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vm_clone(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('test:///default', objstore_loc=self.tmp_store)
 
         all_vm_names = inst.vms_get_list()
@@ -1733,7 +1869,13 @@ class ModelTests(unittest.TestCase):
             # (and removed) above (i.e. 'name' and 'uuid')
             self.assertEquals(original_vm, clone_vm)
 
-    def test_use_test_host(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_use_test_host(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         with RollbackContext() as rollback:
@@ -1758,7 +1900,13 @@ class ModelTests(unittest.TestCase):
 
             self.assertTrue('kimchi-vm' in vms)
 
-    def test_get_distros(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_get_distros(self, mock_probe_file_perm, mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model('qemu:///session', self.tmp_store)
         distros = inst.distros_get_list()
         for d in distros:
@@ -1769,7 +1917,10 @@ class ModelTests(unittest.TestCase):
             self.assertIn('os_arch', distro)
             self.assertIn('path', distro)
 
-    def test_deep_scan(self):
+    @mock.patch('wok.plugins.kimchi.model.storagevolumes.UserTests.probe_user')
+    def test_deep_scan(self, mock_probe_user):
+        mock_probe_user.return_value = ''
+
         inst = model.Model('qemu:///session', self.tmp_store)
 
         with RollbackContext() as rollback:
@@ -1796,7 +1947,14 @@ class ModelTests(unittest.TestCase):
 
     @unittest.skipUnless(utils.running_as_root() and _host_is_power(),
                          'Only required for Power hosts')
-    def test_pci_hotplug_requires_usb_controller(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_pci_hotplug_requires_usb_controller(self, mock_probe_file_perm,
+                                                 mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         config.set("authentication", "method", "pam")
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
         tpl_params = {'name': 'test', 'memory': 1024, 'cdrom': UBUNTU_ISO}
@@ -1896,7 +2054,14 @@ multifunction='on'/>
 </hostdev>
 """
 
-    def test_vmhostdev_is_hostdev_multifunction(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vmhostdev_is_hostdev_multifunction(self, mock_probe_file_perm,
+                                                mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
 
         hostdev_multi_elem = objectify.fromstring(
@@ -1913,7 +2078,14 @@ multifunction='on'/>
             inst.vmhostdev_is_hostdev_multifunction(hostdev_nomulti_elem)
         )
 
-    def test_vmhostdev_get_devices_same_addr(self):
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
+    def test_vmhostdev_get_devices_same_addr(self, mock_probe_file_perm,
+                                             mock_probe_user):
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
+
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
 
         root = objectify.fromstring(self.get_hostdevs_xml())
@@ -1947,12 +2119,19 @@ multifunction='on'/>
             [ET.tostring(nomatch_elem)]
         )
 
+    @mock.patch('wok.plugins.kimchi.model.templates.UserTests.probe_user')
+    @mock.patch('wok.plugins.kimchi.model.templates.'
+                'probe_file_permission_as_user')
     @mock.patch('wok.plugins.kimchi.model.vmhostdevs.get_vm_config_flag')
-    def test_vmhostdev_unplug_multifunction_pci(self, mock_conf_flag):
+    def test_vmhostdev_unplug_multifunction_pci(self, mock_conf_flag,
+                                                mock_probe_file_perm,
+                                                mock_probe_user):
         class FakeDom():
             def detachDeviceFlags(self, xml, config_flag):
                 pass
 
+        mock_probe_user.return_value = ''
+        mock_probe_file_perm.return_value = (True, None)
         mock_conf_flag.return_value = ''
 
         inst = model.Model("qemu:///session", objstore_loc=self.tmp_store)
-- 
2.7.4



More information about the Kimchi-devel mailing list