summaryrefslogtreecommitdiff
path: root/unittests/dmesg_tests.py
blob: 08e61efc50c54766306e3f6e35cdf281ff60e74f (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright (c) 2015 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 dmesg module.

This module makes extensive use of mock to avoid actually calling into dmesg,
which allows us to test all classes on all platforms, including windows.

"""

from __future__ import absolute_import, division, print_function
import re
import warnings

import mock
import nose.tools as nt

from . import utils
from framework import dmesg, status, results, exceptions

# pylint: disable=invalid-name,line-too-long,attribute-defined-outside-init


@nt.nottest
class TestDmesg(dmesg.BaseDmesg):
    """Test Dmesg class. stubs update_dmesg and __init__"""
    def update_dmesg(self, *args, **kwargs):
        pass

    def __init__(self):
        super(TestDmesg, self).__init__()
        self._new_messages = ['some', 'new', 'messages']


class TestBaseDmesg(object):
    """Tests for the BaseDmesg class."""
    @classmethod
    def setup_class(cls):
        cls.dmesg = TestDmesg()

    def setup(self):
        self.result = results.TestResult()
        self.result.dmesg = mock.sentinel.dmesg

    def test_update_result_dmesg(self):
        """dmesg.BaseDmesg.update_result: records new dmesg content in result"""
        self.dmesg.update_result(self.result)
        nt.assert_is_not(self.result.dmesg, mock.sentinel.dmesg)

    def test_update_result_status_unchanged(self):
        """dmesg.BaseDmesg.update_result: Doesn't change status it shouldn't

        Only 'pass', 'warn', and 'fail' should be changed.

        """
        failed = set()

        for stat in status.ALL:
            if stat in ['pass', 'warn', 'fail']:
                continue
            self.result.result = stat
            self.dmesg.update_result(self.result)
            if self.result.result != stat:
                failed.add(stat)

        if failed:
            raise AssertionError(
                "The following status(es) were changed which should not have "
                "been:\n"
                "{}\n".format('\n'.join(failed)))

    def test_update_result_status_changed(self):
        """dmesg.BaseDmesg.update_result: changes pass fail and warn"""
        failed = set()

        for stat in ['pass', 'warn', 'fail']:
            self.result.result = stat
            self.dmesg.update_result(self.result)
            if self.result.result == stat:
                failed.add(stat)

        if failed:
            raise AssertionError(
                "The following status(es) were not changed which should not "
                "have been:\n"
                "{}\n".format('\n'.join(failed)))

    def test_update_result_subtest_unchanged(self):
        """dmesg.BaseDmesg.update_result: Doesn't change subtests it shouldn't

        Only 'pass', 'warn', and 'fail' should be changed.

        """
        failed = set()

        for stat in status.ALL:
            if stat in ['pass', 'warn', 'fail']:
                continue
            self.result.subtests['foo'] = stat
            self.dmesg.update_result(self.result)
            if self.result.subtests['foo'] != stat:
                failed.add(stat)

        if failed:
            raise AssertionError(
                "The following status(es) were changed which should not have "
                "been:\n"
                "{}\n".format('\n'.join(failed)))

    def test_update_subtest_changed(self):
        """dmesg.BaseDmesg.update_result: changes subtests pass fail and warn"""
        failed = set()

        for stat in status.ALL:
            if stat in ['pass', 'warn', 'fail']:
                continue
            self.result.subtests['foo'] = stat
            self.dmesg.update_result(self.result)
            if self.result.subtests['foo'] != stat:
                failed.add(stat)

        if failed:
            raise AssertionError(
                "The following status(es) were changed which should not have "
                "been:\n"
                "{}\n".format('\n'.join(failed)))


def test_update_result_regex_no_match():
    """dmesg.BaseDmesg.update_result: if no regex matches dont change status"""
    dmesg_ = TestDmesg()
    dmesg_.regex = re.compile(r'nomatchforthisreally')
    result = results.TestResult('pass')
    dmesg_.update_result(result)

    nt.eq_(result.result, 'pass')


def test_update_result_regex_match():
    """dmesg.BaseDmesg.update_result: if regex matches change status"""
    dmesg_ = TestDmesg()
    dmesg_.regex = re.compile(r'.*')
    result = results.TestResult('pass')
    dmesg_.update_result(result)

    nt.assert_not_equal(result.result, 'pass')


@utils.nose_generator
def test_update_result_specific():
    """Generator that tests specific result mappings."""
    dmesg_ = TestDmesg()
    tests = [
        ('pass', 'dmesg-warn'),
        ('warn', 'dmesg-fail'),
        ('fail', 'dmesg-fail'),
    ]
    description = 'dmesg.BaseDmesg.update_result: replaces {} with {}'

    def test(initial, expected):
        result = results.TestResult(initial)
        dmesg_.update_result(result)
        nt.eq_(result.result, expected)

    for initial, expected in tests:
        test.description = description.format(initial, expected)
        yield test, initial, expected


@utils.nose_generator
def test_linuxdmesg_gzip_errors():
    """Generator to test exceptions that need to be passed when reading
    config.gz.

    """
    exceptions_ = {
        OSError,
        IOError,
    }
    description = "dmesg.LinuxDmesg: Doesn't stop on {} when reading gzip."

    @mock.patch('framework.dmesg.LinuxDmesg.update_dmesg', mock.Mock())
    def test(exception):
        try:
            with mock.patch('framework.dmesg.gzip.open',
                            mock.Mock(side_effect=exception)):
                with warnings.catch_warnings():
                    warnings.simplefilter('error')
                    dmesg.LinuxDmesg()
        except (exceptions.PiglitFatalError, RuntimeWarning):
            pass

    for exception in exceptions_:
        test.description = description.format(exception.__name__)
        yield test, exception


@nt.raises(exceptions.PiglitFatalError)
@mock.patch('framework.dmesg.gzip.open', mock.Mock(side_effect=IOError))
def test_linuxdmesg_timestamp():
    """dmesg.LinuxDmesg: If timestamps are not detected raise"""
    with mock.patch('framework.dmesg.subprocess.check_output',
                    mock.Mock(return_value='foo\nbar\n')):
        with warnings.catch_warnings():
            warnings.simplefilter('error')
            dmesg.LinuxDmesg()


@nt.raises(RuntimeWarning)
@mock.patch('framework.dmesg.gzip.open', mock.Mock(side_effect=IOError))
def test_linuxdmesg_warn():
    """dmesg.LinuxDmesg: Warn if timestamp support is uncheckable"""
    with mock.patch('framework.dmesg.LinuxDmesg.update_dmesg', mock.Mock()):
        with warnings.catch_warnings():
            warnings.simplefilter('error')
            dmesg.LinuxDmesg()


def test_linuxdmesg_update_dmesg_update():
    """dmesg.LinuxDmesg.update_dmesg: calculate dmesg changes correctly with changes"""
    result = results.TestResult('pass')

    with mock.patch('framework.dmesg.subprocess.check_output',
                    mock.Mock(return_value='[1.0]this')):
        dmesg_ = dmesg.LinuxDmesg()

    with mock.patch('framework.dmesg.subprocess.check_output',
                    mock.Mock(return_value='[1.0]this\n[2.0]is\n[3.0]dmesg\n')):
        dmesg_.update_result(result)

    nt.eq_(result.dmesg, '[2.0]is\n[3.0]dmesg')


def test_linuxdmesg_update_dmesg_update_no_change():
    """dmesg.LinuxDmesg.update_dmesg: calculate dmesg changes correctly with no changes"""
    result = results.TestResult('pass')
    result.dmesg = mock.sentinel.dmesg

    with mock.patch('framework.dmesg.subprocess.check_output',
                    mock.Mock(return_value='[1.0]this')):
        dmesg_ = dmesg.LinuxDmesg()
        dmesg_.update_result(result)

    nt.eq_(result.dmesg, mock.sentinel.dmesg)


def test_dummydmesg_uupate_result():
    """dmesg.DummyDmesg.update_result: returns result unmodified"""
    dmesg_ = dmesg.DummyDmesg()
    result = mock.MagicMock(spec=results.TestResult())
    result.dmesg = mock.sentinel.dmesg
    result.result = mock.sentinel.result
    dmesg_.update_result(result)

    nt.eq_(result.dmesg, mock.sentinel.dmesg)
    nt.eq_(result.result, mock.sentinel.result)


@utils.nose_generator
def test_get_dmesg():
    """Generate tests for get_dmesg."""
    tests = [
        ('linux', dmesg.LinuxDmesg),
        # There is no dmesg on windows, thus it will always get the dummy
        ('win32', dmesg.DummyDmesg),
    ]
    description = 'dmesg.get_dmesg: returns correct class when platform is {}'

    def test(platform, class_):
        with mock.patch('framework.dmesg.sys.platform', platform):
            ret = dmesg.get_dmesg()
        nt.assert_is_instance(ret, class_)

    for platform, class_ in tests:
        test.description = description.format(platform)
        yield test, platform, class_


def test_get_dmesg_dummy():
    """dmesg.get_dmesg: when not_dummy=False a dummy is provided"""
    # Linux was selected since it would normally return LinuxDmesg
    with mock.patch('framework.dmesg.sys.platform', 'linux'):
        ret = dmesg.get_dmesg(False)
    nt.assert_is_instance(ret, dmesg.DummyDmesg)


def test_partial_wrap():
    """dmesg.LinuxDmesg.update_dmesg: correctly handles partial wrap

    Since dmesg is a ringbuffer (at least on Linux) it can roll over, and we
    need to ensure that we're handling that correctly.

    """
    result = results.TestResult()

    mock_out = mock.Mock(return_value='[1.0]This\n[2.0]is\n[3.0]dmesg')
    with mock.patch('framework.dmesg.subprocess.check_output', mock_out):
        test = dmesg.LinuxDmesg()

    mock_out.return_value = '[3.0]dmesg\n[4.0]whoo!'
    with mock.patch('framework.dmesg.subprocess.check_output', mock_out):
        test.update_result(result)

    nt.eq_(result.dmesg, '[4.0]whoo!')


def test_complete_wrap():
    """dmesg.LinuxDmesg.update_dmesg: correctly handles complete wrap

    Since dmesg is a ringbuffer (at least on Linux) it can roll over, and we
    need to ensure that we're handling that correctly.

    """
    result = results.TestResult()

    mock_out = mock.Mock(return_value='[1.0]This\n[2.0]is\n[3.0]dmesg')
    with mock.patch('framework.dmesg.subprocess.check_output', mock_out):
        test = dmesg.LinuxDmesg()

    mock_out.return_value = '[4.0]whoo!\n[5.0]doggy'
    with mock.patch('framework.dmesg.subprocess.check_output', mock_out):
        test.update_result(result)

    nt.eq_(result.dmesg, '[4.0]whoo!\n[5.0]doggy')