clan-core/pkgs/clan-cli/tests/fixtures_flakes.py

145 lines
4.4 KiB
Python
Raw Normal View History

import fileinput
import logging
2023-10-25 11:10:30 +00:00
import os
import shutil
2023-10-25 17:23:28 +00:00
import subprocess as sp
import tempfile
2023-11-29 11:40:48 +00:00
from collections.abc import Iterator
from pathlib import Path
2023-11-29 11:40:48 +00:00
from typing import NamedTuple
import pytest
from pydantic import AnyUrl
from pydantic.tools import parse_obj_as
from root import CLAN_CORE
from clan_cli.dirs import nixpkgs_source
log = logging.getLogger(__name__)
# substitutes string sin a file.
# This can be used on the flake.nix or default.nix of a machine
def substitute(
file: Path,
clan_core_flake: Path | None = None,
flake: Path = Path(__file__).parent,
) -> None:
sops_key = str(flake.joinpath("sops.key"))
for line in fileinput.input(file, inplace=True):
line = line.replace("__NIXPKGS__", str(nixpkgs_source()))
if clan_core_flake:
line = line.replace("__CLAN_CORE__", str(clan_core_flake))
line = line.replace("__CLAN_SOPS_KEY_PATH__", sops_key)
line = line.replace("__CLAN_SOPS_KEY_DIR__", str(flake))
print(line, end="")
class FlakeForTest(NamedTuple):
path: Path
def create_flake(
monkeypatch: pytest.MonkeyPatch,
2023-10-23 20:31:12 +00:00
temporary_home: Path,
2023-11-15 13:28:40 +00:00
flake_name: str,
clan_core_flake: Path | None = None,
machines: list[str] = [],
remote: bool = False,
) -> Iterator[FlakeForTest]:
"""
Creates a flake with the given name and machines.
The machine names map to the machines in ./test_machines
"""
template = Path(__file__).parent / flake_name
# copy the template to a new temporary location
2023-11-15 13:28:40 +00:00
flake = temporary_home / flake_name
shutil.copytree(template, flake)
# lookup the requested machines in ./test_machines and include them
if machines:
(flake / "machines").mkdir(parents=True, exist_ok=True)
for machine_name in machines:
machine_path = Path(__file__).parent / "machines" / machine_name
shutil.copytree(machine_path, flake / "machines" / machine_name)
substitute(flake / "machines" / machine_name / "default.nix", flake)
# in the flake.nix file replace the string __CLAN_URL__ with the the clan flake
# provided by get_test_flake_toplevel
flake_nix = flake / "flake.nix"
# this is where we would install the sops key to, when updating
substitute(flake_nix, clan_core_flake, flake)
2023-10-24 14:44:54 +00:00
2023-10-25 11:15:38 +00:00
if "/tmp" not in str(os.environ.get("HOME")):
2023-10-25 17:23:28 +00:00
log.warning(
f"!! $HOME does not point to a temp directory!! HOME={os.environ['HOME']}"
)
2023-10-25 11:10:30 +00:00
# TODO: Find out why test_vms_api.py fails in nix build
# but works in pytest when this bottom line is commented out
2023-10-25 17:23:28 +00:00
sp.run(
2023-10-25 11:15:38 +00:00
["git", "config", "--global", "init.defaultBranch", "main"],
2023-10-25 17:23:28 +00:00
cwd=flake,
2023-10-25 11:15:38 +00:00
check=True,
)
2023-10-25 17:23:28 +00:00
sp.run(["git", "init"], cwd=flake, check=True)
sp.run(["git", "add", "."], cwd=flake, check=True)
sp.run(["git", "config", "user.name", "clan-tool"], cwd=flake, check=True)
sp.run(["git", "config", "user.email", "clan@example.com"], cwd=flake, check=True)
sp.run(["git", "commit", "-a", "-m", "Initial commit"], cwd=flake, check=True)
2023-10-24 14:44:54 +00:00
if remote:
2023-10-25 11:10:30 +00:00
with tempfile.TemporaryDirectory():
2023-11-15 13:28:40 +00:00
yield FlakeForTest(flake)
else:
2023-11-15 13:28:40 +00:00
yield FlakeForTest(flake)
@pytest.fixture
def test_flake(
2023-10-25 17:23:28 +00:00
monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]:
2023-11-15 13:28:40 +00:00
yield from create_flake(monkeypatch, temporary_home, "test_flake")
@pytest.fixture
def test_flake_with_core(
2023-10-25 17:23:28 +00:00
monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]:
if not (CLAN_CORE / "flake.nix").exists():
raise Exception(
"clan-core flake not found. This test requires the clan-core flake to be present"
)
yield from create_flake(
2023-10-24 14:44:54 +00:00
monkeypatch,
temporary_home,
2023-11-15 13:28:40 +00:00
"test_flake_with_core",
2023-10-24 14:44:54 +00:00
CLAN_CORE,
)
@pytest.fixture
def test_democlan_url(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[AnyUrl]:
yield parse_obj_as(
AnyUrl,
"https://git.clan.lol/clan/democlan/archive/main.tar.gz",
)
@pytest.fixture
def test_flake_with_core_and_pass(
2023-10-25 17:23:28 +00:00
monkeypatch: pytest.MonkeyPatch, temporary_home: Path
) -> Iterator[FlakeForTest]:
if not (CLAN_CORE / "flake.nix").exists():
raise Exception(
"clan-core flake not found. This test requires the clan-core flake to be present"
)
2023-10-14 13:17:58 +00:00
yield from create_flake(
monkeypatch,
2023-10-23 20:31:12 +00:00
temporary_home,
2023-11-15 13:28:40 +00:00
"test_flake_with_core_and_pass",
CLAN_CORE,
2023-10-14 13:17:58 +00:00
)