From aeb7cb00bdef2476aa5155f6cc68227f24b90397 Mon Sep 17 00:00:00 2001 From: timeshifter Date: Sun, 5 Dec 2021 18:21:40 +0100 Subject: [PATCH] add: TestZFSPath --- test_zfs_backup.py | 172 +++++++++++++++++++++++++++++++++ zfs-backup.py => zfs_backup.py | 7 +- 2 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 test_zfs_backup.py rename zfs-backup.py => zfs_backup.py (99%) diff --git a/test_zfs_backup.py b/test_zfs_backup.py new file mode 100644 index 0000000..fdf7b08 --- /dev/null +++ b/test_zfs_backup.py @@ -0,0 +1,172 @@ +import pytest + +from zfs_backup import ZFSPath + + +class TestZFSPath: + @pytest.mark.parametrize( + "elements", + [ + ["pool"], + ["pool", "dataset1", "dataset2"], + ], + ) + def test__sanity_check_valid(self, elements): + ZFSPath._sanity_check(elements) + + @pytest.mark.parametrize( + "elements", + [ + [""], + ["", "dataset1"], + ["pool", "dataset1", ""], + ["pool", "", "dataset2"], + ], + ) + def test__sanity_check_invalid(self, elements): + with pytest.raises(ValueError): + ZFSPath._sanity_check(elements) + + @pytest.mark.parametrize( + "elements, snapshot_name, result", + [ + [["pool"], None, ""], + [["pool", "dataset"], None, "dataset"], + [["pool", "dataset"], "snapshot", "dataset@snapshot"], + [ + ["pool", "dataset1", "dataset2"], + "snapshot", + "dataset1/dataset2@snapshot", + ], + ], + ids=[ + "only pool", + "pool with one dataset", + "pool with dataset and snapshot", + "pool with two nested datasets and snapshot", + ], + ) + def test_name_without_pool(self, elements, snapshot_name, result): + instance = ZFSPath(elements, snapshot_name) + assert instance.name_without_pool == result + + @pytest.mark.parametrize( + "elements, snapshot_name, result", + [ + [["pool"], None, "pool"], + [["pool", "dataset"], None, "pool/dataset"], + [["pool", "dataset"], "snapshot", "pool/dataset@snapshot"], + [ + ["pool", "dataset1", "dataset2"], + "snapshot", + "pool/dataset1/dataset2@snapshot", + ], + ], + ids=[ + "only pool", + "pool with one dataset", + "pool with dataset and snapshot", + "pool with two nested datasets and snapshot", + ], + ) + def test___repr__(self, elements, snapshot_name, result): + assert str(ZFSPath(elements, snapshot_name)) == result + + @pytest.mark.parametrize( + "instance1, instance2, result", + [ + [ZFSPath(["pool"]), ZFSPath(["pool"]), True], + [ZFSPath(["pool"]), ZFSPath(["tank"]), False], + [ZFSPath(["pool", "dataset"]), ZFSPath(["pool", "dataset"]), True], + [ZFSPath(["pool", "dataset"]), ZFSPath(["pool", "bla"]), False], + [ + ZFSPath(["pool", "dataset"], "snapshot"), + ZFSPath(["pool", "dataset"], "snapshot"), + True, + ], + [ + ZFSPath(["pool", "dataset"], "snapshot"), + ZFSPath(["pool", "dataset"], "bla"), + False, + ], + ], + ids=[ + "only pool equal", + "only pool not equal", + "dataset equal", + "dataset not equal", + "snapshot equal", + "snapshot not equal", + ], + ) + def test___eq__(self, instance1, instance2, result): + assert (instance1 == instance2) == result + + @pytest.mark.parametrize( + "string, elements, snapshot_name", + [ + ["pool/dataset", ["pool", "dataset"], None], + ["pool/dataset@snapshot", ["pool", "dataset"], "snapshot"], + [ + "pool/dataset1/dataset2@snapshot", + ["pool", "dataset1", "dataset2"], + "snapshot", + ], + ], + ids=[ + "pool/dataset", + "pool/dataset@snapshot", + "pool/dataset1/dataset2@snapshot", + ], + ) + def test_from_string(self, string, elements, snapshot_name): + instance = ZFSPath.from_string(string) + assert instance._elements == elements + assert instance._snapshot_name == instance.snapshot_name == snapshot_name + + @pytest.mark.parametrize( + "instance1, instance2", + [ + [ZFSPath(["pool", "dataset"]), ZFSPath(["pool", "dataset"])], + [ZFSPath(["pool", "dataset"], "snapshot"), ZFSPath(["pool", "dataset"])], + ], + ids=[ + "pool with dataset", + "pool with dataset and snapshot", + ], + ) + def test_dataset(self, instance1, instance2): + assert instance1.dataset == instance2 + + @pytest.mark.parametrize( + "instance, result", + [ + [ZFSPath(["pool", "dataset"]), "dataset"], + [ZFSPath(["pool", "dataset"], "snapshot"), "dataset@snapshot"], + ], + ids=[ + "pool with dataset", + "pool with dataset and snapshot", + ], + ) + def test_name_without_pool(self, instance, result): + assert instance.name_without_pool == result + + @pytest.mark.parametrize( + "instance1, instance2", + [ + [ZFSPath(["pool"]), ZFSPath(["tank"])], + [ZFSPath(["pool", "dataset"]), ZFSPath(["tank", "dataset"])], + [ + ZFSPath(["pool", "dataset"], "snapshot"), + ZFSPath(["tank", "dataset"], "snapshot"), + ], + ], + ids=[ + "pool", + "pool with dataset", + "pool with dataset and snapshot", + ], + ) + def test_replace_pool(self, instance1, instance2): + assert instance1.replace_pool(instance2.pool_name) == instance2 diff --git a/zfs-backup.py b/zfs_backup.py similarity index 99% rename from zfs-backup.py rename to zfs_backup.py index 64ab537..d397225 100755 --- a/zfs-backup.py +++ b/zfs_backup.py @@ -195,7 +195,7 @@ class ZFSPath: self._is_snapshot = False if snapshot_name: self._is_snapshot = True - self._sanity_check() + self._sanity_check(self._elements) def __repr__(self): """Return the path in the same manner as `zfs`.""" @@ -251,8 +251,9 @@ class ZFSPath: name = "@".join([name, self.snapshot_name]) return name - def _sanity_check(self) -> None: - for item in self._elements: + @staticmethod + def _sanity_check(elements: list[str]) -> None: + for item in elements: if len(item) == 0: raise ValueError(Messages.NOT_VALID_ZFS_PATH)