def test_run_and_get_output_timeout_exceeded(self): """Verifies the returned values of run_and_get_output() when timeout is set and the binary does not exit before it is hit. """ temp_dir = tempfile.mkdtemp() fifo_path = os.path.join(temp_dir, 'fifo') os.mkfifo(fifo_path) class Data: fifo = None # Any write to the fifo will block until it is open for reading by cat, # hence write on a background thread. def _write_to_fifo(): Data.fifo = open(fifo_path, 'w') print >> Data.fifo, 'abc' Data.fifo.flush() write_thread = threading.Thread(target=_write_to_fifo) write_thread.start() # The call to cat should read what is written to the fifo ('abc') and then # stall forever, as we don't close the fifo after writing. shell = LinuxShell('cat') args = [fifo_path] _, output, did_time_out = shell.run_and_get_output(args, timeout=1) write_thread.join() if Data.fifo: Data.fifo.close() # Verify that the process did time out and that the output was correctly # produced before that. self.assertEquals(True, did_time_out) self.assertEquals('abc', output.strip()) shutil.rmtree(temp_dir)
def test_run_and_get_output(self): """Verifies that run_and_get_output() correctly builds and passes the argument list to the binary. """ shell = LinuxShell('echo') shell_args = ['--some-argument 42', 'unicornA', 'unicornB'] return_code, output, did_time_out = shell.run_and_get_output(shell_args) self.assertEquals(0, return_code) self.assertEquals(' '.join(shell_args), output.strip()) self.assertEquals(False, did_time_out)
def test_run_and_get_output_timeout_met(self): """Verifies the returned values of run_and_get_output() when timeout is set but the binary exits before it is hit. """ shell = LinuxShell('echo') shell_args = ['--some-argument 42', 'unicornA', 'unicornB'] return_code, output, did_time_out = shell.run_and_get_output(shell_args, timeout=1) self.assertEquals(0, return_code) self.assertEquals(' '.join(shell_args), output.strip()) self.assertEquals(False, did_time_out)