add: TestZFSPath
This commit is contained in:
@@ -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
|
||||||
@@ -195,7 +195,7 @@ class ZFSPath:
|
|||||||
self._is_snapshot = False
|
self._is_snapshot = False
|
||||||
if snapshot_name:
|
if snapshot_name:
|
||||||
self._is_snapshot = True
|
self._is_snapshot = True
|
||||||
self._sanity_check()
|
self._sanity_check(self._elements)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
"""Return the path in the same manner as `zfs`."""
|
"""Return the path in the same manner as `zfs`."""
|
||||||
@@ -251,8 +251,9 @@ class ZFSPath:
|
|||||||
name = "@".join([name, self.snapshot_name])
|
name = "@".join([name, self.snapshot_name])
|
||||||
return name
|
return name
|
||||||
|
|
||||||
def _sanity_check(self) -> None:
|
@staticmethod
|
||||||
for item in self._elements:
|
def _sanity_check(elements: list[str]) -> None:
|
||||||
|
for item in elements:
|
||||||
if len(item) == 0:
|
if len(item) == 0:
|
||||||
raise ValueError(Messages.NOT_VALID_ZFS_PATH)
|
raise ValueError(Messages.NOT_VALID_ZFS_PATH)
|
||||||
|
|
||||||
Reference in New Issue
Block a user