add tests for machines api
All checks were successful
build / test (pull_request) Successful in 15s

This commit is contained in:
Jörg Thalheim 2023-08-24 19:16:44 +02:00
parent 58adf91af8
commit 33b43ae146
5 changed files with 71 additions and 13 deletions

View File

@ -3,11 +3,15 @@ import argparse
from .folders import machine_folder
def create_command(args: argparse.Namespace) -> None:
folder = machine_folder(args.host)
def create_machine(name: str) -> None:
folder = machine_folder(name)
folder.mkdir(parents=True, exist_ok=True)
def create_command(args: argparse.Namespace) -> None:
create_machine(args.host)
def register_create_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("host", type=str)
parser.set_defaults(func=create_command)

View File

@ -1,14 +1,19 @@
from enum import Enum
from typing import Annotated
from fastapi import APIRouter
from fastapi import APIRouter, Body
from pydantic import BaseModel, Field
from ...machines.create import create_machine as _create_machine
from ...machines.list import list_machines as _list_machines
router = APIRouter()
class Status(Enum):
ONLINE = "online"
OFFLINE = "offline"
UNKNOWN = "unknown"
class Machine(BaseModel):
@ -16,6 +21,10 @@ class Machine(BaseModel):
status: Status
class MachineCreate(BaseModel):
name: str
class MachinesResponse(BaseModel):
machines: list[Machine]
@ -42,24 +51,38 @@ class SchemaResponse(BaseModel):
@router.get("/api/machines")
async def list_machines() -> MachinesResponse:
return MachinesResponse(machines=[])
machines = []
for m in _list_machines():
machines.append(Machine(name=m, status=Status.UNKNOWN))
return MachinesResponse(machines=machines)
@router.get("/api/machines/{machine}")
async def get_machine(machine: str) -> MachineResponse:
return MachineResponse(machine=Machine(name=machine, status=Status.ONLINE))
@router.post("/api/machines", status_code=201)
async def create_machine(machine: Annotated[MachineCreate, Body()]) -> MachineResponse:
_create_machine(machine.name)
return MachineResponse(machine=Machine(name=machine.name, status=Status.UNKNOWN))
@router.get("/api/machines/{machine}/config")
async def get_machine_config(machine: str) -> ConfigResponse:
@router.get("/api/machines/{name}")
async def get_machine(name: str) -> MachineResponse:
print("TODO")
return MachineResponse(machine=Machine(name=name, status=Status.UNKNOWN))
@router.get("/api/machines/{name}/config")
async def get_machine_config(name: str) -> ConfigResponse:
return ConfigResponse(config=Config())
@router.post("/api/machines/{machine}/config")
async def set_machine_config(machine: str, config: Config) -> ConfigResponse:
@router.put("/api/machines/{name}/config")
async def set_machine_config(
name: str, config: Annotated[Config, Body()]
) -> ConfigResponse:
print("TODO")
return ConfigResponse(config=config)
@router.get("/api/machines/{machine}/schema")
async def get_machine_schema(machine: str) -> SchemaResponse:
@router.get("/api/machines/{name}/schema")
async def get_machine_schema(name: str) -> SchemaResponse:
print("TODO")
return SchemaResponse(schema=Schema())

View File

@ -0,0 +1,9 @@
import pytest
from fastapi.testclient import TestClient
from clan_cli.webui.app import app
@pytest.fixture(scope="session")
def api() -> TestClient:
return TestClient(app)

View File

@ -4,6 +4,7 @@ import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
pytest_plugins = [
"api",
"temporary_dir",
"clan_flake",
"root",

View File

@ -0,0 +1,21 @@
from pathlib import Path
from api import TestClient
def test_machines(api: TestClient, clan_flake: Path) -> None:
response = api.get("/api/machines")
assert response.status_code == 200
assert response.json() == {"machines": []}
response = api.post("/api/machines", json={"name": "test"})
assert response.status_code == 201
assert response.json() == {"machine": {"name": "test", "status": "unknown"}}
response = api.get("/api/machines/test")
assert response.status_code == 200
assert response.json() == {"machine": {"name": "test", "status": "unknown"}}
response = api.get("/api/machines")
assert response.status_code == 200
assert response.json() == {"machines": [{"name": "test", "status": "unknown"}]}