Skip to content

Commit bbc0173

Browse files
committed
misc: Add test for supplementary groups
Signed-off-by: Sachin Prabhu <[email protected]>
1 parent 17d9fb0 commit bbc0173

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

test-info.yml.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ users:
88
# Backend filesystem of the exported shares
99
backend: glusterfs
1010

11+
# Additional information which may be used by tests here
12+
# some tests may be skipped if the required config option is not set
13+
extra:
14+
# The supplementary group to be used by test_supplementary_group
15+
supplementary_group: sg
16+
1117
# shares: List of dict of exported shares
1218
shares:
1319
# share name export1
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
# also needs direct access to underlying filesystem
5+
#
6+
# Requirements:
7+
# 1. username in share["user"] exists
8+
# 2. username is part of supplementary group provided in
9+
# user["extra"]["supplementary_group"]
10+
# 3. share["backend"]["path"] should be set
11+
#
12+
# Steps:
13+
# 1. Create folder test_subdir/ on direct path with
14+
# group set to sgroup and mode 0770
15+
# 2. Upload file to test_subdir/test-cp
16+
#
17+
# Expected:
18+
# Copy passes
19+
20+
import testhelper
21+
import pytest
22+
import shutil
23+
import pwd
24+
import grp
25+
import os
26+
from pathlib import Path
27+
28+
test_info_file = os.getenv("TEST_INFO_FILE")
29+
test_info = testhelper.read_yaml(test_info_file)
30+
test_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
31+
test_subdir = Path("supplementary_group")
32+
33+
34+
def check_reqs(username: str, groupname: str) -> bool:
35+
try:
36+
pwd.getpwnam(username)
37+
if username not in grp.getgrnam(groupname).gr_mem:
38+
return False
39+
except KeyError:
40+
return False
41+
return True
42+
43+
44+
def setup_local_testdir(root: Path, group: str) -> Path:
45+
testdir = root / test_subdir
46+
testdir.mkdir(exist_ok=True)
47+
shutil.chown(testdir, group=group)
48+
testdir.chmod(0o770)
49+
return testdir
50+
51+
52+
def write_file_remote(
53+
mount_point: Path,
54+
ipaddr: str,
55+
share_name: str,
56+
) -> None:
57+
mount_params = testhelper.get_mount_parameters(test_info, share_name)
58+
mount_params["host"] = ipaddr
59+
try:
60+
test_file = testhelper.get_tmp_file(mount_point)
61+
test_file_remote = test_subdir / Path("test-cp")
62+
with open(test_file, "w") as f:
63+
f.write(test_string)
64+
put_cmds = "put %s %s" % (test_file, test_file_remote)
65+
(ret, output) = testhelper.smbclient(mount_params, put_cmds)
66+
assert ret == 0, "Failed to copy file to server: " + output
67+
finally:
68+
if test_file.exists():
69+
test_file.unlink()
70+
71+
72+
def gen_supplementary_group_param(test_info: dict) -> list:
73+
if not test_info:
74+
return []
75+
sgroup = testhelper.get_conf_extra(test_info, "supplementary_group")
76+
if sgroup is None:
77+
return []
78+
arr = []
79+
for share in testhelper.get_shares_with_directmnt(test_info):
80+
s = testhelper.get_share(test_info, share)
81+
username = list(s["users"].keys())[0]
82+
if check_reqs(username, sgroup):
83+
arr.append((s["server"], share))
84+
return arr
85+
86+
87+
@pytest.mark.parametrize(
88+
"ipaddr,share_name", gen_supplementary_group_param(test_info)
89+
)
90+
def test_supplementary_group(ipaddr: str, share_name: str) -> None:
91+
share = testhelper.get_share(test_info, share_name)
92+
fs_path = Path(share["backend"]["path"])
93+
sgroup = testhelper.get_conf_extra(test_info, "supplementary_group")
94+
testdir = setup_local_testdir(fs_path, sgroup)
95+
try:
96+
tmp_root = testhelper.get_tmp_root()
97+
mount_point = testhelper.get_tmp_mount_point(tmp_root)
98+
write_file_remote(mount_point, ipaddr, share_name)
99+
finally:
100+
shutil.rmtree(testdir)

testhelper/testhelper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,21 @@ def get_shares_with_directmnt(test_info: dict) -> typing.List[str]:
206206
if share["backend"].get("path", False):
207207
arr.append(share["name"])
208208
return arr
209+
210+
211+
def get_conf_extra(test_info: dict, key: str) -> typing.Any:
212+
"""
213+
We can pass a test specific configuration under extra in the
214+
test-info.yml file. This function allows easy access to this
215+
configuration section.
216+
217+
Parameters:
218+
test_info: Dict containing the parsed yaml file.
219+
key: key for the extra configuration
220+
Returns:
221+
contents in extra[key]
222+
"""
223+
extra = test_info.get("extra")
224+
if extra is None or key not in extra:
225+
return None
226+
return extra[key]

0 commit comments

Comments
 (0)