summaryrefslogtreecommitdiff
path: root/unittests/framework/test_profile.py
blob: 99403ccaf05c5039390001f19de26696129265cb (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
# Copyright (c) 2014, 2016 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 (
    absolute_import, division, print_function, unicode_literals
)

import pytest
import six

from framework import exceptions
from framework import grouptools
from framework import profile
from framework.test.gleantest import GleanTest
from . import utils

# pylint: disable=invalid-name,no-self-use,protected-access


class TestLoadTestProfile(object):
    """Tests for profile.load_test_profile."""

    def test_no_profile_attribute(self, tmpdir):
        """profile.load_test_profile: Loading a module with no profile name
        exits.

        Because 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
        """
        p = tmpdir.join('foo.profile')

        with pytest.raises(exceptions.PiglitFatalError):
            profile.load_test_profile(six.text_type(p))

    def test_no_such_module(self, tmpdir):
        """profile.load_test_profile: Trying to load a non-existent module
        exits.
        """
        # Ensure that the module will not exist by moving to an empty temporary
        # directory
        tmpdir.chdir()
        with pytest.raises(exceptions.PiglitFatalError):
            profile.load_test_profile('this_module_will_never_ever_exist')

    def test_return_type(self):
        """profile.load_test_profile: returns a TestProfile instance."""
        assert isinstance(profile.load_test_profile('sanity'),
                          profile.TestProfile)


class TestTestProfile(object):
    """Tests for profile.TestProfile."""

    class TestCopy(object):
        """Tests for the copy method."""

        @pytest.fixture
        def fixture(self):
            orig = profile.TestProfile()
            orig.test_list['foo'] = utils.Test(['foo'])
            orig.test_list['bar'] = utils.Test(['bar'])
            orig.filters = [lambda name, _: name != 'bar']
            orig.forced_test_list = ['foo']
            return orig

        def test_filters(self, fixture):
            """The filters attribute is copied correctly."""
            new = fixture.copy()

            # Assert that the fixtures are equivalent, but not the same
            assert fixture.filters == new.filters
            assert fixture.filters is not new.filters

            # And double check by modifying one of them and asserting that the
            # other has not changed.
            new.filters.append(lambda name, _: name != 'oink')
            assert len(fixture.filters) == 1

        def test_forced_test_list(self, fixture):
            """The forced_test_list attribute is copied correctly."""
            new = fixture.copy()

            # Assert that the fixtures are equivalent, but not the same
            assert fixture.forced_test_list == new.forced_test_list
            assert fixture.forced_test_list is not new.forced_test_list

            # And double check by modifying one of them and asserting that the
            # other has not changed.
            del new.forced_test_list[0]
            assert fixture.forced_test_list[0] == 'foo'

        def test_test_list(self, fixture):
            """The test_list attribute is copied correctly."""
            new = fixture.copy()

            # Assert that the fixtures are equivalent, but not the same
            assert fixture.test_list == new.test_list
            assert fixture.test_list is not new.test_list


