|
| 1 | +# This test is to check writes to a folder owned by a supplementary group. |
| 2 | +# expected username in share["users"] and |
| 3 | +# share["extra"]["supplementary_group"] with supplementary group |
| 4 | +# |
| 5 | +# Requirements: |
| 6 | +# 1. username in share["user"] exists |
| 7 | +# 2. username is part of supplementary group provided in |
| 8 | +# user["extra"]["supplementary_group"] |
| 9 | +# |
| 10 | +# Steps: |
| 11 | +# 1. Create folder test_subdir/ with group set to sgroup and mode 0770 |
| 12 | +# 2. Upload file to test_subdir/test-cp |
| 13 | +# |
| 14 | +# Expected: |
| 15 | +# Copy passes |
| 16 | + |
| 17 | +import testhelper |
| 18 | +import pytest |
| 19 | +import shutil |
| 20 | +import pwd |
| 21 | +import grp |
| 22 | +import os |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +test_info_file = os.getenv("TEST_INFO_FILE") |
| 26 | +test_info = testhelper.read_yaml(test_info_file) |
| 27 | +test_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 28 | +test_subdir = Path("supplementary_group") |
| 29 | + |
| 30 | + |
| 31 | +def check_reqs(username, groupname): |
| 32 | + try: |
| 33 | + pwd.getpwnam(username) |
| 34 | + if username not in grp.getgrnam(groupname).gr_mem: |
| 35 | + return False |
| 36 | + except KeyError: |
| 37 | + return False |
| 38 | + return True |
| 39 | + |
| 40 | + |
| 41 | +def setup_local_testdir(root: Path, group: str) -> Path: |
| 42 | + testdir = root / test_subdir |
| 43 | + testdir.mkdir(exist_ok=True) |
| 44 | + shutil.chown(testdir, group=group) |
| 45 | + testdir.chmod(0o770) |
| 46 | + return testdir |
| 47 | + |
| 48 | + |
| 49 | +def write_file_remote( |
| 50 | + mount_point: Path, |
| 51 | + ipaddr: str, |
| 52 | + share_name: str, |
| 53 | +) -> None: |
| 54 | + mount_params = testhelper.get_mount_parameters(test_info, share_name) |
| 55 | + mount_params["host"] = ipaddr |
| 56 | + try: |
| 57 | + test_file = testhelper.get_tmp_file(mount_point) |
| 58 | + test_file_remote = test_subdir / Path("test-cp") |
| 59 | + with open(test_file, "w") as f: |
| 60 | + f.write(test_string) |
| 61 | + put_cmds = "put %s %s" % (test_file, test_file_remote) |
| 62 | + (ret, output) = testhelper.smbclient(mount_params, put_cmds) |
| 63 | + assert ret == 0, "Failed to copy file to server: " + output |
| 64 | + finally: |
| 65 | + if test_file.exists(): |
| 66 | + test_file.unlink() |
| 67 | + |
| 68 | + |
| 69 | +def gen_supplementary_group_param(test_info: dict) -> list: |
| 70 | + if not test_info: |
| 71 | + return [] |
| 72 | + arr = [] |
| 73 | + for share in testhelper.get_shares_with_directmnt(test_info): |
| 74 | + s = testhelper.get_share(test_info, share) |
| 75 | + username = list(s["users"].keys())[0] |
| 76 | + sgroup = s["extra"]["supplementary_group"] |
| 77 | + if check_reqs(username, sgroup): |
| 78 | + arr.append((s["server"], share)) |
| 79 | + return arr |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize( |
| 83 | + "ipaddr,share_name", gen_supplementary_group_param(test_info) |
| 84 | +) |
| 85 | +def test_supplementary_group(ipaddr: str, share_name: str) -> None: |
| 86 | + share = testhelper.get_share(test_info, share_name) |
| 87 | + fs_path = Path(share["backend"]["path"]) |
| 88 | + sgroup = share["extra"]["supplementary_group"] |
| 89 | + testdir = setup_local_testdir(fs_path, sgroup) |
| 90 | + try: |
| 91 | + tmp_root = testhelper.get_tmp_root() |
| 92 | + mount_point = testhelper.get_tmp_mount_point(tmp_root) |
| 93 | + write_file_remote(mount_point, ipaddr, share_name) |
| 94 | + finally: |
| 95 | + shutil.rmtree(testdir) |
0 commit comments