Remove Feature logger
[functest-xtesting.git] / xtesting / 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 xtesting.core import feature
18 from xtesting.core import testcase
19
20
21 class FeatureTestingBase(unittest.TestCase):
22
23     _case_name = "foo"
24     _project_name = "bar"
25     _repo = "dir_repo_bar"
26     _cmd = "run_bar_tests.py"
27     _output_file = '/var/lib/xtesting/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
42 class FeatureTesting(FeatureTestingBase):
43
44     def setUp(self):
45         # logging must be disabled else it calls time.time()
46         # what will break these unit tests.
47         logging.disable(logging.CRITICAL)
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
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)
59
60     def test_run(self):
61         self._test_run(testcase.TestCase.EX_RUN_ERROR)
62
63
64 class BashFeatureTesting(FeatureTestingBase):
65
66     def setUp(self):
67         # logging must be disabled else it calls time.time()
68         # what will break these unit tests.
69         logging.disable(logging.CRITICAL)
70         with mock.patch('six.moves.builtins.open'):
71             self.feature = feature.BashFeature(
72                 project_name=self._project_name, case_name=self._case_name)
73
74     @mock.patch('subprocess.Popen')
75     def test_run_no_cmd(self, mock_subproc):
76         self.assertEqual(
77             self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
78         mock_subproc.assert_not_called()
79
80     @mock.patch('subprocess.Popen')
81     def test_run_ko(self, mock_subproc):
82         with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen:
83             mock_obj = mock.Mock()
84             attrs = {'wait.return_value': 1}
85             mock_obj.configure_mock(**attrs)
86
87             mock_subproc.return_value = mock_obj
88             self._test_run(testcase.TestCase.EX_RUN_ERROR)
89             mopen.assert_called_once_with(self._output_file, "w+")
90
91     @mock.patch('subprocess.Popen')
92     def test_run(self, mock_subproc):
93         with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen:
94             mock_obj = mock.Mock()
95             attrs = {'wait.return_value': 0}
96             mock_obj.configure_mock(**attrs)
97
98             mock_subproc.return_value = mock_obj
99             self._test_run(testcase.TestCase.EX_OK)
100             mopen.assert_called_once_with(self._output_file, "w+")
101
102
103 if __name__ == "__main__":
104     unittest.main(verbosity=2)