diff options
Diffstat (limited to 'functest/tests/unit/utils')
-rw-r--r-- | functest/tests/unit/utils/test_functest_utils.py | 81 |
1 files changed, 58 insertions, 23 deletions
diff --git a/functest/tests/unit/utils/test_functest_utils.py b/functest/tests/unit/utils/test_functest_utils.py index 4f3f16f23..4b642ff9d 100644 --- a/functest/tests/unit/utils/test_functest_utils.py +++ b/functest/tests/unit/utils/test_functest_utils.py @@ -100,17 +100,18 @@ class FunctestUtilsTesting(unittest.TestCase): mock.mock_open()) as mopen: stream = six.BytesIO() stream.write(self.cmd_readline().encode("utf-8")) - mock_obj2 = mock.Mock() - attrs = {'stdout': stream, 'wait.return_value': 1} - mock_obj2.configure_mock(**attrs) - mock_subproc_open.return_value = mock_obj2 + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 1} + mock_subproc_open.configure_mock(**attrs) resp = functest_utils.execute_command( self.cmd, info=True, error_msg=self.error_msg, verbose=True, output_file=self.output_file) self.assertEqual(resp, 1) - msg_exec = ("Executing command: '%s'" % self.cmd) + msg_exec = f"Executing command: '{self.cmd}'" mock_logger_info.assert_called_once_with(msg_exec) - mopen.assert_called_once_with(self.output_file, "w") + mopen.assert_called_once_with( + self.output_file, "w", encoding='utf-8') mock_logger_error.assert_called_once_with(self.error_msg) @mock.patch('functest.utils.functest_utils.LOGGER.info') @@ -121,17 +122,18 @@ class FunctestUtilsTesting(unittest.TestCase): mock.mock_open()) as mopen: stream = six.BytesIO() stream.write(self.cmd_readline().encode("utf-8")) - mock_obj2 = mock.Mock() - attrs = {'stdout': stream, 'wait.return_value': 0} - mock_obj2.configure_mock(**attrs) - mock_subproc_open.return_value = mock_obj2 + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 0} + mock_subproc_open.configure_mock(**attrs) resp = functest_utils.execute_command( self.cmd, info=True, error_msg=self.error_msg, verbose=True, output_file=self.output_file) self.assertEqual(resp, 0) - msg_exec = ("Executing command: '%s'" % self.cmd) + msg_exec = (f"Executing command: '{self.cmd}'") mock_logger_info.assert_called_once_with(msg_exec) - mopen.assert_called_once_with(self.output_file, "w") + mopen.assert_called_once_with( + self.output_file, "w", encoding='utf-8') @mock.patch('sys.stdout') def test_exec_cmd_args_missing_ok(self, stdout=None): @@ -140,10 +142,10 @@ class FunctestUtilsTesting(unittest.TestCase): as mock_subproc_open: stream = six.BytesIO() stream.write(self.cmd_readline().encode("utf-8")) - mock_obj2 = mock.Mock() - attrs = {'stdout': stream, 'wait.return_value': 0} - mock_obj2.configure_mock(**attrs) - mock_subproc_open.return_value = mock_obj2 + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 0} + mock_subproc_open.configure_mock(**attrs) resp = functest_utils.execute_command( self.cmd, info=False, error_msg="", verbose=False, output_file=None) @@ -154,12 +156,13 @@ class FunctestUtilsTesting(unittest.TestCase): # pylint: disable=unused-argument with mock.patch('functest.utils.functest_utils.subprocess.Popen') \ as mock_subproc_open: + attrs = {} stream = six.BytesIO() stream.write(self.cmd_readline().encode("utf-8")) - mock_obj2 = mock.Mock() - attrs = {'stdout': stream, 'wait.return_value': 1} - mock_obj2.configure_mock(**attrs) - mock_subproc_open.return_value = mock_obj2 + attrs = { + 'return_value.__enter__.return_value.stdout': stream, + 'return_value.__enter__.return_value.wait.return_value': 1} + mock_subproc_open.configure_mock(**attrs) resp = functest_utils.execute_command( self.cmd, info=False, error_msg="", verbose=False, output_file=None) @@ -174,9 +177,9 @@ class FunctestUtilsTesting(unittest.TestCase): mock_yaml.return_value = self.file_yaml functest_utils.get_parameter_from_yaml(self.parameter, self.test_file) - self.assertTrue(("The parameter %s is not" - " defined in config_functest.yaml" % - self.parameter) in excep.exception) + self.assertTrue((f"The parameter {self.parameter} is not" + " defined in config_functest.yaml" + ) in excep.exception) def test_get_param_from_yaml_def(self): with mock.patch('six.moves.builtins.open', mock.mock_open()), \ @@ -317,6 +320,38 @@ class FunctestUtilsTesting(unittest.TestCase): args[0].assert_called_once_with(cloud) @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 87)) + def test_openstack_version13(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Ussuri") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 88)) + def test_openstack_version14(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Wallaby") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 89)) + def test_openstack_version15(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Xena") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', + return_value=(2, 92)) + def test_openstack_version16(self, *args): + cloud = mock.Mock() + self.assertEqual(functest_utils.get_openstack_version( + cloud), "Zed") + args[0].assert_called_once_with(cloud) + + @mock.patch('functest.utils.functest_utils.get_nova_version', return_value=None) def test_openstack_version_exc(self, *args): cloud = mock.Mock() |