Skip to content

Commit d24ea4d

Browse files
committed
testcases/misc: add supplementary group permission test
Signed-off-by: Sachin Prabhu <[email protected]>
1 parent 97b1ae7 commit d24ea4d

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

testcases/misc/test_permissions.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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(
50+
test_info, "supplementary_group"
51+
)
52+
if sgroup is None:
53+
return []
54+
55+
arr = []
56+
for s in testhelper.get_shares(test_info).values():
57+
if check_reqs_supplementary_group(s, sgroup):
58+
arr.append((s["server"], s["name"]))
59+
return arr
60+
61+
62+
@pytest.mark.privileged
63+
@pytest.mark.parametrize(
64+
"ipaddr,share_name", gen_supplementary_group_param(test_info)
65+
)
66+
def test_supplementary_group(ipaddr: str, share_name: str) -> None:
67+
share = testhelper.get_share(test_info, share_name)
68+
fs_path = Path(share["backend"]["path"])
69+
sgroup = testhelper.get_extra_configuration(
70+
test_info, "supplementary_group"
71+
)
72+
test_subdir = Path("supplementary_group")
73+
mount_params = testhelper.get_mount_parameters(test_info, share_name)
74+
75+
# setup local testdir
76+
testdir = fs_path / test_subdir
77+
testdir.mkdir(exist_ok=True)
78+
shutil.chown(testdir, group=sgroup)
79+
testdir.chmod(0o770)
80+
81+
smbclient = testhelper.SMBClient(
82+
ipaddr,
83+
mount_params["share"],
84+
mount_params["username"],
85+
mount_params["password"],
86+
)
87+
88+
try:
89+
remote_test_file = str(Path("/") / test_subdir / Path("test-cp"))
90+
smbclient.write_text(remote_test_file, test_string)
91+
finally:
92+
smbclient.disconnect()
93+
shutil.rmtree(testdir)

0 commit comments

Comments
 (0)