2023-09-22 16:32:09 +00:00
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
2023-10-09 12:01:34 +00:00
|
|
|
from api import TestClient
|
2023-09-22 16:32:09 +00:00
|
|
|
from cli import Cli
|
2023-10-21 15:19:06 +00:00
|
|
|
|
|
|
|
from clan_cli.dirs import clan_flakes_dir
|
2023-10-19 23:11:32 +00:00
|
|
|
from clan_cli.flakes.create import DEFAULT_URL
|
2023-10-21 15:19:06 +00:00
|
|
|
|
2023-09-22 16:32:09 +00:00
|
|
|
|
2023-09-22 17:17:45 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def cli() -> Cli:
|
|
|
|
return Cli()
|
|
|
|
|
|
|
|
|
2023-10-09 12:01:34 +00:00
|
|
|
@pytest.mark.impure
|
|
|
|
def test_create_flake_api(
|
2023-10-19 23:11:32 +00:00
|
|
|
monkeypatch: pytest.MonkeyPatch, api: TestClient, temporary_home: Path
|
2023-10-09 12:01:34 +00:00
|
|
|
) -> None:
|
2023-10-19 23:11:32 +00:00
|
|
|
monkeypatch.chdir(clan_flakes_dir())
|
|
|
|
flake_name = "flake_dir"
|
|
|
|
flake_dir = clan_flakes_dir() / flake_name
|
2023-10-09 12:01:34 +00:00
|
|
|
response = api.post(
|
|
|
|
"/api/flake/create",
|
|
|
|
json=dict(
|
2023-10-29 18:51:35 +00:00
|
|
|
flake_name=str(flake_dir),
|
2023-10-19 23:11:32 +00:00
|
|
|
url=str(DEFAULT_URL),
|
2023-10-09 12:01:34 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-10-12 20:46:32 +00:00
|
|
|
assert response.status_code == 201, f"Failed to create flake {response.text}"
|
2023-10-09 12:01:34 +00:00
|
|
|
assert (flake_dir / ".clan-flake").exists()
|
|
|
|
assert (flake_dir / "flake.nix").exists()
|
|
|
|
|
|
|
|
|
2023-09-22 16:32:09 +00:00
|
|
|
@pytest.mark.impure
|
2023-10-04 09:01:12 +00:00
|
|
|
def test_create_flake(
|
2023-09-22 17:17:45 +00:00
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
capsys: pytest.CaptureFixture,
|
2023-10-19 23:11:32 +00:00
|
|
|
temporary_home: Path,
|
2023-09-22 17:17:45 +00:00
|
|
|
cli: Cli,
|
2023-09-22 16:32:09 +00:00
|
|
|
) -> None:
|
2023-10-19 23:11:32 +00:00
|
|
|
monkeypatch.chdir(clan_flakes_dir())
|
|
|
|
flake_name = "flake_dir"
|
|
|
|
flake_dir = clan_flakes_dir() / flake_name
|
|
|
|
|
|
|
|
cli.run(["flakes", "create", flake_name])
|
2023-10-09 12:01:34 +00:00
|
|
|
assert (flake_dir / ".clan-flake").exists()
|
|
|
|
monkeypatch.chdir(flake_dir)
|
2023-10-19 23:11:32 +00:00
|
|
|
cli.run(["machines", "create", "machine1", flake_name])
|
2023-09-22 16:32:09 +00:00
|
|
|
capsys.readouterr() # flush cache
|
2023-10-19 23:11:32 +00:00
|
|
|
|
|
|
|
cli.run(["machines", "list", flake_name])
|
2023-09-22 16:32:09 +00:00
|
|
|
assert "machine1" in capsys.readouterr().out
|
|
|
|
flake_show = subprocess.run(
|
|
|
|
["nix", "flake", "show", "--json"],
|
|
|
|
check=True,
|
|
|
|
capture_output=True,
|
|
|
|
text=True,
|
|
|
|
)
|
|
|
|
flake_outputs = json.loads(flake_show.stdout)
|
|
|
|
try:
|
|
|
|
flake_outputs["nixosConfigurations"]["machine1"]
|
|
|
|
except KeyError:
|
|
|
|
pytest.fail("nixosConfigurations.machine1 not found in flake outputs")
|
2023-09-22 17:17:45 +00:00
|
|
|
# configure machine1
|
|
|
|
capsys.readouterr()
|
2023-10-21 15:19:06 +00:00
|
|
|
cli.run(
|
|
|
|
["config", "--machine", "machine1", "services.openssh.enable", "", flake_name]
|
|
|
|
)
|
2023-09-22 17:17:45 +00:00
|
|
|
capsys.readouterr()
|
2023-10-21 15:19:06 +00:00
|
|
|
cli.run(
|
|
|
|
[
|
|
|
|
"config",
|
|
|
|
"--machine",
|
|
|
|
"machine1",
|
|
|
|
"services.openssh.enable",
|
|
|
|
"true",
|
|
|
|
flake_name,
|
|
|
|
]
|
|
|
|
)
|