Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Updating tests to use unittest.mock instead of mox. #157

Merged
merged 4 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions nss_cache/caches/caches_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import stat
import tempfile
import unittest
from mox3 import mox
from unittest import mock

from nss_cache import config
from nss_cache.caches import caches
Expand All @@ -42,7 +42,7 @@ def GetCacheFilename(self):
return os.path.join(self.output_dir, self.CACHE_FILENAME + '.test')


class TestCls(mox.MoxTestBase):
class TestCls(unittest.TestCase):

def setUp(self):
self.workdir = tempfile.mkdtemp()
Expand Down Expand Up @@ -77,20 +77,15 @@ def testCopyOwnerPresent(self):
os.unlink(cache.GetCacheFilename())


class TestCache(mox.MoxTestBase):
class TestCache(unittest.TestCase):

def testWriteMap(self):
cache_map = caches.Cache({}, config.MAP_PASSWORD, None)
self.mox.StubOutWithMock(cache_map, '_Commit')
self.mox.StubOutWithMock(cache_map, 'Write')
self.mox.StubOutWithMock(cache_map, 'Verify')
cache_map._Commit()
cache_map.Write('writable_map').AndReturn('entries_written')
cache_map.Verify('entries_written').AndReturn(True)
self.mox.ReplayAll()

self.assertEqual(0, cache_map.WriteMap('writable_map'))

with mock.patch.object(cache_map, 'Write') as write, mock.patch.object(cache_map, 'Verify') as verify, mock.patch.object(cache_map, '_Commit') as commit:
write.return_value = 'entries_written'
verify.return_value = True
self.assertEqual(0, cache_map.WriteMap('writable_map'))


if __name__ == '__main__':
unittest.main()
42 changes: 14 additions & 28 deletions nss_cache/caches/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import shutil
import tempfile
import unittest
from unittest import mock
import sys
from mox3 import mox

from nss_cache import config
from nss_cache.maps import automount
Expand All @@ -34,7 +34,7 @@
from nss_cache.caches import files


class TestFilesCache(mox.MoxTestBase):
class TestFilesCache(unittest.TestCase):

def setUp(self):
super(TestFilesCache, self).setUp()
Expand Down Expand Up @@ -78,8 +78,7 @@ def testCacheFilenameSuffixOption(self):
def testWritePasswdEntry(self):
"""We correctly write a typical entry in /etc/passwd format."""
cache = files.FilesPasswdMapHandler(self.config)
file_mock = self.mox.CreateMock(sys.stdout)
file_mock.write(b'root:x:0:0:Rootsy:/root:/bin/bash\n')
file_mock = mock.create_autospec(sys.stdout)

map_entry = passwd.PasswdMapEntry()
map_entry.name = 'root'
Expand All @@ -90,81 +89,68 @@ def testWritePasswdEntry(self):
map_entry.dir = '/root'
map_entry.shell = '/bin/bash'

self.mox.ReplayAll()

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'root:x:0:0:Rootsy:/root:/bin/bash\n')

def testWriteGroupEntry(self):
"""We correctly write a typical entry in /etc/group format."""
cache = files.FilesGroupMapHandler(self.config)
file_mock = self.mox.CreateMock(sys.stdout)
file_mock.write(b'root:x:0:zero_cool,acid_burn\n')
file_mock = mock.create_autospec(sys.stdout)

map_entry = group.GroupMapEntry()
map_entry.name = 'root'
map_entry.passwd = 'x'
map_entry.gid = 0
map_entry.members = ['zero_cool', 'acid_burn']

self.mox.ReplayAll()

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'root:x:0:zero_cool,acid_burn\n')

def testWriteShadowEntry(self):
"""We correctly write a typical entry in /etc/shadow format."""
cache = files.FilesShadowMapHandler(self.config)
file_mock = self.mox.CreateMock(sys.stdout)
file_mock.write(b'root:$1$zomgmd5support:::::::\n')
file_mock = mock.create_autospec(sys.stdout)

map_entry = shadow.ShadowMapEntry()
map_entry.name = 'root'
map_entry.passwd = '$1$zomgmd5support'

self.mox.ReplayAll()

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'root:$1$zomgmd5support:::::::\n')

def testWriteNetgroupEntry(self):
"""We correctly write a typical entry in /etc/netgroup format."""
cache = files.FilesNetgroupMapHandler(self.config)
file_mock = self.mox.CreateMock(sys.stdout)
file_mock.write(
b'administrators unix_admins noc_monkeys (-,zero_cool,)\n')
file_mock = mock.create_autospec(sys.stdout)

map_entry = netgroup.NetgroupMapEntry()
map_entry.name = 'administrators'
map_entry.entries = 'unix_admins noc_monkeys (-,zero_cool,)'

self.mox.ReplayAll()

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'administrators unix_admins noc_monkeys (-,zero_cool,)\n')

def testWriteAutomountEntry(self):
"""We correctly write a typical entry in /etc/auto.* format."""
cache = files.FilesAutomountMapHandler(self.config)
file_mock = self.mox.CreateMock(sys.stdout)
file_mock.write(b'scratch -tcp,rw,intr,bg fileserver:/scratch\n')
file_mock = mock.create_autospec(sys.stdout)

map_entry = automount.AutomountMapEntry()
map_entry.key = 'scratch'
map_entry.options = '-tcp,rw,intr,bg'
map_entry.location = 'fileserver:/scratch'

self.mox.ReplayAll()
cache._WriteData(file_mock, map_entry)
self.mox.VerifyAll()

file_mock = self.mox.CreateMock(sys.stdout)
file_mock.write(b'scratch fileserver:/scratch\n')
file_mock.write.assert_called_with(b'scratch -tcp,rw,intr,bg fileserver:/scratch\n')

file_mock = mock.create_autospec(sys.stdout)
map_entry = automount.AutomountMapEntry()
map_entry.key = 'scratch'
map_entry.options = None
map_entry.location = 'fileserver:/scratch'

self.mox.ReplayAll()

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'scratch fileserver:/scratch\n')

def testAutomountSetsFilename(self):
"""We set the correct filename based on mountpoint information."""
Expand Down
Loading