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