Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Tests for the shell abstraction.""" | |
|
etiennej
2015/08/18 09:58:43
This is actually about LinuxShell. Maybe rename th
ppi
2015/08/18 10:11:05
Done.
| |
| 6 | |
| 7 import imp | |
| 8 import os.path | |
| 9 import sys | |
| 10 import unittest | |
| 11 import tempfile | |
| 12 import shutil | |
| 13 import threading | |
| 14 | |
| 15 try: | |
| 16 imp.find_module("devtoolslib") | |
| 17 except ImportError: | |
| 18 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| 19 | |
| 20 from devtoolslib.linux_shell import LinuxShell | |
| 21 | |
| 22 | |
| 23 class ShellTest(unittest.TestCase): | |
| 24 """Tests the shell abstraction. | |
| 25 | |
| 26 These do not actually run the shell binary, it is substituted for shell utils | |
| 27 like sleep or cat. Currently these only run against the Linux shell | |
| 28 abstraction. | |
| 29 """ | |
| 30 | |
| 31 def test_run_and_get_output(self): | |
| 32 """Verifies that run_and_get_output() correctly builds and passes the | |
| 33 argument list to the binary. | |
| 34 """ | |
| 35 shell = LinuxShell('echo') | |
| 36 shell_args = ['--some-argument 42', 'unicornA', 'unicornB'] | |
| 37 return_code, output, did_time_out = shell.run_and_get_output(shell_args) | |
| 38 | |
| 39 self.assertEquals(0, return_code) | |
| 40 self.assertEquals(' '.join(shell_args), output.strip()) | |
| 41 self.assertEquals(False, did_time_out) | |
| 42 | |
| 43 def test_run_and_get_output_timeout_met(self): | |
| 44 """Verifies the returned values of run_and_get_output() when timeout is set | |
| 45 but the binary exits before it is hit. | |
| 46 """ | |
| 47 shell = LinuxShell('echo') | |
| 48 shell_args = ['--some-argument 42', 'unicornA', 'unicornB'] | |
| 49 return_code, output, did_time_out = shell.run_and_get_output(shell_args, | |
| 50 timeout=1) | |
| 51 | |
| 52 self.assertEquals(0, return_code) | |
| 53 self.assertEquals(' '.join(shell_args), output.strip()) | |
| 54 self.assertEquals(False, did_time_out) | |
| 55 | |
| 56 def test_run_and_get_output_timeout_exceeded(self): | |
| 57 """Verifies the returned values of run_and_get_output() when timeout is set | |
| 58 and the binary does not exit before it is hit. | |
| 59 """ | |
| 60 temp_dir = tempfile.mkdtemp() | |
| 61 fifo_path = os.path.join(temp_dir, 'fifo') | |
| 62 os.mkfifo(fifo_path) | |
| 63 | |
| 64 class Data: | |
| 65 fifo = None | |
| 66 | |
| 67 # Any write to the fifo will block until it is open for reading by cat, | |
| 68 # hence write on a background thread. | |
| 69 def _write_to_fifo(): | |
| 70 Data.fifo = open(fifo_path, 'w') | |
| 71 print >> Data.fifo, 'abc' | |
| 72 Data.fifo.flush() | |
| 73 write_thread = threading.Thread(target=_write_to_fifo) | |
| 74 write_thread.start() | |
| 75 | |
| 76 # The call to cat should read what is written to the fifo ('abc') and then | |
| 77 # stall forever, as we don't close the fifo after writing. | |
| 78 shell = LinuxShell('cat') | |
| 79 args = [fifo_path] | |
| 80 _, output, did_time_out = shell.run_and_get_output(args, timeout=1) | |
| 81 | |
| 82 write_thread.join() | |
| 83 if Data.fifo: | |
| 84 Data.fifo.close() | |
| 85 | |
| 86 # Verify that the process did time out and that the output was correctly | |
| 87 # produced before that. | |
| 88 self.assertEquals(True, did_time_out) | |
| 89 self.assertEquals('abc', output.strip()) | |
| 90 shutil.rmtree(temp_dir) | |
| 91 | |
| 92 | |
| 93 if __name__ == "__main__": | |
| 94 unittest.main() | |
| OLD | NEW |