Debian Codegraph @

testing/mozharness/scripts/does_it_crash.py

raw
1 #!/usr/bin/env python
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 # You can obtain one at http://mozilla.org/MPL/2.0/.
5 """ does_it_crash.py
6
7 Runs a thing to see if it crashes within a set period.
8 """
9 import os
10 import signal
11 import subprocess
12 import sys
13
14 import requests
15
16 sys.path.insert(1, os.path.dirname(sys.path[0]))
17
18 import mozinstall
19 import mozprocess
20 from mozharness.base.script import BaseScript
21
22
23 class DoesItCrash(BaseScript):
24 config_options = [
25 [
26 [
27 "--thing-url",
28 ],
29 {
30 "action": "store",
31 "dest": "thing_url",
32 "type": str,
33 "help": "An URL that points to a package containing the thing to run",
34 },
35 ],
36 [
37 [
38 "--thing-to-run",
39 ],
40 {
41 "action": "store",
42 "dest": "thing_to_run",
43 "type": str,
44 "help": "The thing to run. If --thing-url is a package, this should be "
45 "its location relative to the root of the package.",
46 },
47 ],
48 [
49 [
50 "--thing-arg",
51 ],
52 {
53 "action": "append",
54 "dest": "thing_args",
55 "type": str,
56 "default": [],
57 "help": "Args for the thing. May be passed multiple times",
58 },
59 ],
60 [
61 [
62 "--run-for",
63 ],
64 {
65 "action": "store",
66 "dest": "run_for",
67 "default": 30,
68 "type": int,
69 "help": "How long to run the thing for, in seconds",
70 },
71 ],
72 ]
73
74 def __init__(self):
75 super(DoesItCrash, self).__init__(
76 all_actions=[
77 "download",
78 "run-thing",
79 ],
80 default_actions=[
81 "download",
82 "run-thing",
83 ],
84 config_options=self.config_options,
85 )
86
87 def downloadFile(self, url, file_name):
88 req = requests.get(url, stream=True, timeout=30)
89 file_path = os.path.join(os.getcwd(), file_name)
90
91 with open(file_path, "wb") as f:
92 for chunk in req.iter_content(chunk_size=1024):
93 if not chunk:
94 continue
95 f.write(chunk)
96 f.flush()
97 return file_path
98
99 def download(self):
100 url = self.config["thing_url"]
101 fn = "thing." + url.split(".")[-1]
102 self.downloadFile(url=url, file_name=fn)
103 if mozinstall.is_installer(fn):
104 self.install_dir = mozinstall.install(fn, "thing")
105 else:
106 self.install_dir = ""
107
108 def kill(self, proc):
109 is_win = os.name == "nt"
110 for retry in range(3):
111 if is_win:
112 proc.send_signal(signal.CTRL_BREAK_EVENT)
113 else:
114 os.killpg(proc.pid, signal.SIGKILL)
115 try:
116 proc.wait(5)
117 self.log("process terminated")
118 break
119 except subprocess.TimeoutExpired:
120 self.error("unable to terminate process!")
121
122 def run_thing(self):
123 self.timed_out = False
124
125 def timeout_handler(proc):
126 self.log(f"timeout detected: killing pid {proc.pid}")
127 self.timed_out = True
128 self.kill(proc)
129
130 self.output = []
131
132 def output_line_handler(proc, line):
133 self.output.append(line)
134
135 thing = os.path.abspath(
136 os.path.join(self.install_dir, self.config["thing_to_run"])
137 )
138 # thing_args is a LockedTuple, which mozprocess doesn't like
139 args = list(self.config["thing_args"])
140 timeout = self.config["run_for"]
141
142 self.log(f"Running {thing} with args {args}")
143 cmd = [thing]
144 cmd.extend(args)
145 mozprocess.run_and_wait(
146 cmd,
147 timeout=timeout,
148 timeout_handler=timeout_handler,
149 output_line_handler=output_line_handler,
150 )
151 if not self.timed_out:
152 # It crashed, oh no!
153 self.critical(
154 f"TEST-UNEXPECTED-FAIL: {thing} did not run for {timeout} seconds"
155 )
156 self.critical("Output was:")
157 for l in self.output:
158 self.critical(l)
159 self.fatal("fail")
160 else:
161 self.info(f"PASS: {thing} ran successfully for {timeout} seconds")
162
163
164 # __main__ {{{1
165 if __name__ == "__main__":
166 crashit = DoesItCrash()
167 crashit.run_and_exit()