Skip to content

Commit

Permalink
test: Fix test errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaqx0r committed May 28, 2023
1 parent fa94d65 commit 432f9c5
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions nss_cache/nss_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

__author__ = "vasilios@google.com (Vasilios Hoffman)"

import grp
import pwd
import subprocess
import unittest
from unittest import mock
Expand All @@ -39,16 +37,17 @@ def testGetMap(self):
"""that GetMap is calling the right GetFooMap routines."""

# stub, retval, arg
maps = (
("nss.GetPasswdMap", "TEST_PASSWORD", config.MAP_PASSWORD),
("nss.GetGroupMap", "TEST_GROUP", config.MAP_GROUP),
('nss.GetShadowMap, "TEST_SHADOW', config.MAP_SHADOW),
)

for map_type, retval, arg in maps:
with mock.patch(map_type) as mock_map:
maps = [
('GetPasswdMap', 'TEST_PASSWORD', config.MAP_PASSWORD),
('GetGroupMap', 'TEST_GROUP', config.MAP_GROUP),
('GetShadowMap', 'TEST_SHADOW', config.MAP_SHADOW),
]

for (fn, retval, arg) in maps:
with mock.patch.object(nss, fn) as mock_map:
mock_map.return_value = retval
self.assertEqual(retval, nss.GetMap(arg))
mock_map.assert_called_once()

def testGetMapException(self):
"""GetMap throws error.UnsupportedMap for unsupported maps."""
Expand All @@ -69,14 +68,14 @@ def testGetPasswdMap(self):
entry2.name = "bar"
entry2.uid = 20
entry2.gid = 20
entry2.gecos = "foo bar"
entry2.dir = "/home/monkeyboy"
entry2.shell = "/bin/shell"
foo = ("foo", "x", 10, 10, "foo bar", "/home/foo", "/bin/shell")
bar = ("bar", "x", 20, 20, "foo bar", "/home/monkeyboy", "/bin/shell")
entry2.gecos = 'foo bar'
entry2.dir = '/home/monkeyboy'
entry2.shell = '/bin/shell'
foo = ('foo', 'x', 10, 10, 'foo bar', '/home/foo', '/bin/shell')
bar = ('bar', 'x', 20, 20, 'foo bar', '/home/monkeyboy', '/bin/shell')

# stubs
with mock.patch("pwd.getpwall") as mock_pwall:
with mock.patch('pwd.getpwall') as mock_pwall:
mock_pwall.return_value = [foo, bar]
password_map = nss.GetPasswdMap()
self.assertTrue(isinstance(password_map, passwd.PasswdMap))
Expand All @@ -92,17 +91,17 @@ def testGetGroupMap(self):
entry1.name = "foo"
entry1.passwd = "*"
entry1.gid = 10
entry1.members = [""]
entry1.members = ['']
entry2 = group.GroupMapEntry()
entry2.name = "bar"
entry2.passwd = "*"
entry2.gid = 20
entry2.members = ["foo", "bar"]
foo = ("foo", "*", 10, [])
bar = ("bar", "*", 20, ["foo", "bar"])
entry2.members = ['foo', 'bar']
foo = ('foo', '*', 10, [])
bar = ('bar', '*', 20, ['foo', 'bar'])

# stubs
with mock.patch("grp.getgrall") as mock_grpall:
with mock.patch('grp.getgrall') as mock_grpall:
mock_grpall.return_value = [foo, bar]
group_map = nss.GetGroupMap()
self.assertTrue(isinstance(group_map, group.GroupMap))
Expand All @@ -112,20 +111,21 @@ def testGetGroupMap(self):

def testGetShadowMap(self):
"""Verify we build a correct shadow map from nss calls."""
line1 = b"foo:!!::::::::"
line2 = b"bar:!!::::::::"
line1 = b'foo:!!::::::::'
line2 = b'bar:!!::::::::'

entry1 = shadow.ShadowMapEntry()
entry1.name = "foo"
entry2 = shadow.ShadowMapEntry()
entry2.name = "bar"

with mock.patch("nss._SpawnGetent") as mock_getent:
with mock.patch.object(nss,
'_SpawnGetent') as mock_getent:
# stub
mock_process = mock.create_autospec(subprocess.Popen)
mock_getent.return_value = mock_process
mock_process.communicate.return_value = [b"\n".join([line1, line2]), b""]
mock_process.return_code = 0
mock_process.communicate.return_value = [b'\n'.join([line1, line2]), b'']
mock_process.returncode = 0
# test
shadow_map = nss.GetShadowMap()
self.assertTrue(isinstance(shadow_map, shadow.ShadowMap))
Expand Down

0 comments on commit 432f9c5

Please sign in to comment.