From 95873c9ac3c20445311320aec737934f90ab9192 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Jan 15 2018 16:30:04 +0000 Subject: Add tests for sanlock lockfile Add test for sanlock lockfile functionality: - Only one instance can be started - Being able to start the daemon after killing it Signed-off-by: Nir Soffer --- diff --git a/tests/daemon_test.py b/tests/daemon_test.py index 352a065..1c304ae 100644 --- a/tests/daemon_test.py +++ b/tests/daemon_test.py @@ -5,6 +5,7 @@ Test sanlock client operations. import errno import io import os +import signal import socket import struct import subprocess @@ -60,6 +61,19 @@ def wait_for_socket(timeout): s.close() +def wait_for_termination(p, timeout): + """ + Wait until process terminates, or timeout expires. + """ + deadline = time.time() + timeout + while True: + if p.poll() is not None: + return + if time.time() > deadline: + raise TimeoutExpired + time.sleep(0.05) + + @pytest.fixture def sanlock_daemon(): p = start_sanlock_daemon() @@ -71,6 +85,29 @@ def sanlock_daemon(): p.wait() +def test_single_instance(sanlock_daemon): + # Starting another instance while the daemon must fail. + p = start_sanlock_daemon() + try: + wait_for_termination(p, 1.0) + except TimeoutExpired: + p.kill() + p.wait() + assert p.returncode == 255 # exit code -1 + + +def test_start_after_kill(): + # After killing the daemon, next instance should be able to start. + for i in range(5): + p = start_sanlock_daemon() + try: + wait_for_socket(0.5) + finally: + p.kill() + p.wait() + assert p.returncode == -signal.SIGKILL + + def test_init_lockspace(tmpdir, sanlock_daemon): path = tmpdir.join("lockspace") with io.open(str(path), "wb") as f: