|
@@ -341,6 +341,18 @@ class TestCacheCommands(unittest.TestCase):
|
|
|
|
|
|
|
|
|
class TestBoB(unittest.TestCase):
|
|
|
+ def setUp(self):
|
|
|
+ # Save original values that may be tweaked in some tests
|
|
|
+ self.__orig_setgid = bind10_src.posix.setgid
|
|
|
+ self.__orig_setuid = bind10_src.posix.setuid
|
|
|
+ self.__orig_logger_class = isc.log.Logger
|
|
|
+
|
|
|
+ def tearDown(self):
|
|
|
+ # Restore original values saved in setUp()
|
|
|
+ bind10_src.posix.setgid = self.__orig_setgid
|
|
|
+ bind10_src.posix.setuid = self.__orig_setuid
|
|
|
+ isc.log.Logger = self.__orig_logger_class
|
|
|
+
|
|
|
def test_init(self):
|
|
|
bob = BoB()
|
|
|
self.assertEqual(bob.verbose, False)
|
|
@@ -349,10 +361,56 @@ class TestBoB(unittest.TestCase):
|
|
|
self.assertEqual(bob.ccs, None)
|
|
|
self.assertEqual(bob.components, {})
|
|
|
self.assertEqual(bob.runnable, False)
|
|
|
- self.assertEqual(bob.uid, None)
|
|
|
self.assertEqual(bob.username, None)
|
|
|
self.assertIsNone(bob._socket_cache)
|
|
|
|
|
|
+ def __setgid(self, gid):
|
|
|
+ self.__gid_set = gid
|
|
|
+
|
|
|
+ def __setuid(self, uid):
|
|
|
+ self.__uid_set = uid
|
|
|
+
|
|
|
+ def test_change_user(self):
|
|
|
+ bind10_src.posix.setgid = self.__setgid
|
|
|
+ bind10_src.posix.setuid = self.__setuid
|
|
|
+
|
|
|
+ self.__gid_set = None
|
|
|
+ self.__uid_set = None
|
|
|
+ bob = BoB()
|
|
|
+ bob.change_user()
|
|
|
+ # No gid/uid set in boss, nothing called.
|
|
|
+ self.assertIsNone(self.__gid_set)
|
|
|
+ self.assertIsNone(self.__uid_set)
|
|
|
+
|
|
|
+ BoB(setuid=42, setgid=4200).change_user()
|
|
|
+ # This time, it get's called
|
|
|
+ self.assertEqual(4200, self.__gid_set)
|
|
|
+ self.assertEqual(42, self.__uid_set)
|
|
|
+
|
|
|
+ def raising_set_xid(gid_or_uid):
|
|
|
+ ex = OSError()
|
|
|
+ ex.errno, ex.strerror = errno.EPERM, 'Operation not permitted'
|
|
|
+ raise ex
|
|
|
+
|
|
|
+ # Let setgid raise an exception
|
|
|
+ bind10_src.posix.setgid = raising_set_xid
|
|
|
+ bind10_src.posix.setuid = self.__setuid
|
|
|
+ self.assertRaises(bind10_src.ChangeUserError,
|
|
|
+ BoB(setuid=42, setgid=4200).change_user)
|
|
|
+
|
|
|
+ # Let setuid raise an exception
|
|
|
+ bind10_src.posix.setgid = self.__setgid
|
|
|
+ bind10_src.posix.setuid = raising_set_xid
|
|
|
+ self.assertRaises(bind10_src.ChangeUserError,
|
|
|
+ BoB(setuid=42, setgid=4200).change_user)
|
|
|
+
|
|
|
+ # Let initial log output after setuid raise an exception
|
|
|
+ bind10_src.posix.setgid = self.__setgid
|
|
|
+ bind10_src.posix.setuid = self.__setuid
|
|
|
+ isc.log.Logger = raising_set_xid
|
|
|
+ self.assertRaises(bind10_src.ChangeUserError,
|
|
|
+ BoB(setuid=42, setgid=4200).change_user)
|
|
|
+
|
|
|
def test_set_creator(self):
|
|
|
"""
|
|
|
Test the call to set_creator. First time, the cache is created
|
|
@@ -423,7 +481,6 @@ class TestBoB(unittest.TestCase):
|
|
|
self.assertEqual(bob.ccs, None)
|
|
|
self.assertEqual(bob.components, {})
|
|
|
self.assertEqual(bob.runnable, False)
|
|
|
- self.assertEqual(bob.uid, None)
|
|
|
self.assertEqual(bob.username, None)
|
|
|
|
|
|
def test_command_handler(self):
|
|
@@ -2021,8 +2078,10 @@ class TestBossComponents(unittest.TestCase):
|
|
|
|
|
|
def start_all_components(self):
|
|
|
self.started = True
|
|
|
- if self.throw:
|
|
|
+ if self.throw is True:
|
|
|
raise Exception('Assume starting components has failed.')
|
|
|
+ elif self.throw:
|
|
|
+ raise self.throw
|
|
|
|
|
|
def kill_started_components(self):
|
|
|
self.killed = True
|
|
@@ -2067,6 +2126,12 @@ class TestBossComponents(unittest.TestCase):
|
|
|
r = bob.startup()
|
|
|
self.assertEqual({'BIND10_MSGQ_SOCKET_FILE': 'foo'}, bob.c_channel_env)
|
|
|
|
|
|
+ # Check failure of changing user results in a different message
|
|
|
+ bob = MockBobStartup(bind10_src.ChangeUserError('failed to chusr'))
|
|
|
+ r = bob.startup()
|
|
|
+ self.assertIn('failed to chusr', r)
|
|
|
+ self.assertTrue(bob.killed)
|
|
|
+
|
|
|
# Check the case when socket file already exists
|
|
|
isc.cc.Session = DummySessionSocketExists
|
|
|
bob = MockBobStartup(False)
|
|
@@ -2277,11 +2342,15 @@ class TestFunctions(unittest.TestCase):
|
|
|
self.assertFalse(os.path.exists(self.lockfile_testpath))
|
|
|
os.mkdir(self.lockfile_testpath)
|
|
|
self.assertTrue(os.path.isdir(self.lockfile_testpath))
|
|
|
+ self.__isfile_orig = bind10_src.os.path.isfile
|
|
|
+ self.__unlink_orig = bind10_src.os.unlink
|
|
|
|
|
|
def tearDown(self):
|
|
|
os.rmdir(self.lockfile_testpath)
|
|
|
self.assertFalse(os.path.isdir(self.lockfile_testpath))
|
|
|
os.environ["B10_LOCKFILE_DIR_FROM_BUILD"] = "@abs_top_builddir@"
|
|
|
+ bind10_src.os.path.isfile = self.__isfile_orig
|
|
|
+ bind10_src.os.unlink = self.__unlink_orig
|
|
|
|
|
|
def test_remove_lock_files(self):
|
|
|
os.environ["B10_LOCKFILE_DIR_FROM_BUILD"] = self.lockfile_testpath
|
|
@@ -2305,6 +2374,28 @@ class TestFunctions(unittest.TestCase):
|
|
|
# second call should not assert anyway
|
|
|
bind10_src.remove_lock_files()
|
|
|
|
|
|
+ def test_remove_lock_files_fail(self):
|
|
|
+ # Permission error on unlink is ignored; other exceptions are really
|
|
|
+ # unexpected and propagated.
|
|
|
+ def __raising_unlink(unused, ex):
|
|
|
+ raise ex
|
|
|
+
|
|
|
+ bind10_src.os.path.isfile = lambda _: True
|
|
|
+ os_error = OSError()
|
|
|
+ bind10_src.os.unlink = lambda f: __raising_unlink(f, os_error)
|
|
|
+
|
|
|
+ os_error.errno = errno.EPERM
|
|
|
+ bind10_src.remove_lock_files() # no disruption
|
|
|
+
|
|
|
+ os_error.errno = errno.EACCES
|
|
|
+ bind10_src.remove_lock_files() # no disruption
|
|
|
+
|
|
|
+ os_error.errno = errno.ENOENT
|
|
|
+ self.assertRaises(OSError, bind10_src.remove_lock_files)
|
|
|
+
|
|
|
+ bind10_src.os.unlink = lambda f: __raising_unlink(f, Exception('bad'))
|
|
|
+ self.assertRaises(Exception, bind10_src.remove_lock_files)
|
|
|
+
|
|
|
def test_get_signame(self):
|
|
|
# just test with some samples
|
|
|
signame = bind10_src.get_signame(signal.SIGTERM)
|