3 # Copyright (c) 2017 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
17 from functest.core import feature
18 from functest.core import testcase
20 # logging must be disabled else it calls time.time()
21 # what will break these unit tests.
22 logging.disable(logging.CRITICAL)
25 class FeatureTestingBase(unittest.TestCase):
29 _repo = "dir_repo_copper"
30 _cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -"
31 _output_file = '/home/opnfv/functest/results/bar.log'
34 @mock.patch('time.time', side_effect=[1, 2])
35 def _test_run(self, status, mock_method=None):
36 self.assertEqual(self.feature.run(cmd=self._cmd), status)
37 if status == testcase.TestCase.EX_OK:
38 self.assertEqual(self.feature.result, 100)
40 self.assertEqual(self.feature.result, 0)
41 mock_method.assert_has_calls([mock.call(), mock.call()])
42 self.assertEqual(self.feature.start_time, 1)
43 self.assertEqual(self.feature.stop_time, 2)
46 class FeatureTesting(FeatureTestingBase):
49 self.feature = feature.Feature(
50 project_name=self._project_name, case_name=self._case_name)
52 def test_run_exc(self):
53 # pylint: disable=bad-continuation
54 with mock.patch.object(
55 self.feature, 'execute',
56 side_effect=Exception) as mock_method:
57 self._test_run(testcase.TestCase.EX_RUN_ERROR)
58 mock_method.assert_called_once_with(cmd=self._cmd)
61 self._test_run(testcase.TestCase.EX_RUN_ERROR)
64 class BashFeatureTesting(FeatureTestingBase):
67 self.feature = feature.BashFeature(
68 project_name=self._project_name, case_name=self._case_name)
70 @mock.patch("functest.utils.functest_utils.execute_command")
71 def test_run_no_cmd(self, mock_method=None):
72 self.assertEqual(self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
73 mock_method.assert_not_called()
75 @mock.patch("functest.utils.functest_utils.execute_command",
77 def test_run_ko(self, mock_method=None):
78 self._test_run(testcase.TestCase.EX_RUN_ERROR)
79 mock_method.assert_called_once_with(
80 self._cmd, output_file=self._output_file)
82 @mock.patch("functest.utils.functest_utils.execute_command",
83 side_effect=Exception)
84 def test_run_exc(self, mock_method=None):
85 self._test_run(testcase.TestCase.EX_RUN_ERROR)
86 mock_method.assert_called_once_with(
87 self._cmd, output_file=self._output_file)
89 @mock.patch("functest.utils.functest_utils.execute_command",
91 def test_run(self, mock_method):
92 self._test_run(testcase.TestCase.EX_OK)
93 mock_method.assert_called_once_with(
94 self._cmd, output_file=self._output_file)
97 if __name__ == "__main__":
98 unittest.main(verbosity=2)