1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
#!/usr/bin/env python
# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
# pylint: disable=missing-docstring
import os
from io import BytesIO
import logging
import subprocess
import unittest
import mock
from xtesting.core import feature
from xtesting.core import testcase
from xtesting.utils import constants
class FakeTestCase(feature.Feature):
def execute(self, **kwargs):
pass
class AbstractFeatureTesting(unittest.TestCase):
def test_run_unimplemented(self):
# pylint: disable=abstract-class-instantiated
with self.assertRaises(TypeError):
feature.Feature(case_name="feature", project_name="xtesting")
class FeatureTestingBase(unittest.TestCase):
_case_name = "foo"
_project_name = "bar"
_repo = "dir_repo_bar"
_cmd = "run_bar_tests.py"
_output_file = os.path.join(constants.RESULTS_DIR, 'foo/foo.log')
_max_duration = 1
feature = None
@mock.patch('time.time', side_effect=[1, 2])
def _test_run(self, status, mock_method=None):
self.assertEqual(self.feature.run(cmd=self._cmd), status)
if status == testcase.TestCase.EX_OK:
self.assertEqual(self.feature.result, 100)
else:
self.assertEqual(self.feature.result, 0)
mock_method.assert_has_calls([mock.call(), mock.call()])
self.assertEqual(self.feature.start_time, 1)
self.assertEqual(self.feature.stop_time, 2)
@mock.patch('time.time', side_effect=[1, 2])
def _test_run_console(self, console, status, mock_method=None):
self.assertEqual(
self.feature.run(cmd=self._cmd, console=console), status)
self.assertEqual(self.feature.result, 100)
mock_method.assert_has_calls([mock.call(), mock.call()])
self.assertEqual(self.feature.start_time, 1)
self.assertEqual(self.feature.stop_time, 2)
@mock.patch('time.time', side_effect=[1, 2])
def _test_run_max_duration(self, status, mock_method=None):
self.assertEqual(
self.feature.run(cmd=self._cmd, max_duration=self._max_duration),
status)
mock_method.assert_has_calls([mock.call(), mock.call()])
self.assertEqual(self.feature.start_time, 1)
self.assertEqual(self.feature.stop_time, 2)
class FeatureTesting(FeatureTestingBase):
def setUp(self):
# logging must be disabled else it calls time.time()
# what will break these unit tests.
logging.disable(logging.CRITICAL)
with mock.patch('builtins.open'):
self.feature = FakeTestCase(
project_name=self._project_name, case_name=self._case_name)
def test_run_exc(self):
with mock.patch.object(
self.feature, 'execute',
side_effect=Exception) as mock_method:
self._test_run(testcase.TestCase.EX_RUN_ERROR)
mock_method.assert_called_once_with(cmd=self._cmd)
def test_run(self):
self._test_run(testcase.TestCase.EX_RUN_ERROR)
class BashFeatureTesting(FeatureTestingBase):
def setUp(self):
# logging must be disabled else it calls time.time()
# what will break these unit tests.
logging.disable(logging.CRITICAL)
with mock.patch('builtins.open'):
self.feature = feature.BashFeature(
project_name=self._project_name, case_name=self._case_name)
@mock.patch('subprocess.Popen')
def test_run_no_cmd(self, mock_subproc):
self.assertEqual(
self.feature.run(), testcase.TestCase.EX_RUN_ERROR)
mock_subproc.assert_not_called()
@mock.patch('os.path.isdir', return_value=True)
@mock.patch('subprocess.Popen',
side_effect=subprocess.CalledProcessError(0, '', ''))
def test_run_ko1(self, *args):
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run(testcase.TestCase.EX_RUN_ERROR)
mopen.assert_called_once_with(self._output_file, "w")
args[0].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
args[1].assert_called_once_with(self.feature.res_dir)
@mock.patch('os.path.isdir', return_value=True)
@mock.patch('subprocess.Popen')
def test_run_ko2(self, *args):
stream = BytesIO()
stream.write(b"foo")
stream.seek(0)
attrs = {'return_value.__enter__.return_value.stdout': stream,
'return_value.__enter__.return_value.returncode': 1}
args[0].configure_mock(**attrs)
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run(testcase.TestCase.EX_RUN_ERROR)
self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls)
self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls)
args[0].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
args[1].assert_called_once_with(self.feature.res_dir)
@mock.patch('subprocess.Popen')
@mock.patch('os.path.isdir', return_value=True)
def test_run_ko3(self, *args):
stream = BytesIO()
stream.write(b"foo")
stream.seek(0)
wait = mock.MagicMock(side_effect=subprocess.TimeoutExpired(
cmd=FeatureTestingBase._cmd,
timeout=FeatureTestingBase._max_duration))
kill = mock.MagicMock()
attrs = {'return_value.__enter__.return_value.wait': wait,
'return_value.__enter__.return_value.kill': kill,
'return_value.__enter__.return_value.stdout': stream,
'return_value.__enter__.return_value.returncode': 0}
args[1].configure_mock(**attrs)
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run_max_duration(testcase.TestCase.EX_RUN_ERROR)
self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls)
self.assertNotIn(mock.call(self._output_file, 'r'), mopen.mock_calls)
args[1].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
wait.assert_called_once_with(timeout=FeatureTestingBase._max_duration)
kill.assert_called_once()
args[0].assert_called_once_with(self.feature.res_dir)
@mock.patch('os.path.isdir', return_value=True)
@mock.patch('subprocess.Popen')
def test_run1(self, *args):
stream = BytesIO()
stream.write(b"foo")
stream.seek(0)
attrs = {'return_value.__enter__.return_value.stdout': stream,
'return_value.__enter__.return_value.returncode': 0}
args[0].configure_mock(**attrs)
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run(testcase.TestCase.EX_OK)
self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls)
self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls)
args[0].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
args[1].assert_called_once_with(self.feature.res_dir)
@mock.patch('os.path.isdir', return_value=True)
@mock.patch('subprocess.Popen')
def test_run2(self, *args):
stream = BytesIO()
stream.write(b"foo")
stream.seek(0)
attrs = {'return_value.__enter__.return_value.stdout': stream,
'return_value.__enter__.return_value.returncode': 0}
args[0].configure_mock(**attrs)
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run_console(True, testcase.TestCase.EX_OK)
self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls)
self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls)
args[0].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
args[1].assert_called_once_with(self.feature.res_dir)
@mock.patch('os.path.isdir', return_value=True)
@mock.patch('subprocess.Popen')
def test_run3(self, *args):
stream = BytesIO()
stream.write(b"foo")
stream.seek(0)
attrs = {'return_value.__enter__.return_value.stdout': stream,
'return_value.__enter__.return_value.returncode': 0}
args[0].configure_mock(**attrs)
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run_console(False, testcase.TestCase.EX_OK)
self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls)
self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls)
args[0].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
args[1].assert_called_once_with(self.feature.res_dir)
@mock.patch('os.makedirs')
@mock.patch('os.path.isdir', return_value=False)
@mock.patch('subprocess.Popen')
def test_run4(self, *args):
stream = BytesIO()
stream.write(b"foo")
stream.seek(0)
attrs = {'return_value.__enter__.return_value.stdout': stream,
'return_value.__enter__.return_value.returncode': 0}
args[0].configure_mock(**attrs)
with mock.patch('builtins.open', mock.mock_open()) as mopen:
self._test_run_console(False, testcase.TestCase.EX_OK)
self.assertIn(mock.call(self._output_file, 'w'), mopen.mock_calls)
self.assertIn(mock.call(self._output_file, 'r'), mopen.mock_calls)
args[0].assert_called_once_with(
self._cmd, shell=True, stderr=mock.ANY, stdout=mock.ANY)
args[1].assert_called_once_with(self.feature.res_dir)
args[2].assert_called_once_with(self.feature.res_dir)
if __name__ == "__main__":
unittest.main(verbosity=2)
|