From 8f845a82937967de9bf4dae431ba09a3c1ba55e1 Mon Sep 17 00:00:00 2001 From: Sachin Prabhu Date: Tue, 30 Jan 2024 15:19:57 +0000 Subject: [PATCH] misc: Add test for supplementary groups Signed-off-by: Sachin Prabhu --- testcases/misc/test_supplemantary_group.py | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 testcases/misc/test_supplemantary_group.py diff --git a/testcases/misc/test_supplemantary_group.py b/testcases/misc/test_supplemantary_group.py new file mode 100644 index 0000000..818a6e9 --- /dev/null +++ b/testcases/misc/test_supplemantary_group.py @@ -0,0 +1,84 @@ +# This test is to check writes as part of a supplementary group. +# The following is required to run this test: +# 1. user1 exists +# 2. user1 is part of supplementary group sg + +import testhelper +import pytest +import shutil +import pwd +import grp +import os +from pathlib import Path + +test_info_file = os.getenv("TEST_INFO_FILE") +test_info = testhelper.read_yaml(test_info_file) +test_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +test_subdir = Path("supplementary_group") +test_sgroup = "sg" + + +def check_reqs(): + try: + pwd.getpwnam("test1") + grp.getgrnam(test_sgroup) + except KeyError: + return False + return True + + +def setup_local_testdir(root: Path, group: str) -> Path: + testdir = root / test_subdir + testdir.mkdir(exist_ok=True) + shutil.chown(testdir, group=group) + testdir.chmod(0o770) + return testdir + + +def write_file_remote( + mount_point: Path, + ipaddr: str, + share_name: str, +) -> None: + mount_params = testhelper.get_mount_parameters(test_info, share_name) + mount_params["host"] = ipaddr + try: + test_file = testhelper.get_tmp_file(mount_point) + test_file_remote = test_subdir / Path("test-cp") + with open(test_file, "w") as f: + f.write(test_string) + put_cmds = "put %s %s" % (test_file, test_file_remote) + (ret, output) = testhelper.smbclient(mount_params, put_cmds) + assert ret == 0, "Failed to copy file to server: " + output + finally: + if test_file.exists(): + test_file.unlink() + + +def gen_supplementary_group_param(test_info: dict) -> list: + if not check_reqs(): + return [] + if not test_info: + return [] + arr = [] + for ipaddr in test_info["public_interfaces"]: + for share in testhelper.get_shares_with_directmnt(test_info): + arr.append((ipaddr, share["name"])) + return arr + + +@pytest.mark.parametrize( + "ipaddr,share_name", gen_supplementary_group_param(test_info) +) +def test_supplementary_group( + ipaddr: str, share_name: str +) -> None: + share = testhelper.get_share(test_info, share_name) + fs_path = Path(share["directmnt"]) + testdir = setup_local_testdir(fs_path, test_sgroup) + try: + tmp_root = testhelper.get_tmp_root() + mount_point = testhelper.get_tmp_mount_point(tmp_root) + write_file_remote(mount_point, ipaddr, share_name) + finally: + shutil.rmtree(testdir)