Commit 73a14b0e authored by Łukasz Nowak's avatar Łukasz Nowak Committed by Łukasz Nowak

updater: Unlink lock file

Also cover loop method.
parent 1f273c26
...@@ -34,6 +34,7 @@ import sys ...@@ -34,6 +34,7 @@ import sys
import tempfile import tempfile
import time import time
import unittest import unittest
import zc.lockfile
from cryptography import x509 from cryptography import x509
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
...@@ -1337,3 +1338,64 @@ class KedifaUpdaterUpdateCertificateTest( ...@@ -1337,3 +1338,64 @@ class KedifaUpdaterUpdateCertificateTest(
self.assertEqual('content', certificate) self.assertEqual('content', certificate)
self.assertTrue(update) self.assertTrue(update)
self.assertState({self.certificate_file_name: True}) self.assertState({self.certificate_file_name: True})
class KedifaUpdaterLoopTest(
KedifaUpdaterMixin, unittest.TestCase):
def test(self):
u = updater.Updater(
1, None, self.state, None, None, None, None, True)
lock_file = u.state_lock_file
os.unlink(self.state)
self.assertFalse(os.path.exists(lock_file))
self.assertFalse(os.path.exists(self.state))
with mock.patch.object(
updater.Updater, 'action', return_value=None) as mock_object:
u.loop()
mock_object.assert_called()
self.assertFalse(os.path.exists(lock_file))
self.assertFalse(os.path.exists(self.state))
def test_raises(self):
u = updater.Updater(
1, None, self.state, None, None, None, None, True)
lock_file = u.state_lock_file
self.assertFalse(os.path.exists(lock_file))
with mock.patch.object(
updater.Updater, 'action', side_effect=Exception()) as mock_object:
self.assertRaises(Exception, u.loop)
mock_object.assert_called()
self.assertFalse(os.path.exists(lock_file))
def test_lock(self):
u = updater.Updater(
1, None, self.state, None, None, None, None, True)
lock_file = u.state_lock_file
lock = zc.lockfile.LockFile(lock_file)
try:
self.assertTrue(os.path.exists(lock_file))
with mock.patch.object(
updater.Updater, 'action', return_value=None) as mock_object:
self.assertRaises(SystemExit, u.loop)
mock_object.assert_not_called()
self.assertTrue(os.path.exists(lock_file))
finally:
lock.close()
def test_infinite(self):
u = updater.Updater(
1, None, self.state, None, None, None, None, False)
lock_file = u.state_lock_file
os.unlink(self.state)
self.assertFalse(os.path.exists(lock_file))
self.assertFalse(os.path.exists(self.state))
with mock.patch.object(
updater.Updater, 'action', return_value=None) as mock_object:
with mock.patch.object(
updater.time, 'sleep', side_effect=ValueError('timer')) as timer:
self.assertRaises(ValueError, u.loop)
timer.assert_called_with(1)
mock_object.assert_called()
self.assertFalse(os.path.exists(lock_file))
self.assertFalse(os.path.exists(self.state))
...@@ -156,8 +156,14 @@ class Updater(object): ...@@ -156,8 +156,14 @@ class Updater(object):
else: else:
print "...will try again later." print "...will try again later."
else: else:
self.action() try:
lock.close() self.action()
finally:
lock.close()
try:
os.unlink(self.state_lock_file)
except Exception as e:
print 'Problem while unlinking %r' % (self.state_lock_file,)
if self.once: if self.once:
break break
print 'Sleeping for %is' % (self.sleep,) print 'Sleeping for %is' % (self.sleep,)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment