Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d079d10

Browse files
committed
Replace spwd in linux_shadow tests
Signed-off-by: Georg Pfuetzenreuter <[email protected]>
1 parent 302be70 commit d079d10

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

tests/pytests/unit/modules/test_linux_shadow.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
:codeauthor: Erik Johnson <[email protected]>
2+
:codeauthor: Erik Johnson <[email protected]>
33
"""
44

55
import types
@@ -175,6 +175,11 @@ def test_info(password):
175175
Test if info shows the correct user information
176176
"""
177177

178+
data = {
179+
"/etc/shadow": f"foo:{password.pw_hash}:31337:0:99999:7:::",
180+
"*": Exception("Attempted to open something other than /etc/shadow"),
181+
}
182+
178183
# First test is with a succesful call
179184
expected_result = [
180185
("expire", -1),
@@ -186,10 +191,7 @@ def test_info(password):
186191
("passwd", password.pw_hash),
187192
("warn", 7),
188193
]
189-
getspnam_return = spwd.struct_spwd(
190-
["foo", password.pw_hash, 31337, 0, 99999, 7, -1, -1, -1]
191-
)
192-
with patch("spwd.getspnam", return_value=getspnam_return):
194+
with patch("salt.utils.files.fopen", mock_open(read_data=data)):
193195
result = shadow.info("foo")
194196
assert expected_result == sorted(result.items(), key=lambda x: x[0])
195197

@@ -204,15 +206,8 @@ def test_info(password):
204206
("passwd", ""),
205207
("warn", ""),
206208
]
207-
# We get KeyError exception for non-existent users in glibc based systems
208-
getspnam_return = KeyError
209-
with patch("spwd.getspnam", side_effect=getspnam_return):
210-
result = shadow.info("foo")
211-
assert expected_result == sorted(result.items(), key=lambda x: x[0])
212-
# And FileNotFoundError in musl based systems
213-
getspnam_return = FileNotFoundError
214-
with patch("spwd.getspnam", side_effect=getspnam_return):
215-
result = shadow.info("foo")
209+
with patch("salt.utils.files.fopen", mock_open(read_data=data)):
210+
result = shadow.info("bar")
216211
assert expected_result == sorted(result.items(), key=lambda x: x[0])
217212

218213

0 commit comments

Comments
 (0)