Fixed test_webui only failing in nix_sandbox

This commit is contained in:
Qubasa
2023-10-29 19:35:29 +01:00
parent 63a0adb678
commit 1fef2d1732
11 changed files with 146 additions and 26 deletions

View File

@@ -9,6 +9,6 @@ from clan_cli.webui.app import app
# TODO: Why stateful
@pytest.fixture(scope="session")
def api() -> TestClient:
logging.getLogger("httpx").setLevel(level=logging.WARNING)
# logging.getLogger("httpx").setLevel(level=logging.WARNING)
logging.getLogger("asyncio").setLevel(logging.INFO)
return TestClient(app)

View File

@@ -1,9 +1,43 @@
import json
import logging
from pathlib import Path
import pytest
from api import TestClient
from fixtures_flakes import FlakeForTest
from clan_cli.flakes.create import DEFAULT_URL
log = logging.getLogger(__name__)
@pytest.mark.impure
def test_flake_create(api: TestClient, temporary_home: Path) -> None:
params = {"flake_name": "defaultFlake", "url": str(DEFAULT_URL)}
response = api.post(
"/api/flake/create",
json=params,
)
response.json()
assert response.status_code == 201, "Failed to create flake"
# @pytest.mark.impure
# def test_flake_create_fail(api: TestClient, temporary_home: Path) -> None:
# params = {
# "flake_name": "../../../defaultFlake/",
# "url": str(DEFAULT_URL)
# }
# response = api.post(
# "/api/flake/create",
# json=params,
# )
# data = response.json()
# log.debug("Data: %s", data)
# assert response.status_code == 422, "This should have failed"
@pytest.mark.impure
def test_inspect_ok(api: TestClient, test_flake_with_core: FlakeForTest) -> None:

View File

@@ -5,16 +5,23 @@ import subprocess
import sys
from pathlib import Path
import pytest
from cli import Cli
from ports import PortFunction
from clan_cli.debug import breakpoint_container
@pytest.mark.timeout(10)
# @pytest.mark.timeout(10)
def test_start_server(unused_tcp_port: PortFunction, temporary_home: Path) -> None:
Cli()
port = unused_tcp_port()
fifo = temporary_home / "fifo"
os.mkfifo(fifo)
# Create a script called "firefox" in the temporary home directory that
# writes "1" to the fifo. This is used to notify the test that the firefox has been
# started.
notify_script = temporary_home / "firefox"
bash = shutil.which("bash")
assert bash is not None
@@ -26,11 +33,27 @@ echo "1" > {fifo}
)
notify_script.chmod(0o700)
# Add the temporary home directory to the PATH so that the script is found
env = os.environ.copy()
print(str(temporary_home.absolute()))
env["PATH"] = ":".join([str(temporary_home.absolute())] + env["PATH"].split(":"))
env["PATH"] = f"{temporary_home}:{env['PATH']}"
# Add build/src to PYTHONPATH so that the webui module is found in nix sandbox
python_path = env.get("PYTHONPATH")
if python_path:
env["PYTHONPATH"] = f"/build/src:{python_path}"
breakpoint_container(
cmd=[sys.executable, "-m", "clan_cli.webui", "--port", str(port)],
env=env,
work_dir=temporary_home,
)
with subprocess.Popen(
[sys.executable, "-m", "clan_cli.webui", "--port", str(port)], env=env
[sys.executable, "-m", "clan_cli.webui", "--port", str(port)],
env=env,
stdout=sys.stderr,
stderr=sys.stderr,
text=True,
) as p:
try:
with open(fifo) as f: