This is an automated email from the git hooks/post-receive script.
teigland pushed a commit to branch master
in repository sanlock.
commit 77842ac4167216e610ac80d21b53c2c5fbef966c
Author: Nir Soffer <nsoffer(a)redhat.com>
Date: Sun Feb 25 18:05:50 2018 +0200
Add create_file and check_guard utilities
util.create_file - created a test file poisoned with data, with optional
guard area after the end of the file.
util.check_guard - check that nothing was written into the guard area at
the end of the file.
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
tests/daemon_test.py | 9 +++------
tests/direct_test.py | 9 +++------
tests/python_test.py | 10 ++++------
tests/util.py | 29 +++++++++++++++++++++++++++++
4 files changed, 39 insertions(+), 18 deletions(-)
diff --git a/tests/daemon_test.py b/tests/daemon_test.py
index 84afacb..ea06f31 100644
--- a/tests/daemon_test.py
+++ b/tests/daemon_test.py
@@ -44,9 +44,8 @@ def test_client_failure():
def test_init_lockspace(tmpdir, sanlock_daemon):
path = tmpdir.join("lockspace")
- with io.open(str(path), "wb") as f:
- # Poison with junk data.
- f.write(b"x" * 1024**2 + b"X" * 512)
+ size = 1024**2
+ util.create_file(str(path), size)
lockspace = "name:1:%s:0" % path
util.sanlock("client", "init", "-s", lockspace)
@@ -57,6 +56,4 @@ def test_init_lockspace(tmpdir, sanlock_daemon):
# TODO: check more stuff here...
- # Do not modify data after the lockspace area.
- f.seek(1024**2, io.SEEK_SET)
- assert f.read(512) == b"X" * 512
+ util.check_guard(str(path), size)
diff --git a/tests/direct_test.py b/tests/direct_test.py
index eca118b..e293dc2 100644
--- a/tests/direct_test.py
+++ b/tests/direct_test.py
@@ -11,9 +11,8 @@ from . import util
def test_init_lockspace(tmpdir):
path = tmpdir.join("lockspace")
- with io.open(str(path), "wb") as f:
- # Poison with junk data.
- f.write(b"x" * 1024**2 + b"X" * 512)
+ size = 1024**2
+ util.create_file(str(path), size)
lockspace = "name:1:%s:0" % path
util.sanlock("direct", "init", "-s", lockspace)
@@ -24,6 +23,4 @@ def test_init_lockspace(tmpdir):
# TODO: check more stuff here...
- # Do not modify data after the lockspace area.
- f.seek(1024**2, io.SEEK_SET)
- assert f.read(512) == b"X" * 512
+ util.check_guard(str(path), size)
diff --git a/tests/python_test.py b/tests/python_test.py
index 9f1b9a0..495b809 100644
--- a/tests/python_test.py
+++ b/tests/python_test.py
@@ -8,13 +8,13 @@ import struct
import sanlock
from . import constants
+from . import util
def test_write_lockspace(tmpdir, sanlock_daemon):
path = tmpdir.join("lockspace")
- with io.open(str(path), "wb") as f:
- # Poison with junk data.
- f.write(b"x" * 1024**2 + b"X" * 512)
+ size = 1024**2
+ util.create_file(str(path), size)
sanlock.write_lockspace("name", str(path), offset=0)
@@ -24,6 +24,4 @@ def test_write_lockspace(tmpdir, sanlock_daemon):
# TODO: check more stuff here...
- # Do not modify data after the lockspace area.
- f.seek(1024**2, io.SEEK_SET)
- assert f.read(512) == b"X" * 512
+ util.check_guard(str(path), size)
diff --git a/tests/util.py b/tests/util.py
index 9bd35fb..98f1d14 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -3,6 +3,7 @@ Testing utilities
"""
import errno
+import io
import os
import socket
import subprocess
@@ -93,3 +94,31 @@ def wait_for_termination(p, timeout):
if time.time() > deadline:
raise TimeoutExpired
time.sleep(0.05)
+
+
+def create_file(path, size, poison=b"x", guard=b"X",
guard_size=4096):
+ """
+ Create file filled with poison byte.
+
+ To create an empty file, set poison to None.
+
+ If guard is set, add a guard area after the end of the file and fill it
+ with guard bytes. This allows testing that the code under test do not write
+ anything after the end of the file.
+ """
+ with io.open(path, "wb") as f:
+ f.truncate(size)
+ if poison:
+ f.write(poison * size)
+ if guard:
+ f.seek(size)
+ f.write(guard * 4096)
+
+
+def check_guard(path, size, guard=b"X", guard_size=4096):
+ """
+ Assert that a file ends with a guard area filled with guard bytes.
+ """
+ with io.open(path, "rb") as f:
+ f.seek(size)
+ assert f.read() == guard * guard_size
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.