Add missing tests and improve existing python binding tests, coverting all positive flows used by oVirt.
- Test add_lockspace and rem_lockspace - Test acquire and release - Test read_resource, read_resource_owners, read_lockspace, inq_lockspace - Fix bad test name
We probbaly need more tests to cover negative flows.
These tests will help to port the bindings to python 3.
Nir Soffer (4): tests: Fix test name tests: Test add_lockspace() and rem_lockspace() tests: Test acquire() and release() tests: Improve write_{lockspace,resource} tests
tests/python_test.py | 168 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 152 insertions(+), 16 deletions(-)
Signed-off-by: Nir Soffer nsoffer@redhat.com --- tests/python_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/python_test.py b/tests/python_test.py index 9cd6355..504503e 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -25,11 +25,11 @@ def test_write_lockspace(tmpdir, sanlock_daemon): # TODO: check more stuff here...
util.check_guard(str(path), size)
-def test_init_resource(tmpdir, sanlock_daemon): +def test_write_resource(tmpdir, sanlock_daemon): path = tmpdir.join("resources") size = 1024**2 util.create_file(str(path), size)
sanlock.write_resource("ls_name", "res_name", [(str(path), 0)])
Add tests for add_lockspace() and rem_lockspace() using the python bindings.
Signed-off-by: Nir Soffer nsoffer@redhat.com --- tests/python_test.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+)
diff --git a/tests/python_test.py b/tests/python_test.py index 504503e..bd9be65 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -2,10 +2,11 @@ Test sanlock python binding with sanlock daemon. """
import io import struct +import time
import sanlock
from . import constants from . import util @@ -41,10 +42,69 @@ def test_write_resource(tmpdir, sanlock_daemon): # TODO: check more stuff here...
util.check_guard(str(path), size)
+def test_add_rem_lockspace(tmpdir, sanlock_daemon): + path = str(tmpdir.join("ls_name")) + util.create_file(path, 1024**2) + + sanlock.write_lockspace("ls_name", path, iotimeout=1) + + # Since the lockspace is not acquired, we exepect to get False. + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) + assert acquired is False + + sanlock.add_lockspace("ls_name", 1, path, iotimeout=1) + + # Once the lockspace is acquired, we exepect to get True. + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) + assert acquired is True + + sanlock.rem_lockspace("ls_name", 1, path) + + # Once the lockspace is released, we exepect to get False. + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) + assert acquired is False + + +def test_add_rem_lockspace_async(tmpdir, sanlock_daemon): + path = str(tmpdir.join("ls_name")) + util.create_file(path, 1024**2) + + sanlock.write_lockspace("ls_name", path, iotimeout=1) + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) + assert acquired is False + + # This will take 3 seconds. + sanlock.add_lockspace("ls_name", 1, path, iotimeout=1, **{"async": True}) + + # While the lockspace is being aquired, we expect to get None. + time.sleep(1) + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) + assert acquired is None + + # Once the lockspace is acquired, we exepect to get True. + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=True) + assert acquired is True + + # This will take about 3 seconds. + sanlock.rem_lockspace("ls_name", 1, path, **{"async": True}) + + # Wait until the lockspace change state from True to None. + while sanlock.inq_lockspace("ls_name", 1, path, wait=False): + time.sleep(1) + + # While the lockspace is being released, we expect to get None. + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) + assert acquired is None + + # Once the lockspace was released, we expect to get False. + acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=True) + assert acquired is False + + def test_read_resource_owners(tmpdir, sanlock_daemon): path = tmpdir.join("resources") size = 1024**2 util.create_file(str(path), size) disks = [(str(path), 0)]
Test acquiring and releasing a resource, and querying resource and host status.
Since the new test uses read_resource_owners, we don't need the separate test for read_resource_owner.
Signed-off-by: Nir Soffer nsoffer@redhat.com --- tests/python_test.py | 73 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 7 deletions(-)
diff --git a/tests/python_test.py b/tests/python_test.py index bd9be65..306d3c8 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -1,13 +1,16 @@ """ Test sanlock python binding with sanlock daemon. """
+import errno import io import struct import time
+import pytest + import sanlock
from . import constants from . import util
@@ -101,14 +104,70 @@ def test_add_rem_lockspace_async(tmpdir, sanlock_daemon): # Once the lockspace was released, we expect to get False. acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=True) assert acquired is False
-def test_read_resource_owners(tmpdir, sanlock_daemon): - path = tmpdir.join("resources") - size = 1024**2 - util.create_file(str(path), size) - disks = [(str(path), 0)] +def test_acquire_release_resource(tmpdir, sanlock_daemon): + ls_path = str(tmpdir.join("ls_name")) + util.create_file(ls_path, 1024**2) + + res_path = str(tmpdir.join("res_name")) + util.create_file(res_path, 1024**2) + + sanlock.write_lockspace("ls_name", ls_path, iotimeout=1) + sanlock.add_lockspace("ls_name", 1, ls_path, iotimeout=1) + + # Host status is not available until the first renewal. + with pytest.raises(sanlock.SanlockException) as e: + sanlock.get_hosts("ls_name", 1) + assert e.value.errno == errno.EAGAIN + + time.sleep(1) + host = sanlock.get_hosts("ls_name", 1)[0] + assert host["flags"] == sanlock.HOST_LIVE + + disks = [(res_path, 0)] sanlock.write_resource("ls_name", "res_name", disks)
- res = sanlock.read_resource_owners("ls_name", "res_name", disks) - assert res == [] + res = sanlock.read_resource(res_path, 0) + assert res == { + "lockspace": "ls_name", + "resource": "res_name", + "version": 0 + } + + owners = sanlock.read_resource_owners("ls_name", "res_name", disks) + assert owners == [] + + fd = sanlock.register() + sanlock.acquire("ls_name", "res_name", disks, slkfd=fd) + + res = sanlock.read_resource(res_path, 0) + assert res == { + "lockspace": "ls_name", + "resource": "res_name", + "version": 1 + } + + owner = sanlock.read_resource_owners("ls_name", "res_name", disks)[0] + + assert owner["host_id"] == 1 + assert owner["flags"] == 0 + assert owner["generation"] == 1 + assert owner["io_timeout"] == 0 # Why 0? + # TODO: check timestamp. + + host = sanlock.get_hosts("ls_name", 1)[0] + assert host["flags"] == sanlock.HOST_LIVE + assert host["generation"] == owner["generation"] + + sanlock.release("ls_name", "res_name", disks, slkfd=fd) + + res = sanlock.read_resource(res_path, 0) + assert res == { + "lockspace": "ls_name", + "resource": "res_name", + "version": 1 + } + + owners = sanlock.read_resource_owners("ls_name", "res_name", disks) + assert owners == []
Use read_resource, read_resource_owners, read_lockspace, and inq_lockspace to verify creation of lockspace and resource.
Signed-off-by: Nir Soffer nsoffer@redhat.com --- tests/python_test.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-)
diff --git a/tests/python_test.py b/tests/python_test.py index 306d3c8..2dd4e17 100644 --- a/tests/python_test.py +++ b/tests/python_test.py @@ -14,15 +14,21 @@ import sanlock from . import constants from . import util
def test_write_lockspace(tmpdir, sanlock_daemon): - path = tmpdir.join("lockspace") + path = str(tmpdir.join("lockspace")) size = 1024**2 - util.create_file(str(path), size) + util.create_file(path, size)
- sanlock.write_lockspace("name", str(path), offset=0) + sanlock.write_lockspace("name", path, offset=0, iotimeout=1) + + ls = sanlock.read_lockspace(path, offset=0) + assert ls == {"iotimeout": 1, "lockspace": "name"} + + acquired = sanlock.inq_lockspace("name", 1, path, wait=False) + assert acquired is False
with io.open(str(path), "rb") as f: magic, = struct.unpack("< I", f.read(4)) assert magic == constants.DELTA_DISK_MAGIC
@@ -30,23 +36,34 @@ def test_write_lockspace(tmpdir, sanlock_daemon):
util.check_guard(str(path), size)
def test_write_resource(tmpdir, sanlock_daemon): - path = tmpdir.join("resources") + path = str(tmpdir.join("resources")) size = 1024**2 - util.create_file(str(path), size) + util.create_file(path, size) + disks = [(path, 0)]
- sanlock.write_resource("ls_name", "res_name", [(str(path), 0)]) + sanlock.write_resource("ls_name", "res_name", disks)
- with io.open(str(path), "rb") as f: + res = sanlock.read_resource(path, 0) + assert res == { + "lockspace": "ls_name", + "resource": "res_name", + "version": 0 + } + + owners = sanlock.read_resource_owners("ls_name", "res_name", disks) + assert owners == [] + + with io.open(path, "rb") as f: magic, = struct.unpack("< I", f.read(4)) assert magic == constants.PAXOS_DISK_MAGIC
# TODO: check more stuff here...
- util.check_guard(str(path), size) + util.check_guard(path, size)
def test_add_rem_lockspace(tmpdir, sanlock_daemon): path = str(tmpdir.join("ls_name")) util.create_file(path, 1024**2)
sanlock-devel@lists.fedorahosted.org