mirror of
https://github.com/nix-community/disko.git
synced 2026-11-30 13:03:28 +01:00
disko2: Validate config for type-safety in python
This commit is contained in:
1 parent
ae7891e21f
commit
6651792c28
15 files changed
+299
-83
No files matched your search
@@ -112,6 +112,11 @@ def _parse_simple_type(type_str: str) -> str:
|
||||
return "int"
|
||||
case "anything":
|
||||
return "Any"
|
||||
# Set up discriminated unions to reduce error messages when validation fails
|
||||
case "deviceType":
|
||||
return '"deviceType" = Field(..., discriminator="type")'
|
||||
case "partitionType":
|
||||
return '"partitionType" = Field(..., discriminator="type")'
|
||||
case _:
|
||||
# Probably a type alias, needs to be quoted in case the type is defined later
|
||||
return f'"{type_str}"'
|
||||
@@ -212,7 +217,7 @@ def generate_python_code(schema: JsonDict) -> io.StringIO:
|
||||
# Disable auto-formatting for this file
|
||||
# fmt: off
|
||||
from typing import Any, Literal, Union
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
"""
|
||||
|
||||
+10
-4
@@ -6,7 +6,8 @@ from typing import Any, Literal, cast
|
||||
|
||||
from disko.mode_dev import run_dev
|
||||
from disko.mode_generate import run_generate
|
||||
from disko_lib.eval_config import eval_config
|
||||
from disko_lib.config_type import DiskoConfig
|
||||
from disko_lib.eval_config import eval_and_validate_config
|
||||
from disko_lib.logging import LOGGER, debug, info
|
||||
from disko_lib.messages.msgs import err_missing_mode
|
||||
from disko_lib.result import DiskoError, DiskoResult, exit_on_error
|
||||
@@ -46,13 +47,13 @@ MODE_DESCRIPTION: dict[Mode, str] = {
|
||||
|
||||
def run_apply(
|
||||
*, mode: str, disko_file: str | None, flake: str | None, **_kwargs: dict[str, Any]
|
||||
) -> DiskoResult[JsonDict]:
|
||||
return eval_config(disko_file=disko_file, flake=flake)
|
||||
) -> DiskoResult[DiskoConfig]:
|
||||
return eval_and_validate_config(disko_file=disko_file, flake=flake)
|
||||
|
||||
|
||||
def run(
|
||||
args: argparse.Namespace,
|
||||
) -> DiskoResult[None | JsonDict]:
|
||||
) -> DiskoResult[None | JsonDict | DiskoConfig]:
|
||||
if cast(bool, args.verbose):
|
||||
LOGGER.setLevel("DEBUG")
|
||||
debug("Enabled debug logging.")
|
||||
@@ -129,6 +130,11 @@ def parse_args() -> argparse.Namespace:
|
||||
"eval", help="Evaluate a disko configuration and print the result as JSON"
|
||||
)
|
||||
add_common_apply_args(dev_eval_parser)
|
||||
dev_validate_parser = dev_parsers.add_parser(
|
||||
"validate",
|
||||
help="Validate a disko configuration file or flake",
|
||||
)
|
||||
add_common_apply_args(dev_validate_parser)
|
||||
|
||||
return root_parser.parse_args()
|
||||
|
||||
|
||||
+19
-3
@@ -3,7 +3,7 @@ import json
|
||||
from typing import Any, cast
|
||||
|
||||
from disko_lib.ansi import Colors
|
||||
from disko_lib.eval_config import eval_config
|
||||
from disko_lib.eval_config import eval_and_validate_config, eval_config_as_json
|
||||
from disko_lib.messages.msgs import err_missing_mode
|
||||
from disko_lib.result import DiskoError, DiskoSuccess, DiskoResult
|
||||
from disko_lib.types.device import run_lsblk
|
||||
@@ -31,7 +31,7 @@ def run_dev_ansi() -> DiskoResult[None]:
|
||||
def run_dev_eval(
|
||||
*, disko_file: str | None, flake: str | None, **_: Any
|
||||
) -> DiskoResult[None]:
|
||||
result = eval_config(disko_file=disko_file, flake=flake)
|
||||
result = eval_config_as_json(disko_file=disko_file, flake=flake)
|
||||
|
||||
if isinstance(result, DiskoError):
|
||||
return result
|
||||
@@ -40,6 +40,18 @@ def run_dev_eval(
|
||||
return DiskoSuccess(None, "run disko dev eval")
|
||||
|
||||
|
||||
def run_dev_validate(
|
||||
*, disko_file: str | None, flake: str | None, **_: Any
|
||||
) -> DiskoResult[None]:
|
||||
result = eval_and_validate_config(disko_file=disko_file, flake=flake)
|
||||
|
||||
if isinstance(result, DiskoError):
|
||||
return result
|
||||
|
||||
print(result.value.model_dump_json(indent=2))
|
||||
return DiskoSuccess(None, "run disko dev validate")
|
||||
|
||||
|
||||
def run_dev(args: argparse.Namespace) -> DiskoResult[None]:
|
||||
match cast(str | None, args.dev_command):
|
||||
case "lsblk":
|
||||
@@ -48,7 +60,11 @@ def run_dev(args: argparse.Namespace) -> DiskoResult[None]:
|
||||
return run_dev_ansi()
|
||||
case "eval":
|
||||
return run_dev_eval(**vars(args)) # type: ignore[misc]
|
||||
case "validate":
|
||||
return run_dev_validate(**vars(args)) # type: ignore[misc]
|
||||
case _:
|
||||
return DiskoError.single_message(
|
||||
err_missing_mode, "select mode", valid_modes=["lsblk", "ansi", "eval"]
|
||||
err_missing_mode,
|
||||
"select mode",
|
||||
valid_modes=["lsblk", "ansi", "eval", "validate"],
|
||||
)
|
||||
@@ -4,46 +4,34 @@
|
||||
# Disable auto-formatting for this file
|
||||
# fmt: off
|
||||
from typing import Any, Literal, Union
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
|
||||
|
||||
class btrfs_subvolumes_options_swap_options(BaseModel):
|
||||
class btrfs_subvolumes_swap(BaseModel):
|
||||
options: list[str]
|
||||
path: str
|
||||
priority: None | int
|
||||
size: str
|
||||
|
||||
|
||||
class btrfs_subvolumes_options_swap(BaseModel):
|
||||
options: btrfs_subvolumes_options_swap_options
|
||||
|
||||
|
||||
class btrfs_subvolumes_options(BaseModel):
|
||||
class btrfs_subvolumes(BaseModel):
|
||||
extraArgs: list[str]
|
||||
mountOptions: list[str]
|
||||
mountpoint: None | str
|
||||
name: str
|
||||
swap: dict[str, btrfs_subvolumes_options_swap]
|
||||
swap: dict[str, btrfs_subvolumes_swap]
|
||||
type: Literal['btrfs_subvol']
|
||||
|
||||
|
||||
class btrfs_subvolumes(BaseModel):
|
||||
options: btrfs_subvolumes_options
|
||||
|
||||
|
||||
class btrfs_swap_options(BaseModel):
|
||||
class btrfs_swap(BaseModel):
|
||||
options: list[str]
|
||||
path: str
|
||||
priority: None | int
|
||||
size: str
|
||||
|
||||
|
||||
class btrfs_swap(BaseModel):
|
||||
options: btrfs_swap_options
|
||||
|
||||
|
||||
class btrfs(BaseModel):
|
||||
device: str
|
||||
extraArgs: list[str]
|
||||
@@ -56,10 +44,10 @@ class btrfs(BaseModel):
|
||||
|
||||
|
||||
|
||||
deviceType = Union["btrfs", "filesystem", "gpt", "luks", "lvm_pv", "mdraid", "swap", "table", "zfs"]
|
||||
deviceType = None | Union["btrfs", "filesystem", "gpt", "luks", "lvm_pv", "mdraid", "swap", "table", "zfs"]
|
||||
|
||||
class disk(BaseModel):
|
||||
content: "deviceType"
|
||||
content: "deviceType" = Field(..., discriminator="type")
|
||||
device: str
|
||||
imageName: str
|
||||
imageSize: str
|
||||
@@ -78,22 +66,18 @@ class filesystem(BaseModel):
|
||||
|
||||
|
||||
|
||||
class gpt_partitions_options_hybrid_options(BaseModel):
|
||||
class gpt_partitions_hybrid(BaseModel):
|
||||
mbrBootableFlag: bool
|
||||
mbrPartitionType: None | str
|
||||
|
||||
|
||||
class gpt_partitions_options_hybrid(BaseModel):
|
||||
options: gpt_partitions_options_hybrid_options
|
||||
|
||||
|
||||
class gpt_partitions_options(BaseModel):
|
||||
class gpt_partitions(BaseModel):
|
||||
_index: int
|
||||
alignment: int
|
||||
content: "partitionType"
|
||||
content: "partitionType" = Field(..., discriminator="type")
|
||||
device: str
|
||||
end: str
|
||||
hybrid: None | gpt_partitions_options_hybrid
|
||||
hybrid: None | gpt_partitions_hybrid
|
||||
label: str
|
||||
name: str
|
||||
priority: int
|
||||
@@ -102,10 +86,6 @@ class gpt_partitions_options(BaseModel):
|
||||
type: Union[str, str]
|
||||
|
||||
|
||||
class gpt_partitions(BaseModel):
|
||||
options: gpt_partitions_options
|
||||
|
||||
|
||||
class gpt(BaseModel):
|
||||
device: str
|
||||
efiGptPartitionFirst: bool
|
||||
@@ -116,7 +96,7 @@ class gpt(BaseModel):
|
||||
class luks(BaseModel):
|
||||
additionalKeyFiles: list[str]
|
||||
askPassword: bool
|
||||
content: "deviceType"
|
||||
content: "deviceType" = Field(..., discriminator="type")
|
||||
device: str
|
||||
extraFormatArgs: list[str]
|
||||
extraOpenArgs: list[str]
|
||||
@@ -136,8 +116,8 @@ class lvm_pv(BaseModel):
|
||||
|
||||
|
||||
|
||||
class lvm_vg_lvs_options(BaseModel):
|
||||
content: "partitionType"
|
||||
class lvm_vg_lvs(BaseModel):
|
||||
content: "partitionType" = Field(..., discriminator="type")
|
||||
extraArgs: list[str]
|
||||
lvm_type: None | Literal['mirror', 'raid0', 'raid1', 'raid4', 'raid5', 'raid6', 'thin-pool', 'thinlv']
|
||||
name: str
|
||||
@@ -146,10 +126,6 @@ class lvm_vg_lvs_options(BaseModel):
|
||||
size: str
|
||||
|
||||
|
||||
class lvm_vg_lvs(BaseModel):
|
||||
options: lvm_vg_lvs_options
|
||||
|
||||
|
||||
class lvm_vg(BaseModel):
|
||||
lvs: dict[str, lvm_vg_lvs]
|
||||
name: str
|
||||
@@ -157,7 +133,7 @@ class lvm_vg(BaseModel):
|
||||
|
||||
|
||||
class mdadm(BaseModel):
|
||||
content: "deviceType"
|
||||
content: "deviceType" = Field(..., discriminator="type")
|
||||
level: int
|
||||
metadata: Literal['1', '1.0', '1.1', '1.2', 'default', 'ddf', 'imsm']
|
||||
name: str
|
||||
@@ -180,7 +156,7 @@ class nodev(BaseModel):
|
||||
|
||||
|
||||
|
||||
partitionType = Union["btrfs", "filesystem", "luks", "lvm_pv", "mdraid", "swap", "zfs"]
|
||||
partitionType = None | Union["btrfs", "filesystem", "luks", "lvm_pv", "mdraid", "swap", "zfs"]
|
||||
|
||||
class swap(BaseModel):
|
||||
device: str
|
||||
@@ -195,10 +171,10 @@ class swap(BaseModel):
|
||||
|
||||
|
||||
|
||||
class table_partitions_options(BaseModel):
|
||||
class table_partitions(BaseModel):
|
||||
_index: int
|
||||
bootable: bool
|
||||
content: "partitionType"
|
||||
content: "partitionType" = Field(..., discriminator="type")
|
||||
end: str
|
||||
flags: list[str]
|
||||
fs_type: None | Literal['btrfs', 'ext2', 'ext3', 'ext4', 'fat16', 'fat32', 'hfs', 'hfs+', 'linux-swap', 'ntfs', 'reiserfs', 'udf', 'xfs']
|
||||
@@ -207,10 +183,6 @@ class table_partitions_options(BaseModel):
|
||||
start: str
|
||||
|
||||
|
||||
class table_partitions(BaseModel):
|
||||
options: table_partitions_options
|
||||
|
||||
|
||||
class table(BaseModel):
|
||||
device: str
|
||||
format: Literal['gpt', 'msdos']
|
||||
@@ -233,7 +205,7 @@ class zfs_fs(BaseModel):
|
||||
|
||||
|
||||
class zfs_volume(BaseModel):
|
||||
content: "partitionType"
|
||||
content: "partitionType" = Field(..., discriminator="type")
|
||||
mountOptions: list[str]
|
||||
name: str
|
||||
options: dict[str, str]
|
||||
|
||||
@@ -664,10 +664,10 @@ let
|
||||
str = "str";
|
||||
bool = "bool";
|
||||
int = "int";
|
||||
submodule = x: x {
|
||||
submodule = x: (x {
|
||||
inherit (diskoLib.typesSerializerLib) lib config options;
|
||||
name = "<self.name>";
|
||||
};
|
||||
}).options;
|
||||
};
|
||||
};
|
||||
diskoLib = {
|
||||
@@ -692,18 +692,12 @@ let
|
||||
(diskoLib.serializeType (import ./types/${file} diskoLib.typesSerializerLib))
|
||||
)
|
||||
(lib.filter (name: lib.hasSuffix ".nix" name) (lib.attrNames (builtins.readDir ./types)))
|
||||
) // {
|
||||
partitionType = {
|
||||
type = "oneOf";
|
||||
types = lib.attrNames diskoLib._partitionTypes;
|
||||
"__isCompositeType" = true;
|
||||
};
|
||||
deviceType = {
|
||||
type = "oneOf";
|
||||
types = lib.attrNames diskoLib._deviceTypes;
|
||||
"__isCompositeType" = true;
|
||||
};
|
||||
};
|
||||
) // (
|
||||
let types = diskoLib.typesSerializerLib.lib.types; in {
|
||||
partitionType = types.nullOr (types.oneOf (lib.attrNames diskoLib._partitionTypes));
|
||||
deviceType = types.nullOr (types.oneOf (lib.attrNames diskoLib._deviceTypes));
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
outputs =
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import cast
|
||||
from typing import Any, cast
|
||||
|
||||
from pydantic import ValidationError
|
||||
|
||||
from disko_lib.config_type import DiskoConfig
|
||||
from disko_lib.messages.bugs import bug_validate_config_failed
|
||||
|
||||
from .json_types import JsonDict
|
||||
|
||||
@@ -90,7 +95,9 @@ def _eval_flake(flake_uri: str) -> DiskoResult[JsonDict]:
|
||||
return _eval_config({"flake": flake, "flakeAttr": flake_attr})
|
||||
|
||||
|
||||
def eval_config(*, disko_file: str | None, flake: str | None) -> DiskoResult[JsonDict]:
|
||||
def eval_config_as_json(
|
||||
*, disko_file: str | None, flake: str | None
|
||||
) -> DiskoResult[JsonDict]:
|
||||
# match would be nicer, but mypy doesn't understand type narrowing in tuples
|
||||
if not disko_file and not flake:
|
||||
return DiskoError.single_message(err_missing_arguments, "validate args")
|
||||
@@ -100,3 +107,24 @@ def eval_config(*, disko_file: str | None, flake: str | None) -> DiskoResult[Jso
|
||||
return _eval_disko_file(Path(disko_file))
|
||||
|
||||
return DiskoError.single_message(err_too_many_arguments, "validate args")
|
||||
|
||||
|
||||
def eval_and_validate_config(
|
||||
*, disko_file: str | None, flake: str | None
|
||||
) -> DiskoResult[DiskoConfig]:
|
||||
json_config = eval_config_as_json(disko_file=disko_file, flake=flake)
|
||||
|
||||
if isinstance(json_config, DiskoError):
|
||||
return json_config
|
||||
|
||||
try:
|
||||
result = DiskoConfig(**cast(dict[str, Any], json_config.value)) # type: ignore[misc]
|
||||
except ValidationError as e:
|
||||
return DiskoError.single_message(
|
||||
bug_validate_config_failed,
|
||||
"validate disko config",
|
||||
error=e,
|
||||
config=json_config.value,
|
||||
)
|
||||
|
||||
return DiskoSuccess(result, "validate disko config")
|
||||
@@ -9,7 +9,6 @@ from typing import (
|
||||
Literal,
|
||||
ParamSpec,
|
||||
TypeAlias,
|
||||
cast,
|
||||
)
|
||||
|
||||
from .ansi import Colors
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import json
|
||||
import pydantic
|
||||
from disko_lib.logging import ReadableMessage
|
||||
from disko_lib.messages.colors import INVALID, RESET, VALUE
|
||||
from ..json_types import JsonDict
|
||||
|
||||
|
||||
def __bug_help_message(error_code: str) -> ReadableMessage:
|
||||
@@ -27,3 +31,34 @@ def bug_success_without_context(*, value: object) -> list[ReadableMessage]:
|
||||
),
|
||||
__bug_help_message("bug_success_without_context"),
|
||||
]
|
||||
|
||||
|
||||
def bug_validate_config_failed(
|
||||
*, config: JsonDict, error: pydantic.ValidationError
|
||||
) -> list[ReadableMessage]:
|
||||
errors_printed = json.dumps(error.errors(), indent=2) # type: ignore[misc]
|
||||
return [
|
||||
ReadableMessage(
|
||||
"info",
|
||||
f"""
|
||||
Evaluated configuration:
|
||||
{json.dumps(config, indent=2)}
|
||||
""",
|
||||
),
|
||||
ReadableMessage(
|
||||
"error",
|
||||
f"""
|
||||
Validation errors:
|
||||
{errors_printed}
|
||||
""",
|
||||
),
|
||||
ReadableMessage(
|
||||
"bug",
|
||||
f"""
|
||||
Configuration was evaluated successfully, but failed validation!
|
||||
Most likely, the types in python are out-of-sync with those in nix.
|
||||
The {INVALID}validation errors{RESET} and the {VALUE}evaluated configuration{RESET} are printed above.
|
||||
""",
|
||||
),
|
||||
__bug_help_message("bug_validate_config_failed"),
|
||||
]
|
||||
@@ -1,8 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
import typing
|
||||
from typing import cast
|
||||
|
||||
from ..result import DiskoError, DiskoResult, DiskoSuccess
|
||||
from ..run_cmd import run
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from typing import cast
|
||||
from disko_lib.messages.msgs import (
|
||||
err_unsupported_pttype,
|
||||
warn_generate_partial_failure,
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"disk": {
|
||||
"main": {
|
||||
"content": {
|
||||
"device": "/dev/disk/by-id/some-disk-id",
|
||||
"efiGptPartitionFirst": true,
|
||||
"partitions": {
|
||||
"ESP": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"extraArgs": [],
|
||||
"format": "vfat",
|
||||
"mountOptions": [
|
||||
"umask=0077"
|
||||
],
|
||||
"mountpoint": "/boot",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"end": "+500M",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-ESP",
|
||||
"name": "ESP",
|
||||
"priority": 1000,
|
||||
"size": "500M",
|
||||
"start": "0",
|
||||
"type": "EF00"
|
||||
},
|
||||
"root": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"extraArgs": [],
|
||||
"format": "ext4",
|
||||
"mountOptions": [
|
||||
"defaults"
|
||||
],
|
||||
"mountpoint": "/",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"end": "-0",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-root",
|
||||
"name": "root",
|
||||
"priority": 9001,
|
||||
"size": "100%",
|
||||
"start": "0",
|
||||
"type": "8300"
|
||||
}
|
||||
},
|
||||
"type": "gpt"
|
||||
},
|
||||
"device": "/dev/disk/by-id/some-disk-id",
|
||||
"imageName": "main",
|
||||
"imageSize": "2G",
|
||||
"name": "main",
|
||||
"type": "disk"
|
||||
}
|
||||
},
|
||||
"lvm_vg": {},
|
||||
"mdadm": {},
|
||||
"nodev": {},
|
||||
"zpool": {}
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"disk": {
|
||||
"main": {
|
||||
"content": {
|
||||
"device": "/dev/disk/by-id/ata-Samsung_SSD_850_EVO_250GB_S21PNXAGB12345",
|
||||
"efiGptPartitionFirst": true,
|
||||
"partitions": {
|
||||
"ESP": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"extraArgs": [],
|
||||
"format": "vfat",
|
||||
"mountOptions": [
|
||||
"umask=0077"
|
||||
],
|
||||
"mountpoint": "/boot",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-ESP",
|
||||
"end": "+512M",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-ESP",
|
||||
"name": "ESP",
|
||||
"priority": 1000,
|
||||
"size": "512M",
|
||||
"start": "0",
|
||||
"type": "EF00"
|
||||
},
|
||||
"boot": {
|
||||
"alignment": 0,
|
||||
"content": null,
|
||||
"device": "/dev/disk/by-partlabel/disk-main-boot",
|
||||
"end": "+1M",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-boot",
|
||||
"name": "boot",
|
||||
"priority": 100,
|
||||
"size": "1M",
|
||||
"start": "0",
|
||||
"type": "EF02"
|
||||
},
|
||||
"root": {
|
||||
"alignment": 0,
|
||||
"content": {
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"extraArgs": [],
|
||||
"format": "ext4",
|
||||
"mountOptions": [
|
||||
"defaults"
|
||||
],
|
||||
"mountpoint": "/",
|
||||
"type": "filesystem"
|
||||
},
|
||||
"device": "/dev/disk/by-partlabel/disk-main-root",
|
||||
"end": "-0",
|
||||
"hybrid": null,
|
||||
"label": "disk-main-root",
|
||||
"name": "root",
|
||||
"priority": 9001,
|
||||
"size": "100%",
|
||||
"start": "0",
|
||||
"type": "8300"
|
||||
}
|
||||
},
|
||||
"type": "gpt"
|
||||
},
|
||||
"device": "/dev/disk/by-id/ata-Samsung_SSD_850_EVO_250GB_S21PNXAGB12345",
|
||||
"imageName": "main",
|
||||
"imageSize": "2G",
|
||||
"name": "main",
|
||||
"type": "disk"
|
||||
}
|
||||
},
|
||||
"lvm_vg": {},
|
||||
"mdadm": {},
|
||||
"nodev": {},
|
||||
"zpool": {}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
import readline
|
||||
from typing import cast
|
||||
|
||||
from disko_lib.eval_config import eval_config
|
||||
from disko_lib.eval_config import eval_config_as_json, eval_and_validate_config
|
||||
from disko_lib.messages.msgs import err_missing_arguments, err_too_many_arguments
|
||||
from disko_lib.result import DiskoError, DiskoSuccess
|
||||
from disko_lib.json_types import JsonDict
|
||||
@@ -13,31 +14,48 @@ assert (ROOT_DIR / "flake.nix").exists()
|
||||
|
||||
|
||||
def test_eval_config_missing_arguments() -> None:
|
||||
result = eval_config(disko_file=None, flake=None)
|
||||
result = eval_config_as_json(disko_file=None, flake=None)
|
||||
assert isinstance(result, DiskoError)
|
||||
assert result.messages[0].is_message(err_missing_arguments)
|
||||
assert result.context == "validate args"
|
||||
|
||||
|
||||
def test_eval_config_too_many_arguments() -> None:
|
||||
result = eval_config(disko_file="foo", flake="bar")
|
||||
result = eval_config_as_json(disko_file="foo", flake="bar")
|
||||
assert isinstance(result, DiskoError)
|
||||
assert result.messages[0].is_message(err_too_many_arguments)
|
||||
assert result.context == "validate args"
|
||||
|
||||
|
||||
def test_eval_config_disk_file() -> None:
|
||||
def test_eval_config_disko_file() -> None:
|
||||
disko_file_path = ROOT_DIR / "example" / "simple-efi.nix"
|
||||
result = eval_config(disko_file=str(disko_file_path), flake=None)
|
||||
result = eval_config_as_json(disko_file=str(disko_file_path), flake=None)
|
||||
assert isinstance(result, DiskoSuccess)
|
||||
with open(CURRENT_DIR / "file-simple-efi-result.json") as f:
|
||||
with open(CURRENT_DIR / "file-simple-efi-eval-result.json") as f:
|
||||
expected_result = cast(JsonDict, json.load(f))
|
||||
assert result.value == expected_result
|
||||
|
||||
|
||||
def test_eval_config_flake_testmachine() -> None:
|
||||
result = eval_config(disko_file=None, flake=f"{ROOT_DIR}#testmachine")
|
||||
result = eval_config_as_json(disko_file=None, flake=f"{ROOT_DIR}#testmachine")
|
||||
assert isinstance(result, DiskoSuccess)
|
||||
with open(CURRENT_DIR / "flake-testmachine-result.json") as f:
|
||||
with open(CURRENT_DIR / "flake-testmachine-eval-result.json") as f:
|
||||
expected_result = cast(JsonDict, json.load(f))
|
||||
assert result.value == expected_result
|
||||
|
||||
|
||||
def test_eval_and_validate_config_disko_file() -> None:
|
||||
disko_file_path = ROOT_DIR / "example" / "simple-efi.nix"
|
||||
result = eval_and_validate_config(disko_file=str(disko_file_path), flake=None)
|
||||
assert isinstance(result, DiskoSuccess)
|
||||
with open(CURRENT_DIR / "file-simple-efi-validate-result.json") as f:
|
||||
expected_result = f.read()
|
||||
assert json.loads(result.value.model_dump_json()) == json.loads(expected_result) # type: ignore[misc]
|
||||
|
||||
|
||||
def test_eval_and_validate_flake_testmachine() -> None:
|
||||
result = eval_and_validate_config(disko_file=None, flake=f"{ROOT_DIR}#testmachine")
|
||||
assert isinstance(result, DiskoSuccess)
|
||||
with open(CURRENT_DIR / "flake-testmachine-validate-result.json") as f:
|
||||
expected_result = f.read()
|
||||
assert json.loads(result.value.model_dump_json()) == json.loads(expected_result) # type: ignore[misc]
|
||||
Reference in new issue
Block a user