diff --git a/.gitlab-ci/container/debian/x86_build.sh b/.gitlab-ci/container/debian/x86_build.sh index 60055ea71db..cc39d3e1a61 100644 --- a/.gitlab-ci/container/debian/x86_build.sh +++ b/.gitlab-ci/container/debian/x86_build.sh @@ -92,7 +92,7 @@ ninja install popd rm -rf DirectX-Headers -python3 -m pip install -r ${CI_PROJECT_DIR}/.gitlab-ci/lava/requirements.txt +python3 -m pip install -r .gitlab-ci/lava/requirements.txt # install bindgen RUSTFLAGS='-L native=/usr/local/lib' cargo install \ diff --git a/.gitlab-ci/lava/requirements-test.txt b/.gitlab-ci/lava/requirements-test.txt index a45d2917962..0ff561db901 100644 --- a/.gitlab-ci/lava/requirements-test.txt +++ b/.gitlab-ci/lava/requirements-test.txt @@ -1,5 +1,6 @@ -r requirements.txt freezegun==1.1.0 +hypothesis==6.67.1 pytest==7.2.1 pytest-cov==3.0.0 PyYAML==5.3.1 diff --git a/.gitlab-ci/lava/utils/lava_farm.py b/.gitlab-ci/lava/utils/lava_farm.py new file mode 100644 index 00000000000..dfd51ab9b92 --- /dev/null +++ b/.gitlab-ci/lava/utils/lava_farm.py @@ -0,0 +1,35 @@ +import os +import re +from enum import Enum + + +class LavaFarm(Enum): + """Enum class representing the different LAVA farms.""" + + LIMA = 1 + COLLABORA = 2 + UNKNOWN = 3 + + +LAVA_FARM_RUNNER_PATTERNS: dict[LavaFarm, str] = { + # Lima pattern comes first, since it has the same prefix as the + # Collabora pattern. + LavaFarm.LIMA: r"^mesa-ci-[\x01-\x7F]+-lava-lima$", + LavaFarm.COLLABORA: r"^mesa-ci-[\x01-\x7F]+-lava-[\x01-\x7F]+$", + LavaFarm.UNKNOWN: r"^[\x01-\x7F]+", +} + + +def get_lava_farm() -> LavaFarm: + """ + Returns the LAVA farm based on the RUNNER_TAG environment variable. + + :return: The LAVA farm + """ + runner_tag: str = os.getenv("RUNNER_TAG", "unknown") + + for farm, pattern in LAVA_FARM_RUNNER_PATTERNS.items(): + if re.match(pattern, runner_tag): + return farm + + raise ValueError(f"Unknown LAVA runner tag: {runner_tag}") diff --git a/.gitlab-ci/tests/conftest.py b/.gitlab-ci/tests/conftest.py index 866864d6d56..be71c945301 100644 --- a/.gitlab-ci/tests/conftest.py +++ b/.gitlab-ci/tests/conftest.py @@ -3,9 +3,12 @@ from unittest.mock import MagicMock, patch import pytest import yaml from freezegun import freeze_time +from hypothesis import settings from .lava.helpers import generate_testsuite_result, jobs_logs_response +settings.register_profile("ci", max_examples=1000, derandomize=True) +settings.load_profile("ci") def pytest_configure(config): config.addinivalue_line( diff --git a/.gitlab-ci/tests/utils/test_lava_farm.py b/.gitlab-ci/tests/utils/test_lava_farm.py new file mode 100644 index 00000000000..e11586c6dff --- /dev/null +++ b/.gitlab-ci/tests/utils/test_lava_farm.py @@ -0,0 +1,41 @@ +import re + +import pytest +from hypothesis import given +from hypothesis import strategies as st +from lava.utils.lava_farm import LAVA_FARM_RUNNER_PATTERNS, LavaFarm, get_lava_farm + + +@given( + runner_tag=st.text( + alphabet=st.characters( + min_codepoint=1, max_codepoint=127, blacklist_categories=("C",) + ), + min_size=1, + ) +) +def test_get_lava_farm_invalid_tags(runner_tag): + with pytest.MonkeyPatch().context() as mp: + mp.setenv("RUNNER_TAG", runner_tag) + assert get_lava_farm() == LavaFarm.UNKNOWN + + +def test_get_lava_farm_no_tag(monkeypatch): + monkeypatch.delenv("RUNNER_TAG", raising=False) + assert get_lava_farm() == LavaFarm.UNKNOWN + + +@given( + st.fixed_dictionaries( + {k: st.from_regex(v) for k, v in LAVA_FARM_RUNNER_PATTERNS.items()} + ) +) +def test_get_lava_farm_valid_tags(runner_farm_tag: dict): + with pytest.MonkeyPatch().context() as mp: + for farm, tag in runner_farm_tag.items(): + try: + mp.setenv("RUNNER_TAG", tag) + except ValueError: + # hypothesis may generate null bytes in the string + continue + assert get_lava_farm() == farm