def testLinuxNanny(self): """Tests the linux nanny.""" # Starting nannies is disabled in tests. For this one we need it. self.nanny_stubber.Stop() self.exit_called = False def MockExit(value): self.exit_called = value # Kill the nanny thread. raise RuntimeError("Nannythread exiting.") with utils.Stubber(os, "_exit", MockExit): nanny_controller = client_utils_linux.NannyController() nanny_controller.StartNanny(unresponsive_kill_period=0.5) for _ in range(10): # Unfortunately we really need to sleep because we cant mock out # time.time. time.sleep(0.1) nanny_controller.Heartbeat() self.assertEqual(self.exit_called, False) # Main thread sleeps for long enough for the nanny to fire. time.sleep(1) self.assertEqual(self.exit_called, -1) nanny_controller.StopNanny()
def testLinuxNanny(self): """Tests the linux nanny.""" self.exit_called = False # Mock out the exit call. old_exit = os._exit try: nanny_controller = client_utils_linux.NannyController() nanny_controller.StartNanny(unresponsive_kill_period=0.5) def MockExit(value): self.exit_called = value # Kill the nanny thread. raise RuntimeError("Nannythread exiting.") os._exit = MockExit for _ in range(10): # Unfortunately we really need to sleep because we cant mock out # time.time. time.sleep(0.1) nanny_controller.Heartbeat() self.assertEqual(self.exit_called, False) # Main thread sleeps for long enough for the nanny to fire. time.sleep(1) self.assertEqual(self.exit_called, -1) nanny_controller.StopNanny() finally: os._exit = old_exit
def testLinuxNannyLog(self): """Tests the linux nanny transaction log.""" with tempfile.NamedTemporaryFile() as fd: nanny_controller = client_utils_linux.NannyController() nanny_controller.StartNanny(nanny_logfile=fd.name) grr_message = rdfvalue.GrrMessage(session_id="W:test") nanny_controller.WriteTransactionLog(grr_message) self.assertProtoEqual(grr_message, nanny_controller.GetTransactionLog()) nanny_controller.CleanTransactionLog() self.assert_(nanny_controller.GetTransactionLog() is None) nanny_controller.StopNanny()