ac4b04c4441009c60f055d01db721c04f117cdc5
[functest.git] / functest / tests / unit / core / test_feature.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Orange and others.
4 #
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
9
10 # pylint: disable=missing-docstring
11
12 import logging
13 import unittest
14
15 import mock
16
17 from functest.core import feature
18 from functest.core import testcase
19
20 logging.disable(logging.CRITICAL)
21
22
23 class FeatureTestingBase(unittest.TestCase):
24
25     _case_name = "foo"
26     _project_name = "bar"
27     _repo = "dir_repo_copper"
28     _cmd = "cd /home/opnfv/repos/foo/tests && bash run.sh && cd -"
29     _output_file = '/home/opnfv/functest/results/bar.log'
30
31     @mock.patch('time.time', side_effect=[1, 2])
32     def _test_run(self, status, mock_method=None):
33         self.assertEqual(self.feature.run(), status)
34         if status == testcase.TestCase.EX_OK:
35             self.assertEqual(self.feature.criteria, 'PASS')
36         else:
37             self.assertEqual(self.feature.criteria, 'FAIL')
38         mock_method.assert_has_calls([mock.call(), mock.call()])
39         self.assertEqual(self.feature.start_time, 1)
40         self.assertEqual(self.feature.stop_time, 2)
41
42
43 class FeatureTesting(FeatureTestingBase):
44
45     def setUp(self):
46         self.feature = feature.Feature(
47             project_name=self._project_name, case_name=self._case_name,
48             cmd=self._cmd)
49
50     def test_run(self):
51         self._test_run(testcase.TestCase.EX_RUN_ERROR)
52
53
54 class BashFeatureTesting(FeatureTestingBase):
55
56     def setUp(self):
57         self.feature = feature.BashFeature(
58             project_name=self._project_name, case_name=self._case_name,
59             cmd=self._cmd)
60
61     @mock.patch("functest.utils.functest_utils.execute_command",
62                 return_value=1)
63     def test_run_ko(self, mock_method=None):
64         self._test_run(testcase.TestCase.EX_RUN_ERROR)
65         mock_method.assert_called_once_with(
66             self._cmd, output_file=self._output_file)
67
68     @mock.patch("functest.utils.functest_utils.execute_command",
69                 side_effect=Exception)
70     def test_run_exc(self, mock_method=None):
71         self._test_run(testcase.TestCase.EX_RUN_ERROR)
72         mock_method.assert_called_once_with(
73             self._cmd, output_file=self._output_file)
74
75     @mock.patch("functest.utils.functest_utils.execute_command",
76                 return_value=0)
77     def test_run(self, mock_method):
78         self._test_run(testcase.TestCase.EX_OK)
79         mock_method.assert_called_once_with(
80             self._cmd, output_file=self._output_file)
81
82
83 if __name__ == "__main__":
84     unittest.main(verbosity=2)