blob: 8a556b2291f7c559bd8777995104b4c196359c23 [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
22 def _set_sending(self, is_enabled):
23 if is_enabled:
24 if os.path.exists(self._PAUSE_FILE):
25 os.remove(self._PAUSE_FILE)
26 else:
27 utils.system('touch ' + self._PAUSE_FILE)
28
29
30 def _reset_rate_limiting(self):
31 utils.system('rm -rf ' + self._CRASH_SENDER_RATE_DIR)
32
33
34 def _clear_spooled_crashes(self):
35 utils.system('rm -rf ' + self._SYSTEM_CRASH_DIR)
36 utils.system('rm -rf ' + self._USER_CRASH_DIR)
37
38
39 def _kill_running_sender(self):
40 if not os.path.exists(self._CRASH_SENDER_RUN_PATH):
41 return
42 running_pid = int(utils.read_file(self._CRASH_SENDER_RUN_PATH))
43 logging.warning('Detected running crash sender (%d), killing' %
44 running_pid)
45 utils.system('kill -9 %d' % running_pid)
46 os.remove(self._CRASH_SENDER_RUN_PATH)
47
48
49 def _set_sending_mock(self, mock_enabled, send_success=True):
50 if mock_enabled:
51 if send_success:
52 data = ''
53 else:
54 data = '1'
55 logging.info('Setting sending mock')
56 utils.open_write_close(self._MOCK_CRASH_SENDING, data)
57 else:
58 utils.system('rm -f ' + self._MOCK_CRASH_SENDING)
59
60
61 def _set_consent(self, has_consent):
62 if has_consent:
63 utils.open_write_close(self._CONSENT_FILE, 'test-consent')
64 logging.info('Created ' + self._CONSENT_FILE)
65 else:
66 utils.system('rm -f "%s"' % (self._CONSENT_FILE))
67
68
69 def _get_pushed_consent_file_path(self):
70 return os.path.join(self.bindir, 'pushed_consent')
71
72
73 def _push_consent(self):
74 if os.path.exists(self._CONSENT_FILE):
75 os.rename(self._CONSENT_FILE, self._get_pushed_consent_file_path())
76
77
78 def _pop_consent(self):
79 self._set_consent(False)
80 if os.path.exists(self._get_pushed_consent_file_path()):
81 os.rename(self._get_pushed_consent_file_path(), self._CONSENT_FILE)
82
83
84 def _get_crash_dir(self, username):
85 if username == 'chronos':
86 return self._USER_CRASH_DIR
87 else:
88 return self._SYSTEM_CRASH_DIR
89
90
91 def _initialize_crash_reporter(self):
92 utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH)
93
94
Ken Mixter38dfe852010-08-18 15:24:00 -070095 def create_fake_crash_dir_entry(self, name):
Ken Mixter20d9e472010-08-12 10:58:46 -070096 entry = os.path.join(self._SYSTEM_CRASH_DIR, name)
97 if not os.path.exists(self._SYSTEM_CRASH_DIR):
98 os.makedirs(self._SYSTEM_CRASH_DIR)
99 utils.system('touch ' + entry)
100 return entry
101
102
103 def _prepare_sender_one_crash(self,
104 send_success,
105 reports_enabled,
106 username,
Ken Mixter38dfe852010-08-18 15:24:00 -0700107 report):
Ken Mixter20d9e472010-08-12 10:58:46 -0700108 self._set_sending_mock(mock_enabled=True, send_success=send_success)
109 self._set_consent(reports_enabled)
Ken Mixter38dfe852010-08-18 15:24:00 -0700110 if report is None:
111 report = self.create_fake_crash_dir_entry('fake.dmp')
112 return report
Ken Mixter20d9e472010-08-12 10:58:46 -0700113
114
115 def _parse_sender_output(self, output):
116 """Parse the log output from the crash_sender script.
117
118 This script can run on the logs from either a mocked or true
119 crash send.
120
121 Args:
122 output: output from the script
123
124 Returns:
125 A dictionary with these values:
Ken Mixter38dfe852010-08-18 15:24:00 -0700126 exec_name: name of executable which crashed
127 report_kind: kind of report sent (minidump vs kernel)
128 report_name: name of the report sent
Ken Mixter20d9e472010-08-12 10:58:46 -0700129 send_attempt: did the script attempt to send a crash.
130 send_success: if it attempted, was the crash send successful.
131 sleep_time: if it attempted, how long did it sleep before
132 sending (if mocked, how long would it have slept)
133 output: the output from the script, copied
134 """
135 sleep_match = re.search('Scheduled to send in (\d+)s', output)
136 send_attempt = sleep_match is not None
137 if send_attempt:
138 sleep_time = int(sleep_match.group(1))
139 else:
140 sleep_time = None
Ken Mixter38dfe852010-08-18 15:24:00 -0700141 report_kind_match = re.search('Report: (\S+) \((\S+)\)', output)
142 if report_kind_match:
143 report_name = report_kind_match.group(1)
144 report_kind = report_kind_match.group(2)
145 else:
146 report_name = None
147 report_kind = None
148 exec_name_match = re.search('Exec name: (\S+)', output)
149 if exec_name_match:
150 exec_name = exec_name_match.group(1)
151 else:
152 exec_name = None
Ken Mixter20d9e472010-08-12 10:58:46 -0700153 send_success = 'Mocking successful send' in output
Ken Mixter38dfe852010-08-18 15:24:00 -0700154 return {'exec_name': exec_name,
155 'report_kind': report_kind,
156 'report_name': report_name,
157 'send_attempt': send_attempt,
Ken Mixter20d9e472010-08-12 10:58:46 -0700158 'send_success': send_success,
159 'sleep_time': sleep_time,
160 'output': output}
161
162
163 def _call_sender_one_crash(self,
164 send_success=True,
165 reports_enabled=True,
166 username='root',
Ken Mixter38dfe852010-08-18 15:24:00 -0700167 report=None):
Ken Mixter20d9e472010-08-12 10:58:46 -0700168 """Call the crash sender script to mock upload one crash.
169
170 Args:
171 send_success: Mock a successful send if true
172 reports_enabled: Has the user consented to sending crash reports.
173 username: user to emulate a crash from
Ken Mixter38dfe852010-08-18 15:24:00 -0700174 report: report to use for crash, if None we create one.
Ken Mixter20d9e472010-08-12 10:58:46 -0700175
176 Returns:
177 Returns a dictionary describing the result with the keys
178 from _parse_sender_output, as well as:
Ken Mixter38dfe852010-08-18 15:24:00 -0700179 report_exists: does the minidump still exist after calling
Ken Mixter20d9e472010-08-12 10:58:46 -0700180 send script
181 rate_count: how many crashes have been uploaded in the past
182 24 hours.
183 """
Ken Mixter38dfe852010-08-18 15:24:00 -0700184 report = self._prepare_sender_one_crash(send_success,
185 reports_enabled,
186 username,
187 report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700188 self._log_reader.set_start_by_current()
189 script_output = utils.system_output(
190 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH,
191 ignore_status=True)
192 # Wait for up to 2s for no crash_sender to be running,
193 # otherwise me might get only part of the output.
194 site_utils.poll_for_condition(
195 lambda: utils.system('pgrep crash_sender',
196 ignore_status=True) != 0,
197 timeout=2,
198 exception=error.TestError(
199 'Timeout waiting for crash_sender to finish: ' +
200 self._log_reader.get_logs()))
201
202 output = self._log_reader.get_logs()
203 logging.debug('Crash sender message output:\n' + output)
204 if script_output != '':
205 raise error.TestFail(
206 'Unexpected crash_sender stdout/stderr: ' + script_output)
207
Ken Mixter38dfe852010-08-18 15:24:00 -0700208 if os.path.exists(report):
209 report_exists = True
210 os.remove(report)
Ken Mixter20d9e472010-08-12 10:58:46 -0700211 else:
Ken Mixter38dfe852010-08-18 15:24:00 -0700212 report_exists = False
Ken Mixter20d9e472010-08-12 10:58:46 -0700213 if os.path.exists(self._CRASH_SENDER_RATE_DIR):
214 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR))
215 else:
216 rate_count = 0
217
218 result = self._parse_sender_output(output)
Ken Mixter38dfe852010-08-18 15:24:00 -0700219 result['report_exists'] = report_exists
Ken Mixter20d9e472010-08-12 10:58:46 -0700220 result['rate_count'] = rate_count
221
222 # Show the result for debugging but remove 'output' key
223 # since it's large and earlier in debug output.
224 debug_result = dict(result)
225 del debug_result['output']
226 logging.debug('Result of send (besides output): %s' % debug_result)
227
228 return result
229
230
231 def initialize(self):
232 test.test.initialize(self)
233 self._log_reader = site_log_reader.LogReader()
Ken Mixter38dfe852010-08-18 15:24:00 -0700234 self._leave_crash_sending = True
Ken Mixter20d9e472010-08-12 10:58:46 -0700235
236
237 def cleanup(self):
238 self._reset_rate_limiting()
239 self._clear_spooled_crashes()
Ken Mixter38dfe852010-08-18 15:24:00 -0700240 self._set_sending(self._leave_crash_sending)
Ken Mixter20d9e472010-08-12 10:58:46 -0700241 self._set_sending_mock(mock_enabled=False)
242 self._pop_consent()
243 test.test.cleanup(self)
244
245
Ken Mixter38dfe852010-08-18 15:24:00 -0700246 def run_crash_tests(self,
247 test_names,
248 initialize_crash_reporter=False,
249 clear_spool_first=True,
250 must_run_all=True):
251 """Run crash tests defined in this class.
252
253 Args:
254 test_names: array of test names
255 initialize_crash_reporter: should set up crash reporter for every run
256 must_run_all: should make sure every test in this class is mentioned
257 in test_names
258 """
Ken Mixter20d9e472010-08-12 10:58:46 -0700259 self._push_consent()
260
Ken Mixter38dfe852010-08-18 15:24:00 -0700261 if must_run_all:
262 # Sanity check test_names is complete
263 for attr in dir(self):
264 if attr.find('_test_') == 0:
265 test_name = attr[6:]
266 if not test_name in test_names:
267 raise error.TestError('Test %s is missing' % test_name)
Ken Mixter20d9e472010-08-12 10:58:46 -0700268
269 for test_name in test_names:
270 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20))
Ken Mixter38dfe852010-08-18 15:24:00 -0700271 if initialize_crash_reporter:
272 self._initialize_crash_reporter()
Ken Mixter20d9e472010-08-12 10:58:46 -0700273 self._kill_running_sender()
274 self._reset_rate_limiting()
Ken Mixter38dfe852010-08-18 15:24:00 -0700275 if clear_spool_first:
276 self._clear_spooled_crashes()
Ken Mixter20d9e472010-08-12 10:58:46 -0700277 self._set_sending(False)
278 getattr(self, '_test_' + test_name)()