|
| 1 | +import testhelper |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import pwd |
| 5 | +import grp |
| 6 | +import shutil |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +test_info_file = os.getenv("TEST_INFO_FILE") |
| 10 | +test_info = testhelper.read_yaml(test_info_file) |
| 11 | +test_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 12 | + |
| 13 | +# test_supplementary_group: |
| 14 | +# This test is to check writes to a folder owned by a supplementary group. |
| 15 | +# |
| 16 | +# Requirements: |
| 17 | +# 1. username in share["user"] exists |
| 18 | +# 2. username is part of supplementary group provided in |
| 19 | +# user["extra"]["supplementary_group"] |
| 20 | +# 3. share["backend"]["path"] should be set |
| 21 | +# |
| 22 | +# Steps: |
| 23 | +# 1. Create folder test_subdir/ on direct path with |
| 24 | +# group set to sgroup and mode 0770 |
| 25 | +# 2. Upload file to test_subdir/test-cp |
| 26 | +# |
| 27 | +# Expected: |
| 28 | +# Copy passes |
| 29 | + |
| 30 | + |
| 31 | +# function to check if requirements are met |
| 32 | +def check_reqs_supplementary_group(share: dict, sgroup: str) -> bool: |
| 33 | + if share["backend"]["path"] is None: |
| 34 | + return False |
| 35 | + |
| 36 | + username = list(share["users"].keys())[0] |
| 37 | + try: |
| 38 | + pwd.getpwnam(username) |
| 39 | + if username not in grp.getgrnam(sgroup).gr_mem: |
| 40 | + return False |
| 41 | + except KeyError: |
| 42 | + return False |
| 43 | + return True |
| 44 | + |
| 45 | + |
| 46 | +def gen_supplementary_group_param(test_info: dict) -> list: |
| 47 | + if not test_info: |
| 48 | + return [] |
| 49 | + sgroup = testhelper.get_extra_configuration(test_info, |
| 50 | + "supplementary_group") |
| 51 | + if sgroup is None: |
| 52 | + return [] |
| 53 | + |
| 54 | + arr = [] |
| 55 | + for s in testhelper.get_shares(test_info).values(): |
| 56 | + if check_reqs_supplementary_group(s, sgroup): |
| 57 | + arr.append((s["server"], s["name"])) |
| 58 | + return arr |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.privileged |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "ipaddr,share_name", gen_supplementary_group_param(test_info) |
| 64 | +) |
| 65 | +def test_supplementary_group(ipaddr: str, share_name: str) -> None: |
| 66 | + share = testhelper.get_share(test_info, share_name) |
| 67 | + fs_path = Path(share["backend"]["path"]) |
| 68 | + sgroup = testhelper.get_extra_configuration(test_info, |
| 69 | + "supplementary_group") |
| 70 | + test_subdir = Path("supplementary_group") |
| 71 | + mount_params = testhelper.get_mount_parameters(test_info, share_name) |
| 72 | + |
| 73 | + # setup local testdir |
| 74 | + testdir = fs_path / test_subdir |
| 75 | + testdir.mkdir(exist_ok=True) |
| 76 | + shutil.chown(testdir, group=sgroup) |
| 77 | + testdir.chmod(0o770) |
| 78 | + |
| 79 | + smbclient = testhelper.SMBClient( |
| 80 | + ipaddr, |
| 81 | + mount_params["share"], |
| 82 | + mount_params["username"], |
| 83 | + mount_params["password"] |
| 84 | + ) |
| 85 | + |
| 86 | + try: |
| 87 | + remote_test_file = str(Path("/") / test_subdir / Path("test-cp")) |
| 88 | + smbclient.write_text(remote_test_file, test_string) |
| 89 | + finally: |
| 90 | + smbclient.disconnect() |
| 91 | + shutil.rmtree(testdir) |
0 commit comments