Public Access
DEVX-12: feat: add opentofu helpers, CLI entry points, shared utility, and CI improvements
Post-merge / detect-type (push) Successful in 11s
Post-merge / validate-commit-msg (push) Successful in 9s
Post-merge / configure-repo (push) Successful in 17s
Post-merge / release (push) Successful in 1m22s
Post-merge / vikunja (push) Successful in 32s
Post-merge / badges (push) Successful in 50s
Post-merge / sync-wiki (push) Successful in 57s
Post-merge / detect-type (push) Successful in 11s
Post-merge / validate-commit-msg (push) Successful in 9s
Post-merge / configure-repo (push) Successful in 17s
Post-merge / release (push) Successful in 1m22s
Post-merge / vikunja (push) Successful in 32s
Post-merge / badges (push) Successful in 50s
Post-merge / sync-wiki (push) Successful in 57s
This commit was merged in pull request #22.
This commit is contained in:
@@ -5,8 +5,11 @@ from __future__ import annotations
|
||||
import os
|
||||
import subprocess # nosec B404
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import click
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
@@ -16,7 +19,10 @@ from devx.molecule.molecule_ci_guard import (
|
||||
build_molecule_cmd,
|
||||
cli,
|
||||
get_running_jobs,
|
||||
parse_pair,
|
||||
poll_for_other_failures,
|
||||
resolve_role_dir,
|
||||
write_junit_report,
|
||||
)
|
||||
|
||||
|
||||
@@ -435,3 +441,240 @@ def test_main_module_block() -> None:
|
||||
namespace = dict(mg.__dict__)
|
||||
exec(compile(source, mg.__file__, "exec"), namespace)
|
||||
assert callable(namespace["cli"])
|
||||
|
||||
|
||||
class TestParsePair:
|
||||
def test_single_role_4_part(self) -> None:
|
||||
role, scenario, name, image, cmd = parse_pair("default|ubuntu-2204|ubuntu:22.04|")
|
||||
assert role == ""
|
||||
assert scenario == "default"
|
||||
assert name == "ubuntu-2204"
|
||||
assert image == "ubuntu:22.04"
|
||||
assert cmd == ""
|
||||
|
||||
def test_multi_role_5_part(self) -> None:
|
||||
role, scenario, name, image, cmd = parse_pair("gitea-runner|default|ubuntu-2204|ubuntu:22.04|")
|
||||
assert role == "gitea-runner"
|
||||
assert scenario == "default"
|
||||
assert name == "ubuntu-2204"
|
||||
assert image == "ubuntu:22.04"
|
||||
assert cmd == ""
|
||||
|
||||
def test_multi_role_with_command(self) -> None:
|
||||
role, scenario, name, image, cmd = parse_pair(
|
||||
"docker-base|lifecycle|archlinux|archlinux:latest|/usr/lib/systemd/systemd"
|
||||
)
|
||||
assert role == "docker-base"
|
||||
assert scenario == "lifecycle"
|
||||
assert cmd == "/usr/lib/systemd/systemd"
|
||||
|
||||
def test_invalid_pair_raises(self) -> None:
|
||||
with pytest.raises(click.ClickException, match="Invalid pair format"):
|
||||
parse_pair("only|two|parts")
|
||||
|
||||
def test_too_many_parts_raises(self) -> None:
|
||||
with pytest.raises(click.ClickException, match="Invalid pair format"):
|
||||
parse_pair("a|b|c|d|e|f")
|
||||
|
||||
|
||||
class TestResolveRoleDir:
|
||||
def test_multi_role_with_roles_root(self, tmp_path: Path) -> None:
|
||||
roles_root = tmp_path / "ansible" / "roles"
|
||||
roles_root.mkdir(parents=True)
|
||||
result = resolve_role_dir("gitea-runner", roles_root, tmp_path)
|
||||
assert result == roles_root / "gitea-runner"
|
||||
|
||||
def test_multi_role_default_roles_root(self, tmp_path: Path) -> None:
|
||||
result = resolve_role_dir("docker-base", None, tmp_path)
|
||||
assert result == tmp_path / "ansible" / "roles" / "docker-base"
|
||||
|
||||
def test_single_role_uses_default(self, tmp_path: Path) -> None:
|
||||
result = resolve_role_dir("", None, tmp_path)
|
||||
assert result == tmp_path / "ansible" / "roles" / "gitea-runner"
|
||||
|
||||
|
||||
class TestWriteJunitReport:
|
||||
def test_writes_report_with_passing_tests(self, tmp_path: Path) -> None:
|
||||
output = str(tmp_path / "junit-results" / "runner-1.xml")
|
||||
testcases = [
|
||||
{"role": "gitea-runner", "scenario": "default", "time": 5.2, "passed": True, "error": None},
|
||||
{"role": "docker-base", "scenario": "lifecycle", "time": 3.1, "passed": True, "error": None},
|
||||
]
|
||||
write_junit_report(output, testcases, 1)
|
||||
tree = ET.parse(output)
|
||||
root = tree.getroot()
|
||||
assert root.get("tests") == "2"
|
||||
assert root.get("failures") == "0"
|
||||
assert len(root) == 2
|
||||
|
||||
def test_writes_report_with_failures(self, tmp_path: Path) -> None:
|
||||
output = str(tmp_path / "runner-2.xml")
|
||||
testcases = [
|
||||
{"role": "", "scenario": "default", "time": 1.0, "passed": False, "error": "Exit code: 1"},
|
||||
]
|
||||
write_junit_report(output, testcases, 2)
|
||||
tree = ET.parse(output)
|
||||
root = tree.getroot()
|
||||
assert root.get("tests") == "1"
|
||||
assert root.get("failures") == "1"
|
||||
failure = root[0][0]
|
||||
assert failure.tag == "failure"
|
||||
assert failure.text == "Exit code: 1"
|
||||
|
||||
def test_creates_parent_directory(self, tmp_path: Path) -> None:
|
||||
output = str(tmp_path / "deep" / "nested" / "dir" / "runner.xml")
|
||||
write_junit_report(output, [], 0)
|
||||
assert Path(output).exists()
|
||||
|
||||
|
||||
class TestCliMultiRole:
|
||||
def test_multi_role_pair_passes(self, tmp_path: Path) -> None:
|
||||
from click.testing import CliRunner
|
||||
|
||||
roles_root = tmp_path / "ansible" / "roles"
|
||||
(roles_root / "gitea-runner").mkdir(parents=True)
|
||||
|
||||
with (
|
||||
patch("devx.molecule.molecule_ci_guard.subprocess.Popen") as mock_popen,
|
||||
patch("time.sleep"),
|
||||
):
|
||||
proc = MagicMock()
|
||||
proc.poll.return_value = 0
|
||||
proc.returncode = 0
|
||||
mock_popen.return_value = proc
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
["--roles-root", str(roles_root), "gitea-runner|default|ubuntu-2204|ubuntu:22.04|"],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "All molecule tests passed" in result.output
|
||||
|
||||
def test_junit_output_written(self, tmp_path: Path) -> None:
|
||||
from click.testing import CliRunner
|
||||
|
||||
roles_root = tmp_path / "ansible" / "roles"
|
||||
(roles_root / "gitea-runner").mkdir(parents=True)
|
||||
junit_path = str(tmp_path / "junit-results" / "runner-1.xml")
|
||||
|
||||
with (
|
||||
patch("devx.molecule.molecule_ci_guard.subprocess.Popen") as mock_popen,
|
||||
patch("time.sleep"),
|
||||
):
|
||||
proc = MagicMock()
|
||||
proc.poll.return_value = 0
|
||||
proc.returncode = 0
|
||||
mock_popen.return_value = proc
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"--roles-root",
|
||||
str(roles_root),
|
||||
"--junit-output",
|
||||
junit_path,
|
||||
"gitea-runner|default|ubuntu-2204|ubuntu:22.04|",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert Path(junit_path).exists()
|
||||
|
||||
def test_junit_output_on_failure(self, tmp_path: Path) -> None:
|
||||
from click.testing import CliRunner
|
||||
|
||||
roles_root = tmp_path / "ansible" / "roles"
|
||||
(roles_root / "gitea-runner").mkdir(parents=True)
|
||||
junit_path = str(tmp_path / "junit-results" / "runner-1.xml")
|
||||
|
||||
with (
|
||||
patch("devx.molecule.molecule_ci_guard.subprocess.Popen") as mock_popen,
|
||||
patch("time.sleep"),
|
||||
):
|
||||
proc = MagicMock()
|
||||
proc.poll.return_value = 1
|
||||
proc.returncode = 1
|
||||
mock_popen.return_value = proc
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"--roles-root",
|
||||
str(roles_root),
|
||||
"--junit-output",
|
||||
junit_path,
|
||||
"gitea-runner|default|ubuntu-2204|ubuntu:22.04|",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 1
|
||||
assert Path(junit_path).exists()
|
||||
tree = ET.parse(junit_path)
|
||||
assert tree.getroot().get("failures") == "1"
|
||||
|
||||
def test_junit_output_on_cancellation(self, tmp_path: Path) -> None:
|
||||
"""JUnit report is written when a runner is cancelled by another runner's failure."""
|
||||
from click.testing import CliRunner
|
||||
|
||||
real_sleep = time.sleep
|
||||
roles_root = tmp_path / "ansible" / "roles"
|
||||
(roles_root / "gitea-runner").mkdir(parents=True)
|
||||
junit_path = str(tmp_path / "junit-results" / "runner-1.xml")
|
||||
call_count = [0]
|
||||
|
||||
def get_jobs_side_effect(*args, **kwargs):
|
||||
call_count[0] += 1
|
||||
if call_count[0] < 2:
|
||||
return [{"name": "molecule-tests (1)", "conclusion": "running"}]
|
||||
return [
|
||||
{"name": "molecule-tests (0)", "conclusion": "running"},
|
||||
{"name": "molecule-tests (1)", "conclusion": "failure"},
|
||||
]
|
||||
|
||||
with (
|
||||
patch.dict(
|
||||
os.environ,
|
||||
{
|
||||
"GITEA_URL": "https://gitea.example",
|
||||
"REPO_TOKEN": "token",
|
||||
"RUN_ID": "123",
|
||||
"JOB_NAME": "molecule-tests",
|
||||
"MATRIX_INDEX": "0",
|
||||
"GITEA_REPOSITORY": "oblachno-oss/infra",
|
||||
"PATH": os.environ.get("PATH", ""),
|
||||
},
|
||||
clear=True,
|
||||
),
|
||||
patch("devx.molecule.molecule_ci_guard.POLL_INTERVAL", 0.01),
|
||||
patch("devx.molecule.molecule_ci_guard.subprocess.Popen") as mock_popen,
|
||||
patch("devx.molecule.molecule_ci_guard.get_running_jobs", side_effect=get_jobs_side_effect),
|
||||
patch("os.killpg"),
|
||||
patch("os.getpgid") as mock_getpgid,
|
||||
patch("time.sleep", side_effect=lambda x: real_sleep(0.1)),
|
||||
):
|
||||
mock_getpgid.return_value = 123
|
||||
proc = MagicMock()
|
||||
proc.poll.return_value = None
|
||||
proc.wait.return_value = 0
|
||||
mock_popen.return_value = proc
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"--roles-root",
|
||||
str(roles_root),
|
||||
"--junit-output",
|
||||
junit_path,
|
||||
"gitea-runner|default|ubuntu-2204|ubuntu:22.04|",
|
||||
],
|
||||
)
|
||||
assert result.exit_code == 1
|
||||
assert Path(junit_path).exists()
|
||||
tree = ET.parse(junit_path)
|
||||
root = tree.getroot()
|
||||
assert root.get("failures") == "1"
|
||||
# The failure message should mention cancellation
|
||||
failure = root[0][0]
|
||||
assert "Cancelled" in (failure.text or "")
|
||||
|
||||
Reference in New Issue
Block a user