Parcourir la source

Add osmorphing unit tests

Cristian Matiut il y a 9 mois
Parent
commit
e0fcc155b4

+ 5 - 4
coriolis/tests/osmorphing/netpreserver/test_ifcfg.py

@@ -33,17 +33,18 @@ class IfcfgNetPreserverTestCase(test_base.CoriolisBaseTestCase):
                 mock.sentinel.osmorphing_parameters,
                 mock.sentinel.operation_timeout))
 
-    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_config_file')
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_config_file_sudo')
     @mock.patch.object(ifcfg.IfcfgNetPreserver, '_get_net_config_files')
     def test_get_ifcfgs_by_type(self, mock_get_net_config_files,
-                                mock_read_config_file):
+                                mock_read_config_file_sudo):
         mock_get_net_config_files.return_value = [mock.sentinel.ifcfg_file]
-        mock_read_config_file.side_effect = [{"TYPE": "Ethernet"}]
+        mock_read_config_file_sudo.side_effect = [{"TYPE": "Ethernet"}]
 
         result = self.netpreserver._get_ifcfgs_by_type(
             "Ethernet", self.netpreserver.network_scripts_path)
 
-        mock_read_config_file.assert_called_once_with(mock.sentinel.ifcfg_file)
+        mock_read_config_file_sudo.assert_called_once_with(
+            mock.sentinel.ifcfg_file)
         mock_get_net_config_files.assert_called_once_with(
             self.netpreserver.network_scripts_path)
 

+ 4 - 4
coriolis/tests/osmorphing/netpreserver/test_nmconnection.py

@@ -44,20 +44,20 @@ class NmconnectionNetPreserverTestCase(test_base.CoriolisBaseTestCase):
             self.netpreserver.nmconnection_file)
         self.assertEqual(result, expected_result)
 
-    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_config_file')
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_config_file_sudo')
     @mock.patch.object(nmconnection.NmconnectionNetPreserver,
                        '_get_nmconnection_files')
     def test_get_keyfiles_by_type(self, mock_get_nmconnection_files,
-                                  mock_read_config_file):
+                                  mock_read_config_file_sudo):
         mock_get_nmconnection_files.return_value = [mock.sentinel.nmconn_file]
-        mock_read_config_file.side_effect = [{"type": "ethernet"}]
+        mock_read_config_file_sudo.side_effect = [{"type": "ethernet"}]
 
         result = self.netpreserver._get_keyfiles_by_type(
             "ethernet", self.netpreserver.nmconnection_file)
 
         mock_get_nmconnection_files.assert_called_once_with(
             self.netpreserver.nmconnection_file)
-        mock_read_config_file.assert_called_once_with(
+        mock_read_config_file_sudo.assert_called_once_with(
             mock.sentinel.nmconn_file)
         self.assertEqual(result, [(mock.sentinel.nmconn_file,
                                    {"type": "ethernet"})])

+ 39 - 0
coriolis/tests/osmorphing/test_base.py

@@ -457,6 +457,45 @@ class BaseLinuxOSMorphingToolsTestBase(test_base.CoriolisBaseTestCase):
             check_exists=False)
         self.assertEqual(result, mock_read_ssh_ini.return_value)
 
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path_chroot')
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_file_sudo')
+    def test__read_config_file_sudo(
+            self, mock_read_file_sudo, mock_test_path_chroot):
+        mock_test_path_chroot.return_value = True
+        mock_read_file_sudo.return_value = b'[connection]\ntype=ethernet'
+
+        result = self.os_morphing_tools._read_config_file_sudo(
+            self.chroot_path)
+
+        mock_read_file_sudo.assert_called_once_with(self.chroot_path)
+        self.assertEqual(result, {'type': 'ethernet'})
+
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path_chroot')
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_file_sudo')
+    def test__read_config_file_none(
+            self, mock_read_file_sudo, mock_test_path_chroot):
+        mock_test_path_chroot.return_value = False
+
+        result = self.os_morphing_tools._read_config_file_sudo(
+            self.chroot_path)
+
+        mock_read_file_sudo.assert_not_called()
+        self.assertEqual(result, {})
+
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path_chroot')
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_read_file_sudo')
+    def test__read_config_file_sudo_raises(
+            self, mock_read_file_sudo, mock_test_path_chroot):
+        mock_test_path_chroot.return_value = False
+
+        self.assertRaises(
+            IOError,
+            self.os_morphing_tools._read_config_file_sudo,
+            self.chroot_path,
+            check_exists=True)
+
+        mock_read_file_sudo.assert_not_called()
+
     @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path')
     @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd')
     def test__copy_resolv_conf(self, mock_exec_cmd, mock_test_path):