From bcf269cab055dbd4df404ed24751b7e9b7fe082a Mon Sep 17 00:00:00 2001 From: Yan Xing'an Date: Thu, 15 Nov 2018 01:17:46 +0800 Subject: [PATCH] Support UT for agent/linux/utils.py Add UT test file agent/linux/test_utils.py Partially Implements: blueprint test-addition-refactoring Change-Id: Icadc53637b8c2b7fb857c4f015f0f4d595d2987f --- tacker/tests/base.py | 36 +++++ tacker/tests/unit/agent/linux/__init__.py | 0 tacker/tests/unit/agent/linux/test_utils.py | 156 ++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 tacker/tests/unit/agent/linux/__init__.py create mode 100644 tacker/tests/unit/agent/linux/test_utils.py diff --git a/tacker/tests/base.py b/tacker/tests/base.py index 182575787..f01cd7cb4 100644 --- a/tacker/tests/base.py +++ b/tacker/tests/base.py @@ -207,3 +207,39 @@ class BaseTestCase(testtools.TestCase): yield return self.fail('Execution of this test timed out') + + def get_new_temp_dir(self): + """Create a new temporary directory. + + :returns: fixtures.TempDir + """ + return self.useFixture(fixtures.TempDir()) + + def get_default_temp_dir(self): + """Create a default temporary directory. + + Returns the same directory during the whole test case. + + :returns: fixtures.TempDir + """ + if not hasattr(self, '_temp_dir'): + self._temp_dir = self.get_new_temp_dir() + return self._temp_dir + + def get_temp_file_path(self, filename, root=None): + """Returns an absolute path for a temporary file. + + If root is None, the file is created in default temporary directory. It + also creates the directory if it's not initialized yet. + + If root is not None, the file is created inside the directory passed as + root= argument. + + :param filename: filename + :type filename: string + :param root: temporary directory to create a new file in + :type root: fixtures.TempDir + :returns: absolute file path string + """ + root = root or self.get_default_temp_dir() + return root.join(filename) diff --git a/tacker/tests/unit/agent/linux/__init__.py b/tacker/tests/unit/agent/linux/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tacker/tests/unit/agent/linux/test_utils.py b/tacker/tests/unit/agent/linux/test_utils.py new file mode 100644 index 000000000..f80dcb6e2 --- /dev/null +++ b/tacker/tests/unit/agent/linux/test_utils.py @@ -0,0 +1,156 @@ +# Copyright 2012, VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import mock +import six + +import oslo_i18n + +from tacker.agent.linux import utils +from tacker.tests import base + + +_marker = object() + + +class AgentUtilsExecuteTest(base.BaseTestCase): + def setUp(self): + super(AgentUtilsExecuteTest, self).setUp() + self.test_file = self.get_temp_file_path('test_execute.tmp') + open(self.test_file, 'w').close() + self.process = mock.patch('eventlet.green.subprocess.Popen').start() + self.process.return_value.returncode = 0 + self.mock_popen = self.process.return_value.communicate + + def test_without_helper(self): + expected = "%s\n" % self.test_file + self.mock_popen.return_value = [expected, ""] + result = utils.execute(["ls", self.test_file]) + self.assertEqual(result, expected) + + def test_with_helper(self): + expected = "ls %s\n" % self.test_file + self.mock_popen.return_value = [expected, ""] + result = utils.execute(["ls", self.test_file], root_helper='echo') + self.assertEqual(result, expected) + + def test_stderr_true(self): + expected = "%s\n" % self.test_file + self.mock_popen.return_value = [expected, ""] + out = utils.execute(["ls", self.test_file], return_stderr=True) + self.assertIsInstance(out, tuple) + self.assertEqual(out, (expected, "")) + + def test_check_exit_code(self): + self.mock_popen.return_value = ["", ""] + stdout = utils.execute(["ls", self.test_file[:-1]], + check_exit_code=False) + self.assertEqual("", stdout) + + def test_execute_raises(self): + self.mock_popen.side_effect = RuntimeError + self.assertRaises(RuntimeError, utils.execute, + ["ls", self.test_file[:-1]]) + + def test_process_input(self): + expected = "%s\n" % self.test_file[:-1] + self.mock_popen.return_value = [expected, ""] + result = utils.execute(["cat"], process_input="%s\n" % + self.test_file[:-1]) + self.assertEqual(result, expected) + + def test_with_addl_env(self): + expected = "%s\n" % self.test_file + self.mock_popen.return_value = [expected, ""] + result = utils.execute(["ls", self.test_file], + addl_env={'foo': 'bar'}) + self.assertEqual(result, expected) + + def test_return_code_log_error_raise_runtime(self): + self.mock_popen.return_value = ('', '') + self.process.return_value.returncode = 1 + with mock.patch.object(utils, 'LOG') as log: + self.assertRaises(RuntimeError, utils.execute, + ['ls']) + self.assertTrue(log.error.called) + + def test_return_code_log_error_no_raise_runtime(self): + self.mock_popen.return_value = ('', '') + self.process.return_value.returncode = 1 + with mock.patch.object(utils, 'LOG') as log: + utils.execute(['ls'], check_exit_code=False) + self.assertTrue(log.error.called) + + def test_return_code_log_debug(self): + self.mock_popen.return_value = ('', '') + with mock.patch.object(utils, 'LOG') as log: + utils.execute(['ls']) + self.assertTrue(log.debug.called) + + def test_return_code_log_error_change_locale(self): + ja_output = 'std_out in Japanese' + ja_error = 'std_err in Japanese' + ja_message_out = oslo_i18n._message.Message(ja_output) + ja_message_err = oslo_i18n._message.Message(ja_error) + ja_translate_out = oslo_i18n._translate.translate(ja_message_out, 'ja') + ja_translate_err = oslo_i18n._translate.translate(ja_message_err, 'ja') + self.mock_popen.return_value = (ja_translate_out, ja_translate_err) + self.process.return_value.returncode = 1 + + with mock.patch.object(utils, 'LOG') as log: + utils.execute(['ls'], check_exit_code=False) + self.assertIn(ja_translate_out, str(log.error.call_args_list)) + self.assertIn(ja_translate_err, str(log.error.call_args_list)) + + def test_return_code_raise_runtime_log_fail_as_error(self): + self.mock_popen.return_value = ('', '') + self.process.return_value.returncode = 1 + with mock.patch.object(utils, 'LOG') as log: + self.assertRaises(RuntimeError, utils.execute, + ['ls']) + self.assertTrue(log.error.called) + + def test_encode_process_input(self): + str_idata = "%s\n" % self.test_file[:-1] + str_odata = "%s\n" % self.test_file + if six.PY3: + bytes_idata = str_idata.encode(encoding='utf-8') + bytes_odata = str_odata.encode(encoding='utf-8') + self.mock_popen.return_value = [bytes_odata, b''] + result = utils.execute(['cat'], process_input=str_idata) + self.mock_popen.assert_called_once_with(bytes_idata) + else: + self.mock_popen.return_value = [str_odata, ''] + result = utils.execute(['cat'], process_input=str_idata) + self.mock_popen.assert_called_once_with(str_idata) + self.assertEqual(str_odata, result) + + def test_return_str_data(self): + str_data = "%s\n" % self.test_file + self.mock_popen.return_value = [str_data, ''] + result = utils.execute(['ls', self.test_file], return_stderr=True) + self.assertEqual((str_data, ''), result) + + +class AgentUtilsExecuteEncodeTest(base.BaseTestCase): + def setUp(self): + super(AgentUtilsExecuteEncodeTest, self).setUp() + self.test_file = self.get_temp_file_path('test_execute.tmp') + open(self.test_file, 'w').close() + + def test_decode_return_data(self): + str_data = "%s\n" % self.test_file + result = utils.execute(['ls', self.test_file], return_stderr=True) + self.assertEqual((str_data, ''), result)