Skip to content

Commit a6ababe

Browse files
Query all usernames and group names
1 parent 8d69308 commit a6ababe

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

test/test_modules.py

+13
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ def test_user(host):
272272
assert user.password == "*"
273273

274274

275+
def test_user_get_all_users(host):
276+
user_list = host.user("root").get_all_users
277+
assert "root" in user_list
278+
assert "man" in user_list
279+
assert "nobody" in user_list
280+
281+
275282
def test_user_password_days(host):
276283
assert host.user("root").password_max_days == 99999
277284
assert host.user("root").password_min_days == 0
@@ -303,6 +310,12 @@ def test_current_user(host):
303310
def test_group(host):
304311
assert host.group("root").exists
305312
assert host.group("root").gid == 0
313+
group_list = host.group("root").get_all_groups
314+
assert "root" in group_list
315+
assert "bin" in group_list
316+
group_list = host.group("root").get_local_groups
317+
assert "root" in group_list
318+
assert "bin" in group_list
306319

307320

308321
def test_empty_command_output(host):

testinfra/modules/group.py

+26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ def exists(self):
3333

3434
return self.run_expect([0, 2], "getent group %s", self.name).rc == 0
3535

36+
@property
37+
def get_all_groups(self):
38+
"""Returns a list of local and remote group names
39+
40+
>>> host.group("anyname").get_all_groups
41+
["root", "wheel", "man", "tty", <...>]
42+
"""
43+
all_groups = [
44+
line.split(":")[0]
45+
for line in self.check_output("getent group").splitlines()
46+
]
47+
return all_groups
48+
49+
@property
50+
def get_local_groups(self):
51+
"""Returns a list of local group names
52+
53+
>>> host.group("anyname").get_local_groups
54+
["root", "wheel", "man", "tty", <...>]
55+
"""
56+
local_groups = [
57+
line.split(":")[0]
58+
for line in self.check_output("cat /etc/group").splitlines()
59+
]
60+
return local_groups
61+
3662
@property
3763
def gid(self):
3864
return int(self.check_output("getent group %s | cut -d':' -f3", self.name))

testinfra/modules/user.py

+28
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ def expiration_date(self):
133133
epoch = datetime.datetime.utcfromtimestamp(0)
134134
return epoch + datetime.timedelta(days=int(days))
135135

136+
@property
137+
def get_all_users(self):
138+
"""Returns a list of local and remote user names
139+
140+
>>> host.user().get_all_users
141+
["root", "bin", "daemon", "lp", <...>]
142+
"""
143+
all_users = [
144+
line.split(":")[0]
145+
for line in self.check_output("getent passwd").splitlines()
146+
]
147+
return all_users
148+
149+
@property
150+
def get_local_users(self):
151+
"""Returns a list of local user names
152+
153+
>>> host.user().get_local_users
154+
["root", "bin", "daemon", "lp", <...>]
155+
"""
156+
local_users = [
157+
line.split(":")[0]
158+
for line in self.check_output("cat /etc/passwd").splitlines()
159+
]
160+
# strip NIS compat mode entries
161+
local_users = [i for i in local_users if not i.startswith("+")]
162+
return local_users
163+
136164
@classmethod
137165
def get_module_class(cls, host):
138166
if host.system_info.type.endswith("bsd"):

0 commit comments

Comments
 (0)