blob: f3887c3718d5e54691256aa9a0c7be55b3118473 [file] [log] [blame]
Ken Mixter20d9e472010-08-12 10:58:46 -07001# Copyright (c) 2010 The Chromium OS 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
5import logging, os, re
6from autotest_lib.client.bin import site_log_reader, site_utils, test
7from autotest_lib.client.common_lib import error, utils
8
9
10class CrashTest(test.test):
11
12 _CONSENT_FILE = '/home/chronos/Consent To Send Stats'
13 _CRASH_REPORTER_PATH = '/sbin/crash_reporter'
14 _CRASH_SENDER_PATH = '/sbin/crash_sender'
15 _CRASH_SENDER_RATE_DIR = '/var/lib/crash_sender'
16 _CRASH_SENDER_RUN_PATH = '/var/run/crash_sender.pid'
17 _MOCK_CRASH_SENDING = '/tmp/mock-crash-sending'
Ken Mixter38dfe852010-08-18 15:24:00 -070018 _PAUSE_FILE = '/var/lib/crash_sender_paused'
Ken Mixter20d9e472010-08-12 10:58:46 -070019 _SYSTEM_CRASH_DIR = '/var/spool/crash'
20 _USER_CRASH_DIR = '/home/chronos/user/crash'
21
Ken Mixter4f619652010-10-18 12:11:18 -070022 def _set_system_sending(self, is_enabled):
23 """Sets whether or not the system crash_sender is allowed to run.
24
25 crash_sender may still be allowed to run if _set_child_sending is
26 called with true and it is run as a child process."""
Ken Mixter20d9e472010-08-12 10:58:46 -070027 if is_enabled:
28 if os.path.exists(self._PAUSE_FILE):
29 os.remove(self._PAUSE_FILE)
30 else:
31 utils.system('touch ' + self._PAUSE_FILE)
32
33
Ken Mixter4f619652010-10-18 12:11:18 -070034 def _set_child_sending(self, is_enabled):
35 """Overrides crash sending enabling for child processes."""
36 if is_enabled:
37 os.environ['OVERRIDE_PAUSE_SENDING'] = "1"
38 else:
39 del os.environ['OVERRIDE_PAUSE_SENDING']
40
41
Ken Mixter20d9e472010-08-12 10:58:46 -070042 def _reset_rate_limiting(self):
43 utils.system('rm -rf ' + self._CRASH_SENDER_RATE_DIR)
44
45
46 def _clear_spooled_crashes(self):
47 utils.system('rm -rf ' + self._SYSTEM_CRASH_DIR)
48 utils.system('rm -rf ' + self._USER_CRASH_DIR)
49
50
51 def _kill_running_sender(self):
52 if not os.path.exists(self._CRASH_SENDER_RUN_PATH):
53 return
54 running_pid = int(utils.read_file(self._CRASH_SENDER_RUN_PATH))
55 logging.warning('Detected running crash sender (%d), killing' %
56 running_pid)
57 utils.system('kill -9 %d' % running_pid)
58 os.remove(self._CRASH_SENDER_RUN_PATH)
59
60
61 def _set_sending_mock(self, mock_enabled, send_success=True):
62 if mock_enabled:
63 if send_success:
64 data = ''
65 else:
66 data = '1'
67 logging.info('Setting sending mock')
68 utils.open_write_close(self._MOCK_CRASH_SENDING, data)
69 else:
70 utils.system('rm -f ' + self._MOCK_CRASH_SENDING)
71
72
73 def _set_consent(self, has_consent):
74 if has_consent:
75 utils.open_write_close(self._CONSENT_FILE, 'test-consent')
76 logging.info('Created ' + self._CONSENT_FILE)
77 else:
78 utils.system('rm -f "%s"' % (self._CONSENT_FILE))
79
80
81 def _get_pushed_consent_file_path(self):
82 return os.path.join(self.bindir, 'pushed_consent')
83
84
85 def _push_consent(self):
86 if os.path.exists(self._CONSENT_FILE):
87 os.rename(self._CONSENT_FILE, self._get_pushed_consent_file_path())
88
89
90 def _pop_consent(self):
91 self._set_consent(False)
92 if os.path.exists(self._get_pushed_consent_file_path()):
93 os.rename(self._get_pushed_consent_file_path(), self._CONSENT_FILE)
94
95
96 def _get_crash_dir(self, username):
97 if username == 'chronos':
98 return self._USER_CRASH_DIR
99 else:
100 return self._SYSTEM_CRASH_DIR
101
102
103 def _initialize_crash_reporter(self):
104 utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH)
105
106
Ken Mixter67ff5622010-09-30 15:32:17 -0700107 def write_crash_dir_entry(self, name, contents):
Ken Mixter20d9e472010-08-12 10:58:46 -0700108 entry = os.path.join(self._SYSTEM_CRASH_DIR, name)
109 if not os.path.exists(self._SYSTEM_CRASH_DIR):
110 os.makedirs(self._SYSTEM_CRASH_DIR)
Ken Mixter67ff5622010-09-30 15:32:17 -0700111 utils.open_write_close(entry, contents)
Ken Mixter20d9e472010-08-12 10:58:46 -0700112 return entry
113
114
Ken Mixter67ff5622010-09-30 15:32:17 -0700115 def write_fake_meta(self, name, exec_name):
116 return self.write_crash_dir_entry(name,
117 'exec_name=%s\n'
118 'ver=my_ver\n'
119 'done=1\n' % exec_name)
120
121
Ken Mixter20d9e472010-08-12 10:58:46 -0700122 def _prepare_sender_one_crash(self,
123 send_success,
124 reports_enabled,
125 username,
Ken Mixter38dfe852010-08-18 15:24:00 -0700126 report):
Ken Mixter20d9e472010-08-12 10:58:46 -0700127 self._set_sending_mock(mock_enabled=True, send_success=send_success)
128 self._set_consent(reports_enabled)
Ken Mixter38dfe852010-08-18 15:24:00 -0700129 if report is None:
Ken Mixter67ff5622010-09-30 15:32:17 -0700130 self.write_crash_dir_entry('fake.dmp', '')
131 report = self.write_fake_meta('fake.meta', 'fake')
Ken Mixter38dfe852010-08-18 15:24:00 -0700132 return report
Ken Mixter20d9e472010-08-12 10:58:46 -0700133
134
135 def _parse_sender_output(self, output):
136 """Parse the log output from the crash_sender script.
137
138 This script can run on the logs from either a mocked or true
139 crash send.
140
141 Args:
142 output: output from the script
143
144 Returns:
145 A dictionary with these values:
Ken Mixter38dfe852010-08-18 15:24:00 -0700146 exec_name: name of executable which crashed
Ken Mixter67ff5622010-09-30 15:32:17 -0700147 meta_path: path to the report metadata file
148 output: the output from the script, copied
Ken Mixter38dfe852010-08-18 15:24:00 -0700149 report_kind: kind of report sent (minidump vs kernel)
Ken Mixter20d9e472010-08-12 10:58:46 -0700150 send_attempt: did the script attempt to send a crash.
151 send_success: if it attempted, was the crash send successful.
152 sleep_time: if it attempted, how long did it sleep before
153 sending (if mocked, how long would it have slept)
Ken Mixter20d9e472010-08-12 10:58:46 -0700154 """
155 sleep_match = re.search('Scheduled to send in (\d+)s', output)
156 send_attempt = sleep_match is not None
157 if send_attempt:
158 sleep_time = int(sleep_match.group(1))
159 else:
160 sleep_time = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700161 meta_match = re.search('Metadata: (\S+) \((\S+)\)', output)
162 if meta_match:
163 meta_path = meta_match.group(1)
164 report_kind = meta_match.group(2)
Ken Mixter38dfe852010-08-18 15:24:00 -0700165 else:
Ken Mixter67ff5622010-09-30 15:32:17 -0700166 meta_path = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700167 report_kind = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700168 payload_match = re.search('Payload: (\S+)', output)
169 if payload_match:
170 report_payload = payload_match.group(1)
171 else:
172 report_payload = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700173 exec_name_match = re.search('Exec name: (\S+)', output)
174 if exec_name_match:
175 exec_name = exec_name_match.group(1)
176 else:
177 exec_name = None
Ken Mixter20d9e472010-08-12 10:58:46 -0700178 send_success = 'Mocking successful send' in output
Ken Mixter38dfe852010-08-18 15:24:00 -0700179 return {'exec_name': exec_name,
180 'report_kind': report_kind,
Ken Mixter67ff5622010-09-30 15:32:17 -0700181 'meta_path': meta_path,
182 'report_payload': report_payload,
Ken Mixter38dfe852010-08-18 15:24:00 -0700183 'send_attempt': send_attempt,
Ken Mixter20d9e472010-08-12 10:58:46 -0700184 'send_success': send_success,
185 'sleep_time': sleep_time,
186 'output': output}
187
188
189 def _call_sender_one_crash(self,
190 send_success=True,
191 reports_enabled=True,
192 username='root',
Ken Mixter38dfe852010-08-18 15:24:00 -0700193 report=None):
Ken Mixter20d9e472010-08-12 10:58:46 -0700194 """Call the crash sender script to mock upload one crash.
195
196 Args:
197 send_success: Mock a successful send if true
198 reports_enabled: Has the user consented to sending crash reports.
199 username: user to emulate a crash from
Ken Mixter38dfe852010-08-18 15:24:00 -0700200 report: report to use for crash, if None we create one.
Ken Mixter20d9e472010-08-12 10:58:46 -0700201
202 Returns:
203 Returns a dictionary describing the result with the keys
204 from _parse_sender_output, as well as:
Ken Mixter38dfe852010-08-18 15:24:00 -0700205 report_exists: does the minidump still exist after calling
Ken Mixter20d9e472010-08-12 10:58:46 -0700206 send script
207 rate_count: how many crashes have been uploaded in the past
208 24 hours.
209 """
Ken Mixter38dfe852010-08-18 15:24:00 -0700210 report = self._prepare_sender_one_crash(send_success,
211 reports_enabled,
212 username,
213 report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700214 self._log_reader.set_start_by_current()
215 script_output = utils.system_output(
216 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH,
217 ignore_status=True)
218 # Wait for up to 2s for no crash_sender to be running,
219 # otherwise me might get only part of the output.
220 site_utils.poll_for_condition(
221 lambda: utils.system('pgrep crash_sender',
222 ignore_status=True) != 0,
223 timeout=2,
224 exception=error.TestError(
225 'Timeout waiting for crash_sender to finish: ' +
226 self._log_reader.get_logs()))
227
228 output = self._log_reader.get_logs()
229 logging.debug('Crash sender message output:\n' + output)
230 if script_output != '':
231 raise error.TestFail(
232 'Unexpected crash_sender stdout/stderr: ' + script_output)
233
Ken Mixter38dfe852010-08-18 15:24:00 -0700234 if os.path.exists(report):
235 report_exists = True
236 os.remove(report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700237 else:
Ken Mixter38dfe852010-08-18 15:24:00 -0700238 report_exists = False
Ken Mixter20d9e472010-08-12 10:58:46 -0700239 if os.path.exists(self._CRASH_SENDER_RATE_DIR):
240 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR))
241 else:
242 rate_count = 0
243
244 result = self._parse_sender_output(output)
Ken Mixter38dfe852010-08-18 15:24:00 -0700245 result['report_exists'] = report_exists
Ken Mixter20d9e472010-08-12 10:58:46 -0700246 result['rate_count'] = rate_count
247
248 # Show the result for debugging but remove 'output' key
249 # since it's large and earlier in debug output.
250 debug_result = dict(result)
251 del debug_result['output']
252 logging.debug('Result of send (besides output): %s' % debug_result)
253
254 return result
255
256
257 def initialize(self):
258 test.test.initialize(self)
259 self._log_reader = site_log_reader.LogReader()
Ken Mixter38dfe852010-08-18 15:24:00 -0700260 self._leave_crash_sending = True
Ken Mixter67ff5622010-09-30 15:32:17 -0700261 self._automatic_consent_saving = True
Ken Mixter20d9e472010-08-12 10:58:46 -0700262
263
264 def cleanup(self):
265 self._reset_rate_limiting()
266 self._clear_spooled_crashes()
Ken Mixter4f619652010-10-18 12:11:18 -0700267 self._set_system_sending(self._leave_crash_sending)
Ken Mixter20d9e472010-08-12 10:58:46 -0700268 self._set_sending_mock(mock_enabled=False)
Ken Mixter67ff5622010-09-30 15:32:17 -0700269 if self._automatic_consent_saving:
270 self._pop_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700271 test.test.cleanup(self)
272
273
Ken Mixter38dfe852010-08-18 15:24:00 -0700274 def run_crash_tests(self,
275 test_names,
276 initialize_crash_reporter=False,
277 clear_spool_first=True,
278 must_run_all=True):
279 """Run crash tests defined in this class.
280
281 Args:
282 test_names: array of test names
283 initialize_crash_reporter: should set up crash reporter for every run
284 must_run_all: should make sure every test in this class is mentioned
285 in test_names
286 """
Ken Mixter67ff5622010-09-30 15:32:17 -0700287 if self._automatic_consent_saving:
288 self._push_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700289
Ken Mixter38dfe852010-08-18 15:24:00 -0700290 if must_run_all:
291 # Sanity check test_names is complete
292 for attr in dir(self):
293 if attr.find('_test_') == 0:
294 test_name = attr[6:]
295 if not test_name in test_names:
296 raise error.TestError('Test %s is missing' % test_name)
Ken Mixter20d9e472010-08-12 10:58:46 -0700297
298 for test_name in test_names:
299 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20))
Ken Mixter38dfe852010-08-18 15:24:00 -0700300 if initialize_crash_reporter:
301 self._initialize_crash_reporter()
Ken Mixter4f619652010-10-18 12:11:18 -0700302 # Disable crash_sender from running, kill off any running ones, but
303 # set environment so crash_sender may run as a child process.
304 self._set_system_sending(False)
305 self._set_child_sending(True)
Ken Mixter20d9e472010-08-12 10:58:46 -0700306 self._kill_running_sender()
307 self._reset_rate_limiting()
Ken Mixter38dfe852010-08-18 15:24:00 -0700308 if clear_spool_first:
309 self._clear_spooled_crashes()
Ken Mixter20d9e472010-08-12 10:58:46 -0700310 getattr(self, '_test_' + test_name)()