mirror of
https://github.com/nix-community/disko.git
synced 2026-12-01 05:23: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
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