All tests passing babyyy !!

This commit is contained in:
Qubasa
2023-10-24 16:44:54 +02:00
parent 0be3dac289
commit 916fa8e570
7 changed files with 42 additions and 20 deletions

View File

@@ -46,7 +46,7 @@ async def create_machine(
return MachineResponse(machine=Machine(name=machine.name, status=Status.UNKNOWN)) return MachineResponse(machine=Machine(name=machine.name, status=Status.UNKNOWN))
@router.get("/api/machines/{name}") @router.get("/api/{flake_name}/machines/{name}")
async def get_machine(name: str) -> MachineResponse: async def get_machine(name: str) -> MachineResponse:
log.error("TODO") log.error("TODO")
return MachineResponse(machine=Machine(name=name, status=Status.UNKNOWN)) return MachineResponse(machine=Machine(name=name, status=Status.UNKNOWN))

View File

@@ -1,7 +1,8 @@
import os import os
import signal import signal
import subprocess import subprocess
from typing import IO, Any, Dict, Iterator, List, Union from pathlib import Path
from typing import IO, Any, Dict, Iterator, List, Optional, Union
import pytest import pytest
@@ -19,6 +20,7 @@ class Command:
stdin: _FILE = None, stdin: _FILE = None,
stdout: _FILE = None, stdout: _FILE = None,
stderr: _FILE = None, stderr: _FILE = None,
workdir: Optional[Path] = None,
) -> subprocess.Popen[str]: ) -> subprocess.Popen[str]:
env = os.environ.copy() env = os.environ.copy()
env.update(extra_env) env.update(extra_env)
@@ -31,6 +33,7 @@ class Command:
stderr=stderr, stderr=stderr,
stdin=stdin, stdin=stdin,
text=True, text=True,
cwd=workdir,
) )
self.processes.append(p) self.processes.append(p)
return p return p

View File

