blob: 8144fe42f04f8196f641f8918e35c689800bac49 [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 Mixter1a894e02010-10-28 15:42:52 -0700115 def write_fake_meta(self, name, exec_name, payload, complete=True):
116 last_line = ''
117 if complete:
118 last_line = 'done=1\n'
Ken Mixter67ff5622010-09-30 15:32:17 -0700119 return self.write_crash_dir_entry(name,
120 'exec_name=%s\n'
121 'ver=my_ver\n'
Ken Mixter1a894e02010-10-28 15:42:52 -0700122 'payload=%s\n'
123 '%s' % (exec_name, payload,
124 last_line))
Ken Mixter67ff5622010-09-30 15:32:17 -0700125
126
Ken Mixter20d9e472010-08-12 10:58:46 -0700127 def _prepare_sender_one_crash(self,
128 send_success,
129 reports_enabled,
130 username,
Ken Mixter38dfe852010-08-18 15:24:00 -0700131 report):
Ken Mixter20d9e472010-08-12 10:58:46 -0700132 self._set_sending_mock(mock_enabled=True, send_success=send_success)
133 self._set_consent(reports_enabled)
Ken Mixter38dfe852010-08-18 15:24:00 -0700134 if report is None:
Ken Mixter1a894e02010-10-28 15:42:52 -0700135 payload = self.write_crash_dir_entry('fake.dmp', '')
136 report = self.write_fake_meta('fake.meta', 'fake', payload)
Ken Mixter38dfe852010-08-18 15:24:00 -0700137 return report
Ken Mixter20d9e472010-08-12 10:58:46 -0700138
139
140 def _parse_sender_output(self, output):
141 """Parse the log output from the crash_sender script.
142
143 This script can run on the logs from either a mocked or true
144 crash send.
145
146 Args:
147 output: output from the script
148
149 Returns:
150 A dictionary with these values:
Ken Mixter38dfe852010-08-18 15:24:00 -0700151 exec_name: name of executable which crashed
Ken Mixter67ff5622010-09-30 15:32:17 -0700152 meta_path: path to the report metadata file
153 output: the output from the script, copied
Ken Mixter38dfe852010-08-18 15:24:00 -0700154 report_kind: kind of report sent (minidump vs kernel)
Ken Mixter20d9e472010-08-12 10:58:46 -0700155 send_attempt: did the script attempt to send a crash.
156 send_success: if it attempted, was the crash send successful.
Ken Mixterd79140e2010-10-26 14:45:30 -0700157 sig: signature of the report, if given.
Ken Mixter20d9e472010-08-12 10:58:46 -0700158 sleep_time: if it attempted, how long did it sleep before
159 sending (if mocked, how long would it have slept)
Ken Mixter20d9e472010-08-12 10:58:46 -0700160 """
161 sleep_match = re.search('Scheduled to send in (\d+)s', output)
162 send_attempt = sleep_match is not None
163 if send_attempt:
164 sleep_time = int(sleep_match.group(1))
165 else:
166 sleep_time = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700167 meta_match = re.search('Metadata: (\S+) \((\S+)\)', output)
168 if meta_match:
169 meta_path = meta_match.group(1)
170 report_kind = meta_match.group(2)
Ken Mixter38dfe852010-08-18 15:24:00 -0700171 else:
Ken Mixter67ff5622010-09-30 15:32:17 -0700172 meta_path = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700173 report_kind = None
Ken Mixter67ff5622010-09-30 15:32:17 -0700174 payload_match = re.search('Payload: (\S+)', output)
175 if payload_match:
176 report_payload = payload_match.group(1)
177 else:
178 report_payload = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700179 exec_name_match = re.search('Exec name: (\S+)', output)
180 if exec_name_match:
181 exec_name = exec_name_match.group(1)
182 else:
183 exec_name = None
Ken Mixter10c48672010-11-01 13:37:08 -0700184 sig_match = re.search('sig: (\S+)', output)
Ken Mixterd79140e2010-10-26 14:45:30 -0700185 if sig_match:
186 sig = sig_match.group(1)
187 else:
188 sig = None
Ken Mixter20d9e472010-08-12 10:58:46 -0700189 send_success = 'Mocking successful send' in output
Ken Mixter38dfe852010-08-18 15:24:00 -0700190 return {'exec_name': exec_name,
191 'report_kind': report_kind,
Ken Mixter67ff5622010-09-30 15:32:17 -0700192 'meta_path': meta_path,
193 'report_payload': report_payload,
Ken Mixter38dfe852010-08-18 15:24:00 -0700194 'send_attempt': send_attempt,
Ken Mixter20d9e472010-08-12 10:58:46 -0700195 'send_success': send_success,
Ken Mixterd79140e2010-10-26 14:45:30 -0700196 'sig': sig,
Ken Mixter20d9e472010-08-12 10:58:46 -0700197 'sleep_time': sleep_time,
198 'output': output}
199
200
Ken Mixter654f32e2010-10-20 11:47:31 -0700201 def wait_for_sender_completion(self):
202 """Wait for crash_sender to complete.
203
204 Wait for no crash_sender's last message to be placed in the
205 system log before continuing and for the process to finish.
206 Otherwise we might get only part of the output."""
207 site_utils.poll_for_condition(
208 lambda: self._log_reader.can_find('crash_sender done.'),
209 timeout=60,
210 exception=error.TestError(
211 'Timeout waiting for crash_sender to emit done: ' +
212 self._log_reader.get_logs()))
213 site_utils.poll_for_condition(
214 lambda: utils.system('pgrep crash_sender',
215 ignore_status=True) != 0,
216 timeout=60,
217 exception=error.TestError(
218 'Timeout waiting for crash_sender to finish: ' +
219 self._log_reader.get_logs()))
220
221
Ken Mixter20d9e472010-08-12 10:58:46 -0700222 def _call_sender_one_crash(self,
223 send_success=True,
224 reports_enabled=True,
225 username='root',
Ken Mixter38dfe852010-08-18 15:24:00 -0700226 report=None):
Ken Mixter20d9e472010-08-12 10:58:46 -0700227 """Call the crash sender script to mock upload one crash.
228
229 Args:
230 send_success: Mock a successful send if true
231 reports_enabled: Has the user consented to sending crash reports.
232 username: user to emulate a crash from
Ken Mixter38dfe852010-08-18 15:24:00 -0700233 report: report to use for crash, if None we create one.
Ken Mixter20d9e472010-08-12 10:58:46 -0700234
235 Returns:
236 Returns a dictionary describing the result with the keys
237 from _parse_sender_output, as well as:
Ken Mixter38dfe852010-08-18 15:24:00 -0700238 report_exists: does the minidump still exist after calling
Ken Mixter20d9e472010-08-12 10:58:46 -0700239 send script
240 rate_count: how many crashes have been uploaded in the past
241 24 hours.
242 """
Ken Mixter38dfe852010-08-18 15:24:00 -0700243 report = self._prepare_sender_one_crash(send_success,
244 reports_enabled,
245 username,
246 report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700247 self._log_reader.set_start_by_current()
248 script_output = utils.system_output(
249 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH,
250 ignore_status=True)
Ken Mixter654f32e2010-10-20 11:47:31 -0700251 self.wait_for_sender_completion()
Ken Mixter20d9e472010-08-12 10:58:46 -0700252 output = self._log_reader.get_logs()
253 logging.debug('Crash sender message output:\n' + output)
254 if script_output != '':
255 raise error.TestFail(
256 'Unexpected crash_sender stdout/stderr: ' + script_output)
257
Ken Mixter38dfe852010-08-18 15:24:00 -0700258 if os.path.exists(report):
259 report_exists = True
260 os.remove(report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700261 else:
Ken Mixter38dfe852010-08-18 15:24:00 -0700262 report_exists = False
Ken Mixter20d9e472010-08-12 10:58:46 -0700263 if os.path.exists(self._CRASH_SENDER_RATE_DIR):
264 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR))
265 else:
266 rate_count = 0
267
268 result = self._parse_sender_output(output)
Ken Mixter38dfe852010-08-18 15:24:00 -0700269 result['report_exists'] = report_exists
Ken Mixter20d9e472010-08-12 10:58:46 -0700270 result['rate_count'] = rate_count
271
272 # Show the result for debugging but remove 'output' key
273 # since it's large and earlier in debug output.
274 debug_result = dict(result)
275 del debug_result['output']
276 logging.debug('Result of send (besides output): %s' % debug_result)
277
278 return result
279
280
281 def initialize(self):
282 test.test.initialize(self)
283 self._log_reader = site_log_reader.LogReader()
Ken Mixter38dfe852010-08-18 15:24:00 -0700284 self._leave_crash_sending = True
Ken Mixter67ff5622010-09-30 15:32:17 -0700285 self._automatic_consent_saving = True
Ken Mixter20d9e472010-08-12 10:58:46 -0700286
287
288 def cleanup(self):
289 self._reset_rate_limiting()
290 self._clear_spooled_crashes()
Ken Mixter4f619652010-10-18 12:11:18 -0700291 self._set_system_sending(self._leave_crash_sending)
Ken Mixter20d9e472010-08-12 10:58:46 -0700292 self._set_sending_mock(mock_enabled=False)
Ken Mixter67ff5622010-09-30 15:32:17 -0700293 if self._automatic_consent_saving:
294 self._pop_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700295 test.test.cleanup(self)
296
297
Ken Mixter38dfe852010-08-18 15:24:00 -0700298 def run_crash_tests(self,
299 test_names,
300 initialize_crash_reporter=False,
301 clear_spool_first=True,
302 must_run_all=True):
303 """Run crash tests defined in this class.
304
305 Args:
306 test_names: array of test names
307 initialize_crash_reporter: should set up crash reporter for every run
308 must_run_all: should make sure every test in this class is mentioned
309 in test_names
310 """
Ken Mixter67ff5622010-09-30 15:32:17 -0700311 if self._automatic_consent_saving:
312 self._push_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700313
Ken Mixter38dfe852010-08-18 15:24:00 -0700314 if must_run_all:
315 # Sanity check test_names is complete
316 for attr in dir(self):
317 if attr.find('_test_') == 0:
318 test_name = attr[6:]
319 if not test_name in test_names:
320 raise error.TestError('Test %s is missing' % test_name)
Ken Mixter20d9e472010-08-12 10:58:46 -0700321
322 for test_name in test_names:
323 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20))
Ken Mixter38dfe852010-08-18 15:24:00 -0700324 if initialize_crash_reporter:
325 self._initialize_crash_reporter()
Ken Mixter4f619652010-10-18 12:11:18 -0700326 # Disable crash_sender from running, kill off any running ones, but
327 # set environment so crash_sender may run as a child process.
328 self._set_system_sending(False)
329 self._set_child_sending(True)
Ken Mixter20d9e472010-08-12 10:58:46 -0700330 self._kill_running_sender()
331 self._reset_rate_limiting()
Ken Mixter38dfe852010-08-18 15:24:00 -0700332 if clear_spool_first:
333 self._clear_spooled_crashes()
Ken Mixter20d9e472010-08-12 10:58:46 -0700334 getattr(self, '_test_' + test_name)()