def test_send_signals_signal_throws(self): procs = [ make_mock_process(12345, ['/tmp/a', '/tmp/2'], signal_throw=True), make_mock_process(54321, ['/tmp/1', '/tmp/3']), ] self.assertTrue(pidutil.send_signals(procs, signal.SIGKILL)) self.assertEqual(sum(p.wait.call_count for p in procs), 1)
def test_send_signals_all_throw(self): # type: () -> None procs = [ make_mock_process(12345, ["/tmp/a", "/tmp/2"], signal_throw=True), make_mock_process(54321, ["/tmp/1", "/tmp/3"], wait_throw=True), ] # type: t.List[psutil.Process] self.assertFalse(pidutil.send_signals(procs, signal.SIGKILL))
def test_send_signals_success(self): procs = [ make_mock_process(12345, ["/tmp/a", "/tmp/2"]), make_mock_process(54321, ["/tmp/1", "/tmp/3"]), ] self.assertTrue(pidutil.send_signals(procs, signal.SIGKILL)) self.assertEqual(sum(p.send_signal.call_count for p in procs), len(procs))
def test_send_signals_signal_throws(self): # type: () -> None procs = [ make_mock_process(12345, ["/tmp/a", "/tmp/2"], signal_throw=True), make_mock_process(54321, ["/tmp/1", "/tmp/3"]), ] # type: t.List[psutil.Process] self.assertTrue(pidutil.send_signals(procs, signal.SIGKILL)) self.assertEqual(sum(p.wait.call_count for p in procs), 1)
def test_pids_holding_file_some(self): procs = [ make_mock_process(12345, ["/tmp/a", "/tmp/2"]), make_mock_process(54321, ["/tmp/1", "/tmp/3"]), ] with patch("psutil.process_iter", return_value=procs): procs = pidutil.pids_holding_file("/tmp/a") self.assertEqual(len(procs), 1)
def test_pids_holding_file_some(self): procs = [ make_mock_process(12345, ['/tmp/a', '/tmp/2']), make_mock_process(54321, ['/tmp/1', '/tmp/3']), ] with patch('psutil.process_iter', return_value=procs): procs = pidutil.pids_holding_file('/tmp/a') self.assertEqual(len(procs), 1)
def test_pids_holding_file_no_process(self): procs = [ # throw only on the one that would match. make_mock_process(12345, ["/tmp/a", "/tmp/2"], as_dict_throw=True), make_mock_process(54321, ["/tmp/1", "/tmp/3"]), ] with patch("psutil.process_iter", return_value=procs): procs = pidutil.pids_holding_file("/tmp/a") self.assertEqual(len(procs), 0)
def test_pids_holding_file_no_process(self): procs = [ # throw only on the one that would match. make_mock_process( 12345, ['/tmp/a', '/tmp/2'], as_dict_throw=True, ), make_mock_process(54321, ['/tmp/1', '/tmp/3']), ] with patch('psutil.process_iter', return_value=procs): procs = pidutil.pids_holding_file('/tmp/a') self.assertEqual(len(procs), 0)
class TestStuckYum(unittest.TestCase): def setUp(self): self.stuckyum = StuckYum() # run @patch(pidutil_mod + '.pidfile_info', side_effect=IOError()) def test_run_filenotfound(self, mock_pidinfo): self.assertTrue(self.stuckyum.run()) @patch(pidutil_mod + '.pidfile_info', side_effect=ValueError()) def test_run_valueerror(self, mock_pidinfo): self.assertFalse(self.stuckyum.run()) @patch(pidutil_mod + '.pidfile_info', side_effect=Exception()) def test_run_other_exception(self, mock_pidinfo): self.assertFalse(self.stuckyum.run()) @patch( pidutil_mod + '.pidfile_info', return_value=(12345, int(time.time()) - 3600) ) def test_run_yumpid_not_old_enough(self, mock_pidinfo): self.assertTrue(self.stuckyum.run()) @patch( pidutil_mod + '.pidfile_info', return_value=(12345, int(time.time()) - 7 * 3600) ) @patch(pidutil_mod + '.process', return_value=None) def test_run_yumpid_no_such_process(self, mock_proc, mock_pidinfo): self.assertFalse(self.stuckyum.run()) @patch( pidutil_mod + '.pidfile_info', return_value=(12345, int(time.time()) - 7 * 3600) ) @patch( pidutil_mod + '.process', return_value=make_mock_process(pid=12345, open_files=[], name='yum') ) def test_run_kill_yumpid(self, mock_proc, mock_pidinfo): self.assertTrue(self.stuckyum.run()) @patch( pidutil_mod + '.pidfile_info', return_value=(12345, int(time.time()) - 7 * 3600) ) @patch(pidutil_mod + '.process', return_value=None) def test_run_kill_yumpid_no_such_process(self, mock_proc, mock_pidinfo): self.assertFalse(self.stuckyum.run()) @patch( pidutil_mod + '.pidfile_info', return_value=(12345, int(time.time()) - 7 * 3600) ) @patch(pidutil_mod + '.process', return_value=None) def test_run_kill_yumpid_timeout(self, mock_proc, mock_pidinfo): self.assertFalse(self.stuckyum.run())
def test_check_stuck_kill_yumpid(self): # type: () -> None (self.mock_callable(pidutil, "pidfile_info").for_call( yum.YUM_PID_PATH).to_return_value( (12345, int(time.time()) - 7 * 3600)).and_assert_called_once()) (self.mock_callable( pidutil, "process").for_call(12345).to_return_value( make_mock_process(pid=12345, open_files=[], name="fakeyum")).and_assert_called_once()) self.assertTrue(self.yum.check_stuck())
class TestYum(unittest.TestCase): def setUp(self): self.yum = Yum() # check_stuck @patch(pidutil_mod + ".pidfile_info", side_effect=IOError()) def test_check_stuck_filenotfound(self, mock_pidinfo): self.assertTrue(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", side_effect=ValueError()) def test_check_stuck_valueerror(self, mock_pidinfo): self.assertFalse(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", side_effect=Exception()) def test_check_stuck_other_exception(self, mock_pidinfo): self.assertFalse(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", return_value=(12345, int(time.time()) - 3600)) def test_check_stuck_yumpid_not_old_enough(self, mock_pidinfo): self.assertTrue(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", return_value=(12345, int(time.time()) - 7 * 3600)) @patch(pidutil_mod + ".process", return_value=None) def test_check_stuck_yumpid_no_such_process(self, mock_proc, mock_pidinfo): self.assertFalse(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", return_value=(12345, int(time.time()) - 7 * 3600)) @patch( pidutil_mod + ".process", return_value=make_mock_process(pid=12345, open_files=[], name="yum"), ) def test_check_stuck_kill_yumpid(self, mock_proc, mock_pidinfo): self.assertTrue(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", return_value=(12345, int(time.time()) - 7 * 3600)) @patch(pidutil_mod + ".process", return_value=None) def test_check_stuck_kill_yumpid_no_such_process(self, mock_proc, mock_pidinfo): self.assertFalse(self.yum.check_stuck()) @patch(pidutil_mod + ".pidfile_info", return_value=(12345, int(time.time()) - 7 * 3600)) @patch(pidutil_mod + ".process", return_value=None) def test_check_stuck_kill_yumpid_timeout(self, mock_proc, mock_pidinfo): self.assertFalse(self.yum.check_stuck())
def test_send_signals_all_throw(self): procs = [ make_mock_process(12345, ['/tmp/a', '/tmp/2'], signal_throw=True), make_mock_process(54321, ['/tmp/1', '/tmp/3'], wait_throw=True), ] self.assertFalse(pidutil.send_signals(procs, signal.SIGKILL))
def test_send_signal_timeout(self): proc = make_mock_process(12345, [], wait_throw=True) self.assertFalse(pidutil.send_signal(proc, signal.SIGKILL))
def test_send_signal_success(self): proc = make_mock_process(12345, []) self.assertTrue(pidutil.send_signal(proc, signal.SIGKILL))
def test_send_signals_wait_throws(self): procs = [ make_mock_process(12345, ["/tmp/a", "/tmp/2"], wait_throw=True), make_mock_process(54321, ["/tmp/1", "/tmp/3"]), ] self.assertTrue(pidutil.send_signals(procs, signal.SIGKILL))
def test_send_signal_no_such_process(self): # type: () -> None proc = make_mock_process(12345, [], signal_throw=True) self.assertFalse(pidutil.send_signal(proc, signal.SIGKILL))
def test_kill_spinning_rpm_query_processes_success(self): # type: () -> None young_rpm = make_mock_process(123, cmdline="rpm -q foo-124.x86_64", create_time=9000) young_non_rpm = make_mock_process(456, cmdline="java foobar", create_time=8000) young_non_rpm2 = make_mock_process(789, cmdline="ps aux", create_time=8000) old_bin_rpm = make_mock_process(111, cmdline="/bin/rpm -q bar-456.x86_64", create_time=3000) old_usr_bin_rpm = make_mock_process( 222, cmdline="/usr/bin/rpm -something -q bar-456.x86_64", create_time=2000) old_usr_bin_rpm_wait_throw = make_mock_process( 222, cmdline="/usr/bin/rpm -q bar-456.x86_64", create_time=1000, wait_throw=True, ) old_usr_bin_rpm_cmdline_throw = make_mock_process( 222, cmdline="/usr/bin/rpm -q bar-456.x86_64", create_time=4000, cmdline_throw=True, ) young_bin_rpm = make_mock_process(333, cmdline="/bin/rpm -q bar-788.x86_64", create_time=9000) test_procs = [ young_rpm, young_non_rpm, young_non_rpm2, old_bin_rpm, old_usr_bin_rpm, old_usr_bin_rpm_wait_throw, old_usr_bin_rpm_cmdline_throw, young_bin_rpm, ] # type: t.List[psutil.Process] (self.mock_callable(psutil, "process_iter").to_yield_values( test_procs).and_assert_called_once()) self.mock_callable(time, "time").to_return_value(10000) self.rpmutil.kill_spinning_rpm_query_processes() assert_called_like(young_rpm, create_time=True, send_signal=False, wait=False) assert_called_like(young_non_rpm, create_time=False, send_signal=False, wait=False) assert_called_like(young_non_rpm2, create_time=False, send_signal=False, wait=False) assert_called_like(old_bin_rpm, create_time=True, send_signal=True, wait=True) assert_called_like(old_usr_bin_rpm, create_time=True, send_signal=True, wait=True) assert_called_like(old_usr_bin_rpm_wait_throw, create_time=True, send_signal=True, wait=True) assert_called_like( old_usr_bin_rpm_cmdline_throw, create_time=False, send_signal=False, wait=False, ) assert_called_like(young_bin_rpm, create_time=True, send_signal=False, wait=False)
def test_kill_spinning_rpm_query_processes_success(self, mock_time, mock_iter): mock_time.return_value = 10000 young_rpm = make_mock_process(123, cmdline="rpm -q foo-124.x86_64", create_time=9000) young_non_rpm = make_mock_process(456, cmdline="java foobar", create_time=8000) young_non_rpm2 = make_mock_process(789, cmdline="ps aux", create_time=8000) old_bin_rpm = make_mock_process(111, cmdline="/bin/rpm -q bar-456.x86_64", create_time=3000) old_usr_bin_rpm = make_mock_process( 222, cmdline="/usr/bin/rpm -something -q bar-456.x86_64", create_time=2000) old_usr_bin_rpm_wait_throw = make_mock_process( 222, cmdline="/usr/bin/rpm -q bar-456.x86_64", create_time=1000, wait_throw=True, ) old_usr_bin_rpm_cmdline_throw = make_mock_process( 222, cmdline="/usr/bin/rpm -q bar-456.x86_64", create_time=4000, cmdline_throw=True, ) young_bin_rpm = make_mock_process(333, cmdline="/bin/rpm -q bar-788.x86_64", create_time=9000) mock_iter.return_value = [ young_rpm, young_non_rpm, young_non_rpm2, old_bin_rpm, old_usr_bin_rpm, old_usr_bin_rpm_wait_throw, old_usr_bin_rpm_cmdline_throw, young_bin_rpm, ] self.rpmutil.kill_spinning_rpm_query_processes() assert_called_like(young_rpm, { "create_time": True, "send_signal": False, "wait": False }) assert_called_like(young_non_rpm, { "create_time": False, "send_signal": False, "wait": False }) assert_called_like(young_non_rpm2, { "create_time": False, "send_signal": False, "wait": False }) assert_called_like(old_bin_rpm, { "create_time": True, "send_signal": True, "wait": True }) assert_called_like(old_usr_bin_rpm, { "create_time": True, "send_signal": True, "wait": True }) assert_called_like( old_usr_bin_rpm_wait_throw, { "create_time": True, "send_signal": True, "wait": True }, ) assert_called_like( old_usr_bin_rpm_cmdline_throw, { "create_time": False, "send_signal": False, "wait": False }, ) assert_called_like(young_bin_rpm, { "create_time": True, "send_signal": False, "wait": False })