@@ -6,6 +6,7 @@ from pathlib import Path
from typing import Iterator, NamedTuple from typing import Iterator, NamedTuple
import pytest import pytest
from command import Command
from root import CLAN_CORE from root import CLAN_CORE
from clan_cli.dirs import nixpkgs_source from clan_cli.dirs import nixpkgs_source
@@ -40,6 +41,7 @@ def create_flake(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
temporary_home: Path, temporary_home: Path,
flake_name: FlakeName, flake_name: FlakeName,
command: Command,
clan_core_flake: Path | None = None, clan_core_flake: Path | None = None,
machines: list[str] = [], machines: list[str] = [],
remote: bool = False, remote: bool = False,
@@ -67,6 +69,14 @@ def create_flake(
flake_nix = flake / "flake.nix" flake_nix = flake / "flake.nix"
# this is where we would install the sops key to, when updating # this is where we would install the sops key to, when updating
substitute(flake_nix, clan_core_flake, flake) 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)
if remote: if remote:
with tempfile.TemporaryDirectory() as workdir: with tempfile.TemporaryDirectory() as workdir:
monkeypatch.chdir(workdir) monkeypatch.chdir(workdir)
@@ -80,28 +90,33 @@ def create_flake(
@pytest.fixture @pytest.fixture
def test_flake( def test_flake(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake(monkeypatch, temporary_home, FlakeName("test_flake")) yield from create_flake(
monkeypatch, temporary_home, FlakeName("test_flake"), command
)
@pytest.fixture @pytest.fixture
def test_flake_with_core( def test_flake_with_core(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
if not (CLAN_CORE / "flake.nix").exists(): if not (CLAN_CORE / "flake.nix").exists():
raise Exception( raise Exception(
"clan-core flake not found. This test requires the clan-core flake to be present" "clan-core flake not found. This test requires the clan-core flake to be present"
) )
yield from create_flake( yield from create_flake(
monkeypatch, temporary_home, FlakeName("test_flake_with_core"), CLAN_CORE monkeypatch,
temporary_home,
FlakeName("test_flake_with_core"),
command,
CLAN_CORE,
) )
@pytest.fixture @pytest.fixture
def test_flake_with_core_and_pass( def test_flake_with_core_and_pass(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command
temporary_home: Path,
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
if not (CLAN_CORE / "flake.nix").exists(): if not (CLAN_CORE / "flake.nix").exists():
raise Exception( raise Exception(
@@ -111,5 +126,6 @@ def test_flake_with_core_and_pass(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core_and_pass"), FlakeName("test_flake_with_core_and_pass"),
command,
CLAN_CORE, CLAN_CORE,
) )

View File

@@ -41,7 +41,7 @@ def test_import_sops(
str(test_root.joinpath("data", "secrets.yaml")), str(test_root.joinpath("data", "secrets.yaml")),
test_flake.name, test_flake.name,
] ]
repro_env_break(work_dir=test_flake.path, cmd=cmd)
cli.run(cmd) cli.run(cmd)
capsys.readouterr() capsys.readouterr()
cli.run(["secrets", "users", "list", test_flake.name]) cli.run(["secrets", "users", "list", test_flake.name])

View File

@@ -7,9 +7,9 @@ def test_machines(api: TestClient, test_flake: FlakeForTest) -> None:
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == {"machines": []} assert response.json() == {"machines": []}
# TODO: Fails because the test_flake fixture needs to init a git repo, which it currently does not
response = api.post(f"/api/{test_flake.name}/machines", json={"name": "test"}) response = api.post(f"/api/{test_flake.name}/machines", json={"name": "test"})
assert response.status_code == 201 assert response.status_code == 201
assert response.json() == {"machine": {"name": "test", "status": "unknown"}} assert response.json() == {"machine": {"name": "test", "status": "unknown"}}
response = api.get(f"/api/{test_flake.name}/machines/test") response = api.get(f"/api/{test_flake.name}/machines/test")
@@ -91,13 +91,13 @@ def test_configure_machine(api: TestClient, test_flake: FlakeForTest) -> None:
devices=["/dev/fake_disk"], devices=["/dev/fake_disk"],
), ),
), ),
f"/api/{test_flake.name}machines/machine1/config",
json=dict( json=dict(
clan=dict( clan=dict(
jitsi=True, jitsi=True,
) )
), ),
) ))
# set some valid config # set some valid config
config2 = dict( config2 = dict(

View File

@@ -2,20 +2,20 @@ from pathlib import Path
import pytest import pytest
from cli import Cli from cli import Cli
from fixtures_flakes import FlakeForTest
def test_machine_subcommands(test_flake: FlakeForTest, capsys: pytest.CaptureFixture) -> None:
def test_machine_subcommands(test_flake: Path, capsys: pytest.CaptureFixture) -> None:
cli = Cli() cli = Cli()
cli.run(["machines", "create", "machine1"]) cli.run(["machines", "create", "machine1", test_flake.name])
capsys.readouterr() capsys.readouterr()
cli.run(["machines", "list"]) cli.run(["machines", "list", test_flake.name])
out = capsys.readouterr() out = capsys.readouterr()
assert "machine1\n" == out.out assert "machine1\n" == out.out
cli.run(["machines", "remove", "machine1"]) cli.run(["machines", "delete", "machine1", test_flake.name])
capsys.readouterr() capsys.readouterr()
cli.run(["machines", "list"]) cli.run(["machines", "list", test_flake.name])
out = capsys.readouterr() out = capsys.readouterr()
assert "" == out.out assert "" == out.out

View File

@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Iterator
import pytest import pytest
from api import TestClient from api import TestClient
from cli import Cli from cli import Cli
from command import Command
from fixtures_flakes import FlakeForTest, create_flake from fixtures_flakes import FlakeForTest, create_flake
from httpx import SyncByteStream from httpx import SyncByteStream
from root import CLAN_CORE from root import CLAN_CORE
@@ -17,12 +18,13 @@ if TYPE_CHECKING:
@pytest.fixture @pytest.fixture
def flake_with_vm_with_secrets( def flake_with_vm_with_secrets(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake( yield from create_flake(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core_dynamic_machines"), FlakeName("test_flake_with_core_dynamic_machines"),
command,
CLAN_CORE, CLAN_CORE,
machines=["vm_with_secrets"], machines=["vm_with_secrets"],
) )
@@ -30,12 +32,13 @@ def flake_with_vm_with_secrets(
@pytest.fixture @pytest.fixture
def remote_flake_with_vm_without_secrets( def remote_flake_with_vm_without_secrets(
monkeypatch: pytest.MonkeyPatch, temporary_home: Path monkeypatch: pytest.MonkeyPatch, temporary_home: Path, command: Command
) -> Iterator[FlakeForTest]: ) -> Iterator[FlakeForTest]:
yield from create_flake( yield from create_flake(
monkeypatch, monkeypatch,
temporary_home, temporary_home,
FlakeName("test_flake_with_core_dynamic_machines"), FlakeName("test_flake_with_core_dynamic_machines"),
command,
CLAN_CORE, CLAN_CORE,
machines=["vm_without_secrets"], machines=["vm_without_secrets"],
remote=True, remote=True,