From 0cd949092da817e477058531497fec0946992233 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Thu, 31 Oct 2024 17:32:44 +0100 Subject: [PATCH] disko2: Add test for generate_config --- .vscode/settings.json | 3 + pyproject.toml | 4 + src/tests/test_disk.py | 0 tests/disko_lib/generate-result.json | 110 +++++++ tests/disko_lib/lsblk-output.json | 423 +++++++++++++++++++++++++++ tests/disko_lib/test_types_disk.py | 36 +++ 6 files changed, 576 insertions(+) delete mode 100644 src/tests/test_disk.py create mode 100644 tests/disko_lib/generate-result.json create mode 100644 tests/disko_lib/lsblk-output.json create mode 100644 tests/disko_lib/test_types_disk.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 5993328..7f3f663 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,4 +15,7 @@ "nixos", "nixpkgs" ], + "python.analysis.extraPaths": [ + "./src" + ] } \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 9d968b9..3535b5d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,10 @@ package-dir = { "" = "src" } [tool.mypy] strict = true +mypy_path = "src" + +[tool.pytest.ini_options] +pythonpath = ["src"] [tool.autoflake] remove_all_unused_imports = true diff --git a/src/tests/test_disk.py b/src/tests/test_disk.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/disko_lib/generate-result.json b/tests/disko_lib/generate-result.json new file mode 100644 index 0000000..0f8fc0b --- /dev/null +++ b/tests/disko_lib/generate-result.json @@ -0,0 +1,110 @@ +{ + "disks": { + "MODEL:ST2000LM003 HN-M201RAD,SN:S321J9GFC01497": { + "device": "sda", + "type": "disk", + "content": { + "partitions": { + "PARTUUID:c090741b-68e2-4867-96df-2ec00765f2c0": { + "size": "128M", + "content": { + "type": "filesystem", + "format": "", + "mountpoint": "" + } + }, + "UUID:7CA41E5EA41E1B6A": { + "size": "1.8T", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": "/mnt/g" + } + } + } + } + }, + "MODEL:SanDisk SD8TB8U256G1001,SN:171887425854": { + "device": "sdb", + "type": "disk", + "content": { + "partitions": { + "UUID:01D60069CEED69C0": { + "size": "499.5M", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": "" + } + }, + "UUID:01D6006B90875FE0": { + "size": "236.8G", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": "" + } + }, + "UUID:708B-C192": { + "size": "100M", + "content": { + "type": "filesystem", + "format": "vfat", + "mountpoint": "" + }, + "type": "EF00" + }, + "UUID:90B6FB81B6FB65DE": { + "size": "544M", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": "" + } + } + } + } + }, + "MODEL:CT2000MX500SSD1,SN:2105E4F0DE85": { + "device": "sdd", + "type": "disk", + "content": { + "partitions": { + "UUID:2740-1628": { + "size": "1000M", + "content": { + "type": "filesystem", + "format": "vfat", + "mountpoint": "/boot" + }, + "type": "EF00" + }, + "UUID:ca548f68-4e51-4364-b366-690ecc27590f": { + "size": "588.4G", + "content": { + "type": "filesystem", + "format": "ext4", + "mountpoint": "/" + } + }, + "UUID:879299db-4147-4fac-9f34-5e8e92073efc": { + "size": "588.4G", + "content": { + "type": "filesystem", + "format": "crypto_LUKS", + "mountpoint": "" + } + }, + "UUID:9A48E8C248E89E6F": { + "size": "685.2G", + "content": { + "type": "filesystem", + "format": "ntfs", + "mountpoint": "/mnt/s" + } + } + } + } + } + } +} \ No newline at end of file diff --git a/tests/disko_lib/lsblk-output.json b/tests/disko_lib/lsblk-output.json new file mode 100644 index 0000000..33e70e7 --- /dev/null +++ b/tests/disko_lib/lsblk-output.json @@ -0,0 +1,423 @@ +{ + "blockdevices": [ + { + "id-link": "wwn-0x50004cf20ecb1679", + "fstype": null, + "fssize": null, + "fsuse%": null, + "kname": "sda", + "label": null, + "model": "ST2000LM003 HN-M201RAD", + "partflags": null, + "partlabel": null, + "partn": null, + "parttype": null, + "parttypename": null, + "partuuid": null, + "path": "/dev/sda", + "phy-sec": 4096, + "pttype": "gpt", + "rev": "2BC10001", + "serial": "S321J9GFC01497", + "size": "1.8T", + "start": null, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "disk", + "uuid": null, + "children": [ + { + "id-link": "wwn-0x50004cf20ecb1679-part1", + "fstype": null, + "fssize": null, + "fsuse%": null, + "kname": "sda1", + "label": null, + "model": null, + "partflags": null, + "partlabel": "Microsoft reserved partition", + "partn": 1, + "parttype": "e3c9e316-0b5c-4db8-817d-f92df00215ae", + "parttypename": "Microsoft reserved", + "partuuid": "c090741b-68e2-4867-96df-2ec00765f2c0", + "path": "/dev/sda1", + "phy-sec": 4096, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "128M", + "start": 34, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "part", + "uuid": null + },{ + "id-link": "wwn-0x50004cf20ecb1679-part2", + "fstype": "ntfs", + "fssize": "1.8T", + "fsuse%": "51%", + "kname": "sda2", + "label": "Groß", + "model": null, + "partflags": null, + "partlabel": "Basic data partition", + "partn": 2, + "parttype": "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", + "parttypename": "Microsoft basic data", + "partuuid": "425b6415-db2b-4684-b619-994bdd0f9b71", + "path": "/dev/sda2", + "phy-sec": 4096, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "1.8T", + "start": 264192, + "mountpoint": "/mnt/g", + "mountpoints": [ + "/mnt/g" + ], + "type": "part", + "uuid": "7CA41E5EA41E1B6A" + } + ] + },{ + "id-link": "wwn-0x5001b444a63292c3", + "fstype": null, + "fssize": null, + "fsuse%": null, + "kname": "sdb", + "label": null, + "model": "SanDisk SD8TB8U256G1001", + "partflags": null, + "partlabel": null, + "partn": null, + "parttype": null, + "parttypename": null, + "partuuid": null, + "path": "/dev/sdb", + "phy-sec": 512, + "pttype": "gpt", + "rev": "X4133101", + "serial": "171887425854", + "size": "238.5G", + "start": null, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "disk", + "uuid": null, + "children": [ + { + "id-link": "wwn-0x5001b444a63292c3-part1", + "fstype": "ntfs", + "fssize": null, + "fsuse%": null, + "kname": "sdb1", + "label": "System Reserved", + "model": null, + "partflags": null, + "partlabel": null, + "partn": 1, + "parttype": "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", + "parttypename": "Microsoft basic data", + "partuuid": "5cbaf771-8ffa-11eb-952d-fcaa14203853", + "path": "/dev/sdb1", + "phy-sec": 512, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "499.5M", + "start": 64, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "part", + "uuid": "01D60069CEED69C0" + },{ + "id-link": "wwn-0x5001b444a63292c3-part2", + "fstype": "ntfs", + "fssize": null, + "fsuse%": null, + "kname": "sdb2", + "label": null, + "model": null, + "partflags": null, + "partlabel": null, + "partn": 2, + "parttype": "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", + "parttypename": "Microsoft basic data", + "partuuid": "5cbaf772-8ffa-11eb-952d-fcaa14203853", + "path": "/dev/sdb2", + "phy-sec": 512, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "236.8G", + "start": 1022976, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "part", + "uuid": "01D6006B90875FE0" + },{ + "id-link": "wwn-0x5001b444a63292c3-part3", + "fstype": "vfat", + "fssize": null, + "fsuse%": null, + "kname": "sdb3", + "label": null, + "model": null, + "partflags": "0x8000000000000000", + "partlabel": null, + "partn": 3, + "parttype": "c12a7328-f81f-11d2-ba4b-00a0c93ec93b", + "parttypename": "EFI System", + "partuuid": "5cbaf773-8ffa-11eb-952d-fcaa14203853", + "path": "/dev/sdb3", + "phy-sec": 512, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "100M", + "start": 497704960, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "part", + "uuid": "708B-C192" + },{ + "id-link": "wwn-0x5001b444a63292c3-part4", + "fstype": "ntfs", + "fssize": null, + "fsuse%": null, + "kname": "sdb4", + "label": null, + "model": null, + "partflags": "0x8000000000000001", + "partlabel": null, + "partn": 4, + "parttype": "de94bba4-06d1-4d40-a16a-bfd50179d6ac", + "parttypename": "Windows recovery environment", + "partuuid": "5cbaf774-8ffa-11eb-952d-fcaa14203853", + "path": "/dev/sdb4", + "phy-sec": 512, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "544M", + "start": 497909760, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "part", + "uuid": "90B6FB81B6FB65DE" + } + ] + },{ + "id-link": "wwn-0x5002538043584d30", + "fstype": null, + "fssize": null, + "fsuse%": null, + "kname": "sdc", + "label": null, + "model": "SAMSUNG SSD PM830 mSATA 256GB", + "partflags": null, + "partlabel": null, + "partn": null, + "parttype": null, + "parttypename": null, + "partuuid": null, + "path": "/dev/sdc", + "phy-sec": 512, + "pttype": "dos", + "rev": "CXM13D1Q", + "serial": "S0XPNYAD407619", + "size": "238.5G", + "start": null, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "disk", + "uuid": null + },{ + "id-link": "wwn-0x500a0751e4f0de85", + "fstype": null, + "fssize": null, + "fsuse%": null, + "kname": "sdd", + "label": null, + "model": "CT2000MX500SSD1", + "partflags": null, + "partlabel": null, + "partn": null, + "parttype": null, + "parttypename": null, + "partuuid": null, + "path": "/dev/sdd", + "phy-sec": 4096, + "pttype": "gpt", + "rev": "M3CR033", + "serial": "2105E4F0DE85", + "size": "1.8T", + "start": null, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "disk", + "uuid": null, + "children": [ + { + "id-link": "wwn-0x500a0751e4f0de85-part1", + "fstype": "vfat", + "fssize": "998M", + "fsuse%": "6%", + "kname": "sdd1", + "label": "boot", + "model": null, + "partflags": null, + "partlabel": "boot", + "partn": 1, + "parttype": "c12a7328-f81f-11d2-ba4b-00a0c93ec93b", + "parttypename": "EFI System", + "partuuid": "7f623bea-5891-49ee-9980-6534716f0f50", + "path": "/dev/sdd1", + "phy-sec": 4096, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "1000M", + "start": 2048, + "mountpoint": "/boot", + "mountpoints": [ + "/boot" + ], + "type": "part", + "uuid": "2740-1628" + },{ + "id-link": "wwn-0x500a0751e4f0de85-part2", + "fstype": "ext4", + "fssize": "578.1G", + "fsuse%": "10%", + "kname": "sdd2", + "label": "root", + "model": null, + "partflags": null, + "partlabel": "root", + "partn": 2, + "parttype": "0fc63daf-8483-4772-8e79-3d69d8477de4", + "parttypename": "Linux filesystem", + "partuuid": "d562416c-1632-40c6-88ed-5095fb921698", + "path": "/dev/sdd2", + "phy-sec": 4096, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "588.4G", + "start": 2050048, + "mountpoint": "/", + "mountpoints": [ + "/nix/store", "/" + ], + "type": "part", + "uuid": "ca548f68-4e51-4364-b366-690ecc27590f" + },{ + "id-link": "wwn-0x500a0751e4f0de85-part3", + "fstype": "crypto_LUKS", + "fssize": null, + "fsuse%": null, + "kname": "sdd3", + "label": "home", + "model": null, + "partflags": null, + "partlabel": "home", + "partn": 3, + "parttype": "0fc63daf-8483-4772-8e79-3d69d8477de4", + "parttypename": "Linux filesystem", + "partuuid": "0ca34f17-5c80-4f2d-97b5-2a5f559b55b5", + "path": "/dev/sdd3", + "phy-sec": 4096, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "588.4G", + "start": 1236023296, + "mountpoint": null, + "mountpoints": [ + null + ], + "type": "part", + "uuid": "879299db-4147-4fac-9f34-5e8e92073efc", + "children": [ + { + "id-link": "dm-name-crypt-home", + "fstype": "btrfs", + "fssize": "588.4G", + "fsuse%": "49%", + "kname": "dm-0", + "label": null, + "model": null, + "partflags": null, + "partlabel": null, + "partn": null, + "parttype": null, + "parttypename": null, + "partuuid": null, + "path": "/dev/mapper/crypt-home", + "phy-sec": 4096, + "pttype": null, + "rev": null, + "serial": null, + "size": "588.4G", + "start": null, + "mountpoint": "/home", + "mountpoints": [ + "/home" + ], + "type": "crypt", + "uuid": "1900f25d-5b93-41f1-a3d4-18fdbd70fe8b" + } + ] + },{ + "id-link": "wwn-0x500a0751e4f0de85-part4", + "fstype": "ntfs", + "fssize": "685.2G", + "fsuse%": "90%", + "kname": "sdd4", + "label": "Schnell", + "model": null, + "partflags": null, + "partlabel": "Basic data partition", + "partn": 4, + "parttype": "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7", + "parttypename": "Microsoft basic data", + "partuuid": "1d55f1cf-5a42-4fa1-a0ba-573fbd0d152c", + "path": "/dev/sdd4", + "phy-sec": 4096, + "pttype": "gpt", + "rev": null, + "serial": null, + "size": "685.2G", + "start": 2470027264, + "mountpoint": "/mnt/s", + "mountpoints": [ + "/mnt/s" + ], + "type": "part", + "uuid": "9A48E8C248E89E6F" + } + ] + } + ] +} + diff --git a/tests/disko_lib/test_types_disk.py b/tests/disko_lib/test_types_disk.py new file mode 100644 index 0000000..2be88af --- /dev/null +++ b/tests/disko_lib/test_types_disk.py @@ -0,0 +1,36 @@ +import json +from pathlib import Path, PosixPath +import pytest + +from disko_lib.result import DiskoError, DiskoSuccess +from disko_lib.types import disk +from disko_lib.types import device + +CURRENT_DIR = Path(__file__).parent + + +def test_generate_config_partial_failure_dos_table() -> None: + with open(CURRENT_DIR / "lsblk-output.json") as f: + lsblk_result = device.list_block_devices(f.read()) + + assert isinstance(lsblk_result, DiskoSuccess) + + result = disk.generate_config(lsblk_result.value) + + assert isinstance(result, DiskoError) + + assert result.messages[0].code == "ERR_UNSUPPORTED_PTTYPE" + assert result.messages[0].details == { + "pttype": "dos", + "device": PosixPath("/dev/sdc"), + } + + assert result.messages[1].code == "WARN_GENERATE_PARTIAL_FAILURE" + with open(CURRENT_DIR / "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"] == [ + PosixPath("/dev/sda"), + PosixPath("/dev/sdb"), + PosixPath("/dev/sdd"), + ]