Skip to content

Commit 2e0e478

Browse files
CarstenGrohmannphilpep
authored andcommitted
Query all usernames and group names
1 parent a10baf0 commit 2e0e478

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
@@ -234,6 +234,13 @@ def test_user(host):
234234
assert user.password == "!"
235235

236236

237+
def test_user_get_all_users(host):
238+
user_list = host.user("root").get_all_users
239+
assert "root" in user_list
240+
assert "man" in user_list
241+
assert "nobody" in user_list
242+
243+
237244
def test_user_password_days(host):
238245
assert host.user("root").password_max_days == 99999
239246
assert host.user("root").password_min_days == 0
@@ -266,6 +273,12 @@ def test_current_user(host):
266273
def test_group(host):
267274
assert host.group("root").exists
268275
assert host.group("root").gid == 0
276+
group_list = host.group("root").get_all_groups
277+
assert "root" in group_list
278+
assert "bin" in group_list
279+
group_list = host.group("root").get_local_groups
280+
assert "root" in group_list
281+
assert "bin" in group_list
269282

270283

271284
def test_empty_command_output(host):

testinfra/modules/group.py

+26
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ def exists(self):
3131
"""
3232
return self.run_expect([0, 2], "getent group %s", self.name).rc == 0
3333

34+
@property
35+
def get_all_groups(self):
36+
"""Returns a list of local and remote group names
37+
38+
>>> host.group("anyname").get_all_groups
39+
["root", "wheel", "man", "tty", <...>]
40+
"""
41+
all_groups = [
42+
line.split(":")[0]
43+
for line in self.check_output("getent group").splitlines()
44+
]
45+
return all_groups
46+
47+
@property
48+
def get_local_groups(self):
49+
"""Returns a list of local group names
50+
51+
>>> host.group("anyname").get_local_groups
52+
["root", "wheel", "man", "tty", <...>]
53+
"""
54+
local_groups = [
55+
line.split(":")[0]
56+
for line in self.check_output("cat /etc/group").splitlines()
57+
]
58+
return local_groups
59+
3460
@property
3561
def gid(self):
3662
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)