Fixing test_vms_api test

This commit is contained in:
Qubasa
2023-10-25 13:10:30 +02:00
parent 86790a6282
commit 674d84a43a
5 changed files with 31 additions and 20 deletions

View File

@@ -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

View File

@@ -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" ]

View File

@@ -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:

View File

@@ -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)

View File

@@ -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"]