From 674d84a43a1eeee8c76c2d362b5041e4c077fbcc Mon Sep 17 00:00:00 2001 From: Qubasa Date: Wed, 25 Oct 2023 13:10:30 +0200 Subject: [PATCH] Fixing test_vms_api test --- pkgs/clan-cli/clan_cli/vms/inspect.py | 2 +- pkgs/clan-cli/pyproject.toml | 2 +- pkgs/clan-cli/tests/command.py | 9 ++++++++ pkgs/clan-cli/tests/fixtures_flakes.py | 30 +++++++++++++++----------- pkgs/clan-cli/tests/test_vms_api.py | 8 +++---- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/vms/inspect.py b/pkgs/clan-cli/clan_cli/vms/inspect.py index d50e6c6cf..74722235f 100644 --- a/pkgs/clan-cli/clan_cli/vms/inspect.py +++ b/pkgs/clan-cli/clan_cli/vms/inspect.py @@ -8,7 +8,7 @@ from pydantic import AnyUrl, BaseModel from ..async_cmd import run from ..dirs import specific_flake_dir from ..nix import nix_config, nix_eval -from ..debug import repro_env_break + class VmConfig(BaseModel): flake_url: AnyUrl | Path diff --git a/pkgs/clan-cli/pyproject.toml b/pkgs/clan-cli/pyproject.toml index aa96848cb..39e4500f1 100644 --- a/pkgs/clan-cli/pyproject.toml +++ b/pkgs/clan-cli/pyproject.toml @@ -21,7 +21,7 @@ testpaths = "tests" faulthandler_timeout = 60 log_level = "DEBUG" log_format = "%(levelname)s: %(message)s" -addopts = "--cov . --cov-report term --cov-report html:.reports/html --no-cov-on-fail --durations 5 --color=yes --new-first -n0 -s" # Add --pdb for debugging +addopts = "--cov . --cov-report term --cov-report html:.reports/html --no-cov-on-fail --durations 5 --color=yes --new-first --maxfail=1 -s" # Add --pdb for debugging norecursedirs = "tests/helpers" markers = [ "impure" ] diff --git a/pkgs/clan-cli/tests/command.py b/pkgs/clan-cli/tests/command.py index ba58243a5..65ea40c98 100644 --- a/pkgs/clan-cli/tests/command.py +++ b/pkgs/clan-cli/tests/command.py @@ -21,6 +21,7 @@ class Command: stdout: _FILE = None, stderr: _FILE = None, workdir: Optional[Path] = None, + check: Optional[bool] = True, ) -> subprocess.Popen[str]: env = os.environ.copy() env.update(extra_env) @@ -36,6 +37,14 @@ class Command: cwd=workdir, ) self.processes.append(p) + if check: + p.wait() + if p.returncode != 0: + aout = p.stdout.read() if p.stdout else "" + bout = p.stderr.read() if p.stderr else "" + raise subprocess.CalledProcessError( + p.returncode, command, output=aout, stderr=bout + ) return p def terminate(self) -> None: diff --git a/pkgs/clan-cli/tests/fixtures_flakes.py b/pkgs/clan-cli/tests/fixtures_flakes.py index 575be18ea..15722e7dc 100644 --- a/pkgs/clan-cli/tests/fixtures_flakes.py +++ b/pkgs/clan-cli/tests/fixtures_flakes.py @@ -1,5 +1,6 @@ import fileinput import logging +import os import shutil import tempfile from pathlib import Path @@ -53,8 +54,7 @@ def create_flake( template = Path(__file__).parent / flake_name # copy the template to a new temporary location - home = Path(temporary_home) - flake = home / ".local/state/clan/flake" / flake_name + flake = temporary_home / ".local/state/clan/flake" / flake_name shutil.copytree(template, flake) # lookup the requested machines in ./test_machines and include them @@ -70,21 +70,25 @@ def create_flake( # this is where we would install the sops key to, when updating substitute(flake_nix, clan_core_flake, flake) - # Init git - command.run(["git", "init"], workdir=flake) - command.run(["git", "add", "."], workdir=flake) - command.run(["git", "config", "user.name", "clan-tool"], workdir=flake) - command.run(["git", "config", "user.email", "clan@example.com"], workdir=flake) - command.run(["git", "commit", "-a", "-m", "Initial commit"], workdir=flake) + assert "/tmp" in str(os.environ.get("HOME")) + + # TODO: Find out why test_vms_api.py fails in nix build + # but works in pytest when this bottom line is commented out + command.run(["git", "config", "--global", "init.defaultBranch", "main"], workdir=flake, check=True) + command.run(["git", "init"], workdir=flake, check=True) + command.run(["git", "add", "."], workdir=flake, check=True) + command.run(["git", "config", "user.name", "clan-tool"], workdir=flake, check=True) + command.run( + ["git", "config", "user.email", "clan@example.com"], workdir=flake, check=True + ) + command.run( + ["git", "commit", "-a", "-m", "Initial commit"], workdir=flake, check=True + ) if remote: - with tempfile.TemporaryDirectory() as workdir: - monkeypatch.chdir(workdir) - monkeypatch.setenv("HOME", str(home)) + with tempfile.TemporaryDirectory(): yield FlakeForTest(flake_name, flake) else: - monkeypatch.chdir(flake) - monkeypatch.setenv("HOME", str(home)) yield FlakeForTest(flake_name, flake) diff --git a/pkgs/clan-cli/tests/test_vms_api.py b/pkgs/clan-cli/tests/test_vms_api.py index 13a022278..aeee77068 100644 --- a/pkgs/clan-cli/tests/test_vms_api.py +++ b/pkgs/clan-cli/tests/test_vms_api.py @@ -1,9 +1,7 @@ import pytest from api import TestClient from fixtures_flakes import FlakeForTest -from clan_cli.debug import repro_env_break -import sys -import time + @pytest.mark.impure def test_inspect(api: TestClient, test_flake_with_core: FlakeForTest) -> None: @@ -12,8 +10,8 @@ def test_inspect(api: TestClient, test_flake_with_core: FlakeForTest) -> None: json=dict(flake_url=str(test_flake_with_core.path), flake_attr="vm1"), ) - print(f"SLEEPING FOR EVER: {99999}", file=sys.stderr) - time.sleep(99999) + # print(f"SLEEPING FOR EVER: {99999}", file=sys.stderr) + # time.sleep(99999) assert response.status_code == 200, f"Failed to inspect vm: {response.text}" config = response.json()["config"]