class TestTestDict(object):
    """Tests for the TestDict object."""

    test = None

    def setup(self):
        self.test = profile.TestDict()

    @pytest.mark.parametrize(
        "arg",
        [None, utils.Test(['foo']), ['a'], {'a': 1}],
        ids=six.text_type)
    def test_key_not_string(self, arg):
        """profile.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.
        """
        with pytest.raises(exceptions.PiglitFatalError):
            self.test[arg] = utils.Test(['foo'])

    def test_reassignment(self):
        """reassigning a key raises an exception."""
        self.test['foo'] = utils.Test(['foo'])
        with pytest.raises(exceptions.PiglitFatalError):
            self.test['foo'] = utils.Test(['foo', 'bar'])

    class TestAllowReassignment(object):
        """Tests for TestDict.allow_reassignment."""

        @pytest.fixture
        def test(self):
            return profile.TestDict()

        def test_case_insensitve(self, test):
            """reassigning a key raises an exception (capitalization is
            ignored)."""
            test['foo'] = utils.Test(['foo'])
            with pytest.raises(exceptions.PiglitFatalError):
                test['Foo'] = utils.Test(['foo', 'bar'])

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

            assert test['a'].command == ['bar']

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

            assert test[testname].command == ['bar']

        def test_stacked(self, test):
            """profile.profile.TestDict.allow_reassignment: check stacking
            cornercase.

            There is an odd corner case in the original (obvious)
            implementation 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['a'] = utils.Test(['foo'])
            with test.allow_reassignment:
                with test.allow_reassignment:
                    pass
                test['a'] = utils.Test(['bar'])

            assert test['a'].command == ['bar']

    class TestGroupManager(object):
        """Test the group_manager method."""

        @pytest.fixture
        def inst(self):
            return profile.TestDict()

        def test_no_name_args_eq_one(self, inst):
            """no name and len(args) == 1 is valid."""
            with inst.group_manager(utils.Test, 'foo') as g:
                g(['a'])

            assert grouptools.join('foo', 'a') in inst

        def test_no_name_args_gt_one(self, inst):
            """no name and len(args) > 1 is valid."""
            with inst.group_manager(utils.Test, 'foo') as g:
                g(['a', 'b'])

            assert grouptools.join('foo', 'a b') in inst

        def test_name(self, inst):
            """name plus len(args) > 1 is valid."""
            with inst.group_manager(utils.Test, 'foo') as g:
                g(['a', 'b'], 'a')

            assert grouptools.join('foo', 'a') in inst

        def test_adder_kwargs_passed(self, inst):
            """Extra kwargs passed to the adder function are passed to the
            Test.
            """
            with inst.group_manager(utils.Test, 'foo') as g:
                g(['a'], run_concurrent=True)
            test = inst[grouptools.join('foo', 'a')]

            assert test.run_concurrent is True

        def test_context_manager_keyword_args_passed(self, inst):
            """kwargs passed to the context_manager are passed to the Test."""
            with inst.group_manager(
                    utils.Test, 'foo', run_concurrent=True) as g:  # pylint: disable=bad-continuation
                    # This is a pylint bug, there's an upstream report
                g(['a'])
            test = inst[grouptools.join('foo', 'a')]

            assert test.run_concurrent is True

        def test_adder_kwargs_overwrite_context_manager_kwargs(self, inst):
            """default_args are overwritten by kwargs."""
            with inst.group_manager(
                    utils.Test, 'foo', run_concurrent=True) as g:  # pylint: disable=bad-continuation
                    # This is a pylint bug, there's an upstream report
                g(['a'], run_concurrent=False)

            test = inst[grouptools.join('foo', 'a')]
            assert test.run_concurrent is False

        def test_name_as_str(self, inst):
            """if args is a string it is not joined.

            This is a "feature" of glean, and no longer needs to be protected
            whenever glean dies.
            """
            with inst.group_manager(GleanTest, 'foo') as g:
                g('abc')

            assert grouptools.join('foo', 'abc') in inst


class TestRegexFilter(object):
    """Tests for the RegexFilter class."""

    class TestNormal(object):
        """Tests for inverse set to False (default)."""

        def test_empty(self):
            """Returns True when no filters are provided."""
            test = profile.RegexFilter([])
            assert test('foobob', None)

        def test_matches(self):
            """Returns True when the test matches any regex."""
            test = profile.RegexFilter([r'foo', r'bar'])
            assert test('foobob', None)

        def test_not_matches(self):
            """Returns True when the test matches any regex."""
            test = profile.RegexFilter([r'fob', r'bar'])
            assert not test('foobob', None)

    class TestInverse(object):
        """Tests for inverse set to True."""

        def test_empty(self):
            """Returns True when no filters are provided."""
            test = profile.RegexFilter([], inverse=True)
            assert test('foobob', None)

        def test_matches(self):
            """Returns False when the test matches any regex."""
            test = profile.RegexFilter([r'foo', r'bar'], inverse=True)
            assert not test('foobob', None)

        def test_not_matches(self):
            """Returns False when the test matches any regex."""
            test = profile.RegexFilter([r'fob', r'bar'], inverse=True)
            assert test('foobob', None)