diff options
author | Cédric Ollivier <cedric.ollivier@orange.com> | 2018-12-16 17:50:38 +0100 |
---|---|---|
committer | Cédric Ollivier <cedric.ollivier@orange.com> | 2018-12-27 15:51:41 +0100 |
commit | c94c4d5ef8b3b4625e2e2d85bfeb26d738396926 (patch) | |
tree | c751c476e657f311b89f256a55f99ae6bd106f4c /xtesting/tests/unit | |
parent | 8f9e24797f46c49796298ff17c7396d8135169bf (diff) |
Allow printing bash cmd output in console
It switches to Popen to print real-time console.
Console has to be enabled per testcase (testcases.yaml).
Change-Id: Id36b42c8409262f7c443e98ae2bcc465984b287f
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
(cherry picked from commit ebe4291c4457d84cb8425999cfa12371c1c7ce40)
Diffstat (limited to 'xtesting/tests/unit')
-rw-r--r-- | xtesting/tests/unit/core/test_feature.py | 111 |
1 files changed, 97 insertions, 14 deletions
diff --git a/xtesting/tests/unit/core/test_feature.py b/xtesting/tests/unit/core/test_feature.py index 47766cdd..cbee8511 100644 --- a/xtesting/tests/unit/core/test_feature.py +++ b/xtesting/tests/unit/core/test_feature.py @@ -14,6 +14,7 @@ import subprocess import unittest import mock +import six from xtesting.core import feature from xtesting.core import testcase @@ -38,7 +39,7 @@ class FeatureTestingBase(unittest.TestCase): _project_name = "bar" _repo = "dir_repo_bar" _cmd = "run_bar_tests.py" - _output_file = '/var/lib/xtesting/results/foo.log' + _output_file = '/var/lib/xtesting/results/foo/foo.log' feature = None @mock.patch('time.time', side_effect=[1, 2]) @@ -52,6 +53,15 @@ class FeatureTestingBase(unittest.TestCase): self.assertEqual(self.feature.start_time, 1) self.assertEqual(self.feature.stop_time, 2) + @mock.patch('time.time', side_effect=[1, 2]) + def _test_run_console(self, console, status, mock_method=None): + self.assertEqual( + self.feature.run(cmd=self._cmd, console=console), status) + self.assertEqual(self.feature.result, 100) + mock_method.assert_has_calls([mock.call(), mock.call()]) + self.assertEqual(self.feature.start_time, 1) + self.assertEqual(self.feature.stop_time, 2) + class FeatureTesting(FeatureTestingBase): @@ -85,31 +95,104 @@ class BashFeatureTesting(FeatureTestingBase): self.feature = feature.BashFeature( project_name=self._project_name, case_name=self._case_name) - @mock.patch('subprocess.check_call') + @mock.patch('subprocess.Popen') def test_run_no_cmd(self, mock_subproc): - delattr(FeatureTesting, "_cmd") self.assertEqual( self.feature.run(), testcase.TestCase.EX_RUN_ERROR) mock_subproc.assert_not_called() - @mock.patch('subprocess.check_call', + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('subprocess.Popen', side_effect=subprocess.CalledProcessError(0, '', '')) - def test_run_ko(self, mock_subproc): - setattr(FeatureTesting, "_cmd", "run_bar_tests.py") + def test_run_ko1(self, *args): with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen: self._test_run(testcase.TestCase.EX_RUN_ERROR) - mopen.assert_called_once_with(self._output_file, "w+") - mock_subproc.assert_called_once_with( + mopen.assert_called_once_with(self._output_file, "w") + args[0].assert_called_once_with( self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY) - - @mock.patch('subprocess.check_call') - def test_run(self, mock_subproc): - setattr(FeatureTesting, "_cmd", "run_bar_tests.py") + args[1].assert_called_once_with(self.feature.res_dir) + + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('subprocess.Popen') + def test_run_ko2(self, *args): + stream = six.StringIO() + stream.write("foo") + stream.seek(0) + attrs = {'return_value.stdout': stream, 'return_value.returncode': 1} + args[0].configure_mock(**attrs) + with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen: + self._test_run(testcase.TestCase.EX_RUN_ERROR) + self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls) + self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls) + args[0].assert_called_once_with( + self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY) + args[1].assert_called_once_with(self.feature.res_dir) + + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('subprocess.Popen') + def test_run1(self, *args): + stream = six.StringIO() + stream.write("foo") + stream.seek(0) + attrs = {'return_value.stdout': stream, 'return_value.returncode': 0} + args[0].configure_mock(**attrs) with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen: self._test_run(testcase.TestCase.EX_OK) - mopen.assert_called_once_with(self._output_file, "w+") - mock_subproc.assert_called_once_with( + self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls) + self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls) + args[0].assert_called_once_with( + self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY) + args[1].assert_called_once_with(self.feature.res_dir) + + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('subprocess.Popen') + def test_run2(self, *args): + stream = six.StringIO() + stream.write("foo") + stream.seek(0) + attrs = {'return_value.stdout': stream, 'return_value.returncode': 0} + args[0].configure_mock(**attrs) + with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen: + self._test_run_console(True, testcase.TestCase.EX_OK) + self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls) + self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls) + args[0].assert_called_once_with( + self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY) + args[1].assert_called_once_with(self.feature.res_dir) + + @mock.patch('os.path.isdir', return_value=True) + @mock.patch('subprocess.Popen') + def test_run3(self, *args): + stream = six.StringIO() + stream.write("foo") + stream.seek(0) + attrs = {'return_value.stdout': stream, 'return_value.returncode': 0} + args[0].configure_mock(**attrs) + with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen: + self._test_run_console(False, testcase.TestCase.EX_OK) + self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls) + self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls) + args[0].assert_called_once_with( + self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY) + args[1].assert_called_once_with(self.feature.res_dir) + + @mock.patch('os.makedirs') + @mock.patch('os.path.isdir', return_value=False) + @mock.patch('subprocess.Popen') + def test_run4(self, *args): + stream = six.StringIO() + stream.write("foo") + stream.seek(0) + attrs = {'return_value.stdout': stream, 'return_value.returncode': 0} + args[0].configure_mock(**attrs) + with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen: + self._test_run_console(False, testcase.TestCase.EX_OK) + self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls) + self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls) + args[0].assert_called_once_with( self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY) + args[1].assert_called_once_with(self.feature.res_dir) + args[2].assert_called_once_with(self.feature.res_dir) if __name__ == "__main__": |