Skip to content

Commit

Permalink
Introduce setup and teardown fixtures
Browse files Browse the repository at this point in the history
With this change, multiple test modules triggered from testcases/mount
can be run as individual tests complying to pytest standards.
Also any number of new tests can be added efficiently on the mount.

Fixes: #30
Signed-off-by: Shwetha K Acharya <[email protected]>
  • Loading branch information
Shwetha-Acharya committed Oct 20, 2023
1 parent cd9bd71 commit 1a4153f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 64 deletions.
48 changes: 48 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

import pytest
import os
import shutil
import testhelper
import typing

flag_mounted: bool = False
tmp_root: str
test_dir: str
test_info = os.getenv("TEST_INFO_FILE")
tmp_root = testhelper.get_tmp_root()
mount_point = testhelper.get_tmp_mount_point(tmp_root)


@pytest.fixture
def setup_mount(ipaddr: str, sharename: str) -> str:
global flag_mounted, tmp_root, test_dir
try:
mount_params = testhelper.get_mount_parameters(
testhelper.read_yaml(test_info), sharename
)
mount_params["host"] = ipaddr

# mount cifs share
testhelper.cifs_mount(mount_params, mount_point)
flag_mounted = True
test_dir = os.path.join(mount_point, "mount_test")
os.makedirs(test_dir, exist_ok=True)
except Exception as e:
raise Exception(f"Setup failed: {str(e)}")
return test_dir


@pytest.fixture
def teardown_mount() -> typing.Generator[None, str, None]:
yield

global flag_mounted, tmp_root, test_dir
try:
if flag_mounted:
shutil.rmtree(test_dir, ignore_errors=True)
testhelper.cifs_umount(mount_point)
os.rmdir(mount_point)
os.rmdir(tmp_root)
except Exception as e:
raise Exception(f"Teardown failed: {str(e)}")
58 changes: 0 additions & 58 deletions testcases/mount/test_mount.py

This file was deleted.

20 changes: 18 additions & 2 deletions testcases/mount/mount_dbm.py → testcases/mount/test_mount_dbm.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env python3
# Test various database operations via SMB mount-point.

import pytest
import dbm
import hashlib
import pickle
import pathlib
import shutil
import typing
import random
import os
import testhelper


class Record:
Expand Down Expand Up @@ -89,12 +92,25 @@ def _check_dbm_consistency(base: pathlib.Path, nrecs: int) -> None:
db.destroy()


def check_dbm_consistency(rootdir: str) -> None:
base = pathlib.Path(rootdir) / "dbm-consistency"
test_info_file = os.getenv("TEST_INFO_FILE")


@pytest.mark.parametrize(
"ipaddr,sharename", testhelper.generate_mount_check(test_info_file)
)
def test_check_dbm_consistency(
ipaddr: str,
sharename: str,
setup_mount: typing.Any,
teardown_mount: typing.Any,
) -> None:
mountpoint = setup_mount(ipaddr, sharename)
base = pathlib.Path(mountpoint) / "dbm-consistency"
base.mkdir(parents=True, exist_ok=True)
try:
_check_dbm_consistency(base, 10)
_check_dbm_consistency(base, 100)
_check_dbm_consistency(base, 10000)
finally:
shutil.rmtree(base, ignore_errors=True)
teardown_mount()
20 changes: 16 additions & 4 deletions testcases/mount/mount_io.py → testcases/mount/test_mount_io.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

# Test various file-system I/O operations via local SMB mount-point.

import pytest
import datetime
import pathlib
import shutil
import typing
import testhelper
import random
import os
from typing import Any


class DataPath:
Expand Down Expand Up @@ -101,11 +104,11 @@ def _run_checks(dsets: typing.List[DataPath]) -> None:
dset.verify_noent()


def _check_io_consistency(rootdir: str) -> None:
def _check_io_consistency(test_dir: str) -> None:
base = None
try:
print("\n")
base = pathlib.Path(rootdir) / "test_io_consistency"
base = pathlib.Path(test_dir) / "test_io_consistency"
base.mkdir(parents=True)
# Case-1: single 4K file
_run_checks(_make_datasets(base, 4096, 1))
Expand All @@ -129,6 +132,15 @@ def _reset_random_seed() -> None:
random.seed(seed)


def check_io_consistency(rootdir: str) -> None:
@pytest.mark.parametrize(
"ipaddr,sharename",
testhelper.generate_mount_check(os.getenv("TEST_INFO_FILE")),
)
def test_check_io_consistency(
ipaddr: str, sharename: str, setup_mount: Any, teardown_mount: Any
) -> None:
print("%s - %s" % (ipaddr, sharename))
_reset_random_seed()
_check_io_consistency(rootdir)
mountpoint = setup_mount(ipaddr, sharename)
_check_io_consistency(mountpoint)
teardown_mount()
11 changes: 11 additions & 0 deletions testhelper/testhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,14 @@ def generate_random_bytes(size: int) -> bytes:
rnd = bytearray(random.randbytes(min(rem, 1024)))
rba = rba + rnd + rba
return rba[:size]


def generate_mount_check(
test_info_file: typing.Optional[str],
) -> typing.List[typing.Tuple[str, str]]:
test_info = read_yaml(test_info_file)
arr = []
for ipaddr in test_info["public_interfaces"]:
for share_name in test_info["exported_sharenames"]:
arr.append((ipaddr, share_name))
return arr

0 comments on commit 1a4153f

Please sign in to comment.