From: Nir Soffer <nsoffer(a)redhat.com>
Change the sanlock_daemon fixture to run a new instance for every test.
This is little slower but allows testing starting and killing the daemon.
To make sure the daemon is ready before we test the client, wait until
the daemon is listening on the socket before yielding to the test.
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
tests/daemon_test.py | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/tests/daemon_test.py b/tests/daemon_test.py
index 5c8c3c8..352a065 100644
--- a/tests/daemon_test.py
+++ b/tests/daemon_test.py
@@ -2,10 +2,13 @@
Test sanlock client operations.
"""
+import errno
import io
import os
+import socket
import struct
import subprocess
+import time
import pytest
@@ -17,6 +20,10 @@ ENV["SANLOCK_RUN_DIR"] = "/tmp/sanlock"
ENV["SANLOCK_PRIVILEGED"] = "0"
+class TimeoutExpired(Exception):
+ """ Raised when timeout expired """
+
+
def start_sanlock_daemon():
cmd = [SANLOCK, "daemon",
# no fork and print all logging to stderr
@@ -33,10 +40,31 @@ def start_sanlock_daemon():
return subprocess.Popen(cmd, env=ENV)
-(a)pytest.fixture(scope="session")
+def wait_for_socket(timeout):
+ """ Wait until deamon is accepting connections """
+ deadline = time.time() + timeout
+ path = os.path.join(ENV["SANLOCK_RUN_DIR"], "sanlock.sock")
+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ try:
+ while True:
+ try:
+ s.connect(path)
+ return
+ except socket.error as e:
+ if e[0] not in (errno.ECONNREFUSED, errno.ENOENT):
+ raise # Unexpected error
+ if time.time() > deadline:
+ raise TimeoutExpired
+ time.sleep(0.05)
+ finally:
+ s.close()
+
+
+(a)pytest.fixture
def sanlock_daemon():
p = start_sanlock_daemon()
try:
+ wait_for_socket(0.5)
yield
finally:
p.terminate()
--
2.9.5