Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Configuration and fixtures for pytest. |
| 7 | |
| 8 | See the following doc link for an explanation of conftest.py and how it is used |
| 9 | by pytest: |
| 10 | https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions |
| 11 | """ |
| 12 | |
Chris McDonald | 05511c9 | 2020-05-01 16:45:56 -0600 | [diff] [blame] | 13 | from __future__ import division |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 14 | from __future__ import print_function |
| 15 | |
Chris McDonald | ffe2a41 | 2020-04-15 02:27:25 -0600 | [diff] [blame] | 16 | import multiprocessing |
| 17 | |
Chris McDonald | ee2fbda | 2020-04-30 18:10:01 -0600 | [diff] [blame] | 18 | import pytest |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 19 | |
Chris McDonald | 05511c9 | 2020-05-01 16:45:56 -0600 | [diff] [blame] | 20 | import chromite as cr |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 21 | from chromite.lib import cidb |
Chris McDonald | 320ef42 | 2020-04-18 04:57:04 -0600 | [diff] [blame] | 22 | from chromite.lib import parallel |
Chris McDonald | 05511c9 | 2020-05-01 16:45:56 -0600 | [diff] [blame] | 23 | from chromite.lib import portage_util |
Chris McDonald | 6ce0096 | 2020-04-14 04:05:21 -0600 | [diff] [blame] | 24 | from chromite.lib import retry_stats |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 25 | |
Chris McDonald | ee2fbda | 2020-04-30 18:10:01 -0600 | [diff] [blame] | 26 | # We use wildcard imports here to make fixtures defined in the test module |
| 27 | # globally visible. |
| 28 | # pylint: disable=unused-wildcard-import, wildcard-import |
| 29 | |
| 30 | # Importing from *_fixtures.py files into conftest.py is the only time a |
| 31 | # module should use a wildcard import. *_fixtures.py files should ensure |
| 32 | # that the only items visible to a wildcard import are pytest fixtures, |
| 33 | # usually by declaring __all__ if necessary. |
| 34 | from chromite.test.portage_fixtures import * |
| 35 | |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 36 | |
Chris McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 37 | @pytest.fixture(scope='class', autouse=True) |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 38 | def mock_cidb_connection(): |
| 39 | """Ensure that the CIDB connection factory is initialized as a mock. |
| 40 | |
| 41 | Unit tests should never connect to any live instances of CIDB and this |
| 42 | initialization ensures that they only ever get a mock connection instance. |
| 43 | |
| 44 | Previously cros_test_lib.TestProgram.runTests was responsible for globally |
| 45 | initializing this mock and multiple tests are flaky if this mock connection |
| 46 | is not initialized before any tests are run. |
| 47 | """ |
Chris McDonald | e53dde1 | 2020-04-07 13:43:14 -0600 | [diff] [blame] | 48 | # pylint: disable=protected-access |
| 49 | cidb.CIDBConnectionFactory._ClearCIDBSetup() |
Chris McDonald | 2e9a09c | 2020-04-03 16:09:32 -0600 | [diff] [blame] | 50 | cidb.CIDBConnectionFactory.SetupMockCidb() |
Chris McDonald | ffe2a41 | 2020-04-15 02:27:25 -0600 | [diff] [blame] | 51 | |
| 52 | |
| 53 | @pytest.fixture(scope='class', autouse=True) |
| 54 | def assert_no_zombies(): |
| 55 | """Assert that tests have no active child processes after completion. |
| 56 | |
| 57 | This assertion runs after class tearDown methods because of the scope='class' |
| 58 | declaration. |
| 59 | """ |
| 60 | yield |
| 61 | children = multiprocessing.active_children() |
| 62 | if children: |
| 63 | pytest.fail('Test has %s active child processes after tearDown: %s' % |
| 64 | (len(children), children)) |
Chris McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame] | 65 | |
| 66 | |
Chris McDonald | 6ce0096 | 2020-04-14 04:05:21 -0600 | [diff] [blame] | 67 | @pytest.fixture(scope='class', autouse=True) |
| 68 | def clear_retry_stats_manager(): |
| 69 | """Reset the global state of the stats manager before every test. |
| 70 | |
| 71 | Without this fixture many tests fail due to this global value being set and |
| 72 | then not cleared. The child manager process may have been killed but this |
| 73 | module level variable is still pointing at it, leading to the test trying to |
| 74 | write to a closed pipe. |
| 75 | """ |
| 76 | # pylint: disable=protected-access |
| 77 | retry_stats._STATS_COLLECTION = None |
| 78 | |
| 79 | |
Chris McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame] | 80 | @pytest.fixture |
Chris McDonald | 320ef42 | 2020-04-18 04:57:04 -0600 | [diff] [blame] | 81 | def singleton_manager(monkeypatch): |
| 82 | """Force tests to use a singleton Manager and automatically clean it up.""" |
| 83 | m = parallel.Manager() |
| 84 | |
| 85 | def our_manager(): |
| 86 | return m |
| 87 | |
| 88 | monkeypatch.setattr(parallel, 'Manager', our_manager) |
| 89 | yield |
| 90 | m.shutdown() |
| 91 | |
| 92 | |
| 93 | @pytest.fixture |
Chris McDonald | e13f024 | 2020-04-07 18:37:15 -0600 | [diff] [blame] | 94 | def legacy_capture_output(request, capfd): |
| 95 | """Adds the `capfd` fixture to TestCase-style test classes. |
| 96 | |
| 97 | This fixture should only be used on cros_test_lib.TestCase test classes, since |
| 98 | it doesn't yield anything and just attaches the built-in pytest `capfd` |
| 99 | fixture to the requesting class. Tests written as standalone functions should |
| 100 | use pytest's built-in `capfd` fixture instead of this. See the documentation |
| 101 | for more information on how to use the `capfd` fixture that this provides: |
| 102 | https://docs.pytest.org/en/latest/reference.html#capfd |
| 103 | |
| 104 | See the following documentation for an explanation of why fixtures have to be |
| 105 | provided to TestCase classes in this manner: |
| 106 | https://docs.pytest.org/en/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks |
| 107 | """ |
| 108 | request.cls.capfd = capfd |
Chris McDonald | 6ce0096 | 2020-04-14 04:05:21 -0600 | [diff] [blame] | 109 | |
| 110 | |
| 111 | @pytest.fixture |
| 112 | def testcase_caplog(request, caplog): |
| 113 | """Adds the `caplog` fixture to TestCase-style test classes. |
| 114 | |
| 115 | This fixture should only be used on cros_test_lib.TestCase test classes, since |
| 116 | it doesn't yield anything and just attaches the built-in pytest `caplog` |
| 117 | fixture to the requesting class. Tests written as standalone functions should |
| 118 | use pytest's built-in `caplog` fixture instead of this. See the documentation |
| 119 | for more information on how to use the `caplog` fixture that this provides: |
| 120 | https://docs.pytest.org/en/latest/reference.html#caplog |
| 121 | |
| 122 | See the following documentation for an explanation of why fixtures have to be |
| 123 | provided to TestCase classes in this manner: |
| 124 | https://docs.pytest.org/en/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks |
| 125 | """ |
| 126 | request.cls.caplog = caplog |
Chris McDonald | 05511c9 | 2020-05-01 16:45:56 -0600 | [diff] [blame] | 127 | |
| 128 | |
Michael Mortensen | de716a1 | 2020-05-15 11:27:00 -0600 | [diff] [blame] | 129 | @pytest.fixture |
| 130 | def testcase_monkeypatch(request, monkeypatch): |
| 131 | """Adds the `monkeypatch` fixture to TestCase-style test classes. |
| 132 | |
| 133 | This fixture should only be used on cros_test_lib.TestCase test classes, since |
| 134 | it doesn't yield anything and just attaches the built-in pytest `monkeypatch` |
| 135 | fixture to the requesting class. Tests written as standalone functions should |
| 136 | use pytest's built-in `monkeypatch` fixture instead of this. See the |
| 137 | documentation for more information on how to use the `monkeypatch` fixture |
| 138 | that this provides: |
| 139 | https://docs.pytest.org/en/latest/reference.html#monkeypatch |
| 140 | |
| 141 | See the following documentation for an explanation of why fixtures have to be |
| 142 | provided to TestCase classes in this manner: |
| 143 | https://docs.pytest.org/en/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks |
| 144 | """ |
| 145 | request.cls.monkeypatch = monkeypatch |
| 146 | |
| 147 | |
Chris McDonald | 05511c9 | 2020-05-01 16:45:56 -0600 | [diff] [blame] | 148 | def pytest_assertrepr_compare(op, left, right): |
| 149 | """Global hook for defining detailed explanations for failed assertions. |
| 150 | |
| 151 | https://docs.pytest.org/en/latest/assert.html#defining-your-own-explanation-for-failed-assertions |
| 152 | """ |
| 153 | if isinstance(left, portage_util.CPV) and isinstance( |
| 154 | right, cr.test.Overlay) and op == 'in': |
| 155 | package_path = right.path / left.category / left.package |
| 156 | return [ |
| 157 | f'{left.pv}.ebuild exists in {right.path}', |
| 158 | 'Ebuild does not exist in overlay.', |
| 159 | 'Ebuilds found in overlay with same category and package:' |
| 160 | ] + sorted('\t' + str(p.relative_to(package_path)) |
| 161 | for p in package_path.glob('*.ebuild')) |
Chris McDonald | cdfd113 | 2020-05-12 07:09:51 -0600 | [diff] [blame] | 162 | |
| 163 | |
| 164 | def pytest_addoption(parser): |
| 165 | """Adds additional options to the default pytest CLI args.""" |
| 166 | parser.addoption( |
| 167 | '--no-chroot', |
| 168 | dest='chroot', |
| 169 | action='store_false', |
| 170 | help='Skip any tests that require a chroot to function.', |
| 171 | ) |
| 172 | |
| 173 | |
| 174 | def pytest_collection_modifyitems(config, items): |
| 175 | """Modifies the list of test items pytest has collected. |
| 176 | |
| 177 | See the following link for full documentation on pytest collection hooks: |
| 178 | https://docs.pytest.org/en/latest/reference.html?highlight=pytest_collection_modifyitems#collection-hooks |
| 179 | """ |
| 180 | if config.option.chroot: |
| 181 | return |
| 182 | skip_inside_only = pytest.mark.skip(reason='Test requires a chroot to run.') |
| 183 | for item in items: |
| 184 | if 'inside_only' in item.keywords: |
| 185 | item.add_marker(skip_inside_only) |