blob: 9f2d52e1614ca0dc3c1a6d9a005020a3ec1c5c54 [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'
Ken Mixterddcd92d2010-11-01 19:07:08 -070013 _CORE_PATTERN = '/proc/sys/kernel/core_pattern'
Ken Mixter20d9e472010-08-12 10:58:46 -070014 _CRASH_REPORTER_PATH = '/sbin/crash_reporter'
15 _CRASH_SENDER_PATH = '/sbin/crash_sender'
16 _CRASH_SENDER_RATE_DIR = '/var/lib/crash_sender'
17 _CRASH_SENDER_RUN_PATH = '/var/run/crash_sender.pid'
18 _MOCK_CRASH_SENDING = '/tmp/mock-crash-sending'
Ken Mixter38dfe852010-08-18 15:24:00 -070019 _PAUSE_FILE = '/var/lib/crash_sender_paused'
Ken Mixter20d9e472010-08-12 10:58:46 -070020 _SYSTEM_CRASH_DIR = '/var/spool/crash'
21 _USER_CRASH_DIR = '/home/chronos/user/crash'
22
Ken Mixter4f619652010-10-18 12:11:18 -070023 def _set_system_sending(self, is_enabled):
24 """Sets whether or not the system crash_sender is allowed to run.
25
26 crash_sender may still be allowed to run if _set_child_sending is
27 called with true and it is run as a child process."""
Ken Mixter20d9e472010-08-12 10:58:46 -070028 if is_enabled:
29 if os.path.exists(self._PAUSE_FILE):
30 os.remove(self._PAUSE_FILE)
31 else:
32 utils.system('touch ' + self._PAUSE_FILE)
33
34
Ken Mixter4f619652010-10-18 12:11:18 -070035 def _set_child_sending(self, is_enabled):
36 """Overrides crash sending enabling for child processes."""
37 if is_enabled:
38 os.environ['OVERRIDE_PAUSE_SENDING'] = "1"
39 else:
40 del os.environ['OVERRIDE_PAUSE_SENDING']
41
42
Ken Mixter20d9e472010-08-12 10:58:46 -070043 def _reset_rate_limiting(self):
44 utils.system('rm -rf ' + self._CRASH_SENDER_RATE_DIR)
45
46
47 def _clear_spooled_crashes(self):
48 utils.system('rm -rf ' + self._SYSTEM_CRASH_DIR)
49 utils.system('rm -rf ' + self._USER_CRASH_DIR)
50
51
52 def _kill_running_sender(self):
53 if not os.path.exists(self._CRASH_SENDER_RUN_PATH):
54 return
55 running_pid = int(utils.read_file(self._CRASH_SENDER_RUN_PATH))
56 logging.warning('Detected running crash sender (%d), killing' %
57 running_pid)
58 utils.system('kill -9 %d' % running_pid)
59 os.remove(self._CRASH_SENDER_RUN_PATH)
60
61
62 def _set_sending_mock(self, mock_enabled, send_success=True):
63 if mock_enabled:
64 if send_success:
65 data = ''
66 else:
67 data = '1'
68 logging.info('Setting sending mock')
69 utils.open_write_close(self._MOCK_CRASH_SENDING, data)
70 else:
71 utils.system('rm -f ' + self._MOCK_CRASH_SENDING)
72
73
74 def _set_consent(self, has_consent):
75 if has_consent:
76 utils.open_write_close(self._CONSENT_FILE, 'test-consent')
77 logging.info('Created ' + self._CONSENT_FILE)
78 else:
79 utils.system('rm -f "%s"' % (self._CONSENT_FILE))
80
81
82 def _get_pushed_consent_file_path(self):
83 return os.path.join(self.bindir, 'pushed_consent')
84
85
86 def _push_consent(self):
87 if os.path.exists(self._CONSENT_FILE):
88 os.rename(self._CONSENT_FILE, self._get_pushed_consent_file_path())
89
90
91 def _pop_consent(self):
92 self._set_consent(False)
93 if os.path.exists(self._get_pushed_consent_file_path()):
94 os.rename(self._get_pushed_consent_file_path(), self._CONSENT_FILE)
95
96
97 def _get_crash_dir(self, username):
98 if username == 'chronos':
99 return self._USER_CRASH_DIR
100 else:
101 return self._SYSTEM_CRASH_DIR
102
103
104 def _initialize_crash_reporter(self):
105 utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH)
Ken Mixterddcd92d2010-11-01 19:07:08 -0700106 # Completely disable crash_reporter from generating crash dumps
107 # while any tests are running, otherwise a crashy system can make
108 # these tests flaky.
109 self.enable_crash_filtering('none')
Ken Mixter20d9e472010-08-12 10:58:46 -0700110
111
Ken Mixter67ff5622010-09-30 15:32:17 -0700112 def write_crash_dir_entry(self, name, contents):
Ken Mixter20d9e472010-08-12 10:58:46 -0700113 entry = os.path.join(self._SYSTEM_CRASH_DIR, name)
114 if not os.path.exists(self._SYSTEM_CRASH_DIR):
115 os.makedirs(self._SYSTEM_CRASH_DIR)
Ken Mixter67ff5622010-09-30 15:32:17 -0700116 utils.open_write_close(entry, contents)
Ken Mixter20d9e472010-08-12 10:58:46 -0700117 return entry
118
119
Ken Mixter1a894e02010-10-28 15:42:52 -0700120 def write_fake_meta(self, name, exec_name, payload, complete=True):
121 last_line = ''
122 if complete:
123 last_line = 'done=1\n'
Ken Mixter67ff5622010-09-30 15:32:17 -0700124 return self.write_crash_dir_entry(name,
125 'exec_name=%s\n'
126 'ver=my_ver\n'
Ken Mixter1a894e02010-10-28 15:42:52 -0700127 'payload=%s\n'
128 '%s' % (exec_name, payload,
129 last_line))
Ken Mixter67ff5622010-09-30 15:32:17 -0700130
131
Ken Mixter20d9e472010-08-12 10:58:46 -0700132 def _prepare_sender_one_crash(self,
133 send_success,
134 reports_enabled,
135 username,
Ken Mixter38dfe852010-08-18 15:24:00 -0700136 report):
Ken Mixter20d9e472010-08-12 10:58:46 -0700137 self._set_sending_mock(mock_enabled=True, send_success=send_success)
138 self._set_consent(reports_enabled)
Ken Mixter38dfe852010-08-18 15:24:00 -0700139 if report is None:
Ken Mixter1a894e02010-10-28 15:42:52 -0700140 payload = self.write_crash_dir_entry('fake.dmp', '')
141 report = self.write_fake_meta('fake.meta', 'fake', payload)
Ken Mixter38dfe852010-08-18 15:24:00 -0700142 return report
Ken Mixter20d9e472010-08-12 10:58:46 -0700143
144
145 def _parse_sender_output(self, output):
146 """Parse the log output from the crash_sender script.
147
148 This script can run on the logs from either a mocked or true
149 crash send.
150
151 Args:
152 output: output from the script
153
154 Returns:
155 A dictionary with these values:
Ken Mixter38dfe852010-08-18 15:24:00 -0700156 exec_name: name of executable which crashed
Ken Mixter67ff5622010-09-30 15:32:17 -0700157 meta_path: path to the report metadata file
158 output: the output from the script, copied
Ken Mixter38dfe852010-08-18 15:24:00 -0700159 report_kind: kind of report sent (minidump vs kernel)
Ken Mixter20d9e472010-08-12 10:58:46 -0700160 send_attempt: did the script attempt to send a crash.
161 send_success: if it attempted, was the crash send successful.
Ken Mixterd79140e2010-10-26 14:45:30 -0700162 sig: signature of the report, if given.
Ken Mixter20d9e472010-08-12 10:58:46 -0700163 sleep_time: if it attempted, how long did it sleep before
164 sending (if mocked, how long would it have slept)
Ken Mixter20d9e472010-08-12 10:58:46 -0700165 """
166 sleep_match = re.search('Scheduled to send in (\d+)s', output)
167 send_attempt = sleep_match is not None
168 if send_attempt:
169 sleep_time = int(sleep_match.group(1))
170 else:
171 sleep_time = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700172 meta_match = re.search('Metadata: (\S+) \((\S+)\)', output)
173 if meta_match:
174 meta_path = meta_match.group(1)
175 report_kind = meta_match.group(2)
Ken Mixter38dfe852010-08-18 15:24:00 -0700176 else:
Ken Mixter67ff5622010-09-30 15:32:17 -0700177 meta_path = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700178 report_kind = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700179 payload_match = re.search('Payload: (\S+)', output)
180 if payload_match:
181 report_payload = payload_match.group(1)
182 else:
183 report_payload = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700184 exec_name_match = re.search('Exec name: (\S+)', output)
185 if exec_name_match:
186 exec_name = exec_name_match.group(1)
187 else:
188 exec_name = None
Ken Mixter10c48672010-11-01 13:37:08 -0700189 sig_match = re.search('sig: (\S+)', output)
Ken Mixterd79140e2010-10-26 14:45:30 -0700190 if sig_match:
191 sig = sig_match.group(1)
192 else:
193 sig = None
Ken Mixter20d9e472010-08-12 10:58:46 -0700194 send_success = 'Mocking successful send' in output
Ken Mixter38dfe852010-08-18 15:24:00 -0700195 return {'exec_name': exec_name,
196 'report_kind': report_kind,
Ken Mixter67ff5622010-09-30 15:32:17 -0700197 'meta_path': meta_path,
198 'report_payload': report_payload,
Ken Mixter38dfe852010-08-18 15:24:00 -0700199 'send_attempt': send_attempt,
Ken Mixter20d9e472010-08-12 10:58:46 -0700200 'send_success': send_success,
Ken Mixterd79140e2010-10-26 14:45:30 -0700201 'sig': sig,
Ken Mixter20d9e472010-08-12 10:58:46 -0700202 'sleep_time': sleep_time,
203 'output': output}
204
205
Ken Mixter654f32e2010-10-20 11:47:31 -0700206 def wait_for_sender_completion(self):
207 """Wait for crash_sender to complete.
208
209 Wait for no crash_sender's last message to be placed in the
210 system log before continuing and for the process to finish.
211 Otherwise we might get only part of the output."""
212 site_utils.poll_for_condition(
213 lambda: self._log_reader.can_find('crash_sender done.'),
214 timeout=60,
215 exception=error.TestError(
216 'Timeout waiting for crash_sender to emit done: ' +
217 self._log_reader.get_logs()))
218 site_utils.poll_for_condition(
219 lambda: utils.system('pgrep crash_sender',
220 ignore_status=True) != 0,
221 timeout=60,
222 exception=error.TestError(
223 'Timeout waiting for crash_sender to finish: ' +
224 self._log_reader.get_logs()))
225
226
Ken Mixter20d9e472010-08-12 10:58:46 -0700227 def _call_sender_one_crash(self,
228 send_success=True,
229 reports_enabled=True,
230 username='root',
Ken Mixter38dfe852010-08-18 15:24:00 -0700231 report=None):
Ken Mixter20d9e472010-08-12 10:58:46 -0700232 """Call the crash sender script to mock upload one crash.
233
234 Args:
235 send_success: Mock a successful send if true
236 reports_enabled: Has the user consented to sending crash reports.
237 username: user to emulate a crash from
Ken Mixter38dfe852010-08-18 15:24:00 -0700238 report: report to use for crash, if None we create one.
Ken Mixter20d9e472010-08-12 10:58:46 -0700239
240 Returns:
241 Returns a dictionary describing the result with the keys
242 from _parse_sender_output, as well as:
Ken Mixter38dfe852010-08-18 15:24:00 -0700243 report_exists: does the minidump still exist after calling
Ken Mixter20d9e472010-08-12 10:58:46 -0700244 send script
245 rate_count: how many crashes have been uploaded in the past
246 24 hours.
247 """
Ken Mixter38dfe852010-08-18 15:24:00 -0700248 report = self._prepare_sender_one_crash(send_success,
249 reports_enabled,
250 username,
251 report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700252 self._log_reader.set_start_by_current()
253 script_output = utils.system_output(
254 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH,
255 ignore_status=True)
Ken Mixter654f32e2010-10-20 11:47:31 -0700256 self.wait_for_sender_completion()
Ken Mixter20d9e472010-08-12 10:58:46 -0700257 output = self._log_reader.get_logs()
258 logging.debug('Crash sender message output:\n' + output)
259 if script_output != '':
260 raise error.TestFail(
261 'Unexpected crash_sender stdout/stderr: ' + script_output)
262
Ken Mixter38dfe852010-08-18 15:24:00 -0700263 if os.path.exists(report):
264 report_exists = True
265 os.remove(report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700266 else:
Ken Mixter38dfe852010-08-18 15:24:00 -0700267 report_exists = False
Ken Mixter20d9e472010-08-12 10:58:46 -0700268 if os.path.exists(self._CRASH_SENDER_RATE_DIR):
269 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR))
270 else:
271 rate_count = 0
272
273 result = self._parse_sender_output(output)
Ken Mixter38dfe852010-08-18 15:24:00 -0700274 result['report_exists'] = report_exists
Ken Mixter20d9e472010-08-12 10:58:46 -0700275 result['rate_count'] = rate_count
276
277 # Show the result for debugging but remove 'output' key
278 # since it's large and earlier in debug output.
279 debug_result = dict(result)
280 del debug_result['output']
281 logging.debug('Result of send (besides output): %s' % debug_result)
282
283 return result
284
285
Ken Mixterddcd92d2010-11-01 19:07:08 -0700286 def _replace_crash_reporter_filter_in(self, new_parameter):
287 core_pattern = utils.read_file(self._CORE_PATTERN)[:-1]
288 core_pattern = re.sub('--filter_in=\S*\s*', '',
289 core_pattern).rstrip()
290 if new_parameter:
291 core_pattern += ' ' + new_parameter
292 utils.system('echo "%s" > %s' % (core_pattern, self._CORE_PATTERN))
293
294
295 def enable_crash_filtering(self, name):
296 self._replace_crash_reporter_filter_in('--filter_in=' + name)
297
298
299 def disable_crash_filtering(self):
300 self._replace_crash_reporter_filter_in('')
301
302
Ken Mixter20d9e472010-08-12 10:58:46 -0700303 def initialize(self):
304 test.test.initialize(self)
305 self._log_reader = site_log_reader.LogReader()
Ken Mixter38dfe852010-08-18 15:24:00 -0700306 self._leave_crash_sending = True
Ken Mixter67ff5622010-09-30 15:32:17 -0700307 self._automatic_consent_saving = True
Ken Mixterddcd92d2010-11-01 19:07:08 -0700308 self.enable_crash_filtering('none')
Ken Mixter20d9e472010-08-12 10:58:46 -0700309
310
311 def cleanup(self):
312 self._reset_rate_limiting()
313 self._clear_spooled_crashes()
Ken Mixter4f619652010-10-18 12:11:18 -0700314 self._set_system_sending(self._leave_crash_sending)
Ken Mixter20d9e472010-08-12 10:58:46 -0700315 self._set_sending_mock(mock_enabled=False)
Ken Mixter67ff5622010-09-30 15:32:17 -0700316 if self._automatic_consent_saving:
317 self._pop_consent()
Ken Mixterddcd92d2010-11-01 19:07:08 -0700318 self.disable_crash_filtering()
Ken Mixter20d9e472010-08-12 10:58:46 -0700319 test.test.cleanup(self)
320
321
Ken Mixter38dfe852010-08-18 15:24:00 -0700322 def run_crash_tests(self,
323 test_names,
324 initialize_crash_reporter=False,
325 clear_spool_first=True,
326 must_run_all=True):
327 """Run crash tests defined in this class.
328
329 Args:
330 test_names: array of test names
331 initialize_crash_reporter: should set up crash reporter for every run
332 must_run_all: should make sure every test in this class is mentioned
333 in test_names
334 """
Ken Mixter67ff5622010-09-30 15:32:17 -0700335 if self._automatic_consent_saving:
336 self._push_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700337
Ken Mixter38dfe852010-08-18 15:24:00 -0700338 if must_run_all:
339 # Sanity check test_names is complete
340 for attr in dir(self):
341 if attr.find('_test_') == 0:
342 test_name = attr[6:]
343 if not test_name in test_names:
344 raise error.TestError('Test %s is missing' % test_name)
Ken Mixter20d9e472010-08-12 10:58:46 -0700345
346 for test_name in test_names:
347 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20))
Ken Mixter38dfe852010-08-18 15:24:00 -0700348 if initialize_crash_reporter:
349 self._initialize_crash_reporter()
Ken Mixter4f619652010-10-18 12:11:18 -0700350 # Disable crash_sender from running, kill off any running ones, but
351 # set environment so crash_sender may run as a child process.
352 self._set_system_sending(False)
353 self._set_child_sending(True)
Ken Mixter20d9e472010-08-12 10:58:46 -0700354 self._kill_running_sender()
355 self._reset_rate_limiting()
Ken Mixter38dfe852010-08-18 15:24:00 -0700356 if clear_spool_first:
357 self._clear_spooled_crashes()
Ken Mixter20d9e472010-08-12 10:58:46 -0700358 getattr(self, '_test_' + test_name)()