From ced9ae996cd55ed4e26ca9bf79bd92d2be2abfe3 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Jan 15 2018 16:30:04 +0000 Subject: Start new daemon for every test 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 --- 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) -@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() + + +@pytest.fixture def sanlock_daemon(): p = start_sanlock_daemon() try: + wait_for_socket(0.5) yield finally: p.terminate()