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
21 class FeatureTestingBase(unittest.TestCase):
25 _repo = "dir_repo_copper"
26 _cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -"
27 _output_file = '/home/opnfv/functest/results/foo.log'
30 @mock.patch('time.time', side_effect=[1, 2])
31 def _test_run(self, status, mock_method=None):
32 self.assertEqual(self.feature.run(cmd=self._cmd), status)
33 if status == testcase.TestCase.EX_OK:
34 self.assertEqual(self.feature.result, 100)
36 self.assertEqual(self.feature.result, 0)
37 mock_method.assert_has_calls([mock.call(), mock.call()])
38 self.assertEqual(self.feature.start_time, 1)
39 self.assertEqual(self.feature.stop_time, 2)
42 class FeatureTesting(FeatureTestingBase):
45 self.feature = feature.Feature(
46 project_name=self._project_name, case_name=self._case_name)
48 def test_run_exc(self):
49 # pylint: disable=bad-continuation
50 with mock.patch.object(
51 self.feature, 'execute',
52 side_effect=Exception) as mock_method:
53 self._test_run(testcase.TestCase.EX_RUN_ERROR)
54 mock_method.assert_called_once_with(cmd=self._cmd)
57 self._test_run(testcase.TestCase.EX_RUN_ERROR)
60 class BashFeatureTesting(FeatureTestingBase):
63 self.feature = feature.BashFeature(
64 project_name=self._project_name, case_name=self._case_name)
66 @mock.patch("functest.utils.functest_utils.execute_command")
67 def test_run_no_cmd(self, mock_method=None):
68 self.assertEqual(self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
69 mock_method.assert_not_called()
71 @mock.patch("functest.utils.functest_utils.execute_command",
73 def test_run_ko(self, mock_method=None):
74 self._test_run(testcase.TestCase.EX_RUN_ERROR)
75 mock_method.assert_called_once_with(
76 self._cmd, output_file=self._output_file)
78 @mock.patch("functest.utils.functest_utils.execute_command",
79 side_effect=Exception)
80 def test_run_exc(self, mock_method=None):
81 self._test_run(testcase.TestCase.EX_RUN_ERROR)
82 mock_method.assert_called_once_with(
83 self._cmd, output_file=self._output_file)
85 @mock.patch("functest.utils.functest_utils.execute_command",
87 def test_run(self, mock_method):
88 self._test_run(testcase.TestCase.EX_OK)
89 mock_method.assert_called_once_with(
90 self._cmd, output_file=self._output_file)
93 if __name__ == "__main__":
94 # logging must be disabled else it calls time.time()
95 # what will break these unit tests.
96 logging.disable(logging.CRITICAL)
97 unittest.main(verbosity=2)