OLD | NEW |
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging, os, re | 5 import logging, os, re |
6 from autotest_lib.client.bin import site_log_reader, site_utils, test | 6 from autotest_lib.client.bin import site_log_reader, site_utils, test |
7 from autotest_lib.client.common_lib import error, utils | 7 from autotest_lib.client.common_lib import error, utils |
8 | 8 |
9 | 9 |
10 class CrashTest(test.test): | 10 class CrashTest(test.test): |
11 | 11 |
12 _CONSENT_FILE = '/home/chronos/Consent To Send Stats' | 12 _CONSENT_FILE = '/home/chronos/Consent To Send Stats' |
13 _CRASH_REPORTER_PATH = '/sbin/crash_reporter' | 13 _CRASH_REPORTER_PATH = '/sbin/crash_reporter' |
14 _CRASH_SENDER_PATH = '/sbin/crash_sender' | 14 _CRASH_SENDER_PATH = '/sbin/crash_sender' |
15 _CRASH_SENDER_RATE_DIR = '/var/lib/crash_sender' | 15 _CRASH_SENDER_RATE_DIR = '/var/lib/crash_sender' |
16 _CRASH_SENDER_RUN_PATH = '/var/run/crash_sender.pid' | 16 _CRASH_SENDER_RUN_PATH = '/var/run/crash_sender.pid' |
17 _MOCK_CRASH_SENDING = '/tmp/mock-crash-sending' | 17 _MOCK_CRASH_SENDING = '/tmp/mock-crash-sending' |
18 _PAUSE_FILE = '/tmp/pause-crash-sending' | 18 _PAUSE_FILE = '/var/lib/crash_sender_paused' |
19 _SYSTEM_CRASH_DIR = '/var/spool/crash' | 19 _SYSTEM_CRASH_DIR = '/var/spool/crash' |
20 _USER_CRASH_DIR = '/home/chronos/user/crash' | 20 _USER_CRASH_DIR = '/home/chronos/user/crash' |
21 | 21 |
22 def _set_sending(self, is_enabled): | 22 def _set_sending(self, is_enabled): |
23 if is_enabled: | 23 if is_enabled: |
24 if os.path.exists(self._PAUSE_FILE): | 24 if os.path.exists(self._PAUSE_FILE): |
25 os.remove(self._PAUSE_FILE) | 25 os.remove(self._PAUSE_FILE) |
26 else: | 26 else: |
27 utils.system('touch ' + self._PAUSE_FILE) | 27 utils.system('touch ' + self._PAUSE_FILE) |
28 | 28 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 if username == 'chronos': | 85 if username == 'chronos': |
86 return self._USER_CRASH_DIR | 86 return self._USER_CRASH_DIR |
87 else: | 87 else: |
88 return self._SYSTEM_CRASH_DIR | 88 return self._SYSTEM_CRASH_DIR |
89 | 89 |
90 | 90 |
91 def _initialize_crash_reporter(self): | 91 def _initialize_crash_reporter(self): |
92 utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH) | 92 utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH) |
93 | 93 |
94 | 94 |
95 def _create_fake_crash_dir_entry(self, name): | 95 def create_fake_crash_dir_entry(self, name): |
96 entry = os.path.join(self._SYSTEM_CRASH_DIR, name) | 96 entry = os.path.join(self._SYSTEM_CRASH_DIR, name) |
97 if not os.path.exists(self._SYSTEM_CRASH_DIR): | 97 if not os.path.exists(self._SYSTEM_CRASH_DIR): |
98 os.makedirs(self._SYSTEM_CRASH_DIR) | 98 os.makedirs(self._SYSTEM_CRASH_DIR) |
99 utils.system('touch ' + entry) | 99 utils.system('touch ' + entry) |
100 return entry | 100 return entry |
101 | 101 |
102 | 102 |
103 def _prepare_sender_one_crash(self, | 103 def _prepare_sender_one_crash(self, |
104 send_success, | 104 send_success, |
105 reports_enabled, | 105 reports_enabled, |
106 username, | 106 username, |
107 minidump): | 107 report): |
108 self._set_sending_mock(mock_enabled=True, send_success=send_success) | 108 self._set_sending_mock(mock_enabled=True, send_success=send_success) |
109 self._set_consent(reports_enabled) | 109 self._set_consent(reports_enabled) |
110 if minidump is None: | 110 if report is None: |
111 minidump = self._create_fake_crash_dir_entry('fake.dmp') | 111 report = self.create_fake_crash_dir_entry('fake.dmp') |
112 return minidump | 112 return report |
113 | 113 |
114 | 114 |
115 def _parse_sender_output(self, output): | 115 def _parse_sender_output(self, output): |
116 """Parse the log output from the crash_sender script. | 116 """Parse the log output from the crash_sender script. |
117 | 117 |
118 This script can run on the logs from either a mocked or true | 118 This script can run on the logs from either a mocked or true |
119 crash send. | 119 crash send. |
120 | 120 |
121 Args: | 121 Args: |
122 output: output from the script | 122 output: output from the script |
123 | 123 |
124 Returns: | 124 Returns: |
125 A dictionary with these values: | 125 A dictionary with these values: |
| 126 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 |
126 send_attempt: did the script attempt to send a crash. | 129 send_attempt: did the script attempt to send a crash. |
127 send_success: if it attempted, was the crash send successful. | 130 send_success: if it attempted, was the crash send successful. |
128 sleep_time: if it attempted, how long did it sleep before | 131 sleep_time: if it attempted, how long did it sleep before |
129 sending (if mocked, how long would it have slept) | 132 sending (if mocked, how long would it have slept) |
130 output: the output from the script, copied | 133 output: the output from the script, copied |
131 """ | 134 """ |
132 sleep_match = re.search('Scheduled to send in (\d+)s', output) | 135 sleep_match = re.search('Scheduled to send in (\d+)s', output) |
133 send_attempt = sleep_match is not None | 136 send_attempt = sleep_match is not None |
134 if send_attempt: | 137 if send_attempt: |
135 sleep_time = int(sleep_match.group(1)) | 138 sleep_time = int(sleep_match.group(1)) |
136 else: | 139 else: |
137 sleep_time = None | 140 sleep_time = None |
| 141 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 |
138 send_success = 'Mocking successful send' in output | 153 send_success = 'Mocking successful send' in output |
139 return {'send_attempt': send_attempt, | 154 return {'exec_name': exec_name, |
| 155 'report_kind': report_kind, |
| 156 'report_name': report_name, |
| 157 'send_attempt': send_attempt, |
140 'send_success': send_success, | 158 'send_success': send_success, |
141 'sleep_time': sleep_time, | 159 'sleep_time': sleep_time, |
142 'output': output} | 160 'output': output} |
143 | 161 |
144 | 162 |
145 def _call_sender_one_crash(self, | 163 def _call_sender_one_crash(self, |
146 send_success=True, | 164 send_success=True, |
147 reports_enabled=True, | 165 reports_enabled=True, |
148 username='root', | 166 username='root', |
149 minidump=None): | 167 report=None): |
150 """Call the crash sender script to mock upload one crash. | 168 """Call the crash sender script to mock upload one crash. |
151 | 169 |
152 Args: | 170 Args: |
153 send_success: Mock a successful send if true | 171 send_success: Mock a successful send if true |
154 reports_enabled: Has the user consented to sending crash reports. | 172 reports_enabled: Has the user consented to sending crash reports. |
155 username: user to emulate a crash from | 173 username: user to emulate a crash from |
156 minidump: minidump to use for crash, if None we create one. | 174 report: report to use for crash, if None we create one. |
157 | 175 |
158 Returns: | 176 Returns: |
159 Returns a dictionary describing the result with the keys | 177 Returns a dictionary describing the result with the keys |
160 from _parse_sender_output, as well as: | 178 from _parse_sender_output, as well as: |
161 minidump_exists: does the minidump still exist after calling | 179 report_exists: does the minidump still exist after calling |
162 send script | 180 send script |
163 rate_count: how many crashes have been uploaded in the past | 181 rate_count: how many crashes have been uploaded in the past |
164 24 hours. | 182 24 hours. |
165 """ | 183 """ |
166 minidump = self._prepare_sender_one_crash(send_success, | 184 report = self._prepare_sender_one_crash(send_success, |
167 reports_enabled, | 185 reports_enabled, |
168 username, | 186 username, |
169 minidump) | 187 report) |
170 self._log_reader.set_start_by_current() | 188 self._log_reader.set_start_by_current() |
171 script_output = utils.system_output( | 189 script_output = utils.system_output( |
172 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH, | 190 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH, |
173 ignore_status=True) | 191 ignore_status=True) |
174 # Wait for up to 2s for no crash_sender to be running, | 192 # Wait for up to 2s for no crash_sender to be running, |
175 # otherwise me might get only part of the output. | 193 # otherwise me might get only part of the output. |
176 site_utils.poll_for_condition( | 194 site_utils.poll_for_condition( |
177 lambda: utils.system('pgrep crash_sender', | 195 lambda: utils.system('pgrep crash_sender', |
178 ignore_status=True) != 0, | 196 ignore_status=True) != 0, |
179 timeout=2, | 197 timeout=2, |
180 exception=error.TestError( | 198 exception=error.TestError( |
181 'Timeout waiting for crash_sender to finish: ' + | 199 'Timeout waiting for crash_sender to finish: ' + |
182 self._log_reader.get_logs())) | 200 self._log_reader.get_logs())) |
183 | 201 |
184 output = self._log_reader.get_logs() | 202 output = self._log_reader.get_logs() |
185 logging.debug('Crash sender message output:\n' + output) | 203 logging.debug('Crash sender message output:\n' + output) |
186 if script_output != '': | 204 if script_output != '': |
187 raise error.TestFail( | 205 raise error.TestFail( |
188 'Unexpected crash_sender stdout/stderr: ' + script_output) | 206 'Unexpected crash_sender stdout/stderr: ' + script_output) |
189 | 207 |
190 if os.path.exists(minidump): | 208 if os.path.exists(report): |
191 minidump_exists = True | 209 report_exists = True |
192 os.remove(minidump) | 210 os.remove(report) |
193 else: | 211 else: |
194 minidump_exists = False | 212 report_exists = False |
195 if os.path.exists(self._CRASH_SENDER_RATE_DIR): | 213 if os.path.exists(self._CRASH_SENDER_RATE_DIR): |
196 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR)) | 214 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR)) |
197 else: | 215 else: |
198 rate_count = 0 | 216 rate_count = 0 |
199 | 217 |
200 result = self._parse_sender_output(output) | 218 result = self._parse_sender_output(output) |
201 result['minidump_exists'] = minidump_exists | 219 result['report_exists'] = report_exists |
202 result['rate_count'] = rate_count | 220 result['rate_count'] = rate_count |
203 | 221 |
204 # Show the result for debugging but remove 'output' key | 222 # Show the result for debugging but remove 'output' key |
205 # since it's large and earlier in debug output. | 223 # since it's large and earlier in debug output. |
206 debug_result = dict(result) | 224 debug_result = dict(result) |
207 del debug_result['output'] | 225 del debug_result['output'] |
208 logging.debug('Result of send (besides output): %s' % debug_result) | 226 logging.debug('Result of send (besides output): %s' % debug_result) |
209 | 227 |
210 return result | 228 return result |
211 | 229 |
212 | 230 |
213 def initialize(self): | 231 def initialize(self): |
214 test.test.initialize(self) | 232 test.test.initialize(self) |
215 self._log_reader = site_log_reader.LogReader() | 233 self._log_reader = site_log_reader.LogReader() |
| 234 self._leave_crash_sending = True |
216 | 235 |
217 | 236 |
218 def cleanup(self): | 237 def cleanup(self): |
219 self._reset_rate_limiting() | 238 self._reset_rate_limiting() |
220 self._clear_spooled_crashes() | 239 self._clear_spooled_crashes() |
221 self._set_sending(True) | 240 self._set_sending(self._leave_crash_sending) |
222 self._set_sending_mock(mock_enabled=False) | 241 self._set_sending_mock(mock_enabled=False) |
223 self._pop_consent() | 242 self._pop_consent() |
224 test.test.cleanup(self) | 243 test.test.cleanup(self) |
225 | 244 |
226 | 245 |
227 def run_crash_tests(self, test_names): | 246 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 """ |
228 self._push_consent() | 259 self._push_consent() |
229 | 260 |
230 # Sanity check test_names is complete | 261 if must_run_all: |
231 for attr in dir(self): | 262 # Sanity check test_names is complete |
232 if attr.find('_test_') == 0: | 263 for attr in dir(self): |
233 test_name = attr[6:] | 264 if attr.find('_test_') == 0: |
234 if not test_name in test_names: | 265 test_name = attr[6:] |
235 raise error.TestError('Test %s is missing' % test_name) | 266 if not test_name in test_names: |
| 267 raise error.TestError('Test %s is missing' % test_name) |
236 | 268 |
237 for test_name in test_names: | 269 for test_name in test_names: |
238 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20)) | 270 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20)) |
239 self._initialize_crash_reporter() | 271 if initialize_crash_reporter: |
| 272 self._initialize_crash_reporter() |
240 self._kill_running_sender() | 273 self._kill_running_sender() |
241 self._reset_rate_limiting() | 274 self._reset_rate_limiting() |
242 self._clear_spooled_crashes() | 275 if clear_spool_first: |
| 276 self._clear_spooled_crashes() |
243 self._set_sending(False) | 277 self._set_sending(False) |
244 getattr(self, '_test_' + test_name)() | 278 getattr(self, '_test_' + test_name)() |
OLD | NEW |