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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# Copyright (c) 2014 Intel Corporation
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
""" Tests for the exectest module """
from __future__ import print_function, absolute_import
import mock
import nose.tools as nt
from nose.plugins.attrib import attr
from . import utils
from framework.test.base import (
Test,
TestRunError,
ValgrindMixin,
WindowResizeMixin,
)
from .status_tests import PROBLEMS, STATUSES
from framework.options import _Options as Options
# pylint: disable=invalid-name
# Helpers
class TestTest(Test):
""" A class for testing that implements a dummy interpret_result
interpret_result() can ve overwritten by setting the
self.test_interpret_result name
"""
test_interpret_result = lambda: None
def interpret_result(self):
self.test_interpret_result()
# Tests
def test_run_return_early():
""" Test.run() exits early when Test._run_command() has exception """
def helper():
raise utils.TestFailure("The test didn't return early")
# Of course, this won't work if you actually have a foobarcommand in your
# path...
test = TestTest(['foobaroinkboink_zing'])
test.test_interpret_result = helper
test.run()
@attr('slow')
def test_timeout():
"""test.base.Test.run(): kills tests that exceed timeout when set"""
utils.binary_check('sleep', 1)
class _Test(Test):
def interpret_result(self):
super(_Test, self).interpret_result()
test = _Test(['sleep', '60'])
test.timeout = 1
test.run()
nt.eq_(test.result.result, 'timeout')
@attr('slow')
def test_timeout_pass():
"""test.base.Test.run(): Result is returned when timeout is set but not exceeded
"""
utils.binary_check('true')
def helper():
if (test.result.returncode == 0):
test.result.result = "pass"
test = TestTest(['true'])
test.test_interpret_result = helper
test.timeout = 1
test.run()
nt.eq_(test.result.result, 'pass')
def test_WindowResizeMixin_rerun():
"""test.base.WindowResizeMixin: runs multiple when spurious resize detected
"""
# Because of Python's inheritance order we need the mixin here.
class Mixin(object):
def __init__(self, *args, **kwargs):
super(Mixin, self).__init__(*args, **kwargs)
self.__return_spurious = True
def _run_command(self):
self.result.returncode = None
# IF this is run only once we'll have "got spurious window resize"
# in result.out, if it runs multiple times we'll get 'all good'
if self.__return_spurious:
self.result.out = "Got spurious window resize"
self.__return_spurious = False
else:
self.result.out = 'all good'
class Test_(WindowResizeMixin, Mixin, Test):
def interpret_result(self):
pass
test = Test_(['foo'])
test.run()
nt.assert_equal(test.result.out, 'all good')
def test_run_command_early():
"""test.base.Test.run(): returns early if there is an error in _run_command()
"""
class Test_(Test):
def interpret_result(self):
raise utils.TestFailure("The test didn't return early")
def _run_command(self):
raise TestRunError('an error', 'skip')
# Of course, if there is an executable 'foobarboinkoink' in your path this
# test will fail. It seems pretty unlikely that you would
test = Test_(['foobarboinkoink'])
test.run()
@nt.raises(AssertionError)
def test_no_string():
"""test.base.Test.__init__: Asserts if it is passed a string instead of a list"""
TestTest('foo')
def test_mutation():
"""test.base.Test.command: does not mutate the value it was provided
There is a very subtle bug in all.py that causes the key values to be
changed before they are assigned in some cases. This is because the right
side of an assignment is evalated before the left side, so
>>> profile = {}
>>> args = ['a', 'b']
>>> profile[' '.join(args)] = PiglitGLTest(args)
>>> profile.keys()
['bin/a b']
"""
class _Test(TestTest):
def __init__(self, *args, **kwargs):
super(_Test, self).__init__(*args, **kwargs)
self._command[0] = 'bin/' + self._command[0]
args = ['a', 'b']
_Test(args)
nt.assert_list_equal(args, ['a', 'b'])
@mock.patch('framework.test.base.options.OPTIONS', new_callable=Options)
def test_ValgrindMixin_command(mock_opts):
"""test.base.ValgrindMixin.command: overrides self.command"""
class _Test(ValgrindMixin, utils.Test):
pass
mock_opts.valgrind = True
test = _Test(['foo'])
nt.eq_(test.command, ['valgrind', '--quiet', '--error-exitcode=1',
'--tool=memcheck', 'foo'])
class TestValgrindMixinRun(object):
@classmethod
def setup_class(cls):
class _NoRunTest(utils.Test):
def run(self):
self.interpret_result()
class _Test(ValgrindMixin, _NoRunTest):
pass
cls.test = _Test
@utils.nose_generator
def test_bad_valgrind_true(self):
"""Test non-pass status when options.OPTIONS.valgrind is True."""
def test(status, expected):
test = self.test(['foo'])
test.result.result = status
with mock.patch('framework.test.base.options.OPTIONS',
new_callable=Options) as mock_opts:
mock_opts.valgrind = True
test.run()
nt.eq_(test.result.result, expected)
desc = ('test.base.ValgrindMixin.run: '
'when status is "{}" it is changed to "{}"')
for status in PROBLEMS:
test.description = desc.format(status, 'skip')
yield test, status, 'skip'
@utils.nose_generator
def test_valgrind_false(self):
"""Test non-pass status when options.OPTIONS.valgrind is False."""
def test(status):
test = self.test(['foo'])
test.result.result = status
with mock.patch('framework.test.base.options.OPTIONS',
new_callable=Options) as mock_opts:
mock_opts.valgrind = False
test.run()
nt.eq_(test.result.result, status)
desc = ('test.base.ValgrindMixin.run: when status is "{}" '
'it is not changed when not running valgrind.')
for status in STATUSES:
test.description = desc.format(status)
yield test, status
@mock.patch('framework.test.base.options.OPTIONS', new_callable=Options)
def test_pass(self, mock_opts):
"""test.base.ValgrindMixin.run: when test is 'pass' and returncode is '0' result is pass
"""
test = self.test(['foo'])
mock_opts.valgrind = True
test.result.result = 'pass'
test.result.returncode = 0
test.run()
nt.eq_(test.result.result, 'pass')
@mock.patch('framework.test.base.options.OPTIONS', new_callable=Options)
def test_fallthrough(self, mock_opts):
"""test.base.ValgrindMixin.run: when a test is 'pass' but returncode is not 0 it's 'fail'
"""
test = self.test(['foo'])
mock_opts.valgrind = True
test.result.result = 'pass'
test.result.returncode = 1
test.run()
nt.eq_(test.result.result, 'fail')
def test_interpret_result_greater_zero():
"""test.base.Test.interpret_result: A test with status > 0 is fail"""
class _Test(Test):
def interpret_result(self):
super(_Test, self).interpret_result()
test = _Test(['foobar'])
test.result.returncode = 1
test.result.out = 'this is some\nstdout'
test.result.err = 'this is some\nerrors'
test.interpret_result()
nt.eq_(test.result.result, 'fail')
|