Add tests for clearing the resource. Also extract reading sanlock magic number into helper function.
Vojtech Juranek (2): tests: replace reading of magic number by helper function tests: add tests for clearing resource
tests/constants.py | 4 +++ tests/python_test.py | 72 ++++++++++++++++++++++++++++++++++---------- tests/util.py | 11 +++++++ 3 files changed, 71 insertions(+), 16 deletions(-)
Introduce helper function for reading sanlock magic numbers. This helper function seeks to specified offset in given file and reads unsigned int from this position. --- tests/python_test.py | 25 +++++++++---------------- tests/util.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-)
diff --git a/tests/python_test.py b/tests/python_test.py index 3242f57..58d94c9 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -86,12 +86,9 @@ def test_write_lockspace(tmpdir, sanlock_daemon, filename, encoding, size, offse b"ls_name", 1, path, offset=offset, wait=False) assert acquired is False
- with io.open(path, "rb") as f: - f.seek(offset) - magic, = struct.unpack("< I", f.read(4)) - assert magic == constants.DELTA_DISK_MAGIC - - # TODO: check more stuff here... + magic = util.read_magic(path, offset) + assert magic == constants.DELTA_DISK_MAGIC + # TODO: check more stuff here...
util.check_guard(path, size)
@@ -117,9 +114,8 @@ def test_write_lockspace_4k(user_4k_path, sanlock_daemon, align): assert acquired is False
# Verify that lockspace was written. - with io.open(user_4k_path, "rb") as f: - magic, = struct.unpack("< I", f.read(4)) - assert magic == constants.DELTA_DISK_MAGIC + magic = util.read_magic(user_4k_path) + assert magic == constants.DELTA_DISK_MAGIC
# Check that sanlock did not write beyond the lockspace area. util.check_guard(user_4k_path, align) @@ -179,10 +175,8 @@ def test_write_resource(tmpdir, sanlock_daemon, filename, encoding, size, offset owners = sanlock.read_resource_owners(b"ls_name", b"res_name", disks) assert owners == []
- with io.open(path, "rb") as f: - f.seek(offset) - magic, = struct.unpack("< I", f.read(4)) - assert magic == constants.PAXOS_DISK_MAGIC + magic = util.read_magic(path, offset) + assert magic == constants.PAXOS_DISK_MAGIC
# TODO: check more stuff here...
@@ -216,9 +210,8 @@ def test_write_resource_4k(sanlock_daemon, user_4k_path, align): assert owners == []
# Verify that resource was written. - with io.open(user_4k_path, "rb") as f: - magic, = struct.unpack("< I", f.read(4)) - assert magic == constants.PAXOS_DISK_MAGIC + magic = util.read_magic(user_4k_path) + assert magic == constants.PAXOS_DISK_MAGIC
# Check that sanlock did not write beyond the lockspace area. util.check_guard(user_4k_path, align) diff --git a/tests/util.py b/tests/util.py index 08db236..243b1d3 100644 --- a/tests/util.py +++ b/tests/util.py @@ -177,3 +177,14 @@ def sanlock_is_running(): if e.errno != errno.ENOENT: raise return False + + +def read_magic(path, offset=0): + """ + Read and return unsigned int from specified file at given offset. + Typical usage is to read sanlock magic number. + """ + with io.open(path, "rb") as f: + f.seek(offset) + uint, = struct.unpack("< I", f.read(4)) + return uint
LGTM
On Mon, Jun 17, 2019 at 1:39 PM Vojtech Juranek vjuranek@redhat.com wrote:
Introduce helper function for reading sanlock magic numbers. This helper function seeks to specified offset in given file and reads unsigned int from this position.
tests/python_test.py | 25 +++++++++---------------- tests/util.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-)
diff --git a/tests/python_test.py b/tests/python_test.py index 3242f57..58d94c9 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -86,12 +86,9 @@ def test_write_lockspace(tmpdir, sanlock_daemon, filename, encoding, size, offse b"ls_name", 1, path, offset=offset, wait=False) assert acquired is False
- with io.open(path, "rb") as f:
f.seek(offset)magic, = struct.unpack("< I", f.read(4))assert magic == constants.DELTA_DISK_MAGIC# TODO: check more stuff here...
magic = util.read_magic(path, offset)
assert magic == constants.DELTA_DISK_MAGIC
# TODO: check more stuff here...
util.check_guard(path, size)
@@ -117,9 +114,8 @@ def test_write_lockspace_4k(user_4k_path, sanlock_daemon, align): assert acquired is False
# Verify that lockspace was written.
- with io.open(user_4k_path, "rb") as f:
magic, = struct.unpack("< I", f.read(4))assert magic == constants.DELTA_DISK_MAGIC
magic = util.read_magic(user_4k_path)
assert magic == constants.DELTA_DISK_MAGIC
# Check that sanlock did not write beyond the lockspace area. util.check_guard(user_4k_path, align)
@@ -179,10 +175,8 @@ def test_write_resource(tmpdir, sanlock_daemon, filename, encoding, size, offset owners = sanlock.read_resource_owners(b"ls_name", b"res_name", disks) assert owners == []
- with io.open(path, "rb") as f:
f.seek(offset)magic, = struct.unpack("< I", f.read(4))assert magic == constants.PAXOS_DISK_MAGIC
magic = util.read_magic(path, offset)
assert magic == constants.PAXOS_DISK_MAGIC
# TODO: check more stuff here...@@ -216,9 +210,8 @@ def test_write_resource_4k(sanlock_daemon, user_4k_path, align): assert owners == []
# Verify that resource was written.
- with io.open(user_4k_path, "rb") as f:
magic, = struct.unpack("< I", f.read(4))assert magic == constants.PAXOS_DISK_MAGIC
magic = util.read_magic(user_4k_path)
assert magic == constants.PAXOS_DISK_MAGIC
# Check that sanlock did not write beyond the lockspace area. util.check_guard(user_4k_path, align)
diff --git a/tests/util.py b/tests/util.py index 08db236..243b1d3 100644 --- a/tests/util.py +++ b/tests/util.py @@ -177,3 +177,14 @@ def sanlock_is_running(): if e.errno != errno.ENOENT: raise return False
+def read_magic(path, offset=0):
- """
- Read and return unsigned int from specified file at given offset.
- Typical usage is to read sanlock magic number.
- """
- with io.open(path, "rb") as f:
f.seek(offset)uint, = struct.unpack("< I", f.read(4))return uint-- 2.20.1
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)]
LGTM
On Mon, Jun 17, 2019 at 1:39 PM Vojtech Juranek vjuranek@redhat.com wrote:
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
On Mon, Jun 17, 2019 at 1:39 PM Vojtech Juranek vjuranek@redhat.com wrote:
Add tests for clearing the resource. Also extract reading sanlock magic number into helper function.
Vojtech Juranek (2): tests: replace reading of magic number by helper function tests: add tests for clearing resource
tests/constants.py | 4 +++ tests/python_test.py | 72 ++++++++++++++++++++++++++++++++++---------- tests/util.py | 11 +++++++ 3 files changed, 71 insertions(+), 16 deletions(-)
-- 2.20.1
Thanks, pushed.
sanlock-devel@lists.fedorahosted.org