OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import logging, os, re |
| 6 from autotest_lib.client.bin import site_log_reader, site_utils, test |
| 7 from autotest_lib.client.common_lib import error, utils |
| 8 |
| 9 |
| 10 class 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' |
| 18 _PAUSE_FILE = '/tmp/pause-crash-sending' |
| 19 _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 |
| 95 def _create_fake_crash_dir_entry(self, name): |
| 96 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, |
| 107 minidump): |
| 108 self._set_sending_mock(mock_enabled=True, send_success=send_success) |
| 109 self._set_consent(reports_enabled) |
| 110 if minidump is None: |
| 111 minidump = self._create_fake_crash_dir_entry('fake.dmp') |
| 112 return minidump |
| 113 |
| 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: |
| 126 send_attempt: did the script attempt to send a crash. |
| 127 send_success: if it attempted, was the crash send successful. |
| 128 sleep_time: if it attempted, how long did it sleep before |
| 129 sending (if mocked, how long would it have slept) |
| 130 output: the output from the script, copied |
| 131 """ |
| 132 sleep_match = re.search('Scheduled to send in (\d+)s', output) |
| 133 send_attempt = sleep_match is not None |
| 134 if send_attempt: |
| 135 sleep_time = int(sleep_match.group(1)) |
| 136 else: |
| 137 sleep_time = None |
| 138 send_success = 'Mocking successful send' in output |
| 139 return {'send_attempt': send_attempt, |
| 140 'send_success': send_success, |
| 141 'sleep_time': sleep_time, |
| 142 'output': output} |
| 143 |
| 144 |
| 145 def _call_sender_one_crash(self, |
| 146 send_success=True, |
| 147 reports_enabled=True, |
| 148 username='root', |
| 149 minidump=None): |
| 150 """Call the crash sender script to mock upload one crash. |
| 151 |
| 152 Args: |
| 153 send_success: Mock a successful send if true |
| 154 reports_enabled: Has the user consented to sending crash reports. |
| 155 username: user to emulate a crash from |
| 156 minidump: minidump to use for crash, if None we create one. |
| 157 |
| 158 Returns: |
| 159 Returns a dictionary describing the result with the keys |
| 160 from _parse_sender_output, as well as: |
| 161 minidump_exists: does the minidump still exist after calling |
| 162 send script |
| 163 rate_count: how many crashes have been uploaded in the past |
| 164 24 hours. |
| 165 """ |
| 166 minidump = self._prepare_sender_one_crash(send_success, |
| 167 reports_enabled, |
| 168 username, |
| 169 minidump) |
| 170 self._log_reader.set_start_by_current() |
| 171 script_output = utils.system_output( |
| 172 '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH, |
| 173 ignore_status=True) |
| 174 # Wait for up to 2s for no crash_sender to be running, |
| 175 # otherwise me might get only part of the output. |
| 176 site_utils.poll_for_condition( |
| 177 lambda: utils.system('pgrep crash_sender', |
| 178 ignore_status=True) != 0, |
| 179 timeout=2, |
| 180 exception=error.TestError( |
| 181 'Timeout waiting for crash_sender to finish: ' + |
| 182 self._log_reader.get_logs())) |
| 183 |
| 184 output = self._log_reader.get_logs() |
| 185 logging.debug('Crash sender message output:\n' + output) |
| 186 if script_output != '': |
| 187 raise error.TestFail( |
| 188 'Unexpected crash_sender stdout/stderr: ' + script_output) |
| 189 |
| 190 if os.path.exists(minidump): |
| 191 minidump_exists = True |
| 192 os.remove(minidump) |
| 193 else: |
| 194 minidump_exists = False |
| 195 if os.path.exists(self._CRASH_SENDER_RATE_DIR): |
| 196 rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR)) |
| 197 else: |
| 198 rate_count = 0 |
| 199 |
| 200 result = self._parse_sender_output(output) |
| 201 result['minidump_exists'] = minidump_exists |
| 202 result['rate_count'] = rate_count |
| 203 |
| 204 # Show the result for debugging but remove 'output' key |
| 205 # since it's large and earlier in debug output. |
| 206 debug_result = dict(result) |
| 207 del debug_result['output'] |
| 208 logging.debug('Result of send (besides output): %s' % debug_result) |
| 209 |
| 210 return result |
| 211 |
| 212 |
| 213 def initialize(self): |
| 214 test.test.initialize(self) |
| 215 self._log_reader = site_log_reader.LogReader() |
| 216 |
| 217 |
| 218 def cleanup(self): |
| 219 self._reset_rate_limiting() |
| 220 self._clear_spooled_crashes() |
| 221 self._set_sending(True) |
| 222 self._set_sending_mock(mock_enabled=False) |
| 223 self._pop_consent() |
| 224 test.test.cleanup(self) |
| 225 |
| 226 |
| 227 def run_crash_tests(self, test_names): |
| 228 self._push_consent() |
| 229 |
| 230 # Sanity check test_names is complete |
| 231 for attr in dir(self): |
| 232 if attr.find('_test_') == 0: |
| 233 test_name = attr[6:] |
| 234 if not test_name in test_names: |
| 235 raise error.TestError('Test %s is missing' % test_name) |
| 236 |
| 237 for test_name in test_names: |
| 238 logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20)) |
| 239 self._initialize_crash_reporter() |
| 240 self._kill_running_sender() |
| 241 self._reset_rate_limiting() |
| 242 self._clear_spooled_crashes() |
| 243 self._set_sending(False) |
| 244 getattr(self, '_test_' + test_name)() |
OLD | NEW |