def test_stale_locks_recycled_pid(self, mock_kill): """Publisher starts with stale lock pointing to recycled process.""" mock_kill.side_effect = [ OSError(errno.EPERM, "Operation not permitted") ] sock_path = os.path.join(self.config.sockets_path, u"test.sock") lock_path = u"{}.lock".format(sock_path) # fake a PID recycled by a known process which isn't landscape (init) os.symlink("1", lock_path) component = TestComponent() # Test the actual Unix reactor implementation. Fakes won't do. reactor = LandscapeReactor() publisher = ComponentPublisher(component, reactor, self.config) # Shouldn't raise the exception. publisher.start() # ensure stale lock was replaced self.assertNotEqual("1", os.readlink(lock_path)) mock_kill.assert_not_called() self.assertFalse(publisher._port.lockFile.clean) publisher.stop() reactor._cleanup()
def test_with_valid_lock(self, mock_kill): """Publisher raises lock error if a valid lock is held.""" sock_path = os.path.join(self.config.sockets_path, u"test.sock") lock_path = u"{}.lock".format(sock_path) # fake a landscape process app = self.makeFile(textwrap.dedent("""\ #!/usr/bin/python3 import time time.sleep(10) """), basename="landscape-manager") os.chmod(app, 0o755) call = subprocess.Popen([app]) self.addCleanup(call.terminate) os.symlink(str(call.pid), lock_path) component = TestComponent() # Test the actual Unix reactor implementation. Fakes won't do. reactor = LandscapeReactor() publisher = ComponentPublisher(component, reactor, self.config) with self.assertRaises(CannotListenError): publisher.start() # ensure lock was not replaced self.assertEqual(str(call.pid), os.readlink(lock_path)) mock_kill.assert_called_with(call.pid, 0) reactor._cleanup()
def test_stale_locks_with_dead_pid(self, mock_kill): """Publisher starts with stale lock.""" mock_kill.side_effect = [OSError(errno.ESRCH, "No such process")] sock_path = os.path.join(self.config.sockets_path, u"test.sock") lock_path = u"{}.lock".format(sock_path) # fake a PID which does not exist os.symlink("-1", lock_path) component = TestComponent() # Test the actual Unix reactor implementation. Fakes won't do. reactor = LandscapeReactor() publisher = ComponentPublisher(component, reactor, self.config) # Shouldn't raise the exception. publisher.start() # ensure stale lock was replaced self.assertNotEqual("-1", os.readlink(lock_path)) mock_kill.assert_called_with(-1, 0) publisher.stop() reactor._cleanup()