blob: a1615cfd20b5bd176631d4aa2a235744c7de7aeb [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001from oeqa.selftest.case import OESelftestTestCase
2from oeqa.utils.commands import runCmd
3from oeqa.utils import CommandError
4from oeqa.core.decorator.oeid import OETestID
5
6import subprocess
7import threading
8import time
9import signal
10
11class MemLogger(object):
12 def __init__(self):
13 self.info_msgs = []
14 self.error_msgs = []
15
16 def info(self, msg):
17 self.info_msgs.append(msg)
18
19 def error(self, msg):
20 self.error_msgs.append(msg)
21
22class RunCmdTests(OESelftestTestCase):
23 """ Basic tests for runCmd() utility function """
24
25 # The delta is intentionally smaller than the timeout, to detect cases where
26 # we incorrectly apply the timeout more than once.
Brad Bishop19323692019-04-05 15:28:33 -040027 TIMEOUT = 5
28 DELTA = 3
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029
30 @OETestID(1916)
31 def test_result_okay(self):
32 result = runCmd("true")
33 self.assertEqual(result.status, 0)
34
35 @OETestID(1915)
36 def test_result_false(self):
37 result = runCmd("false", ignore_status=True)
38 self.assertEqual(result.status, 1)
39
40 @OETestID(1917)
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
46 @OETestID(1910)
47 def test_no_shell(self):
48 self.assertRaises(FileNotFoundError,
49 runCmd, "false; true", shell=False)
50
51 @OETestID(1906)
52 def test_list_not_found(self):
53 self.assertRaises(FileNotFoundError,
54 runCmd, ["false; true"])
55
56 @OETestID(1907)
57 def test_list_okay(self):
58 result = runCmd(["true"])
59 self.assertEqual(result.status, 0)
60
61 @OETestID(1913)
62 def test_result_assertion(self):
63 self.assertRaisesRegexp(AssertionError, "Command 'echo .* false' returned non-zero exit status 1:\nfoobar",
64 runCmd, "echo foobar >&2; false", shell=True)
65
66 @OETestID(1914)
67 def test_result_exception(self):
68 self.assertRaisesRegexp(CommandError, "Command 'echo .* false' returned non-zero exit status 1 with output: foobar",
69 runCmd, "echo foobar >&2; false", shell=True, assert_error=False)
70
71 @OETestID(1911)
72 def test_output(self):
73 result = runCmd("echo stdout; echo stderr >&2", shell=True)
74 self.assertEqual("stdout\nstderr", result.output)
75 self.assertEqual("", result.error)
76
77 @OETestID(1912)
78 def test_output_split(self):
79 result = runCmd("echo stdout; echo stderr >&2", shell=True, stderr=subprocess.PIPE)
80 self.assertEqual("stdout", result.output)
81 self.assertEqual("stderr", result.error)
82
83 @OETestID(1920)
84 def test_timeout(self):
85 numthreads = threading.active_count()
86 start = time.time()
87 # Killing a hanging process only works when not using a shell?!
88 result = runCmd(['sleep', '60'], timeout=self.TIMEOUT, ignore_status=True)
89 self.assertEqual(result.status, -signal.SIGTERM)
90 end = time.time()
91 self.assertLess(end - start, self.TIMEOUT + self.DELTA)
92 self.assertEqual(numthreads, threading.active_count())
93
94 @OETestID(1921)
95 def test_timeout_split(self):
96 numthreads = threading.active_count()
97 start = time.time()
98 # Killing a hanging process only works when not using a shell?!
99 result = runCmd(['sleep', '60'], timeout=self.TIMEOUT, ignore_status=True, stderr=subprocess.PIPE)
100 self.assertEqual(result.status, -signal.SIGTERM)
101 end = time.time()
102 self.assertLess(end - start, self.TIMEOUT + self.DELTA)
103 self.assertEqual(numthreads, threading.active_count())
104
105 @OETestID(1918)
106 def test_stdin(self):
107 numthreads = threading.active_count()
108 result = runCmd("cat", data=b"hello world", timeout=self.TIMEOUT)
109 self.assertEqual("hello world", result.output)
110 self.assertEqual(numthreads, threading.active_count())
111
112 @OETestID(1919)
113 def test_stdin_timeout(self):
114 numthreads = threading.active_count()
115 start = time.time()
116 result = runCmd(['sleep', '60'], data=b"hello world", timeout=self.TIMEOUT, ignore_status=True)
117 self.assertEqual(result.status, -signal.SIGTERM)
118 end = time.time()
119 self.assertLess(end - start, self.TIMEOUT + self.DELTA)
120 self.assertEqual(numthreads, threading.active_count())
121
122 @OETestID(1908)
123 def test_log(self):
124 log = MemLogger()
125 result = runCmd("echo stdout; echo stderr >&2", shell=True, output_log=log)
126 self.assertEqual(["Running: echo stdout; echo stderr >&2", "stdout", "stderr"], log.info_msgs)
127 self.assertEqual([], log.error_msgs)
128
129 @OETestID(1909)
130 def test_log_split(self):
131 log = MemLogger()
132 result = runCmd("echo stdout; echo stderr >&2", shell=True, output_log=log, stderr=subprocess.PIPE)
133 self.assertEqual(["Running: echo stdout; echo stderr >&2", "stdout"], log.info_msgs)
134 self.assertEqual(["stderr"], log.error_msgs)