
LavaFarm is a class created to handle the different types of LAVA farms and their tags in Mesa CI. Since specific jobs may require different types of LAVA farms to run on, it is essential to determine which farm the runner is running on to configure the job correctly. LavaFarm provides an easy-to-use interface for checking the runner tag and returning the corresponding LAVA farm, making it simple for Mesa CI to configure jobs appropriately. By adding tests for LavaFarm, the team can ensure that this class is functioning as expected, allowing for the smooth execution of Mesa CI jobs on the correct LAVA farm. The tests ensure that get_lava_farm returns the correct LavaFarm value when given invalid or valid tags and that it returns LavaFarm.UNKNOWN when no tag is provided. The tests use Hypothesis strategies to generate various labels and farms for testing. Example of use: ``` from lava.utils.lava_farm import LavaFarm, get_lava_farm lava_farm = get_lava_farm() if lava_farm == LavaFarm.DUMMY: # Configure the job for the DUMMY farm ... elif lava_farm == LavaFarm.COLLABORA: # Configure the job for the COLLABORA farm ... elif lava_farm == LavaFarm.KERNELCI: # Configure the job for the KERNELCI farm ... else: # Handle the case where the LAVA farm is unknown ... ``` Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21325>
36 lines
886 B
Python
36 lines
886 B
Python
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}")
|