a4ac5af7038f4486b5fa4b0f4ef96c86513b25c7
[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 FakeTestCase(feature.Feature):
22
23     def execute(self, **kwargs):
24         pass
25
26
27 class AbstractFeatureTesting(unittest.TestCase):
28
29     def test_run_unimplemented(self):
30         with self.assertRaises(TypeError):
31             feature.Feature(case_name="feature", project_name="xtesting")
32
33
34 class FeatureTestingBase(unittest.TestCase):
35
36     _case_name = "foo"
37     _project_name = "bar"
38     _repo = "dir_repo_bar"
39     _cmd = "run_bar_tests.py"
40     _output_file = '/var/lib/xtesting/results/foo.log'
41     feature = None
42
43     @mock.patch('time.time', side_effect=[1, 2])
44     def _test_run(self, status, mock_method=None):
45         self.assertEqual(self.feature.run(cmd=self._cmd), status)
46         if status == testcase.TestCase.EX_OK:
47             self.assertEqual(self.feature.result, 100)
48         else:
49             self.assertEqual(self.feature.result, 0)
50         mock_method.assert_has_calls([mock.call(), mock.call()])
51         self.assertEqual(self.feature.start_time, 1)
52         self.assertEqual(self.feature.stop_time, 2)
53
54
55 class FeatureTesting(FeatureTestingBase):
56
57     def setUp(self):
58         # logging must be disabled else it calls time.time()
59         # what will break these unit tests.
60         logging.disable(logging.CRITICAL)
61         with mock.patch('six.moves.builtins.open'):
62             self.feature = FakeTestCase(
63                 project_name=self._project_name, case_name=self._case_name)
64
65     def test_run_exc(self):
66         # pylint: disable=bad-continuation
67         with mock.patch.object(
68                 self.feature, 'execute',
69                 side_effect=Exception) as mock_method:
70             self._test_run(testcase.TestCase.EX_RUN_ERROR)
71             mock_method.assert_called_once_with(cmd=self._cmd)
72
73     def test_run(self):
74         self._test_run(testcase.TestCase.EX_RUN_ERROR)
75
76
77 class BashFeatureTesting(FeatureTestingBase):
78
79     def setUp(self):
80         # logging must be disabled else it calls time.time()
81         # what will break these unit tests.
82         logging.disable(logging.CRITICAL)
83         with mock.patch('six.moves.builtins.open'):
84             self.feature = feature.BashFeature(
85                 project_name=self._project_name, case_name=self._case_name)
86
87     @mock.patch('subprocess.Popen')
88     def test_run_no_cmd(self, mock_subproc):
89         self.assertEqual(
90             self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
91         mock_subproc.assert_not_called()
92
93     @mock.patch('subprocess.Popen')
94     def test_run_ko(self, mock_subproc):
95         with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen:
96             mock_obj = mock.Mock()
97             attrs = {'wait.return_value': 1}
98             mock_obj.configure_mock(**attrs)
99
100             mock_subproc.return_value = mock_obj
101             self._test_run(testcase.TestCase.EX_RUN_ERROR)
102             mopen.assert_called_once_with(self._output_file, "w+")
103
104     @mock.patch('subprocess.Popen')
105     def test_run(self, mock_subproc):
106         with mock.patch('six.moves.builtins.open', mock.mock_open()) as mopen:
107             mock_obj = mock.Mock()
108             attrs = {'wait.return_value': 0}
109             mock_obj.configure_mock(**attrs)
110
111             mock_subproc.return_value = mock_obj
112             self._test_run(testcase.TestCase.EX_OK)
113             mopen.assert_called_once_with(self._output_file, "w+")
114
115
116 if __name__ == "__main__":
117     unittest.main(verbosity=2)