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 <Shwetha.K.Acharya@ibm.com>
  • Loading branch information
Shwetha-Acharya committed Oct 16, 2023
1 parent 67419de commit d4a8ae5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 63 deletions.
47 changes: 47 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

import pytest
import os
import shutil
import testhelper
import typing

flag_mounted: bool = False
tmp_root: str
test_dir: str


@pytest.fixture
def setup_mount(ipaddr: str, share_name: str) -> str:
global flag_mounted, tmp_root, test_dir
try:
mount_params = testhelper.get_mount_parameters(
os.getenv("TEST_INFO_FILE"), share_name
)
mount_params["host"] = ipaddr
tmp_root = testhelper.get_tmp_root()
mount_point = testhelper.get_tmp_mount_point(tmp_root)

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


@pytest.fixture
def teardown_mount(mount_point: str) -> 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)}")
56 changes: 0 additions & 56 deletions testcases/mount/test_mount.py

This file was deleted.

21 changes: 19 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,26 @@ 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,share_name", testhelper.generate_mount_check(test_info_file)
)
def test_check_dbm_consistency(
ipaddr: str,
sharename: str,
rootdir: str,
setup_mount: typing.Any,
teardown_mount: typing.Any,
) -> None:
mount_point = setup_mount(ipaddr, sharename)
base = pathlib.Path(mount_point) / "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(mount_point)
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,share_name",
testhelper.generate_mount_check(os.getenv("TEST_INFO_FILE")),
)
def test_check_io_consistency(
ipaddr: str, share_name: str, setup_mount: Any, teardown_mount: Any
) -> None:
print("%s - %s" % (ipaddr, share_name))
_reset_random_seed()
_check_io_consistency(rootdir)
mount_point = setup_mount(ipaddr, share_name)
_check_io_consistency(mount_point)
teardown_mount(mount_point)
18 changes: 17 additions & 1 deletion testhelper/testhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import typing
import random

test_info: typing.Dict[str, typing.Any] = {}


def read_yaml(test_info):
"""Returns a dict containing the contents of the yaml file.
Expand Down Expand Up @@ -72,7 +74,7 @@ def get_total_mount_parameter_combinations(test_info: dict) -> int:


def get_mount_parameters(
test_info: dict, share: str, combonum: int = 0
test_info_file: typing.Optional[str], share: str, combonum: int = 0
) -> typing.Dict[str, str]:
"""Get the mount_params dict for a given share and given combination number
Expand Down Expand Up @@ -137,3 +139,17 @@ def generate_random_bytes(size: int) -> bytes:
else:
rbytes = rba + rbytes + rba + rbytes
return rbytes[:size]


def generate_mount_check(
test_info_file: typing.Optional[str],
) -> typing.List[typing.Tuple[str, str]]:
global test_info
if not test_info_file:
return []
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 d4a8ae5

Please sign in to comment.