From: Pavel Bar <pbar(a)redhat.com>
Signed-off-by: Pavel Bar <pbar(a)redhat.com>
---
tests/daemon_test.py | 33 +++++++++++++++++----------------
tests/direct_test.py | 5 +++--
tests/python_test.py | 26 ++++++++++++++------------
tests/units.py | 9 +++++++++
tests/util.py | 4 +++-
5 files changed, 46 insertions(+), 31 deletions(-)
create mode 100644 tests/units.py
diff --git a/tests/daemon_test.py b/tests/daemon_test.py
index c7f9042..36648e3 100644
--- a/tests/daemon_test.py
+++ b/tests/daemon_test.py
@@ -9,10 +9,11 @@ import struct
import pytest
from . constants import *
from . import util
+from . units import *
def test_single_instance(sanlock_daemon):
# Starting another instance while the daemon must fail.
p = util.start_daemon()
@@ -43,11 +44,11 @@ def test_client_failure():
assert e.value.returncode == 1
def test_init_lockspace(tmpdir, sanlock_daemon):
path = tmpdir.join("lockspace")
- size = 1024**2
+ size = MiB
util.create_file(str(path), size)
lockspace = "name:1:%s:0" % path
util.sanlock("client", "init", "-s", lockspace)
@@ -60,11 +61,11 @@ def test_init_lockspace(tmpdir, sanlock_daemon):
util.check_guard(str(path), size)
def test_init_resource(tmpdir, sanlock_daemon):
path = tmpdir.join("resources")
- size = 1024**2
+ size = MiB
util.create_file(str(path), size)
resource = "ls_name:res_name:%s:0" % path
util.sanlock("client", "init", "-r", resource)
@@ -77,40 +78,40 @@ def test_init_resource(tmpdir, sanlock_daemon):
util.check_guard(str(path), size)
def test_format(tmpdir, sanlock_daemon):
path = tmpdir.join("rindex")
- size = 1024**2 * 3
+ size = 3 * MiB
util.create_file(str(path), size)
rindex = "ls_name:%s:1M" % path
util.sanlock("client", "format", "-x", rindex)
with io.open(str(path), "rb") as f:
# The first slot should contain the rindex header sector.
- f.seek(1024**2)
+ f.seek(MiB)
magic, = struct.unpack("< I", f.read(4))
assert magic == RINDEX_DISK_MAGIC
# The rindex entries starts at the second rindex slot sector. All
# entries should be zeroed.
- f.seek(1024**2 + 512)
+ f.seek(MiB + 512)
entries_size = 512 * RINDEX_ENTRIES_SECTORS
assert f.read(entries_size) == b"\0" * entries_size
# The next slot should contain the internal lease.
- f.seek(1024**2 * 2)
+ f.seek(2 * MiB)
magic, = struct.unpack("< I", f.read(4))
assert magic == PAXOS_DISK_MAGIC
util.check_guard(str(path), size)
def test_create(tmpdir, sanlock_daemon):
path = tmpdir.join("rindex")
# Slots: lockspace rindex master-lease user-lease-1
- size = 1024**2 * 4
+ size = 4 * MiB
util.create_file(str(path), size)
# Note: using 1 second io timeout (-o 1) for quicker tests.
lockspace = "ls_name:1:%s:0" % path
util.sanlock("client", "init", "-s", lockspace,
"-o", "1")
@@ -122,30 +123,30 @@ def test_create(tmpdir, sanlock_daemon):
util.sanlock("client", "create", "-x", rindex,
"-e", "res")
with io.open(str(path), "rb") as f:
# New entry should be created at the first slot
# The first rindex sector is used by the rindex header.
- f.seek(1024**2 + 512)
+ f.seek(MiB + 512)
util.check_rindex_entry(f.read(RINDEX_ENTRY_SIZE),
- b"res", 1024**2 * 3, 0)
+ b"res", 3 * MiB, 0)
# The rest of the entries should not be modified.
rest = 512 * RINDEX_ENTRIES_SECTORS - RINDEX_ENTRY_SIZE
assert f.read(rest) == b"\0" * rest
# The next slot should contain the internal lease.
- f.seek(1024**2 * 3)
+ f.seek(3 * MiB)
magic, = struct.unpack("< I", f.read(4))
assert magic == PAXOS_DISK_MAGIC
util.check_guard(str(path), size)
def test_delete(tmpdir, sanlock_daemon):
path = tmpdir.join("rindex")
# Slots: lockspace rindex master-lease user-lease-1
- size = 1024**2 * 4
+ size = 4 * MiB
util.create_file(str(path), size)
# Note: using 1 second io timeout (-o 1) for quicker tests.
lockspace = "ls_name:1:%s:0" % path
util.sanlock("client", "init", "-s", lockspace,
"-o", "1")
@@ -157,29 +158,29 @@ def test_delete(tmpdir, sanlock_daemon):
util.sanlock("client", "create", "-x", rindex,
"-e", "res")
util.sanlock("client", "delete", "-x", rindex,
"-e", "res")
with io.open(str(path), "rb") as f:
# First entry should be cleared.
- f.seek(1024**2 + 512)
+ f.seek(MiB + 512)
util.check_rindex_entry(f.read(RINDEX_ENTRY_SIZE), b"", 0, 0)
# Rest of entires should not be modified.
rest = 512 * RINDEX_ENTRIES_SECTORS - RINDEX_ENTRY_SIZE
assert f.read(rest) == b"\0" * rest
# The next slot should contain a cleared lease.
- f.seek(1024**2 * 3)
+ f.seek(3 * MiB)
magic, = struct.unpack("< I", f.read(4))
assert magic == PAXOS_DISK_CLEAR
util.check_guard(str(path), size)
def test_lookup(tmpdir, sanlock_daemon):
path = tmpdir.join("rindex")
# Slots: lockspace rindex master-lease user-lease-1 ... user-lease-7
- size = 1024**2 * 10
+ size = 10 * MiB
util.create_file(str(path), size)
# Note: using 1 second io timeout (-o 1) for quicker tests.
lockspace = "ls_name:1:%s:0" % path
util.sanlock("client", "init", "-s", lockspace,
"-o", "1")
@@ -194,11 +195,11 @@ def test_lookup(tmpdir, sanlock_daemon):
assert lookup == b"lookup done 0\nname res offset 3145728\n"
def test_lookup_uninitialized(tmpdir, sanlock_daemon):
path = tmpdir.join("rindex")
- util.create_file(str(path), 1024**2)
+ util.create_file(str(path), MiB)
rindex = "ls_name:%s:1M" % path
with pytest.raises(util.CommandError) as e:
util.sanlock("client", "lookup", "-x", rindex,
"-e", "res")
@@ -208,11 +209,11 @@ def test_lookup_uninitialized(tmpdir, sanlock_daemon):
def test_lookup_missing(tmpdir, sanlock_daemon):
path = tmpdir.join("rindex")
# Slots: lockspace rindex master-lease user-lease-1 ... user-lease-7
- size = 1024**2 * 10
+ size = 10 * MiB
util.create_file(str(path), size)
# Note: using 1 second io timeout (-o 1) for quicker tests.
lockspace = "ls_name:1:%s:0" % path
util.sanlock("client", "init", "-s", lockspace,
"-o", "1")
diff --git a/tests/direct_test.py b/tests/direct_test.py
index 4ef63bc..a157bc4 100644
--- a/tests/direct_test.py
+++ b/tests/direct_test.py
@@ -6,15 +6,16 @@ from __future__ import absolute_import
import io
import struct
from . import constants
from . import util
+from . units import *
def test_init_lockspace(tmpdir):
path = tmpdir.join("lockspace")
- size = 1024**2
+ size = MiB
util.create_file(str(path), size)
lockspace = "name:1:%s:0" % path
util.sanlock("direct", "init", "-s", lockspace)
@@ -27,11 +28,11 @@ def test_init_lockspace(tmpdir):
util.check_guard(str(path), size)
def test_init_resource(tmpdir, sanlock_daemon):
path = tmpdir.join("resources")
- size = 1024**2
+ size = MiB
util.create_file(str(path), size)
resource = "ls_name:res_name:%s:0" % path
util.sanlock("direct", "init", "-r", resource)
diff --git a/tests/python_test.py b/tests/python_test.py
index 28827d6..e721fe8 100644
--- a/tests/python_test.py
+++ b/tests/python_test.py
@@ -12,24 +12,26 @@ import pytest
import sanlock
from . import constants
from . import util
+from . units import *
+
# Largest file size on ext4 is 16TiB, and on xfs 500 TiB. Use 1 TiB as it is
# large enough to test large offsets, and less likely to fail on developer
# machine or CI slave.
# See
https://access.redhat.com/articles/rhel-limits
-LARGE_FILE_SIZE = 1024**4
+LARGE_FILE_SIZE = TiB
-LOCKSPACE_SIZE = 1024**2
-MIN_RES_SIZE = 1024**2
+LOCKSPACE_SIZE = MiB
+MIN_RES_SIZE = MiB
-ALIGNMENT_1M = 1 * 1024**2
-ALIGNMENT_2M = 2 * 1024**2
+ALIGNMENT_1M = 1 * MiB
+ALIGNMENT_2M = 2 * MiB
SECTOR_SIZE_512 = 512
-SECTOR_SIZE_4K = 4096
+SECTOR_SIZE_4K = 4 * KiB
@pytest.mark.parametrize("size,offset", [
# Smallest offset.
(LOCKSPACE_SIZE, 0),
@@ -238,11 +240,11 @@ def test_read_resource_owners_4k_invalid_sector_size(
assert e.value.errno == errno.EINVAL
def test_read_resource_owners_invalid_align_size(tmpdir, sanlock_daemon):
path = str(tmpdir.join("path"))
- util.create_file(path, 1024**3)
+ util.create_file(path, GiB)
disks = [(path, 0)]
sanlock.write_resource(
"ls_name",
"res_name",
@@ -292,11 +294,11 @@ def test_add_rem_lockspace(tmpdir, sanlock_daemon, size, offset):
assert acquired is False
def test_add_rem_lockspace_async(tmpdir, sanlock_daemon):
path = str(tmpdir.join("ls_name"))
- util.create_file(path, 1024**2)
+ util.create_file(path, MiB)
sanlock.write_lockspace("ls_name", path, iotimeout=1)
acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False)
assert acquired is False
@@ -401,13 +403,13 @@ def test_acquire_release_resource(tmpdir, sanlock_daemon, size,
offset):
assert owners == []
@pytest.mark.parametrize("align, sector", [
# Invalid alignment
- (1024, sanlock.SECTOR_SIZE[0]),
+ (KiB, sanlock.SECTOR_SIZE[0]),
# Invalid sector size
- (sanlock.ALIGN_SIZE[0], 8192),
+ (sanlock.ALIGN_SIZE[0], 8 * KiB),
])
def test_write_lockspace_invalid_align_sector(
tmpdir, sanlock_daemon, align, sector):
path = str(tmpdir.join("lockspace"))
util.create_file(path, LOCKSPACE_SIZE)
@@ -416,13 +418,13 @@ def test_write_lockspace_invalid_align_sector(
sanlock.write_lockspace("name", path, align=align, sector=sector)
@pytest.mark.parametrize("align, sector", [
# Invalid alignment
- (1024, sanlock.SECTOR_SIZE[0]),
+ (KiB, sanlock.SECTOR_SIZE[0]),
# Invalid sector size
- (sanlock.ALIGN_SIZE[0], 8192),
+ (sanlock.ALIGN_SIZE[0], 8 * KiB),
])
def test_write_resource_invalid_align_sector(
tmpdir, sanlock_daemon, align, sector):
path = str(tmpdir.join("resources"))
util.create_file(path, MIN_RES_SIZE)
diff --git a/tests/units.py b/tests/units.py
new file mode 100644
index 0000000..b47d668
--- /dev/null
+++ b/tests/units.py
@@ -0,0 +1,9 @@
+"""
+Constants for file/disk sizes.
+"""
+
+KiB = 1024
+MiB = 1024**2
+GiB = 1024**3
+TiB = 1024**4
+PiB = 1024**5
diff --git a/tests/util.py b/tests/util.py
index e31190e..86c6447 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -9,15 +9,17 @@ import os
import socket
import struct
import subprocess
import time
+from . units import *
+
TESTDIR = os.path.dirname(__file__)
SANLOCK = os.path.join(TESTDIR, os.pardir, "src", "sanlock")
GUARD = b"X"
-GUARD_SIZE = 4096
+GUARD_SIZE = 4 * KiB
class TimeoutExpired(Exception):
""" Raised when timeout expired """
--
2.17.2