summaryrefslogtreecommitdiff
path: root/framework/tests/profile_tests.py
blob: 95f8ddafb2420cbb8bd3830b91cab96924004fb1 (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
342
343
344
345
346
347
348
349
350
351
352
353
# 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.

""" Provides test for the framework.profile modules """

from __future__ import print_function, absolute_import
import sys
import copy
import platform

import nose.tools as nt
from nose.plugins.skip import SkipTest

import framework.core as core
import framework.dmesg as dmesg
import framework.profile as profile
from framework.tests import utils
from framework import grouptools
from framework.test import GleanTest

# Don't print sys.stderr to the console
sys.stderr = sys.stdout


def test_initialize_testprofile():
    """ TestProfile initializes """
    profile.TestProfile()


@nt.raises(SystemExit)
def test_load_test_profile_no_profile():
    """ Loading a module with no profile name exits

    Beacuse load_test_profile uses test.{} to load a module we need a module in
    tests that doesn't have a profile attribute. The only module that currently
    meets that requirement is __init__.py

    """
    profile.load_test_profile('__init__')


def test_load_test_profile_returns():
    """ load_test_profile returns a TestProfile instance """
    profile_ = profile.load_test_profile('sanity')
    assert isinstance(profile_, profile.TestProfile)


def test_testprofile_default_dmesg():
    """ Dmesg initializes false """
    profile_ = profile.TestProfile()
    assert isinstance(profile_.dmesg, dmesg.DummyDmesg)


def test_testprofile_set_dmesg_true():
    """ Dmesg returns an apropriate dmesg is ste to True """
    if not platform.platform().startswith('Linux'):
        raise SkipTest('No dmesg support on this platform')
    profile_ = profile.TestProfile()
    profile_.dmesg = True
    assert isinstance(profile_.dmesg, dmesg.LinuxDmesg)


def test_testprofile_set_dmesg_false():
    """ Dmesg returns a DummyDmesg if set to False """
    if not platform.platform().startswith('Linux'):
        raise SkipTest('No dmesg support on this platform')
    profile_ = profile.TestProfile()
    profile_.dmesg = True
    profile_.dmesg = False
    assert isinstance(profile_.dmesg, dmesg.DummyDmesg)


def test_testprofile_update_test_list():
    """ TestProfile.update() updates TestProfile.test_list """
    profile1 = profile.TestProfile()
    group1 = grouptools.join('group1', 'test1')
    group2 = grouptools.join('group1', 'test2')

    profile1.test_list[group1] = utils.Test(['test1'])


    profile2 = profile.TestProfile()
    profile2.test_list[group1] = utils.Test(['test3'])
    profile2.test_list[group2] = utils.Test(['test2'])

    profile1.update(profile2)

    nt.assert_dict_equal(profile1.test_list, profile2.test_list)


class TestPrepareTestListMatches(object):
    """Create tests for TestProfile.prepare_test_list filtering"""
    def __init__(self):
        self.data = {
            grouptools.join('group1', 'test1'): 'thingy',
            grouptools.join('group1', 'group3', 'test2'): 'thing',
            grouptools.join('group3', 'test5'): 'other',
            grouptools.join('group4', 'Test9'): 'is_caps',
        }

    def test_matches_filter_mar_1(self):
        """TestProfile.prepare_test_list: 'not env.filter or
        matches_any_regex() env.filter is False

        Nothing should be filtered.

        """
        env = core.Options()

        profile_ = profile.TestProfile()
        profile_.test_list = self.data
        profile_._prepare_test_list(env)

        nt.assert_dict_equal(profile_.test_list, self.data)

    def test_matches_filter_mar_2(self):
        """TestProfile.prepare_test_list: 'not env.filter or matches_any_regex()
        mar is False

        """
        env = core.Options(include_filter=['test5'])

        profile_ = profile.TestProfile()
        profile_.test_list = self.data
        profile_._prepare_test_list(env)

        baseline = {grouptools.join('group3', 'test5'): 'other'}

        nt.assert_dict_equal(profile_.test_list, baseline)

    def test_matches_env_exclude(self):
        """TestProfile.prepare_test_list: 'not path in env.exclude_tests"""
        env = core.Options()
        env.exclude_tests.add(grouptools.join('group3', 'test5'))

        profile_ = profile.TestProfile()
        profile_.test_list = self.data
        profile_._prepare_test_list(env)

        baseline = copy.deepcopy(self.data)
        del baseline[grouptools.join('group3', 'test5')]

        nt.assert_dict_equal(profile_.test_list, baseline)

    def test_matches_exclude_mar(self):
        """TestProfile.prepare_test_list: 'not matches_any_regexp()"""
        env = core.Options(exclude_filter=['test5'])

        profile_ = profile.TestProfile()
        profile_.test_list = self.data
        profile_._prepare_test_list(env)

        baseline = copy.deepcopy(self.data)
        del baseline[grouptools.join('group3', 'test5')]

        nt.assert_dict_equal(profile_.test_list, baseline)

    def test_matches_include_caps(self):
        """TestProfile.prepare_test_list: matches capitalized tests."""
        env = core.Options(exclude_filter=['test9'])

        profile_ = profile.TestProfile()
        profile_.test_list = self.data
        profile_._prepare_test_list(env)

        nt.assert_not_in(grouptools.join('group4', 'Test9'), profile_.test_list)


@utils.no_error
def test_testprofile_group_manager_no_name_args_eq_one():
    """TestProfile.group_manager: no name and len(args) == 1 is valid"""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo') as g:
        g(['a'])


def test_testprofile_group_manager_no_name_args_gt_one():
    """TestProfile.group_manager: no name and len(args) > 1 is valid"""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo') as g:
        g(['a', 'b'])

    nt.assert_in(grouptools.join('foo', 'a b'), prof.test_list)


@utils.no_error
def test_testprofile_group_manager_name():
    """TestProfile.group_manager: name plus len(args) > 1 is valid"""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo') as g:
        g(['a', 'b'], 'a')


def test_testprofile_group_manager_is_added():
    """TestProfile.group_manager: Tests are added to the profile"""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo') as g:
        g(['a', 'b'], 'a')

    nt.assert_in(grouptools.join('foo', 'a'), prof.test_list)


def test_testprofile_groupmanager_kwargs():
    """TestProfile.group_manager: Extra kwargs are passed to the Test."""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo') as g:
        g(['a'], run_concurrent=True)

    test = prof.test_list[grouptools.join('foo', 'a')]
    nt.assert_equal(test.run_concurrent, True)


def test_testprofile_groupmanager_default_args():
    """TestProfile.group_manager: group_manater kwargs are passed to the Test."""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo', run_concurrent=True) as g:
        g(['a'])

    test = prof.test_list[grouptools.join('foo', 'a')]
    nt.assert_equal(test.run_concurrent, True)


def test_testprofile_groupmanager_kwargs_overwrite():
    """TestProfile.group_manager: default_args are overwritten by kwargs."""
    prof = profile.TestProfile()
    with prof.group_manager(utils.Test, 'foo', run_concurrent=True) as g:
        g(['a'], run_concurrent=False)

    test = prof.test_list[grouptools.join('foo', 'a')]
    nt.assert_equal(test.run_concurrent, False)


def test_testprofile_groupmanager_name_str():
    """TestProfile.group_manager: if args is a string it is not joined."""
    prof = profile.TestProfile()
    # Yes, this is really about supporting gleantest anyway.
    with prof.group_manager(GleanTest, 'foo') as g:
        g('abc')

    nt.ok_(grouptools.join('foo', 'abc') in prof.test_list)


@nt.raises(profile.TestDictError)
def test_testdict_key_not_string():
    """TestDict: If key value isn't a string an exception is raised.

    This throws a few different things at the key value and expects an error to
    be raised. It isn't a perfect test, but it was the best I could come up
    with.

    """
    test = profile.TestDict()

    for x in [None, utils.Test(['foo']), ['a'], {'a': 1}]:
        test[x] = utils.Test(['foo'])


@nt.raises(profile.TestDictError)
def test_testdict_value_not_valid():
    """TestDict: If the value isn't a Tree, Test, or None an exception is raised.

    Like the key test this isn't perfect, but it does try the common mistakes.

    """
    test = profile.TestDict()

    for x in [{}, 'a']:
        test['foo'] = x


@nt.raises(profile.TestDictError)
def test_testdict_reassignment():
    """TestDict: reassigning a key raises an exception."""
    test = profile.TestDict()
    test['foo'] = utils.Test(['foo'])
    test['foo'] = utils.Test(['foo', 'bar'])


@nt.raises(profile.TestDictError)
def test_testdict_reassignment_lower():
    """TestDict: reassigning a key raises an exception (capitalization is ignored)."""
    test = profile.TestDict()
    test['foo'] = utils.Test(['foo'])
    test['Foo'] = utils.Test(['foo', 'bar'])


def test_testdict_allow_reassignment():
    """TestDict: allow_reassignment works."""
    test = profile.TestDict()
    test['a'] = utils.Test(['foo'])
    with test.allow_reassignment:
        test['a'] = utils.Test(['bar'])

    nt.ok_(test['a'].command == ['bar'])


def test_testprofile_allow_reassignment():
    """TestProfile: allow_reassignment wrapper works."""
    prof = profile.TestProfile()
    prof.test_list['a'] = utils.Test(['foo'])
    with prof.allow_reassignment:
        prof.test_list['a'] = utils.Test(['bar'])

    nt.ok_(prof.test_list['a'].command == ['bar'])


def test_testprofile_allow_reassignment_with_groupmanager():
    """TestProfile: allow_reassignment wrapper works with groupmanager."""
    testname = grouptools.join('a', 'b')
    prof = profile.TestProfile()
    prof.test_list[testname] = utils.Test(['foo'])
    with prof.allow_reassignment:
        with prof.group_manager(utils.Test, 'a') as g:
            g(['bar'], 'b')

    nt.ok_(prof.test_list[testname].command == ['bar'])


@utils.no_error
def test_testprofile_allow_reassignemnt_stacked():
    """profile.TestDict.allow_reassignment: check stacking cornercase.

    There is an odd corner case in the original (obvious) implmentation of this
    function, If one opens two context managers and then returns from the inner
    one assignment will not be allowed, even though one is still inside the
    first context manager.

    """
    test = profile.TestDict()
    test['a'] = utils.Test(['foo'])
    with test.allow_reassignment:
        with test.allow_reassignment:
            pass
        test['a'] = utils.Test(['bar'])

    nt.ok_(test['a'].command == ['bar'])