From 507f7f2bb668a7ad1cd67b73f5980ad38d2a5b9f Mon Sep 17 00:00:00 2001 From: Shwetha K Acharya Date: Wed, 22 Nov 2023 11:31:53 +0530 Subject: [PATCH] Introduce setup and teardown fixture 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. Old approach: - All the different tests are called from test_mount.py: https://github.com/samba-in-kubernetes/sit-test-cases/blob/main/testcases/mount/test_mount.py - Test_mount.py creates a mount, the required tests are called and then test_mount.py cleans the mount. New approach : - This PR complies with the proposal to use fixtures in order to remove all the load on one single module: https://github.com/samba-in-kubernetes/sit-test-cases/pull/25#issuecomment-1721320096 https://github.com/samba-in-kubernetes/sit-test-cases/pull/25#issuecomment-1725320536 - The tests are no more called from test_mount.py. - Setting up of mount and tearing down of mount is taken care by setup and teardown fixture which is defined in testcases/mount/conftest.py More information about fixtures: https://docs.pytest.org/en/6.2.x/fixture.html - With this change, test files remain the same, but the initiator function starts with keyword test and the each test module starts with keyword 'test' That way the tests are called by pytest. Usage of setup and teardown fixture becomes easily possible. We no more need to to call the test function explictly. This also helps with easy debugging and better understanding of the code, as the tests will be failed at individual mount/IO test. In this case, pytest clearly points which test among test_mount_io.py, test_mount_dbm.py and test_mount_stress.py are failing. Can be very useful - when more and more tests are added - when we want to parse test results in sit-environment project Fixes: #30 Signed-off-by: Shwetha K Acharya --- test-info.yml.example | 2 +- testcases/mount/conftest.py | 63 +++++++++++++++++ testcases/mount/test_mount.py | 70 ------------------- .../mount/{mount_dbm.py => test_mount_dbm.py} | 31 ++++++-- .../mount/{mount_io.py => test_mount_io.py} | 24 +++++-- .../{mount_stress.py => test_mount_stress.py} | 28 ++++++-- 6 files changed, 131 insertions(+), 87 deletions(-) create mode 100644 testcases/mount/conftest.py delete mode 100755 testcases/mount/test_mount.py rename testcases/mount/{mount_dbm.py => test_mount_dbm.py} (71%) mode change 100644 => 100755 rename testcases/mount/{mount_io.py => test_mount_io.py} (80%) mode change 100644 => 100755 rename testcases/mount/{mount_stress.py => test_mount_stress.py} (57%) mode change 100644 => 100755 diff --git a/test-info.yml.example b/test-info.yml.example index e84c89c..3d119fe 100644 --- a/test-info.yml.example +++ b/test-info.yml.example @@ -17,4 +17,4 @@ test_backend: glusterfs premounted_shares: - "/testdir1" - - "/testdir2" \ No newline at end of file + - "/testdir2" diff --git a/testcases/mount/conftest.py b/testcases/mount/conftest.py new file mode 100644 index 0000000..b51f22e --- /dev/null +++ b/testcases/mount/conftest.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +import pytest +import os +import shutil +import testhelper +import typing +from pathlib import Path + +test_info_file = os.getenv("TEST_INFO_FILE") +test_info = testhelper.read_yaml(test_info_file) + + +@pytest.fixture +def setup_mount( + ipaddr: str, share_name: str +) -> typing.Generator[Path, None, None]: + flag_mounted: bool = False + tmp_root = testhelper.get_tmp_root() + mount_point = testhelper.get_tmp_mount_point(tmp_root) + try: + mount_params = testhelper.get_mount_parameters(test_info, share_name) + mount_params["host"] = ipaddr + + # mount cifs share + testhelper.cifs_mount(mount_params, mount_point) + flag_mounted = True + test_dir = mount_point / "mount_test" + test_dir.mkdir() + except Exception as e: + raise Exception(f"Setup failed: {str(e)}") + + # Yield the setup result + yield test_dir + + # Perform teardown after the test has run + try: + if flag_mounted and test_dir: + shutil.rmtree(test_dir, ignore_errors=True) + testhelper.cifs_umount(mount_point) + mount_point.rmdir() + tmp_root.rmdir() + except Exception as e: + raise Exception(f"Teardown failed: {str(e)}") + + +@pytest.fixture +def pre_mounted_shares_available(test_info): + pre_mounted_shares = testhelper.get_premounted_shares(test_info) + return bool(pre_mounted_shares) + + +def generate_mount_check() -> typing.List[typing.Tuple[str, str]]: + ipaddr = test_info["public_interfaces"][0] + exported_sharenames = test_info.get("exported_sharenames", []) + arr = [] + for share_name in exported_sharenames: + arr.append((ipaddr, share_name)) + return arr + + +def generate_mount_check_premounted() -> typing.List[Path]: + return testhelper.get_premounted_shares(test_info) diff --git a/testcases/mount/test_mount.py b/testcases/mount/test_mount.py deleted file mode 100755 index 6be7a9d..0000000 --- a/testcases/mount/test_mount.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 - -# Test mounts a cifs share, creates a new file on it, writes to it, -# deletes the file and unmounts - -import testhelper -import os -import pytest -import typing -import shutil -from pathlib import Path - -from .mount_io import check_io_consistency -from .mount_dbm import check_dbm_consistency -from .mount_stress import check_mnt_stress - -test_info_file = os.getenv("TEST_INFO_FILE") -test_info = testhelper.read_yaml(test_info_file) - - -def mount_check_mounted(mount_point: Path) -> None: - try: - test_dir = mount_point / "mount_test" - test_dir.mkdir() - check_io_consistency(test_dir) - check_dbm_consistency(test_dir) - check_mnt_stress(test_dir) - finally: - shutil.rmtree(test_dir, ignore_errors=True) - - -def mount_check(ipaddr: str, share_name: str) -> None: - mount_params = testhelper.get_mount_parameters(test_info, share_name) - mount_params["host"] = ipaddr - tmp_root = testhelper.get_tmp_root() - mount_point = testhelper.get_tmp_mount_point(tmp_root) - flag_mounted = False - try: - testhelper.cifs_mount(mount_params, mount_point) - flag_mounted = True - mount_check_mounted(Path(mount_point)) - finally: - if flag_mounted: - testhelper.cifs_umount(mount_point) - os.rmdir(mount_point) - os.rmdir(tmp_root) - - -def generate_mount_check() -> typing.List[typing.Tuple[str, str]]: - public_interfaces = test_info.get("public_interfaces", []) - exported_sharenames = test_info.get("exported_sharenames", []) - arr = [] - for ipaddr in public_interfaces: - for share_name in exported_sharenames: - arr.append((ipaddr, share_name)) - return arr - - -@pytest.mark.parametrize("ipaddr,share_name", generate_mount_check()) -def test_mount(ipaddr: str, share_name: str) -> None: - mount_check(ipaddr, share_name) - - -def generate_mount_check_premounted() -> typing.List[Path]: - return testhelper.get_premounted_shares(test_info) - - -@pytest.mark.parametrize("test_dir", generate_mount_check_premounted()) -def test_mount_premounted(test_dir: Path) -> None: - mount_check_mounted(test_dir) diff --git a/testcases/mount/mount_dbm.py b/testcases/mount/test_mount_dbm.py old mode 100644 new mode 100755 similarity index 71% rename from testcases/mount/mount_dbm.py rename to testcases/mount/test_mount_dbm.py index f61fb72..75b6c6e --- a/testcases/mount/mount_dbm.py +++ b/testcases/mount/test_mount_dbm.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # Test various database operations via SMB mount-point. +import pytest import dbm import hashlib import pickle @@ -8,6 +9,7 @@ import typing import random from pathlib import Path +from .conftest import generate_mount_check, generate_mount_check_premounted class Record: @@ -89,12 +91,27 @@ def _check_dbm_consistency(base: Path, nrecs: int) -> None: db.destroy() -def check_dbm_consistency(rootdir: Path) -> None: - base = rootdir / "dbm-consistency" - base.mkdir(parents=True, exist_ok=True) +def _run_dbm_consistency_checks(base_path: Path) -> None: + base_path.mkdir(parents=True, exist_ok=True) try: - _check_dbm_consistency(base, 10) - _check_dbm_consistency(base, 100) - _check_dbm_consistency(base, 10000) + _check_dbm_consistency(base_path, 10) + _check_dbm_consistency(base_path, 100) + _check_dbm_consistency(base_path, 10000) finally: - shutil.rmtree(base, ignore_errors=True) + shutil.rmtree(base_path, ignore_errors=True) + + +@pytest.mark.parametrize("ipaddr,share_name", generate_mount_check()) +def test_dbm_consistency(setup_mount: Path) -> None: + base = setup_mount / "dbm-consistency" + _run_dbm_consistency_checks(base) + + +@pytest.mark.parametrize("test_dir", generate_mount_check_premounted()) +def test_dbm_consistency_premounted( + test_dir: Path, pre_mounted_shares_available: bool +) -> None: + if not pre_mounted_shares_available: + pytest.skip("No pre-mounted shares found for testing") + base = test_dir / "dbm-consistency" + _run_dbm_consistency_checks(base) diff --git a/testcases/mount/mount_io.py b/testcases/mount/test_mount_io.py old mode 100644 new mode 100755 similarity index 80% rename from testcases/mount/mount_io.py rename to testcases/mount/test_mount_io.py index f9789bf..2433c7c --- a/testcases/mount/mount_io.py +++ b/testcases/mount/test_mount_io.py @@ -2,12 +2,14 @@ # Test various file-system I/O operations via local SMB mount-point. +import pytest import datetime import shutil import typing import testhelper import random from pathlib import Path +from .conftest import generate_mount_check, generate_mount_check_premounted class DataPath: @@ -99,11 +101,11 @@ def _run_checks(dsets: typing.List[DataPath]) -> None: dset.verify_noent() -def _check_io_consistency(rootdir: Path) -> None: +def _check_io_consistency(test_dir: Path) -> None: base = None try: print("\n") - base = rootdir / "test_io_consistency" + base = test_dir / "test_io_consistency" base.mkdir(parents=True) # Case-1: single 4K file _run_checks(_make_datasets(base, 4096, 1)) @@ -127,6 +129,20 @@ def _reset_random_seed() -> None: random.seed(seed) -def check_io_consistency(rootdir: Path) -> None: +def _perform_io_consistency_check(directory: Path) -> None: _reset_random_seed() - _check_io_consistency(rootdir) + _check_io_consistency(directory) + + +@pytest.mark.parametrize("ipaddr,share_name", generate_mount_check()) +def test_check_io_consistency(setup_mount: Path) -> None: + _perform_io_consistency_check(setup_mount) + + +@pytest.mark.parametrize("test_dir", generate_mount_check_premounted()) +def test_check_io_consistency_premounted( + test_dir: Path, pre_mounted_shares_available: bool +) -> None: + if not pre_mounted_shares_available: + pytest.skip("No pre-mounted shares found for testing") + _perform_io_consistency_check(test_dir) diff --git a/testcases/mount/mount_stress.py b/testcases/mount/test_mount_stress.py old mode 100644 new mode 100755 similarity index 57% rename from testcases/mount/mount_stress.py rename to testcases/mount/test_mount_stress.py index fb4acf5..e5e936d --- a/testcases/mount/mount_stress.py +++ b/testcases/mount/test_mount_stress.py @@ -1,6 +1,8 @@ +import pytest import threading import testhelper from pathlib import Path +from .conftest import generate_mount_check, generate_mount_check_premounted def _perform_file_operations( @@ -43,14 +45,30 @@ def _stress_test( print("Stress test complete.") -def check_mnt_stress(root_dir: Path) -> None: - _stress_test(root_dir, num_clients=5, num_operations=20, file_size=2**22) +def _run_stress_tests(directory: Path) -> None: _stress_test( - root_dir, num_clients=10, num_operations=30, file_size=2**23 + directory, num_clients=5, num_operations=20, file_size=2**22 ) _stress_test( - root_dir, num_clients=20, num_operations=40, file_size=2**24 + directory, num_clients=10, num_operations=30, file_size=2**23 ) _stress_test( - root_dir, num_clients=15, num_operations=25, file_size=2**25 + directory, num_clients=20, num_operations=40, file_size=2**24 ) + _stress_test( + directory, num_clients=15, num_operations=25, file_size=2**25 + ) + + +@pytest.mark.parametrize("ipaddr,share_name", generate_mount_check()) +def test_check_mnt_stress(setup_mount: Path) -> None: + _run_stress_tests(setup_mount) + + +@pytest.mark.parametrize("test_dir", generate_mount_check_premounted()) +def test_dbm_consistency_premounted( + test_dir: Path, pre_mounted_shares_available: bool +) -> None: + if not pre_mounted_shares_available: + pytest.skip("No pre-mounted shares found for testing") + _run_stress_tests(test_dir)