blob: df6ae3662831330d750d0ca961617a303ec7a440 [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
Ken Mixter654f32e2010-10-20 11:47:31 -0700189 def wait_for_sender_completion(self):
190 """Wait for crash_sender to complete.
191
192 Wait for no crash_sender's last message to be placed in the
193 system log before continuing and for the process to finish.
194 Otherwise we might get only part of the output."""
195 site_utils.poll_for_condition(
196 lambda: self._log_reader.can_find('crash_sender done.'),
197 timeout=60,
198 exception=error.TestError(
199 'Timeout waiting for crash_sender to emit done: ' +
200 self._log_reader.get_logs()))
201 site_utils.poll_for_condition(
202 lambda: utils.system('pgrep crash_sender',
203 ignore_status=True) != 0,
204 timeout=60,
205 exception=error.TestError(
206 'Timeout waiting for crash_sender to finish: ' +
207 self._log_reader.get_logs()))
208
209
Ken Mixter20d9e472010-08-12 10:58:46 -0700210 def _call_sender_one_crash(self,
211 send_success=True,
212 reports_enabled=True,
213 username='root',
Ken Mixter38dfe852010-08-18 15:24:00 -0700214 report=None):
Ken Mixter20d9e472010-08-12 10:58:46 -0700215 """Call the crash sender script to mock upload one crash.
216
217 Args:
218 send_success: Mock a successful send if true
219 reports_enabled: Has the user consented to sending crash reports.
220 username: user to emulate a crash from
Ken Mixter38dfe852010-08-18 15:24:00 -0700221 report: report to use for crash, if None we create one.
Ken Mixter20d9e472010-08-12 10:58:46 -0700222
223 Returns:
224 Returns a dictionary describing the result with the keys
225 from _parse_sender_output, as well as:
Ken Mixter38dfe852010-08-18 15:24:00 -0700226 report_exists: does the minidump still exist after calling
Ken Mixter20d9e472010-08-12 10:58:46 -0700227 send script
228 rate_count: how many crashes have been uploaded in the past
229 24 hours.
230 """
Ken Mixter38dfe852010-08-18 15:24:00 -0700231 report = self._prepare_sender_one_crash(send_success,
232 reports_enabled,
233 username,
234 report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700235 self._log_reader.set_start_by_current()
236 script_output = utils.system_output(
237 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH,
238 ignore_status=True)
Ken Mixter654f32e2010-10-20 11:47:31 -0700239 self.wait_for_sender_completion()
Ken Mixter20d9e472010-08-12 10:58:46 -0700240 output = self._log_reader.get_logs()
241 logging.debug('Crash sender message output:\n' + output)
242 if script_output != '':
243 raise error.TestFail(
244 'Unexpected crash_sender stdout/stderr: ' + script_output)
245
Ken Mixter38dfe852010-08-18 15:24:00 -0700246 if os.path.exists(report):
247 report_exists = True
248 os.remove(report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700249 else:
Ken Mixter38dfe852010-08-18 15:24:00 -0700250 report_exists = False
Ken Mixter20d9e472010-08-12 10:58:46 -0700251 if os.path.exists(self._CRASH_SENDER_RATE_DIR):
252 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR))
253 else:
254 rate_count = 0
255
256 result = self._parse_sender_output(output)
Ken Mixter38dfe852010-08-18 15:24:00 -0700257 result['report_exists'] = report_exists
Ken Mixter20d9e472010-08-12 10:58:46 -0700258 result['rate_count'] = rate_count
259
260 # Show the result for debugging but remove 'output' key
261 # since it's large and earlier in debug output.
262 debug_result = dict(result)
263 del debug_result['output']
264 logging.debug('Result of send (besides output): %s' % debug_result)
265
266 return result
267
268
269 def initialize(self):
270 test.test.initialize(self)
271 self._log_reader = site_log_reader.LogReader()
Ken Mixter38dfe852010-08-18 15:24:00 -0700272 self._leave_crash_sending = True
Ken Mixter67ff5622010-09-30 15:32:17 -0700273 self._automatic_consent_saving = True
Ken Mixter20d9e472010-08-12 10:58:46 -0700274
275
276 def cleanup(self):
277 self._reset_rate_limiting()
278 self._clear_spooled_crashes()
Ken Mixter4f619652010-10-18 12:11:18 -0700279 self._set_system_sending(self._leave_crash_sending)
Ken Mixter20d9e472010-08-12 10:58:46 -0700280 self._set_sending_mock(mock_enabled=False)
Ken Mixter67ff5622010-09-30 15:32:17 -0700281 if self._automatic_consent_saving:
282 self._pop_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700283 test.test.cleanup(self)
284
285
Ken Mixter38dfe852010-08-18 15:24:00 -0700286 def run_crash_tests(self,
287 test_names,
288 initialize_crash_reporter=False,
289 clear_spool_first=True,
290 must_run_all=True):
291 """Run crash tests defined in this class.
292
293 Args:
294 test_names: array of test names
295 initialize_crash_reporter: should set up crash reporter for every run
296 must_run_all: should make sure every test in this class is mentioned
297 in test_names
298 """
Ken Mixter67ff5622010-09-30 15:32:17 -0700299 if self._automatic_consent_saving:
300 self._push_consent()
Ken Mixter20d9e472010-08-12 10:58:46 -0700301
Ken Mixter38dfe852010-08-18 15:24:00 -0700302 if must_run_all:
303 # Sanity check test_names is complete
304 for attr in dir(self):
305 if attr.find('_test_') == 0:
306 test_name = attr[6:]
307 if not test_name in test_names:
308 raise error.TestError('Test %s is missing' % test_name)
Ken Mixter20d9e472010-08-12 10:58:46 -0700309
310 for test_name in test_names:
311 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20))
Ken Mixter38dfe852010-08-18 15:24:00 -0700312 if initialize_crash_reporter:
313 self._initialize_crash_reporter()
Ken Mixter4f619652010-10-18 12:11:18 -0700314 # Disable crash_sender from running, kill off any running ones, but
315 # set environment so crash_sender may run as a child process.
316 self._set_system_sending(False)
317 self._set_child_sending(True)
Ken Mixter20d9e472010-08-12 10:58:46 -0700318 self._kill_running_sender()
319 self._reset_rate_limiting()
Ken Mixter38dfe852010-08-18 15:24:00 -0700320 if clear_spool_first:
321 self._clear_spooled_crashes()
Ken Mixter20d9e472010-08-12 10:58:46 -0700322 getattr(self, '_test_' + test_name)()