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 linux shell abstraction.""" |
| 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 LinuxShellTest(unittest.TestCase): |
| 24 """Tests the Linux shell abstraction. |
| 25 |
| 26 These do not actually run the shell binary, it is substituted for shell utils |
| 27 like sleep or cat. |
| 28 """ |
| 29 |
| 30 def test_run_and_get_output(self): |
| 31 """Verifies that run_and_get_output() correctly builds and passes the |
| 32 argument list to the binary. |
| 33 """ |
| 34 shell = LinuxShell('echo') |
| 35 shell_args = ['--some-argument 42', 'unicornA', 'unicornB'] |
| 36 return_code, output, did_time_out = shell.run_and_get_output(shell_args) |
| 37 |
| 38 self.assertEquals(0, return_code) |
| 39 self.assertEquals(' '.join(shell_args), output.strip()) |
| 40 self.assertEquals(False, did_time_out) |
| 41 |
| 42 def test_run_and_get_output_timeout_met(self): |
| 43 """Verifies the returned values of run_and_get_output() when timeout is set |
| 44 but the binary exits before it is hit. |
| 45 """ |
| 46 shell = LinuxShell('echo') |
| 47 shell_args = ['--some-argument 42', 'unicornA', 'unicornB'] |
| 48 return_code, output, did_time_out = shell.run_and_get_output(shell_args, |
| 49 timeout=1) |
| 50 |
| 51 self.assertEquals(0, return_code) |
| 52 self.assertEquals(' '.join(shell_args), output.strip()) |
| 53 self.assertEquals(False, did_time_out) |
| 54 |
| 55 def test_run_and_get_output_timeout_exceeded(self): |
| 56 """Verifies the returned values of run_and_get_output() when timeout is set |
| 57 and the binary does not exit before it is hit. |
| 58 """ |
| 59 temp_dir = tempfile.mkdtemp() |
| 60 fifo_path = os.path.join(temp_dir, 'fifo') |
| 61 os.mkfifo(fifo_path) |
| 62 |
| 63 class Data: |
| 64 fifo = None |
| 65 |
| 66 # Any write to the fifo will block until it is open for reading by cat, |
| 67 # hence write on a background thread. |
| 68 def _write_to_fifo(): |
| 69 Data.fifo = open(fifo_path, 'w') |
| 70 print >> Data.fifo, 'abc' |
| 71 Data.fifo.flush() |
| 72 write_thread = threading.Thread(target=_write_to_fifo) |
| 73 write_thread.start() |
| 74 |
| 75 # The call to cat should read what is written to the fifo ('abc') and then |
| 76 # stall forever, as we don't close the fifo after writing. |
| 77 shell = LinuxShell('cat') |
| 78 args = [fifo_path] |
| 79 _, output, did_time_out = shell.run_and_get_output(args, timeout=1) |
| 80 |
| 81 write_thread.join() |
| 82 if Data.fifo: |
| 83 Data.fifo.close() |
| 84 |
| 85 # Verify that the process did time out and that the output was correctly |
| 86 # produced before that. |
| 87 self.assertEquals(True, did_time_out) |
| 88 self.assertEquals('abc', output.strip()) |
| 89 shutil.rmtree(temp_dir) |
| 90 |
| 91 |
| 92 if __name__ == "__main__": |
| 93 unittest.main() |
OLD | NEW |