Skip to content

Commit

Permalink
misc: Add test for supplementary groups
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Prabhu <[email protected]>
  • Loading branch information
spuiuk committed Feb 29, 2024
1 parent d6ac820 commit deb1772
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test-info.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ shares:
test2: x
# If present, the hostname to use to connect to the test server
server: hostname1
# Add additional information which may be used by tests
extra:
# The supplementary group to be used by test_supplementary_group
supplementary_group: sg
export2:
95 changes: 95 additions & 0 deletions testcases/misc/test_supplemantary_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# This test is to check writes to a folder owned by a supplementary group.
# expected username in share["users"] and
# share["extra"]["supplementary_group"] with supplementary group
#
# Requirements:
# 1. username in share["user"] exists
# 2. username is part of supplementary group provided in
# user["extra"]["supplementary_group"]
#
# Steps:
# 1. Create folder test_subdir/ with group set to sgroup and mode 0770
# 2. Upload file to test_subdir/test-cp
#
# Expected:
# Copy passes

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")


def check_reqs(username, groupname):
try:
pwd.getpwnam(username)
if username not in grp.getgrnam(groupname).gr_mem:
return False
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 test_info:
return []
arr = []
for share in testhelper.get_shares_with_directmnt(test_info):
s = testhelper.get_share(test_info, share)
username = list(s["users"].keys())[0]
sgroup = s["extra"]["supplementary_group"]
if check_reqs(username, sgroup):
arr.append((s["server"], share))
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["backend"]["path"])
sgroup = share["extra"]["supplementary_group"]
testdir = setup_local_testdir(fs_path, 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)

0 comments on commit deb1772

Please sign in to comment.