def test_reads_pid_from_file(self): """ Should read the PID from the specified file. """ 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_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_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.assertEqual(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.assertEqual(expect_line, self.scenario["pidfile"].getvalue())
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_removes_specified_filename(self): """ Should attempt to remove specified PID file filename. """ 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. """ pidfile_path = self.scenario['path'] expect_mock_output = """\ Called __builtin__.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_file(self): """ Should write the current PID to the specified file. """ pidfile_path = self.scenario['path'] self.scenario['pidfile'].close = scaffold.Mock( u"PIDLockFile.close", tracker=self.mock_tracker) expect_line = u"%(pid)d\n" % self.scenario pidlockfile.write_pid_to_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessEqual(expect_line, self.scenario['pidfile'].getvalue())
def tearDown(self): """ Tear down test fixtures. """ scaffold.mock_restore() """ Should return True if PID file exists. """ instance = self.test_instance expect_result = True self.scenario = self.scenarios['exist-currentpid'] result = instance.is_locked() self.failUnlessEqual(expect_result, result)
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 = u"""\ 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_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 = u"""\ Called os.remove(%(pidfile_path)r) """ % vars() pidlockfile.remove_existing_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_removes_existing_pidfile(self): """ Should request removal of specified PID file. """ instance = self.test_instance pidfile_path = self.scenario['path'] expect_mock_output = """\ ... Called pidlockfile.remove_existing_pidfile(%(pidfile_path)r) """ % vars() instance.break_lock() scaffold.mock_restore() self.failUnlessMockCheckerMatch(expect_mock_output)
def test_ignores_file_not_exist_error(self): """ Should ignore error if file does 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_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 = u"""\ ... Called pidlockfile.write_pid_to_pidfile(%(pidfile_path)r) """ % vars() instance.acquire() 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, u"Not there", pidfile_path) os.remove.mock_raises = mock_error expect_mock_output = u"""\ 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 PID file filename. """ pidfile_path = self.scenario['path'] expect_flags = (os.O_CREAT | os.O_EXCL | os.O_WRONLY) expect_mode = 0644 expect_mock_output = u"""\ 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_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 = 0644 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_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_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_writes_pid_to_file(self): """ Should write the current PID to the specified file. """ pidfile_path = self.scenario['path'] pidfile = self.scenario['pidfile'] pidfile_pid = self.scenario['pidfile_pid'] pidfile.close = scaffold.Mock( "mock_pidfile.close", tracker=self.mock_tracker) expect_line = "%(pidfile_pid)d\n" % vars() pidlockfile.write_pid_to_pidfile(pidfile_path) scaffold.mock_restore() self.failUnlessEqual(expect_line, pidfile.getvalue())
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_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_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( u"PIDLockFile.write", tracker=self.mock_tracker) self.scenario['pidfile'].close = scaffold.Mock( u"PIDLockFile.close", tracker=self.mock_tracker) expect_mock_output = u"""\ ... Called PIDLockFile.write(...) Called PIDLockFile.close() """ % vars() pidlockfile.write_pid_to_pidfile(pidfile_path) 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_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_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_raises_error_if_cannot_send_signal_to_process(self): """ Should raise error if cannot send signal to daemon process. """ instance = self.test_instance test_pid = self.scenario['pidlockfile_scenario']['pidfile_pid'] pidfile_path = self.scenario['pidfile_path'] error = OSError(errno.EPERM, "Nice try") os.kill.mock_raises = error expect_error = runner.DaemonRunnerStopFailureError expect_message_content = str(test_pid) exc = None try: instance.do_action() except expect_error as exc_: exc = exc_ else: raise self.failureException("Failed to raise " + expect_error.__name__) scaffold.mock_restore() self.failUnlessIn(str(exc), expect_message_content)
def test_raises_error_if_cannot_send_signal_to_process(self): """ Should raise error if cannot send signal to daemon process. """ instance = self.test_instance test_pid = self.scenario['pidlockfile_scenario']['pidfile_pid'] pidfile_path = self.scenario['pidfile_path'] error = OSError(errno.EPERM, "Nice try") os.kill.mock_raises = error expect_error = runner.DaemonRunnerStopFailureError expect_message_content = str(test_pid) exc = None try: instance.do_action() except expect_error as exc_: exc = exc_ else: raise self.failureException( "Failed to raise " + expect_error.__name__) scaffold.mock_restore() self.failUnlessIn(str(exc), expect_message_content)
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 exc_text = '' try: instance.do_action() except expect_error as exc: exc_text = str(exc) else: raise self.failureException( "Failed to raise " + expect_error.__name__) scaffold.mock_restore() self.failUnlessIn(exc_text, expect_message_content)
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 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 exc_text = '' try: instance.do_action() except expect_error as exc: exc_text = str(exc) else: raise self.failureException("Failed to raise " + expect_error.__name__) scaffold.mock_restore() self.failUnlessIn(exc_text, expect_message_content)
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()
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 try: instance.do_action() except expect_error, exc: pass else: raise self.failureException("Failed to raise " + expect_error.__name__) scaffold.mock_restore() self.failUnlessIn(exc.message, expect_message_content) 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()
def tearDown(self): """ Tear down test fixtures """ scaffold.mock_restore()
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 try: instance.do_action() except expect_error, exc: pass else: raise self.failureException( u"Failed to raise " + expect_error.__name__) scaffold.mock_restore() self.failUnlessIn(str(exc), expect_message_content) 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, u"Not running") os.kill.mock_raises = error lockfile_class_name = self.lockfile_class_name expect_mock_output = u"""\ ... Called %(lockfile_class_name)s.break_lock() """ % vars()