From: Amit Bawer <abawer(a)redhat.com>
New fixture calls util.sanlock_is_running() to check if the
pid showing in sanlock.pid file (if file exists) leads to local
sanlock executable file to conclude if sanlock is currenlty
running in test env. In case sanlock is running, a SunlockIsRunning
exception will be raised from test.
---
tests/conftest.py | 9 +++++++++
tests/util.py | 17 +++++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/tests/conftest.py b/tests/conftest.py
index 93201bb..70e83d4 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -42,5 +42,14 @@ def user_4k_path(request):
if not os.path.exists(request.param):
pytest.skip(
"user storage available - run 'python tests/strorage.py setup'
"
"to enable 4k storage tests")
return request.param
+
+
+(a)pytest.fixture
+def no_sanlock_daemon():
+ if util.sanlock_is_running():
+ raise SanlockIsRunning
+
+class SanlockIsRunning(Exception):
+ pass
diff --git a/tests/util.py b/tests/util.py
index d944611..08db236 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -158,5 +158,22 @@ def generate_path(dirname, filename, encoding=None):
"""
path = os.path.join(str(dirname), filename)
if encoding:
path = path.encode(encoding)
return path
+
+
+def sanlock_is_running():
+ """
+ Return boolean value indicating whether sanlock process
+ is currently executing within pytests run dir.
+ """
+ pid_file = os.path.join(os.environ["SANLOCK_RUN_DIR"],
"sanlock.pid")
+ try:
+ with open(pid_file, "r") as f:
+ pid = f.readline().strip()
+ executable = os.readlink("/proc/{}/exe".format(pid))
+ return os.path.samefile(executable, SANLOCK)
+ except OSError as e:
+ if e.errno != errno.ENOENT:
+ raise
+ return False
--
2.17.2