From c5d1a42af70d76e6eff8a604491d612f731d5127 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Fri, 8 Nov 2024 21:39:34 +0100 Subject: [PATCH] tests: Put tests with snapshots into directories This makes the tests more readable and easier to browse. Making sure the tests always run fine, no matter from which directory they're started is a little detail we should either test for or document. --- tests/disko_lib/eval_config/README.md | 17 ++ .../eval_config/file-simple-efi-result.json | 82 ++++++++ .../eval_config/flake-testmachine-result.json | 95 +++++++++ .../disko_lib/eval_config/test_eval_config.py | 41 ++++ tests/disko_lib/test_eval_config.py | 196 ------------------ ...al_failure_dos_table-generate-result.json} | 0 ...rtial_failure_dos_table-lsblk-output.json} | 0 .../{ => types_disk}/test_types_disk.py | 4 +- 8 files changed, 237 insertions(+), 198 deletions(-) create mode 100644 tests/disko_lib/eval_config/README.md create mode 100644 tests/disko_lib/eval_config/file-simple-efi-result.json create mode 100644 tests/disko_lib/eval_config/flake-testmachine-result.json create mode 100644 tests/disko_lib/eval_config/test_eval_config.py delete mode 100644 tests/disko_lib/test_eval_config.py rename tests/disko_lib/{generate-result.json => types_disk/partial_failure_dos_table-generate-result.json} (100%) rename tests/disko_lib/{lsblk-output.json => types_disk/partial_failure_dos_table-lsblk-output.json} (100%) rename tests/disko_lib/{ => types_disk}/test_types_disk.py (87%) diff --git a/tests/disko_lib/eval_config/README.md b/tests/disko_lib/eval_config/README.md new file mode 100644 index 0000000..02aa030 --- /dev/null +++ b/tests/disko_lib/eval_config/README.md @@ -0,0 +1,17 @@ +# eval_config.py tests + +If you change something about the evaluation and need to update one of the +result files here, you can run a command like this: + + ./disko2 dev eval example/simple-efi.nix > tests/disko_lib/eval_config/file-simple-efi-result.json + +Change the paths depending on the example whose evaluation result changed. + +If you're thinking "this sounds like snapshots to me" and "isn't there a pytest plugin for this?", +then you'd be correct, but [pytest-insta](https://github.com/vberlier/pytest-insta) is not packaged +in nixpkgs at the time of writing (2024-11-08). + +If you're reading this, and you +[search nixpkgs for "pytest-insta"](https://search.nixos.org/packages?channel=unstable&query=pytest-insta) +AND this returns the `pytest-insta` package (or there is a new, better snapshotting plugin for pytest), +please open an issue so we can replace this manual process with it! \ No newline at end of file diff --git a/tests/disko_lib/eval_config/file-simple-efi-result.json b/tests/disko_lib/eval_config/file-simple-efi-result.json new file mode 100644 index 0000000..5f07782 --- /dev/null +++ b/tests/disko_lib/eval_config/file-simple-efi-result.json @@ -0,0 +1,82 @@ +{ + "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", + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "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": "/", + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "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" + } + }, + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "type": "gpt" + }, + "device": "/dev/disk/by-id/some-disk-id", + "imageName": "main", + "imageSize": "2G", + "name": "main", + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "type": "disk" + } + }, + "lvm_vg": {}, + "mdadm": {}, + "nodev": {}, + "zpool": {} +} diff --git a/tests/disko_lib/eval_config/flake-testmachine-result.json b/tests/disko_lib/eval_config/flake-testmachine-result.json new file mode 100644 index 0000000..00abc61 --- /dev/null +++ b/tests/disko_lib/eval_config/flake-testmachine-result.json @@ -0,0 +1,95 @@ +{ + "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", + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "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": "/", + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "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" + } + }, + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "type": "gpt" + }, + "device": "/dev/disk/by-id/ata-Samsung_SSD_850_EVO_250GB_S21PNXAGB12345", + "imageName": "main", + "imageSize": "2G", + "name": "main", + "postCreateHook": "", + "postMountHook": "", + "preCreateHook": "", + "preMountHook": "", + "type": "disk" + } + }, + "lvm_vg": {}, + "mdadm": {}, + "nodev": {}, + "zpool": {} +} diff --git a/tests/disko_lib/eval_config/test_eval_config.py b/tests/disko_lib/eval_config/test_eval_config.py new file mode 100644 index 0000000..249439a --- /dev/null +++ b/tests/disko_lib/eval_config/test_eval_config.py @@ -0,0 +1,41 @@ +import json +from pathlib import Path + +from disko_lib.eval_config import eval_config +from disko_lib.messages.msgs import err_missing_arguments, err_too_many_arguments +from disko_lib.result import DiskoError, DiskoSuccess + +CURRENT_DIR = Path(__file__).parent +ROOT_DIR = CURRENT_DIR.parent.parent.parent +assert (ROOT_DIR / "flake.nix").exists() + + +def test_eval_config_missing_arguments() -> None: + result = eval_config(disko_file=None, flake=None) + assert isinstance(result, DiskoError) + assert result.messages[0].factory == err_missing_arguments + assert result.context == "validate args" + + +def test_eval_config_too_many_arguments() -> None: + result = eval_config(disko_file="foo", flake="bar") + assert isinstance(result, DiskoError) + assert result.messages[0].factory == err_too_many_arguments + assert result.context == "validate args" + + +def test_eval_config_disk_file() -> None: + disko_file_path = ROOT_DIR / "example" / "simple-efi.nix" + result = eval_config(disko_file=str(disko_file_path), flake=None) + assert isinstance(result, DiskoSuccess) + with open(CURRENT_DIR / "file-simple-efi-result.json") as f: + expected_result = 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") + assert isinstance(result, DiskoSuccess) + with open(CURRENT_DIR / "flake-testmachine-result.json") as f: + expected_result = json.load(f) + assert result.value == expected_result diff --git a/tests/disko_lib/test_eval_config.py b/tests/disko_lib/test_eval_config.py deleted file mode 100644 index 710c20e..0000000 --- a/tests/disko_lib/test_eval_config.py +++ /dev/null @@ -1,196 +0,0 @@ -from disko_lib.eval_config import eval_config -from disko_lib.messages.msgs import err_missing_arguments, err_too_many_arguments -from disko_lib.result import DiskoError, DiskoSuccess - - -def test_eval_config_missing_arguments() -> None: - result = eval_config(disko_file=None, flake=None) - assert isinstance(result, DiskoError) - assert result.messages[0].factory == err_missing_arguments - assert result.context == "validate args" - - -def test_eval_config_too_many_arguments() -> None: - result = eval_config(disko_file="foo", flake="bar") - assert isinstance(result, DiskoError) - assert result.messages[0].factory == err_too_many_arguments - assert result.context == "validate args" - - -def test_eval_config_disk_file_example_simple_efi() -> None: - result = eval_config(disko_file="example/simple-efi.nix", flake=None) - assert isinstance(result, DiskoSuccess) - assert result.value == { - "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", - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "filesystem", - }, - "device": "/dev/disk/by-partlabel/disk-main-ESP", - "end": "+500M", - "hybrid": None, - "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": "/", - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "filesystem", - }, - "device": "/dev/disk/by-partlabel/disk-main-root", - "end": "-0", - "hybrid": None, - "label": "disk-main-root", - "name": "root", - "priority": 9001, - "size": "100%", - "start": "0", - "type": "8300", - }, - }, - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "gpt", - }, - "device": "/dev/disk/by-id/some-disk-id", - "imageName": "main", - "imageSize": "2G", - "name": "main", - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "disk", - } - }, - "lvm_vg": {}, - "mdadm": {}, - "nodev": {}, - "zpool": {}, - } - - -def test_eval_config_flake_testmachine() -> None: - result = eval_config(disko_file=None, flake=".#testmachine") - assert isinstance(result, DiskoSuccess) - assert result.value == { - "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", - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "filesystem", - }, - "device": "/dev/disk/by-partlabel/disk-main-ESP", - "end": "+512M", - "hybrid": None, - "label": "disk-main-ESP", - "name": "ESP", - "priority": 1000, - "size": "512M", - "start": "0", - "type": "EF00", - }, - "boot": { - "alignment": 0, - "content": None, - "device": "/dev/disk/by-partlabel/disk-main-boot", - "end": "+1M", - "hybrid": None, - "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": "/", - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "filesystem", - }, - "device": "/dev/disk/by-partlabel/disk-main-root", - "end": "-0", - "hybrid": None, - "label": "disk-main-root", - "name": "root", - "priority": 9001, - "size": "100%", - "start": "0", - "type": "8300", - }, - }, - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "gpt", - }, - "device": "/dev/disk/by-id/ata-Samsung_SSD_850_EVO_250GB_S21PNXAGB12345", - "imageName": "main", - "imageSize": "2G", - "name": "main", - "postCreateHook": "", - "postMountHook": "", - "preCreateHook": "", - "preMountHook": "", - "type": "disk", - } - }, - "lvm_vg": {}, - "mdadm": {}, - "nodev": {}, - "zpool": {}, - } diff --git a/tests/disko_lib/generate-result.json b/tests/disko_lib/types_disk/partial_failure_dos_table-generate-result.json similarity index 100% rename from tests/disko_lib/generate-result.json rename to tests/disko_lib/types_disk/partial_failure_dos_table-generate-result.json diff --git a/tests/disko_lib/lsblk-output.json b/tests/disko_lib/types_disk/partial_failure_dos_table-lsblk-output.json similarity index 100% rename from tests/disko_lib/lsblk-output.json rename to tests/disko_lib/types_disk/partial_failure_dos_table-lsblk-output.json diff --git a/tests/disko_lib/test_types_disk.py b/tests/disko_lib/types_disk/test_types_disk.py similarity index 87% rename from tests/disko_lib/test_types_disk.py rename to tests/disko_lib/types_disk/test_types_disk.py index 9afcc79..47ac6bb 100644 --- a/tests/disko_lib/test_types_disk.py +++ b/tests/disko_lib/types_disk/test_types_disk.py @@ -10,7 +10,7 @@ CURRENT_DIR = Path(__file__).parent def test_generate_config_partial_failure_dos_table() -> None: - with open(CURRENT_DIR / "lsblk-output.json") as f: + with open(CURRENT_DIR / "partial_failure_dos_table-lsblk-output.json") as f: lsblk_result = device.list_block_devices(f.read()) assert isinstance(lsblk_result, DiskoSuccess) @@ -26,7 +26,7 @@ def test_generate_config_partial_failure_dos_table() -> None: } assert result.messages[1].factory == warn_generate_partial_failure - with open(CURRENT_DIR / "generate-result.json") as f: + with open(CURRENT_DIR / "partial_failure_dos_table-generate-result.json") as f: assert result.messages[1].details["partial_config"] == json.load(f) assert result.messages[1].details["failed_devices"] == [PosixPath("/dev/sdc")] assert result.messages[1].details["successful_devices"] == [