Skip to content

Commit

Permalink
Add platform markers for the tests (apache#38173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis authored and utkarsharma2 committed Apr 22, 2024
1 parent 6af4093 commit b8b8dc8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
12 changes: 12 additions & 0 deletions contributing-docs/testing/unit_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,18 @@ This prepares airflow .whl package in the dist folder.
breeze --use-airflow-version wheel --use-packages-from-dist --mount-sources skip
Other Settings
--------------

Skip test on unsupported platform / environment
...............................................

You can apply the marker ``pytest.mark.platform(name)`` to the specific test case, class or module
for prevent to run on unsupported platform.

- ``linux``: Run test only on linux platform
- ``breeze``: Run test only inside of Breeze container, it might be useful in case of run
some potential dangerous things in tests or if it expects to use common Breeze things.

Code Coverage
-------------
Expand Down
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def pytest_configure(config):
config.addinivalue_line("markers", "integration(name): mark test to run with named integration")
config.addinivalue_line("markers", "backend(name): mark test to run with named backend")
config.addinivalue_line("markers", "system(name): mark test to run with named system")
config.addinivalue_line("markers", "platform(name): mark test to run with specific platform/environment")
config.addinivalue_line("markers", "long_running: mark test that run for a long time (many minutes)")
config.addinivalue_line(
"markers", "quarantined: mark test that are in quarantine (i.e. flaky, need to be isolated and fixed)"
Expand Down Expand Up @@ -396,6 +397,26 @@ def skip_if_not_marked_with_backend(selected_backend, item):
)


def skip_if_platform_doesnt_match(marker):
allowed_platforms = ("linux", "breeze")
if not (args := marker.args):
pytest.fail(f"No platform specified, expected one of: {', '.join(map(repr, allowed_platforms))}")
elif not all(a in allowed_platforms for a in args):
pytest.fail(
f"Allowed platforms {', '.join(map(repr, allowed_platforms))}; "
f"but got: {', '.join(map(repr, args))}"
)
if "linux" in args:
if not sys.platform.startswith("linux"):
pytest.skip("Test expected to run on Linux platform.")
if "breeze" in args:
if not os.path.isfile("/.dockerenv") or os.environ.get("BREEZE", "").lower() != "true":
raise pytest.skip(
"Test expected to run into Airflow Breeze container. "
"Maybe because it is to dangerous to run it outside."
)


def skip_if_not_marked_with_system(selected_systems, item):
for marker in item.iter_markers(name="system"):
systems_name = marker.args[0]
Expand Down Expand Up @@ -539,6 +560,8 @@ def pytest_runtest_setup(item):
skip_if_not_marked_with_system(selected_systems_list, item)
else:
skip_system_test(item)
for marker in item.iter_markers(name="platform"):
skip_if_platform_doesnt_match(marker)
for marker in item.iter_markers(name="backend"):
skip_if_wrong_backend(marker, item)
selected_backend = item.config.option.backend
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_impersonation_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

# The entire module into the quarantined mark, this might have unpredictable side effects to other tests
# and should be moved into the isolated environment into the future.
pytestmark = [pytest.mark.db_test, pytest.mark.quarantined]
pytestmark = [pytest.mark.platform("breeze"), pytest.mark.db_test, pytest.mark.quarantined]

DEV_NULL = "/dev/null"
TEST_ROOT_FOLDER = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
Expand Down

0 comments on commit b8b8dc8

Please sign in to comment.