from __future__ import annotations
from pathlib import Path
import pytest
from runner_placement import (
ESTATE_EXPRESSION,
placed_jobs,
placement_faults,
selected_runner,
)
from workflow_reader import Workflow, load_workflows
REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
PLACEMENTS = [
(".github/workflows/ci.yml", "build-test", "ubicloud-standard-4", 66),
(".github/workflows/ci.yml", "msrv", "ubicloud-standard-4", 30),
(".github/workflows/coverage-main.yml", "coverage-upload", "ubicloud-standard-2", 66),
]
@pytest.mark.parametrize(
("origin", "wanted"),
[
("push", "ubicloud-standard-2"),
("same-repository", "ubicloud-standard-2"),
("fork", "ubuntu-latest"),
],
)
def test_the_estate_expression_places_each_run(origin: str, wanted: str) -> None:
assert selected_runner(ESTATE_EXPRESSION, origin) == wanted
@pytest.mark.parametrize(
("runs_on", "expected"),
[
(ESTATE_EXPRESSION, 0),
("ubuntu-latest", 3),
("ubicloud-standard-2", 3),
(
"${{ github.event.pull_request.head.repo.fork && "
"'ubicloud-standard-2' || 'ubuntu-latest' }}",
3,
),
(
"${{ github.event.pull_request.head.repo.fork && 'ubuntu-latest' "
"|| 'ubicloud-standard-4' }}",
2,
),
(
"${{ github.event_name == 'pull_request' && 'ubuntu-latest' "
"|| 'ubicloud-standard-2' }}",
3,
),
(
"${{ github.event.pull_request.head.repo.fork && "
"'ubicloud-standard-2' || 'ubicloud-standard-2' }}",
1,
),
(None, 3),
],
ids=[
"estate",
"always-hosted",
"always-ubicloud",
"inverted-arms",
"another-label",
"another-condition",
"fork-kept-on-ubicloud",
"not-a-string",
],
)
def test_a_misplaced_lane_is_reported(runs_on: object, expected: int) -> None:
assert len(placement_faults(runs_on)) == expected
@pytest.mark.parametrize(
("key", "expected"),
[
(" timeout-minutes: 30\n", 30),
("", None),
(" timeout-minutes: thirty\n", "thirty"),
],
ids=["stated", "missing", "a-string"],
)
def test_a_ceiling_is_read_as_the_file_states_it(
key: str, expected: object
) -> None:
text = f"on: push\njobs:\n lane:\n runs-on: {ESTATE_EXPRESSION}\n{key}"
flow = Workflow.parse("x.yml", text)
assert placed_jobs([flow]) == [("x.yml", "lane", ESTATE_EXPRESSION, expected)]
def test_a_non_ubicloud_job_is_not_inventoried() -> None:
flow = Workflow.parse("x.yml", "on: push\njobs:\n lane:\n runs-on: ubuntu-latest\n")
assert placed_jobs([flow]) == []
def test_every_ubicloud_lane_is_placed_by_the_estate_expression_and_states_a_ceiling() -> (
None
):
placed = placed_jobs(load_workflows(REPOSITORY_ROOT))
assert [(path, job, ceiling) for path, job, _, ceiling in placed] == [
(path, job, ceiling) for path, job, _, ceiling in PLACEMENTS
]
for (path, job, runs_on, _), (_, _, label, _) in zip(placed, PLACEMENTS, strict=True):
assert not placement_faults(runs_on, label), f"{path}: {job} is misplaced"
def test_a_runner_class_other_than_the_inventoried_one_is_reported() -> None:
four = ESTATE_EXPRESSION.replace("standard-2", "standard-4")
assert placement_faults(four, "ubicloud-standard-4") == []
assert len(placement_faults(four, "ubicloud-standard-2")) == 2