--- /dev/null
+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}")
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(
--- /dev/null
+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