webui: implement /api/machines/{name}/schema
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
{ lib, ... }: {
|
||||
options.clan.jitsi.enable = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Enable jitsi on this machine";
|
||||
};
|
||||
}
|
||||
10
pkgs/clan-cli/tests/config/example_flake/flake.nix
Normal file
10
pkgs/clan-cli/tests/config/example_flake/flake.nix
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
inputs = {
|
||||
# this placeholder is replaced by the path to nixpkgs
|
||||
nixpkgs.url = "__CLAN_NIXPKGS__";
|
||||
};
|
||||
|
||||
outputs = _inputs: {
|
||||
clanModules.machine-machine1 = ./clanModules/machine1.nix;
|
||||
};
|
||||
}
|
||||
154
pkgs/clan-cli/tests/config/test_config.py
Normal file
154
pkgs/clan-cli/tests/config/test_config.py
Normal file
@@ -0,0 +1,154 @@
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from cli import Cli
|
||||
|
||||
from clan_cli import config
|
||||
from clan_cli.config import parsing
|
||||
|
||||
example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
|
||||
|
||||
|
||||
# use pytest.parametrize
|
||||
@pytest.mark.parametrize(
|
||||
"args,expected",
|
||||
[
|
||||
(["name", "DavHau"], {"name": "DavHau"}),
|
||||
(
|
||||
["kernelModules", "foo", "bar", "baz"],
|
||||
{"kernelModules": ["foo", "bar", "baz"]},
|
||||
),
|
||||
(["services.opt", "test"], {"services": {"opt": "test"}}),
|
||||
(["userIds.DavHau", "42"], {"userIds": {"DavHau": 42}}),
|
||||
],
|
||||
)
|
||||
def test_set_some_option(
|
||||
args: list[str],
|
||||
expected: dict[str, Any],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setenv("CLAN_OPTIONS_FILE", example_options)
|
||||
|
||||
# create temporary file for out_file
|
||||
with tempfile.NamedTemporaryFile() as out_file:
|
||||
with open(out_file.name, "w") as f:
|
||||
json.dump({}, f)
|
||||
cli = Cli()
|
||||
cli.run(["config", "--quiet", "--settings-file", out_file.name] + args)
|
||||
json_out = json.loads(open(out_file.name).read())
|
||||
assert json_out == expected
|
||||
|
||||
|
||||
def test_walk_jsonschema_all_types() -> None:
|
||||
schema = dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
array=dict(
|
||||
type="array",
|
||||
items=dict(
|
||||
type="string",
|
||||
),
|
||||
),
|
||||
boolean=dict(type="boolean"),
|
||||
integer=dict(type="integer"),
|
||||
number=dict(type="number"),
|
||||
string=dict(type="string"),
|
||||
),
|
||||
)
|
||||
expected = {
|
||||
"array": list[str],
|
||||
"boolean": bool,
|
||||
"integer": int,
|
||||
"number": float,
|
||||
"string": str,
|
||||
}
|
||||
assert config.parsing.options_types_from_schema(schema) == expected
|
||||
|
||||
|
||||
def test_walk_jsonschema_nested() -> None:
|
||||
schema = dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
name=dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
first=dict(type="string"),
|
||||
last=dict(type="string"),
|
||||
),
|
||||
),
|
||||
age=dict(type="integer"),
|
||||
),
|
||||
)
|
||||
expected = {
|
||||
"age": int,
|
||||
"name.first": str,
|
||||
"name.last": str,
|
||||
}
|
||||
assert config.parsing.options_types_from_schema(schema) == expected
|
||||
|
||||
|
||||
# test walk_jsonschema with dynamic attributes (e.g. "additionalProperties")
|
||||
def test_walk_jsonschema_dynamic_attrs() -> None:
|
||||
schema = dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
age=dict(type="integer"),
|
||||
users=dict(
|
||||
type="object",
|
||||
additionalProperties=dict(type="string"),
|
||||
),
|
||||
),
|
||||
)
|
||||
expected = {
|
||||
"age": int,
|
||||
"users.<name>": str, # <name> is a placeholder for any string
|
||||
}
|
||||
assert config.parsing.options_types_from_schema(schema) == expected
|
||||
|
||||
|
||||
def test_type_from_schema_path_simple() -> None:
|
||||
schema = dict(
|
||||
type="boolean",
|
||||
)
|
||||
assert parsing.type_from_schema_path(schema, []) == bool
|
||||
|
||||
|
||||
def test_type_from_schema_path_nested() -> None:
|
||||
schema = dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
name=dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
first=dict(type="string"),
|
||||
last=dict(type="string"),
|
||||
),
|
||||
),
|
||||
age=dict(type="integer"),
|
||||
),
|
||||
)
|
||||
assert parsing.type_from_schema_path(schema, ["age"]) == int
|
||||
assert parsing.type_from_schema_path(schema, ["name", "first"]) == str
|
||||
|
||||
|
||||
def test_type_from_schema_path_dynamic_attrs() -> None:
|
||||
schema = dict(
|
||||
type="object",
|
||||
properties=dict(
|
||||
age=dict(type="integer"),
|
||||
users=dict(
|
||||
type="object",
|
||||
additionalProperties=dict(type="string"),
|
||||
),
|
||||
),
|
||||
)
|
||||
assert parsing.type_from_schema_path(schema, ["age"]) == int
|
||||
assert parsing.type_from_schema_path(schema, ["users", "foo"]) == str
|
||||
|
||||
|
||||
# test the cast function with simple types
|
||||
def test_cast_simple() -> None:
|
||||
assert config.cast(["true"], bool, "foo-option") is True
|
||||
44
pkgs/clan-cli/tests/config/test_machine_schema.py
Normal file
44
pkgs/clan-cli/tests/config/test_machine_schema.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
|
||||
import pytest
|
||||
|
||||
from clan_cli.config import machine
|
||||
|
||||
CLAN_NIXPKGS = os.environ.get("CLAN_NIXPKGS", "")
|
||||
if CLAN_NIXPKGS == "":
|
||||
raise Exception("CLAN_NIXPKGS not set")
|
||||
|
||||
|
||||
# fixture for the example flake located under ./example_flake
|
||||
# The flake is a template that is copied to a temporary location.
|
||||
# Variables like __CLAN_NIXPKGS__ are replaced with the value of the
|
||||
# CLAN_NIXPKGS environment variable.
|
||||
@pytest.fixture
|
||||
def flake_dir() -> Generator[Path, None, None]:
|
||||
template = Path(__file__).parent / "example_flake"
|
||||
# copy the template to a new temporary location
|
||||
with tempfile.TemporaryDirectory() as tmpdir_:
|
||||
tmpdir = Path(tmpdir_)
|
||||
for path in template.glob("**/*"):
|
||||
if path.is_dir():
|
||||
(tmpdir / path.relative_to(template)).mkdir()
|
||||
else:
|
||||
(tmpdir / path.relative_to(template)).write_text(path.read_text())
|
||||
# in the flake.nix file replace the string __CLAN_URL__ with the the clan flake
|
||||
# provided by get_clan_flake_toplevel
|
||||
flake_nix = tmpdir / "flake.nix"
|
||||
flake_nix.write_text(
|
||||
flake_nix.read_text().replace("__CLAN_NIXPKGS__", CLAN_NIXPKGS)
|
||||
)
|
||||
yield tmpdir
|
||||
|
||||
|
||||
def test_schema_for_machine(
|
||||
flake_dir: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.chdir(tmp_path)
|
||||
schema = machine.schema_for_machine("machine1", flake_dir)
|
||||
assert "properties" in schema
|
||||
Reference in New Issue
Block a user