Skip to content

Commit

Permalink
Merge pull request #159 from google/reformat
Browse files Browse the repository at this point in the history
chore: Reformat with `yapf3 -r -i .`
  • Loading branch information
jaqx0r authored Sep 21, 2022
2 parents 5501586 + 3f12a80 commit ff14b97
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
7 changes: 5 additions & 2 deletions nss_cache/caches/caches_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ class TestCache(unittest.TestCase):

def testWriteMap(self):
cache_map = caches.Cache({}, config.MAP_PASSWORD, None)
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:
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()
9 changes: 6 additions & 3 deletions nss_cache/caches/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def testWritePasswdEntry(self):
map_entry.shell = '/bin/bash'

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'root:x:0:0:Rootsy:/root:/bin/bash\n')
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."""
Expand Down Expand Up @@ -128,7 +129,8 @@ def testWriteNetgroupEntry(self):
map_entry.entries = 'unix_admins noc_monkeys (-,zero_cool,)'

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'administrators unix_admins noc_monkeys (-,zero_cool,)\n')
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."""
Expand All @@ -141,7 +143,8 @@ def testWriteAutomountEntry(self):
map_entry.location = 'fileserver:/scratch'

cache._WriteData(file_mock, map_entry)
file_mock.write.assert_called_with(b'scratch -tcp,rw,intr,bg 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()
Expand Down
42 changes: 29 additions & 13 deletions nss_cache/lock_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ def testHandleArgumentsProperly(self):

def testDestructorUnlocks(self):
yes = lock.PidFile()
with mock.patch.object(yes, 'Locked') as locked, mock.patch.object(yes, 'Unlock') as unlock:
with mock.patch.object(yes, 'Locked') as locked, mock.patch.object(
yes, 'Unlock') as unlock:
locked.return_value = True
yes.__del__()
# Destructor should unlock
unlock.assert_called_once()

no = lock.PidFile()
with mock.patch.object(no, 'Locked') as locked, mock.patch.object(yes, 'Unlock') as unlock:
with mock.patch.object(no, 'Locked') as locked, mock.patch.object(
yes, 'Unlock') as unlock:
locked.return_value = False
no.__del__()
# No unlock needed if already not locked.
Expand Down Expand Up @@ -119,11 +121,13 @@ def testLockCreatesPidfiles(self):
def testLockLocksWithFcntl(self):
locker = lock.PidFile(pid='PID')

with mock.patch.object(locker, '_file') as f, mock.patch('fcntl.lockf') as lockf:
with mock.patch.object(
locker, '_file') as f, mock.patch('fcntl.lockf') as lockf:

locker.Lock()
self.assertTrue(locker._locked)
lockf.assert_called_once_with(locker._file, fcntl.LOCK_EX | fcntl.LOCK_NB)
lockf.assert_called_once_with(locker._file,
fcntl.LOCK_EX | fcntl.LOCK_NB)

def testLockStoresPid(self):
locker = lock.PidFile(filename=self.filename, pid='PID')
Expand All @@ -140,31 +144,40 @@ def testLockStoresPid(self):
def testLockTrapsPermissionDeniedOnly(self):
locker = lock.PidFile()
with mock.patch.object(locker, '_Open') as open:
open.side_effect = [IOError(errno.EACCES, ''), IOError(errno.EIO, '')]
open.side_effect = [
IOError(errno.EACCES, ''),
IOError(errno.EIO, '')
]

self.assertEqual(False, locker.Lock())
self.assertRaises(IOError, locker.Lock)

def testForceLockTerminatesAndClearsLock(self):
locker = lock.PidFile(pid='PID')
with mock.patch.object(locker, 'SendTerm'), mock.patch.object(locker, 'ClearLock'), mock.patch.object(locker, '_file') as f:
with mock.patch.object(locker, 'SendTerm'), mock.patch.object(
locker, 'ClearLock'), mock.patch.object(locker, '_file') as f:
with mock.patch('fcntl.lockf') as lockf:
# This is a little weird due to recursion.
# The first time through lockf throws an error and we retry the lock.
# The 2nd time through we should fail, because lockf will still throw
# an error, so we expect False back and the above mock objects
# invoked.
lockf.side_effect = [IOError(errno.EAGAIN, ''), IOError(errno.EAGAIN, '')]
lockf.side_effect = [
IOError(errno.EAGAIN, ''),
IOError(errno.EAGAIN, '')
]
self.assertFalse(locker.Lock(force=True))
lockf.assert_has_calls((mock.call(locker._file, fcntl.LOCK_EX | fcntl.LOCK_NB),
mock.call(locker._file, fcntl.LOCK_EX | fcntl.LOCK_NB)))
lockf.assert_has_calls(
(mock.call(locker._file, fcntl.LOCK_EX | fcntl.LOCK_NB),
mock.call(locker._file, fcntl.LOCK_EX | fcntl.LOCK_NB)))

def testSendTermMatchesCommandAndSendsTerm(self):
locker = lock.PidFile()
# Program mocks
mock_re = mock.create_autospec(re.Pattern)
mock_re.match.return_value = True
with mock.patch('re.compile') as regexp, mock.patch('os.kill') as kill, mock.patch.object(locker, '_file') as f:
with mock.patch('re.compile') as regexp, mock.patch(
'os.kill') as kill, mock.patch.object(locker, '_file') as f:
f.read.return_value = '1234'
regexp.return_value = mock_re

Expand All @@ -191,7 +204,8 @@ def testSendTermMatchesCommandAndSendsTerm(self):

def testSendTermNoPid(self):
locker = lock.PidFile()
with mock.patch.object(locker, '_file') as f, mock.patch('os.kill') as kill:
with mock.patch.object(locker,
'_file') as f, mock.patch('os.kill') as kill:
f.read.return_value = '\n'
locker.PROC = self.workdir
locker.SendTerm()
Expand All @@ -200,7 +214,8 @@ def testSendTermNoPid(self):

def testSendTermNonePid(self):
locker = lock.PidFile()
with mock.patch.object(locker, '_file') as f, mock.patch('os.kill') as kill:
with mock.patch.object(locker,
'_file') as f, mock.patch('os.kill') as kill:
f.read.return_value = None
locker.PROC = self.workdir
locker.SendTerm()
Expand All @@ -209,7 +224,8 @@ def testSendTermNonePid(self):

def testSendTermTrapsENOENT(self):
locker = lock.PidFile()
with mock.patch.object(locker, '_file') as f, mock.patch('os.kill') as kill, mock.patch('builtins.open') as mock_open:
with mock.patch.object(locker, '_file') as f, mock.patch(
'os.kill') as kill, mock.patch('builtins.open') as mock_open:
f.read.return_value = '1234\n'
mock_open.side_effect = IOError(errno.ENOENT, '')
# self.workdir/1234/cmdline should not exist :)
Expand Down
2 changes: 1 addition & 1 deletion nss_cache/util/timestamps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def testReadTimestampInFuture(self):
with mock.patch('time.gmtime') as gmtime:
gmtime.return_value = now
ts = timestamps.ReadTimestamp(ts_filename)
self.assertEqual(now, ts)
self.assertEqual(now, ts)

def testWriteTimestamp(self):
ts_filename = os.path.join(self.workdir, 'tsw')
Expand Down

0 comments on commit ff14b97

Please sign in to comment.