Add tests for write_resource() with parameter 'clear' set to True, which
should result into clearing the resource (overwriting PAXOS_DISK_MAGIC
with PAXOS_DISK_CLEAR). Also add tests for corner cases like calling
clear on empty storage or with empty lockspace and resource name.
---
tests/constants.py | 4 ++++
tests/python_test.py | 47 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/tests/constants.py b/tests/constants.py
index a7eb16b..21089b8 100644
--- a/tests/constants.py
+++ b/tests/constants.py
@@ -17,3 +17,7 @@ RINDEX_DISK_MAGIC = 0x01042018
RINDEX_ENTRY_SIZE = 64
RINDEX_ENTRIES_SECTORS = 2000
+
+# src/sanlock_rv.h
+
+SANLK_LEADER_MAGIC = -223
diff --git a/tests/python_test.py b/tests/python_test.py
index 58d94c9..8822521 100644
--- a/tests/python_test.py
+++ b/tests/python_test.py
@@ -227,6 +227,53 @@ def test_write_resource_4k_invalid_sector_size(sanlock_daemon, user_4k_path):
assert e.value.errno == errno.EINVAL
+def test_clear_resource(tmpdir, sanlock_daemon):
+ path = util.generate_path(tmpdir, "clear_test")
+ util.create_file(path, MiB)
+ disks = [(path, 0)]
+
+ sanlock.write_resource(b"ls_name", b"res_name", disks)
+ sanlock.write_resource(b"ls_name", b"res_name", disks, clear=True)
+
+ with pytest.raises(sanlock.SanlockException) as e:
+ sanlock.read_resource(path)
+ assert e.value.errno == constants.SANLK_LEADER_MAGIC
+
+ magic = util.read_magic(path)
+ assert magic == constants.PAXOS_DISK_CLEAR
+
+ util.check_guard(path, MiB)
+
+ # run clear on already cleared resource
+ sanlock.write_resource(b"ls_name", b"res_name", disks, clear=True)
+ magic = util.read_magic(path)
+ assert magic == constants.PAXOS_DISK_CLEAR
+
+
+def test_clear_empty_lockspace_resource(tmpdir, sanlock_daemon):
+ path = util.generate_path(tmpdir, "clear_test")
+ util.create_file(path, MiB)
+ disks = [(path, 0)]
+
+ sanlock.write_resource(b"ls_name", b"res_name", disks)
+
+ # Clear with empty lockspace and resource - should succeed
+ sanlock.write_resource(b"", b"", disks, clear=True)
+ magic = util.read_magic(path)
+ assert magic == constants.PAXOS_DISK_CLEAR
+
+
+def test_clear_empty_storage(tmpdir, sanlock_daemon):
+ path = util.generate_path(tmpdir, "clear_test")
+ util.create_file(path, MiB)
+ disks = [(path, 0)]
+
+ # Clear area without any resource written - should succeed
+ sanlock.write_resource(b"ls_name", b"inval_res_name", disks, clear=True)
+ magic = util.read_magic(path)
+ assert magic == constants.PAXOS_DISK_CLEAR
+
+
def test_read_resource_4k_invalid_sector_size(sanlock_daemon, user_4k_path):
disks = [(user_4k_path, 0)]
--
2.20.1