Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | from oeqa.selftest.case import OESelftestTestCase |
| 6 | from oeqa.utils.commands import runCmd |
| 7 | from oeqa.utils import CommandError |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 8 | |
| 9 | import subprocess |
| 10 | import threading |
| 11 | import time |
| 12 | import signal |
| 13 | |
| 14 | class MemLogger(object): |
| 15 | def __init__(self): |
| 16 | self.info_msgs = [] |
| 17 | self.error_msgs = [] |
| 18 | |
| 19 | def info(self, msg): |
| 20 | self.info_msgs.append(msg) |
| 21 | |
| 22 | def error(self, msg): |
| 23 | self.error_msgs.append(msg) |
| 24 | |
| 25 | class RunCmdTests(OESelftestTestCase): |
| 26 | """ Basic tests for runCmd() utility function """ |
| 27 | |
| 28 | # The delta is intentionally smaller than the timeout, to detect cases where |
| 29 | # we incorrectly apply the timeout more than once. |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 30 | TIMEOUT = 5 |
| 31 | DELTA = 3 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 32 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 33 | def test_result_okay(self): |
| 34 | result = runCmd("true") |
| 35 | self.assertEqual(result.status, 0) |
| 36 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 37 | def test_result_false(self): |
| 38 | result = runCmd("false", ignore_status=True) |
| 39 | self.assertEqual(result.status, 1) |
| 40 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 41 | def test_shell(self): |
| 42 | # A shell is used for all string commands. |
| 43 | result = runCmd("false; true", ignore_status=True) |
| 44 | self.assertEqual(result.status, 0) |
| 45 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 46 | def test_no_shell(self): |
| 47 | self.assertRaises(FileNotFoundError, |
| 48 | runCmd, "false; true", shell=False) |
| 49 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 50 | def test_list_not_found(self): |
| 51 | self.assertRaises(FileNotFoundError, |
| 52 | runCmd, ["false; true"]) |
| 53 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | def test_list_okay(self): |
| 55 | result = runCmd(["true"]) |
| 56 | self.assertEqual(result.status, 0) |
| 57 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | def test_result_assertion(self): |
| 59 | self.assertRaisesRegexp(AssertionError, "Command 'echo .* false' returned non-zero exit status 1:\nfoobar", |
| 60 | runCmd, "echo foobar >&2; false", shell=True) |
| 61 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 62 | def test_result_exception(self): |
| 63 | self.assertRaisesRegexp(CommandError, "Command 'echo .* false' returned non-zero exit status 1 with output: foobar", |
| 64 | runCmd, "echo foobar >&2; false", shell=True, assert_error=False) |
| 65 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 66 | def test_output(self): |
| 67 | result = runCmd("echo stdout; echo stderr >&2", shell=True) |
| 68 | self.assertEqual("stdout\nstderr", result.output) |
| 69 | self.assertEqual("", result.error) |
| 70 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 71 | def test_output_split(self): |
| 72 | result = runCmd("echo stdout; echo stderr >&2", shell=True, stderr=subprocess.PIPE) |
| 73 | self.assertEqual("stdout", result.output) |
| 74 | self.assertEqual("stderr", result.error) |
| 75 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 76 | def test_timeout(self): |
| 77 | numthreads = threading.active_count() |
| 78 | start = time.time() |
| 79 | # Killing a hanging process only works when not using a shell?! |
| 80 | result = runCmd(['sleep', '60'], timeout=self.TIMEOUT, ignore_status=True) |
| 81 | self.assertEqual(result.status, -signal.SIGTERM) |
| 82 | end = time.time() |
| 83 | self.assertLess(end - start, self.TIMEOUT + self.DELTA) |
| 84 | self.assertEqual(numthreads, threading.active_count()) |
| 85 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 86 | def test_timeout_split(self): |
| 87 | numthreads = threading.active_count() |
| 88 | start = time.time() |
| 89 | # Killing a hanging process only works when not using a shell?! |
| 90 | result = runCmd(['sleep', '60'], timeout=self.TIMEOUT, ignore_status=True, stderr=subprocess.PIPE) |
| 91 | self.assertEqual(result.status, -signal.SIGTERM) |
| 92 | end = time.time() |
| 93 | self.assertLess(end - start, self.TIMEOUT + self.DELTA) |
| 94 | self.assertEqual(numthreads, threading.active_count()) |
| 95 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | def test_stdin(self): |
| 97 | numthreads = threading.active_count() |
| 98 | result = runCmd("cat", data=b"hello world", timeout=self.TIMEOUT) |
| 99 | self.assertEqual("hello world", result.output) |
| 100 | self.assertEqual(numthreads, threading.active_count()) |
| 101 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 102 | def test_stdin_timeout(self): |
| 103 | numthreads = threading.active_count() |
| 104 | start = time.time() |
| 105 | result = runCmd(['sleep', '60'], data=b"hello world", timeout=self.TIMEOUT, ignore_status=True) |
| 106 | self.assertEqual(result.status, -signal.SIGTERM) |
| 107 | end = time.time() |
| 108 | self.assertLess(end - start, self.TIMEOUT + self.DELTA) |
| 109 | self.assertEqual(numthreads, threading.active_count()) |
| 110 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 111 | def test_log(self): |
| 112 | log = MemLogger() |
| 113 | result = runCmd("echo stdout; echo stderr >&2", shell=True, output_log=log) |
| 114 | self.assertEqual(["Running: echo stdout; echo stderr >&2", "stdout", "stderr"], log.info_msgs) |
| 115 | self.assertEqual([], log.error_msgs) |
| 116 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 117 | def test_log_split(self): |
| 118 | log = MemLogger() |
| 119 | result = runCmd("echo stdout; echo stderr >&2", shell=True, output_log=log, stderr=subprocess.PIPE) |
| 120 | self.assertEqual(["Running: echo stdout; echo stderr >&2", "stdout"], log.info_msgs) |
| 121 | self.assertEqual(["stderr"], log.error_msgs) |