def test_returns_none_when_file_nonexist(self): """ Should return None when the PID file does not exist. """ set_pidlockfile_scenario(self, 'not-exist') pidfile_path = self.scenario['path'] pid = pidlockfile.read_pid_from_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessIs(None, pid)
def test_reads_pid_from_file(self): """ Should read the PID from the specified file. """ set_pidlockfile_scenario(self, 'exist-other-pid') pidfile_path = self.scenario['path'] expect_pid = self.scenario['pidfile_pid'] pid = pidlockfile.read_pid_from_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessEqual(expect_pid, pid)
def test_writes_pid_to_file(self): """ Should write the current PID to the specified file. """ pidfile_path = self.scenario['path'] self.scenario['pidfile'].close = scaffold.Mock( "PIDLockFile.close", tracker=self.mock_tracker) expect_line = "%(pid)d\n" % self.scenario pidlockfile.write_pid_to_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessEqual(expect_line, self.scenario['pidfile'].getvalue())
def test_removes_specified_filename(self): """ Should attempt to remove specified PID file filename. """ set_pidlockfile_scenario(self, 'exist-current-pid') pidfile_path = self.scenario['path'] expect_mock_output = """\ Called os.remove(%(pidfile_path)r) """ % vars() pidlockfile.remove_existing_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_opens_specified_filename(self): """ Should attempt to open specified pidfile filename. """ set_pidlockfile_scenario(self, 'exist-other-pid') pidfile_path = self.scenario['path'] expect_mock_output = """\ Called builtins.open(%(pidfile_path)r, 'r') """ % vars() dummy = pidlockfile.read_pid_from_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_writes_pid_to_specified_file(self): """ Should request writing current PID to specified file. """ instance = self.test_instance pidfile_path = self.scenario['path'] expect_mock_output = """\ ... Called pidlockfile.write_pid_to_pidfile(%(pidfile_path)r) """ % vars() instance.acquire() scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_ignores_file_not_exist_error(self): """ Should ignore error if file does not exist. """ set_pidlockfile_scenario(self, 'not-exist') pidfile_path = self.scenario['path'] mock_error = OSError(errno.ENOENT, "Not there", pidfile_path) os.remove.mock_raises = mock_error expect_mock_output = """\ Called os.remove(%(pidfile_path)r) """ % vars() pidlockfile.remove_existing_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_sends_terminate_signal_to_process_from_pidfile(self): """ Should send SIGTERM to the daemon process. """ instance = self.test_instance test_pid = self.scenario['pidlockfile_scenario']['pidfile_pid'] expect_signal = signal.SIGTERM expect_mock_output = """\ ... Called os.kill(%(test_pid)r, %(expect_signal)r) """ % vars() instance.do_action() scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_opens_specified_filename(self): """ Should attempt to open specified PID file filename. """ pidfile_path = self.scenario['path'] expect_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY) expect_mode = 0o644 expect_mock_output = """\ Called os.open(%(pidfile_path)r, %(expect_flags)r, %(expect_mode)r) ... """ % vars() pidlockfile.write_pid_to_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_creates_lock_with_specified_parameters(self): """ Should create a TimeoutPIDLockFile with specified params. """ pidfile_path = self.scenario['pidfile_path'] pidfile_timeout = self.scenario['pidfile_timeout'] lockfile_class_name = self.lockfile_class_name expect_mock_output = """\ ... Called %(lockfile_class_name)s( %(pidfile_path)r, %(pidfile_timeout)r) """ % vars() scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_closes_file_after_write(self): """ Should close the specified file after writing. """ pidfile_path = self.scenario['path'] self.scenario['pidfile'].write = scaffold.Mock( "PIDLockFile.write", tracker=self.mock_tracker) self.scenario['pidfile'].close = scaffold.Mock( "PIDLockFile.close", tracker=self.mock_tracker) expect_mock_output = """\ ... Called PIDLockFile.write(...) Called PIDLockFile.close() """ % vars() pidlockfile.write_pid_to_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_raises_error_if_pidfile_not_locked(self): """ Should raise error if PID file is not locked. """ set_runner_scenario(self, 'simple') instance = self.test_instance self.mock_runner_lock.is_locked.mock_returns = False self.mock_runner_lock.i_am_locking.mock_returns = False self.mock_runner_lock.read_pid.mock_returns = ( self.scenario['pidlockfile_scenario']['pidfile_pid']) pidfile_path = self.scenario['pidfile_path'] expect_error = runner.DaemonRunnerStopFailureError expect_message_content = pidfile_path with self.assertRaises(expect_error) as exc: instance.do_action() scaffold.mock_restore() self.assertIn(expect_message_content, str(exc.exception))
def test_breaks_lock_if_pidfile_stale(self): """ Should break lock if PID file is stale. """ instance = self.test_instance pidfile_path = self.scenario['pidfile_path'] test_pid = self.scenario['pidlockfile_scenario']['pidfile_pid'] expect_signal = signal.SIG_DFL error = OSError(errno.ESRCH, "Not running") os.kill.mock_raises = error lockfile_class_name = self.lockfile_class_name expect_mock_output = """\ ... Called %(lockfile_class_name)s.break_lock() """ % vars() instance.do_action() scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_breaks_lock_if_no_such_process(self): """ Should request breaking lock if PID file process is not running. """ set_runner_scenario(self, 'pidfile-locked') instance = self.test_instance self.mock_runner_lock.read_pid.mock_returns = ( self.scenario['pidlockfile_scenario']['pidfile_pid']) pidfile_path = self.scenario['pidfile_path'] test_pid = self.scenario['pidlockfile_scenario']['pidfile_pid'] expect_signal = signal.SIG_DFL error = OSError(errno.ESRCH, "Not running") os.kill.mock_raises = error lockfile_class_name = self.lockfile_class_name expect_mock_output = """\ ... Called os.kill(%(test_pid)r, %(expect_signal)r) Called %(lockfile_class_name)s.break_lock() ... """ % vars() instance.do_action() scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def tearDown(self): """ Tear down test fixtures. """ scaffold.mock_restore()