988981effa4a4b885ca6b2b27f384da732214266
[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
21 class FeatureTestingBase(unittest.TestCase):
22
23     _case_name = "foo"
24     _project_name = "bar"
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'
28     feature = None
29
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)
35         else:
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)
40
41     def test_logger_module_ko(self):
42         with mock.patch('six.moves.builtins.open'):
43             self.feature = feature.Feature(
44                 project_name=self._project_name, case_name=self._case_name)
45             self.assertEqual(self.feature.logger.name, self._case_name)
46
47     def test_logger_module(self):
48         with mock.patch('six.moves.builtins.open'):
49             self.feature = feature.Feature(
50                 project_name=self._project_name, case_name=self._case_name,
51                 run={'module': 'bar'})
52             self.assertEqual(self.feature.logger.name, 'bar')
53
54
55 class FeatureTesting(FeatureTestingBase):
56
57     def setUp(self):
58         with mock.patch('six.moves.builtins.open'):
59             self.feature = feature.Feature(
60                 project_name=self._project_name, case_name=self._case_name)
61
62     def test_run_exc(self):
63         # pylint: disable=bad-continuation
64         with mock.patch.object(
65                 self.feature, 'execute',
66                 side_effect=Exception) as mock_method:
67             self._test_run(testcase.TestCase.EX_RUN_ERROR)
68             mock_method.assert_called_once_with(cmd=self._cmd)
69
70     def test_run(self):
71         self._test_run(testcase.TestCase.EX_RUN_ERROR)
72
73
74 class BashFeatureTesting(FeatureTestingBase):
75
76     def setUp(self):
77         with mock.patch('six.moves.builtins.open'):
78             self.feature = feature.BashFeature(
79                 project_name=self._project_name, case_name=self._case_name)
80
81     @mock.patch("functest.utils.functest_utils.execute_command")
82     def test_run_no_cmd(self, mock_method=None):
83         self.assertEqual(self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
84         mock_method.assert_not_called()
85
86     @mock.patch("functest.utils.functest_utils.execute_command",
87                 return_value=1)
88     def test_run_ko(self, mock_method=None):
89         self._test_run(testcase.TestCase.EX_RUN_ERROR)
90         mock_method.assert_called_once_with(
91             self._cmd, output_file=self._output_file)
92
93     @mock.patch("functest.utils.functest_utils.execute_command",
94                 side_effect=Exception)
95     def test_run_exc(self, mock_method=None):
96         self._test_run(testcase.TestCase.EX_RUN_ERROR)
97         mock_method.assert_called_once_with(
98             self._cmd, output_file=self._output_file)
99
100     @mock.patch("functest.utils.functest_utils.execute_command",
101                 return_value=0)
102     def test_run(self, mock_method):
103         self._test_run(testcase.TestCase.EX_OK)
104         mock_method.assert_called_once_with(
105             self._cmd, output_file=self._output_file)
106
107
108 if __name__ == "__main__":
109     # logging must be disabled else it calls time.time()
110     # what will break these unit tests.
111     logging.disable(logging.CRITICAL)
112     unittest.main(verbosity=2)