2023-08-09 18:05:31 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
2023-08-24 15:53:33 +00:00
|
|
|
from cli import Cli
|
2023-10-23 20:34:43 +00:00
|
|
|
from fixtures_flakes import FlakeForTest
|
2023-08-09 18:05:31 +00:00
|
|
|
|
|
|
|
from clan_cli import config
|
2023-08-15 11:24:25 +00:00
|
|
|
from clan_cli.config import parsing
|
2023-09-24 13:24:32 +00:00
|
|
|
from clan_cli.errors import ClanError
|
2023-08-09 18:05:31 +00:00
|
|
|
|
2023-08-16 10:13:48 +00:00
|
|
|
example_options = f"{Path(config.__file__).parent}/jsonschema/options.json"
|
2023-08-09 18:05:31 +00:00
|
|
|
|
|
|
|
|
2023-08-26 08:32:04 +00:00
|
|
|
def test_configure_machine(
|
2023-10-21 15:19:06 +00:00
|
|
|
test_flake: FlakeForTest,
|
|
|
|
temporary_home: Path,
|
2023-08-26 08:32:04 +00:00
|
|
|
capsys: pytest.CaptureFixture,
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
) -> None:
|
|
|
|
cli = Cli()
|
2023-10-25 17:23:28 +00:00
|
|
|
|
2023-08-26 08:32:04 +00:00
|
|
|
# clear the output buffer
|
|
|
|
capsys.readouterr()
|
|
|
|
# read a option value
|
2023-11-15 13:28:40 +00:00
|
|
|
cli.run(
|
|
|
|
[
|
2024-05-29 08:10:10 +00:00
|
|
|
"config",
|
2023-11-15 13:28:40 +00:00
|
|
|
"--flake",
|
|
|
|
str(test_flake.path),
|
|
|
|
"-m",
|
|
|
|
"machine1",
|
|
|
|
"clan.jitsi.enable",
|
|
|
|
]
|
|
|
|
)
|
2023-10-25 17:23:28 +00:00
|
|
|
|
2023-08-26 08:32:04 +00:00
|
|
|
# read the output
|
2024-05-07 11:16:33 +00:00
|
|
|
assert capsys.readouterr().out == "false\n"
|
2023-08-26 08:32:04 +00:00
|
|
|
|
|
|
|
|
2023-08-10 22:05:52 +00:00
|
|
|
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,
|
|
|
|
}
|
2023-08-15 11:24:25 +00:00
|
|
|
assert config.parsing.options_types_from_schema(schema) == expected
|
2023-08-10 22:05:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2023-08-15 11:24:25 +00:00
|
|
|
assert config.parsing.options_types_from_schema(schema) == expected
|
2023-08-10 22:05:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
}
|
2023-08-15 11:24:25 +00:00
|
|
|
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
|
2023-08-16 10:13:48 +00:00
|
|
|
|
|
|
|
|
2023-09-24 13:24:32 +00:00
|
|
|
def test_map_type() -> None:
|
|
|
|
with pytest.raises(ClanError):
|
|
|
|
config.map_type("foo")
|
|
|
|
assert config.map_type("string") == str
|
|
|
|
assert config.map_type("integer") == int
|
|
|
|
assert config.map_type("boolean") == bool
|
|
|
|
assert config.map_type("attribute set of string") == dict[str, str]
|
|
|
|
assert config.map_type("attribute set of integer") == dict[str, int]
|
2023-11-29 11:40:48 +00:00
|
|
|
assert config.map_type("null or string") == str | None
|
2023-09-24 13:24:32 +00:00
|
|
|
|
|
|
|
|
2023-08-16 10:13:48 +00:00
|
|
|
# test the cast function with simple types
|
2023-09-24 13:24:32 +00:00
|
|
|
def test_cast() -> None:
|
|
|
|
assert (
|
2023-11-30 13:13:32 +00:00
|
|
|
config.cast(value=["true"], input_type=bool, opt_description="foo-option")
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
config.cast(value=["null"], input_type=str | None, opt_description="foo-option")
|
2023-09-24 13:24:32 +00:00
|
|
|
is None
|
|
|
|
)
|
|
|
|
assert (
|
2023-11-30 13:13:32 +00:00
|
|
|
config.cast(value=["bar"], input_type=str | None, opt_description="foo-option")
|
2023-09-24 13:24:32 +00:00
|
|
|
== "bar"
|
|
|
|
)
|
2023-09-24 12:04:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"option,value,options,expected",
|
|
|
|
[
|
|
|
|
("foo.bar", ["baz"], {"foo.bar": {"type": "str"}}, ("foo.bar", ["baz"])),
|
|
|
|
("foo.bar", ["baz"], {"foo": {"type": "attrs"}}, ("foo", {"bar": ["baz"]})),
|
|
|
|
(
|
|
|
|
"users.users.my-user.name",
|
|
|
|
["my-name"],
|
|
|
|
{"users.users.<name>.name": {"type": "str"}},
|
|
|
|
("users.users.<name>.name", ["my-name"]),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"foo.bar.baz.bum",
|
|
|
|
["val"],
|
|
|
|
{"foo.<name>.baz": {"type": "attrs"}},
|
|
|
|
("foo.<name>.baz", {"bum": ["val"]}),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"userIds.DavHau",
|
|
|
|
["42"],
|
|
|
|
{"userIds": {"type": "attrs"}},
|
|
|
|
("userIds", {"DavHau": ["42"]}),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_find_option(option: str, value: list, options: dict, expected: tuple) -> None:
|
|
|
|
assert config.find_option(option, value, options) == expected
|