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