Skip to content

Commit 4fa35c1

Browse files
Query all usernames and group names
1 parent fa48a25 commit 4fa35c1

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

233233

234+
def test_user_get_all_users(host):
235+
user_list = host.user("root").get_all_users
236+
assert "root" in user_list
237+
assert "man" in user_list
238+
assert "nobody" in user_list
239+
240+
234241
def test_user_password_days(host):
235242
assert host.user("root").password_max_days == 99999
236243
assert host.user("root").password_min_days == 0
@@ -263,6 +270,12 @@ def test_current_user(host):
263270
def test_group(host):
264271
assert host.group("root").exists
265272
assert host.group("root").gid == 0
273+
group_list = host.group("root").get_all_groups
274+
assert "root" in group_list
275+
assert "bin" in group_list
276+
group_list = host.group("root").get_local_groups
277+
assert "root" in group_list
278+
assert "bin" in group_list
266279

267280

268281
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)