Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 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 | |
Dale Curtis | 497c2cb | 2010-11-16 13:44:33 -0800 | [diff] [blame] | 5 | import logging, os, re, shutil |
Eric Li | e7c4cab | 2011-01-05 14:39:19 -0800 | [diff] [blame] | 6 | import common |
| 7 | import cros_logging |
| 8 | from autotest_lib.client.bin import test, utils |
| 9 | from autotest_lib.client.common_lib import error |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class CrashTest(test.test): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 13 | """ |
| 14 | This class deals with running crash tests, which are tests which crash a |
| 15 | user-space program (or the whole machine) and generate a core dump. We |
| 16 | want to check that the correct crash dump is available and can be |
| 17 | retrieved. |
| 18 | |
| 19 | Chromium OS has a crash sender which checks for new crash data and sends |
| 20 | it to a server. This crash data is used to track software quality and find |
| 21 | bugs. The system crash sender normally is always running, but can be paused |
| 22 | by creating _PAUSE_FILE. When crash sender sees this, it pauses operation. |
| 23 | |
| 24 | The pid of the system crash sender is stored in _CRASH_SENDER_RUN_PATH so |
| 25 | we can use this to kill the system crash sender for when we want to run |
| 26 | our own. |
| 27 | |
| 28 | For testing purposes we sometimes want to run the crash sender manually. |
| 29 | In this case we can set 'OVERRIDE_PAUSE_SENDING=1' in the environment and |
| 30 | run the crash sender manually (as a child process). |
| 31 | |
| 32 | Also for testing we sometimes want to mock out the crash sender, and just |
| 33 | have it pretend to succeed or fail. The _MOCK_CRASH_SENDING file is used |
| 34 | for this. If it doesn't exist, then the crash sender runs normally. If |
| 35 | it exists but is empty, the crash sender will succeed (but actually do |
| 36 | nothing). If the file contains something, then the crash sender will fail. |
| 37 | |
| 38 | If the user consents to sending crash tests, then the _CONSENT_FILE will |
| 39 | exist in the home directory. This test needs to create this file for the |
| 40 | crash sending to work. |
| 41 | |
| 42 | Crash reports are rate limited to a certain number of reports each 24 |
| 43 | hours. If the maximum number has already been sent then reports are held |
| 44 | until later. This is administered by a directory _CRASH_SENDER_RATE_DIR |
| 45 | which contains one temporary file for each time a report is sent. |
| 46 | |
| 47 | The class provides the ability to push a consent file. This disables |
| 48 | consent for this test but allows it to be popped back at later. This |
| 49 | makes nested tests easier. If _automatic_consent_saving is True (the |
| 50 | default) then consent will be pushed at the start and popped at the end. |
| 51 | |
| 52 | Interesting variables: |
| 53 | _log_reader: the log reader used for reading log files |
| 54 | _leave_crash_sending: True to enable crash sending on exit from the |
| 55 | test, False to disable it. (Default True). |
| 56 | _automatic_consent_saving: True to push the consent at the start of |
| 57 | the test and pop it afterwards. (Default True). |
| 58 | |
| 59 | Useful places to look for more information are: |
| 60 | |
| 61 | chromeos/src/platform/crash-reporter/crash_sender |
| 62 | - sender script which crash crash reporter to create reports, then |
| 63 | |
| 64 | chromeos/src/platform/crash-reporter/ |
| 65 | - crash reporter program |
| 66 | """ |
| 67 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 68 | |
| 69 | _CONSENT_FILE = '/home/chronos/Consent To Send Stats' |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 70 | _POLICY_FILE = '/var/lib/whitelist/policy' |
| 71 | _OWNER_KEY_FILE = '/var/lib/whitelist/owner.key' |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 72 | _CORE_PATTERN = '/proc/sys/kernel/core_pattern' |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 73 | _CRASH_REPORTER_PATH = '/sbin/crash_reporter' |
| 74 | _CRASH_SENDER_PATH = '/sbin/crash_sender' |
| 75 | _CRASH_SENDER_RATE_DIR = '/var/lib/crash_sender' |
| 76 | _CRASH_SENDER_RUN_PATH = '/var/run/crash_sender.pid' |
Thieu Le | c16253b | 2011-03-03 11:13:54 -0800 | [diff] [blame] | 77 | _CRASH_TEST_IN_PROGRESS = '/tmp/crash-test-in-progress' |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 78 | _MOCK_CRASH_SENDING = '/tmp/mock-crash-sending' |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 79 | _PAUSE_FILE = '/var/lib/crash_sender_paused' |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 80 | _SYSTEM_CRASH_DIR = '/var/spool/crash' |
| 81 | _USER_CRASH_DIR = '/home/chronos/user/crash' |
| 82 | |
Ken Mixter | 4f61965 | 2010-10-18 12:11:18 -0700 | [diff] [blame] | 83 | def _set_system_sending(self, is_enabled): |
| 84 | """Sets whether or not the system crash_sender is allowed to run. |
| 85 | |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 86 | This is done by creating or removing _PAUSE_FILE. |
| 87 | |
Ken Mixter | 4f61965 | 2010-10-18 12:11:18 -0700 | [diff] [blame] | 88 | crash_sender may still be allowed to run if _set_child_sending is |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 89 | called with True and it is run as a child process. |
| 90 | |
| 91 | Args: |
| 92 | is_enabled: True to enable crash_sender, False to disable it. |
| 93 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 94 | if is_enabled: |
| 95 | if os.path.exists(self._PAUSE_FILE): |
| 96 | os.remove(self._PAUSE_FILE) |
| 97 | else: |
| 98 | utils.system('touch ' + self._PAUSE_FILE) |
| 99 | |
| 100 | |
Ken Mixter | 4f61965 | 2010-10-18 12:11:18 -0700 | [diff] [blame] | 101 | def _set_child_sending(self, is_enabled): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 102 | """Overrides crash sending enabling for child processes. |
| 103 | |
| 104 | When the system crash sender is disabled this test can manually run |
| 105 | the crash sender as a child process. Normally this would do nothing, |
| 106 | but this function sets up crash_sender to ignore its disabled status |
| 107 | and do its job. |
| 108 | |
| 109 | Args: |
| 110 | is_enabled: True to enable crash sending for child processes. |
| 111 | """ |
Ken Mixter | 4f61965 | 2010-10-18 12:11:18 -0700 | [diff] [blame] | 112 | if is_enabled: |
| 113 | os.environ['OVERRIDE_PAUSE_SENDING'] = "1" |
| 114 | else: |
| 115 | del os.environ['OVERRIDE_PAUSE_SENDING'] |
| 116 | |
| 117 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 118 | def _reset_rate_limiting(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 119 | """Reset the count of crash reports sent today. |
| 120 | |
| 121 | This clears the contents of the rate limiting directory which has |
| 122 | the effect of reseting our count of crash reports sent. |
| 123 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 124 | utils.system('rm -rf ' + self._CRASH_SENDER_RATE_DIR) |
| 125 | |
| 126 | |
| 127 | def _clear_spooled_crashes(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 128 | """Clears system and user crash directories. |
| 129 | |
| 130 | This will remove all crash reports which are waiting to be sent. |
| 131 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 132 | utils.system('rm -rf ' + self._SYSTEM_CRASH_DIR) |
| 133 | utils.system('rm -rf ' + self._USER_CRASH_DIR) |
| 134 | |
| 135 | |
| 136 | def _kill_running_sender(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 137 | """Kill the the crash_sender process if running. |
| 138 | |
| 139 | We use the PID file to find the process ID, then kill it with signal 9. |
| 140 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 141 | if not os.path.exists(self._CRASH_SENDER_RUN_PATH): |
| 142 | return |
| 143 | running_pid = int(utils.read_file(self._CRASH_SENDER_RUN_PATH)) |
| 144 | logging.warning('Detected running crash sender (%d), killing' % |
| 145 | running_pid) |
| 146 | utils.system('kill -9 %d' % running_pid) |
| 147 | os.remove(self._CRASH_SENDER_RUN_PATH) |
| 148 | |
| 149 | |
| 150 | def _set_sending_mock(self, mock_enabled, send_success=True): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 151 | """Enables / disables mocking of the sending process. |
| 152 | |
| 153 | This uses the _MOCK_CRASH_SENDING file to achieve its aims. See notes |
| 154 | at the top. |
| 155 | |
| 156 | Args: |
| 157 | mock_enabled: If True, mocking is enabled, else it is disabled. |
| 158 | send_success: If mock_enabled this is True for the mocking to |
| 159 | indicate success, False to indicate failure. |
| 160 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 161 | if mock_enabled: |
| 162 | if send_success: |
| 163 | data = '' |
| 164 | else: |
| 165 | data = '1' |
| 166 | logging.info('Setting sending mock') |
| 167 | utils.open_write_close(self._MOCK_CRASH_SENDING, data) |
| 168 | else: |
| 169 | utils.system('rm -f ' + self._MOCK_CRASH_SENDING) |
| 170 | |
| 171 | |
| 172 | def _set_consent(self, has_consent): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 173 | """Sets whether or not we have consent to send crash reports. |
| 174 | |
| 175 | This creates or deletes the _CONSENT_FILE to control whether |
| 176 | crash_sender will consider that it has consent to send crash reports. |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 177 | It also copies a policy blob with the proper policy setting. |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 178 | |
| 179 | Args: |
| 180 | has_consent: True to indicate consent, False otherwise |
| 181 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 182 | if has_consent: |
| 183 | utils.open_write_close(self._CONSENT_FILE, 'test-consent') |
Michael Krebs | 05400be | 2011-06-27 17:14:56 -0700 | [diff] [blame] | 184 | utils.system('chown chronos:chronos "%s"' % (self._CONSENT_FILE)) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 185 | logging.info('Created ' + self._CONSENT_FILE) |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 186 | shutil.copy('/usr/local/autotest/cros/mock_metrics_on.policy', |
| 187 | self._POLICY_FILE) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 188 | else: |
| 189 | utils.system('rm -f "%s"' % (self._CONSENT_FILE)) |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 190 | shutil.copy('/usr/local/autotest/cros/mock_metrics_off.policy', |
| 191 | self._POLICY_FILE) |
| 192 | """Both policy blobs are signed with the same key.""" |
| 193 | shutil.copy('/usr/local/autotest/cros/mock_metrics_owner.key', |
| 194 | self._OWNER_KEY_FILE) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 195 | |
| 196 | |
Thieu Le | c16253b | 2011-03-03 11:13:54 -0800 | [diff] [blame] | 197 | def _set_crash_test_in_progress(self, in_progress): |
| 198 | if in_progress: |
| 199 | utils.open_write_close(self._CRASH_TEST_IN_PROGRESS, 'in-progress') |
| 200 | logging.info('Created ' + self._CRASH_TEST_IN_PROGRESS) |
| 201 | else: |
| 202 | utils.system('rm -f "%s"' % (self._CRASH_TEST_IN_PROGRESS)) |
| 203 | |
| 204 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 205 | def _get_pushed_consent_file_path(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 206 | """Returns filename of the pushed consent file.""" |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 207 | return os.path.join(self.bindir, 'pushed_consent') |
| 208 | |
| 209 | |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 210 | def _get_pushed_policy_file_path(self): |
| 211 | """Returns filename of the pushed policy file.""" |
| 212 | return os.path.join(self.bindir, 'pushed_policy') |
| 213 | |
| 214 | |
| 215 | def _get_pushed_owner_key_file_path(self): |
| 216 | """Returns filename of the pushed owner.key file.""" |
| 217 | return os.path.join(self.bindir, 'pushed_owner_key') |
| 218 | |
| 219 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 220 | def _push_consent(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 221 | """Push the consent file, thus disabling consent. |
| 222 | |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 223 | The consent files can be created in the new test if required. Call |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 224 | _pop_consent() to restore the original state. |
| 225 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 226 | if os.path.exists(self._CONSENT_FILE): |
Dale Curtis | 497c2cb | 2010-11-16 13:44:33 -0800 | [diff] [blame] | 227 | shutil.move(self._CONSENT_FILE, |
| 228 | self._get_pushed_consent_file_path()) |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 229 | if os.path.exists(self._POLICY_FILE): |
| 230 | shutil.move(self._POLICY_FILE, |
| 231 | self._get_pushed_policy_file_path()) |
| 232 | if os.path.exists(self._OWNER_KEY_FILE): |
| 233 | shutil.move(self._OWNER_KEY_FILE, |
| 234 | self._get_pushed_owner_key_file_path()) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 235 | |
| 236 | |
| 237 | def _pop_consent(self): |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 238 | """Pop the consent files, enabling/disabling consent as it was before |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 239 | we pushed the consent.""" |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 240 | if os.path.exists(self._get_pushed_consent_file_path()): |
Dale Curtis | 497c2cb | 2010-11-16 13:44:33 -0800 | [diff] [blame] | 241 | shutil.move(self._get_pushed_consent_file_path(), |
| 242 | self._CONSENT_FILE) |
Julian Pastarmov | d56badb | 2011-07-15 20:24:45 +0200 | [diff] [blame^] | 243 | else: |
| 244 | utils.system('rm -f "%s"' % self._CONSENT_FILE) |
| 245 | if os.path.exists(self._get_pushed_policy_file_path()): |
| 246 | shutil.move(self._get_pushed_policy_file_path(), |
| 247 | self._POLICY_FILE) |
| 248 | else: |
| 249 | utils.system('rm -f "%s"' % self._POLICY_FILE) |
| 250 | if os.path.exists(self._get_pushed_owner_key_file_path()): |
| 251 | shutil.move(self._get_pushed_owner_key_file_path(), |
| 252 | self._OWNER_KEY_FILE) |
| 253 | else: |
| 254 | utils.system('rm -f "%s"' % self._OWNER_KEY_FILE) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 255 | |
| 256 | |
| 257 | def _get_crash_dir(self, username): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 258 | """Returns full path to the crash directory for a given username |
| 259 | |
| 260 | Args: |
| 261 | username: username to use: |
| 262 | 'chronos': Returns user crash directory. |
| 263 | 'root': Returns system crash directory. |
| 264 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 265 | if username == 'chronos': |
| 266 | return self._USER_CRASH_DIR |
| 267 | else: |
| 268 | return self._SYSTEM_CRASH_DIR |
| 269 | |
| 270 | |
| 271 | def _initialize_crash_reporter(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 272 | """Start up the crash reporter.""" |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 273 | utils.system('%s --init --nounclean_check' % self._CRASH_REPORTER_PATH) |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 274 | # Completely disable crash_reporter from generating crash dumps |
| 275 | # while any tests are running, otherwise a crashy system can make |
| 276 | # these tests flaky. |
| 277 | self.enable_crash_filtering('none') |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 278 | |
| 279 | |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 280 | def write_crash_dir_entry(self, name, contents): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 281 | """Writes an empty file to the system crash directory. |
| 282 | |
| 283 | This writes a file to _SYSTEM_CRASH_DIR with the given name. This is |
| 284 | used to insert new crash dump files for testing purposes. |
| 285 | |
| 286 | Args: |
| 287 | name: Name of file to write. |
| 288 | contents: String to write to the file. |
| 289 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 290 | entry = os.path.join(self._SYSTEM_CRASH_DIR, name) |
| 291 | if not os.path.exists(self._SYSTEM_CRASH_DIR): |
| 292 | os.makedirs(self._SYSTEM_CRASH_DIR) |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 293 | utils.open_write_close(entry, contents) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 294 | return entry |
| 295 | |
| 296 | |
Ken Mixter | dee4e29 | 2010-12-14 17:45:21 -0800 | [diff] [blame] | 297 | def write_fake_meta(self, name, exec_name, payload, log=None, |
| 298 | complete=True): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 299 | """Writes a fake meta entry to the system crash directory. |
| 300 | |
| 301 | Args: |
| 302 | name: Name of file to write. |
| 303 | exec_name: Value for exec_name item. |
| 304 | payload: Value for payload item. |
| 305 | log: Value for log item. |
| 306 | complete: True to close off the record, otherwise leave it |
| 307 | incomplete. |
| 308 | """ |
Ken Mixter | 1a894e0 | 2010-10-28 15:42:52 -0700 | [diff] [blame] | 309 | last_line = '' |
| 310 | if complete: |
| 311 | last_line = 'done=1\n' |
Ken Mixter | dee4e29 | 2010-12-14 17:45:21 -0800 | [diff] [blame] | 312 | contents = ('exec_name=%s\n' |
| 313 | 'ver=my_ver\n' |
| 314 | 'payload=%s\n' |
| 315 | '%s' % (exec_name, payload, |
| 316 | last_line)) |
| 317 | if log: |
| 318 | contents = ('log=%s\n' % log) + contents |
| 319 | return self.write_crash_dir_entry(name, contents) |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 320 | |
| 321 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 322 | def _prepare_sender_one_crash(self, |
| 323 | send_success, |
| 324 | reports_enabled, |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 325 | report): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 326 | """Create metadata for a fake crash report. |
| 327 | |
| 328 | This enabled mocking of the crash sender, then creates a fake |
| 329 | crash report for testing purposes. |
| 330 | |
| 331 | Args: |
| 332 | send_success: True to make the crash_sender success, False to make |
| 333 | it fail. |
| 334 | reports_enabled: True to enable consent to that reports will be |
| 335 | sent. |
| 336 | report: Report to use for crash, if None we create one. |
| 337 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 338 | self._set_sending_mock(mock_enabled=True, send_success=send_success) |
| 339 | self._set_consent(reports_enabled) |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 340 | if report is None: |
Ken Mixter | 1a894e0 | 2010-10-28 15:42:52 -0700 | [diff] [blame] | 341 | payload = self.write_crash_dir_entry('fake.dmp', '') |
| 342 | report = self.write_fake_meta('fake.meta', 'fake', payload) |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 343 | return report |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 344 | |
| 345 | |
| 346 | def _parse_sender_output(self, output): |
| 347 | """Parse the log output from the crash_sender script. |
| 348 | |
| 349 | This script can run on the logs from either a mocked or true |
| 350 | crash send. |
| 351 | |
| 352 | Args: |
| 353 | output: output from the script |
| 354 | |
| 355 | Returns: |
| 356 | A dictionary with these values: |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 357 | exec_name: name of executable which crashed |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 358 | meta_path: path to the report metadata file |
| 359 | output: the output from the script, copied |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 360 | report_kind: kind of report sent (minidump vs kernel) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 361 | send_attempt: did the script attempt to send a crash. |
| 362 | send_success: if it attempted, was the crash send successful. |
Ken Mixter | d79140e | 2010-10-26 14:45:30 -0700 | [diff] [blame] | 363 | sig: signature of the report, if given. |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 364 | sleep_time: if it attempted, how long did it sleep before |
| 365 | sending (if mocked, how long would it have slept) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 366 | """ |
| 367 | sleep_match = re.search('Scheduled to send in (\d+)s', output) |
| 368 | send_attempt = sleep_match is not None |
| 369 | if send_attempt: |
| 370 | sleep_time = int(sleep_match.group(1)) |
| 371 | else: |
| 372 | sleep_time = None |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 373 | meta_match = re.search('Metadata: (\S+) \((\S+)\)', output) |
| 374 | if meta_match: |
| 375 | meta_path = meta_match.group(1) |
| 376 | report_kind = meta_match.group(2) |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 377 | else: |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 378 | meta_path = None |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 379 | report_kind = None |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 380 | payload_match = re.search('Payload: (\S+)', output) |
| 381 | if payload_match: |
| 382 | report_payload = payload_match.group(1) |
| 383 | else: |
| 384 | report_payload = None |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 385 | exec_name_match = re.search('Exec name: (\S+)', output) |
| 386 | if exec_name_match: |
| 387 | exec_name = exec_name_match.group(1) |
| 388 | else: |
| 389 | exec_name = None |
Ken Mixter | 10c4867 | 2010-11-01 13:37:08 -0700 | [diff] [blame] | 390 | sig_match = re.search('sig: (\S+)', output) |
Ken Mixter | d79140e | 2010-10-26 14:45:30 -0700 | [diff] [blame] | 391 | if sig_match: |
| 392 | sig = sig_match.group(1) |
| 393 | else: |
| 394 | sig = None |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 395 | send_success = 'Mocking successful send' in output |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 396 | return {'exec_name': exec_name, |
| 397 | 'report_kind': report_kind, |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 398 | 'meta_path': meta_path, |
| 399 | 'report_payload': report_payload, |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 400 | 'send_attempt': send_attempt, |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 401 | 'send_success': send_success, |
Ken Mixter | d79140e | 2010-10-26 14:45:30 -0700 | [diff] [blame] | 402 | 'sig': sig, |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 403 | 'sleep_time': sleep_time, |
| 404 | 'output': output} |
| 405 | |
| 406 | |
Ken Mixter | 654f32e | 2010-10-20 11:47:31 -0700 | [diff] [blame] | 407 | def wait_for_sender_completion(self): |
| 408 | """Wait for crash_sender to complete. |
| 409 | |
| 410 | Wait for no crash_sender's last message to be placed in the |
| 411 | system log before continuing and for the process to finish. |
| 412 | Otherwise we might get only part of the output.""" |
Eric Li | e7c4cab | 2011-01-05 14:39:19 -0800 | [diff] [blame] | 413 | utils.poll_for_condition( |
Ken Mixter | 654f32e | 2010-10-20 11:47:31 -0700 | [diff] [blame] | 414 | lambda: self._log_reader.can_find('crash_sender done.'), |
| 415 | timeout=60, |
| 416 | exception=error.TestError( |
| 417 | 'Timeout waiting for crash_sender to emit done: ' + |
| 418 | self._log_reader.get_logs())) |
Eric Li | e7c4cab | 2011-01-05 14:39:19 -0800 | [diff] [blame] | 419 | utils.poll_for_condition( |
Ken Mixter | 654f32e | 2010-10-20 11:47:31 -0700 | [diff] [blame] | 420 | lambda: utils.system('pgrep crash_sender', |
| 421 | ignore_status=True) != 0, |
| 422 | timeout=60, |
| 423 | exception=error.TestError( |
| 424 | 'Timeout waiting for crash_sender to finish: ' + |
| 425 | self._log_reader.get_logs())) |
| 426 | |
| 427 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 428 | def _call_sender_one_crash(self, |
| 429 | send_success=True, |
| 430 | reports_enabled=True, |
| 431 | username='root', |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 432 | report=None): |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 433 | """Call the crash sender script to mock upload one crash. |
| 434 | |
| 435 | Args: |
| 436 | send_success: Mock a successful send if true |
| 437 | reports_enabled: Has the user consented to sending crash reports. |
| 438 | username: user to emulate a crash from |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 439 | report: report to use for crash, if None we create one. |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 440 | |
| 441 | Returns: |
| 442 | Returns a dictionary describing the result with the keys |
| 443 | from _parse_sender_output, as well as: |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 444 | report_exists: does the minidump still exist after calling |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 445 | send script |
| 446 | rate_count: how many crashes have been uploaded in the past |
| 447 | 24 hours. |
| 448 | """ |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 449 | report = self._prepare_sender_one_crash(send_success, |
| 450 | reports_enabled, |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 451 | report) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 452 | self._log_reader.set_start_by_current() |
| 453 | script_output = utils.system_output( |
| 454 | '/bin/sh -c "%s" 2>&1' % self._CRASH_SENDER_PATH, |
| 455 | ignore_status=True) |
Ken Mixter | 654f32e | 2010-10-20 11:47:31 -0700 | [diff] [blame] | 456 | self.wait_for_sender_completion() |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 457 | output = self._log_reader.get_logs() |
| 458 | logging.debug('Crash sender message output:\n' + output) |
| 459 | if script_output != '': |
| 460 | raise error.TestFail( |
| 461 | 'Unexpected crash_sender stdout/stderr: ' + script_output) |
| 462 | |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 463 | if os.path.exists(report): |
| 464 | report_exists = True |
| 465 | os.remove(report) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 466 | else: |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 467 | report_exists = False |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 468 | if os.path.exists(self._CRASH_SENDER_RATE_DIR): |
| 469 | rate_count = len(os.listdir(self._CRASH_SENDER_RATE_DIR)) |
| 470 | else: |
| 471 | rate_count = 0 |
| 472 | |
| 473 | result = self._parse_sender_output(output) |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 474 | result['report_exists'] = report_exists |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 475 | result['rate_count'] = rate_count |
| 476 | |
| 477 | # Show the result for debugging but remove 'output' key |
| 478 | # since it's large and earlier in debug output. |
| 479 | debug_result = dict(result) |
| 480 | del debug_result['output'] |
| 481 | logging.debug('Result of send (besides output): %s' % debug_result) |
| 482 | |
| 483 | return result |
| 484 | |
| 485 | |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 486 | def _replace_crash_reporter_filter_in(self, new_parameter): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 487 | """Replaces the --filter_in= parameter of the crash reporter. |
| 488 | |
| 489 | The kernel is set up to call the crash reporter with the core dump |
| 490 | as stdin when a process dies. This function adds a filter to the |
| 491 | command line used to call the crash reporter. This is used to ignore |
| 492 | crashes in which we have no interest. |
| 493 | |
| 494 | This removes any --filter_in= parameter and optionally replaces it |
| 495 | with a new one. |
| 496 | |
| 497 | Args: |
| 498 | new_parameter: This is parameter to add to the command line |
| 499 | instead of the --filter_in=... that was there. |
| 500 | """ |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 501 | core_pattern = utils.read_file(self._CORE_PATTERN)[:-1] |
| 502 | core_pattern = re.sub('--filter_in=\S*\s*', '', |
| 503 | core_pattern).rstrip() |
| 504 | if new_parameter: |
| 505 | core_pattern += ' ' + new_parameter |
| 506 | utils.system('echo "%s" > %s' % (core_pattern, self._CORE_PATTERN)) |
| 507 | |
| 508 | |
| 509 | def enable_crash_filtering(self, name): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 510 | """Add a --filter_in argument to the kernel core dump cmdline. |
| 511 | |
| 512 | Args: |
| 513 | name: Filter text to use. This is passed as a --filter_in |
| 514 | argument to the crash reporter. |
| 515 | """ |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 516 | self._replace_crash_reporter_filter_in('--filter_in=' + name) |
| 517 | |
| 518 | |
| 519 | def disable_crash_filtering(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 520 | """Remove the --filter_in argument from the kernel core dump cmdline. |
| 521 | |
| 522 | Next time the crash reporter is invoked (due to a crash) it will not |
| 523 | receive a --filter_in paramter.""" |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 524 | self._replace_crash_reporter_filter_in('') |
| 525 | |
| 526 | |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 527 | def initialize(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 528 | """Initalize the test.""" |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 529 | test.test.initialize(self) |
Eric Li | e7c4cab | 2011-01-05 14:39:19 -0800 | [diff] [blame] | 530 | self._log_reader = cros_logging.LogReader() |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 531 | self._leave_crash_sending = True |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 532 | self._automatic_consent_saving = True |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 533 | self.enable_crash_filtering('none') |
Thieu Le | c16253b | 2011-03-03 11:13:54 -0800 | [diff] [blame] | 534 | self._set_crash_test_in_progress(True) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 535 | |
| 536 | |
| 537 | def cleanup(self): |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 538 | """Cleanup after the test. |
| 539 | |
| 540 | We reset things back to the way we think they should be. This is |
| 541 | intended to allow the system to continue normal operation. |
| 542 | |
| 543 | Some variables silently change the behavior: |
| 544 | _automatic_consent_saving: if True, we pop the consent file. |
| 545 | _leave_crash_sending: True to enable crash sending, False to |
| 546 | disable it |
| 547 | """ |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 548 | self._reset_rate_limiting() |
| 549 | self._clear_spooled_crashes() |
Ken Mixter | 4f61965 | 2010-10-18 12:11:18 -0700 | [diff] [blame] | 550 | self._set_system_sending(self._leave_crash_sending) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 551 | self._set_sending_mock(mock_enabled=False) |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 552 | if self._automatic_consent_saving: |
| 553 | self._pop_consent() |
Ken Mixter | ddcd92d | 2010-11-01 19:07:08 -0700 | [diff] [blame] | 554 | self.disable_crash_filtering() |
Thieu Le | c16253b | 2011-03-03 11:13:54 -0800 | [diff] [blame] | 555 | self._set_crash_test_in_progress(False) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 556 | test.test.cleanup(self) |
| 557 | |
| 558 | |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 559 | def run_crash_tests(self, |
| 560 | test_names, |
| 561 | initialize_crash_reporter=False, |
| 562 | clear_spool_first=True, |
| 563 | must_run_all=True): |
| 564 | """Run crash tests defined in this class. |
| 565 | |
| 566 | Args: |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 567 | test_names: Array of test names. |
| 568 | initialize_crash_reporter: Should set up crash reporter for every |
| 569 | run. |
| 570 | clear_spool_first: Clear all spooled user/system crashes before |
| 571 | starting the test. |
| 572 | must_run_all: Should make sure every test in this class is |
| 573 | mentioned in test_names. |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 574 | """ |
Ken Mixter | 67ff562 | 2010-09-30 15:32:17 -0700 | [diff] [blame] | 575 | if self._automatic_consent_saving: |
| 576 | self._push_consent() |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 577 | |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 578 | if must_run_all: |
| 579 | # Sanity check test_names is complete |
| 580 | for attr in dir(self): |
| 581 | if attr.find('_test_') == 0: |
| 582 | test_name = attr[6:] |
| 583 | if not test_name in test_names: |
| 584 | raise error.TestError('Test %s is missing' % test_name) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 585 | |
| 586 | for test_name in test_names: |
| 587 | logging.info(('=' * 20) + ('Running %s' % test_name) + ('=' * 20)) |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 588 | if initialize_crash_reporter: |
| 589 | self._initialize_crash_reporter() |
Ken Mixter | 4f61965 | 2010-10-18 12:11:18 -0700 | [diff] [blame] | 590 | # Disable crash_sender from running, kill off any running ones, but |
| 591 | # set environment so crash_sender may run as a child process. |
| 592 | self._set_system_sending(False) |
| 593 | self._set_child_sending(True) |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 594 | self._kill_running_sender() |
| 595 | self._reset_rate_limiting() |
Ken Mixter | 38dfe85 | 2010-08-18 15:24:00 -0700 | [diff] [blame] | 596 | if clear_spool_first: |
| 597 | self._clear_spooled_crashes() |
Simon Glass | a47f0d7 | 2011-03-15 11:45:32 -0700 | [diff] [blame] | 598 | |
| 599 | # Call the test function |
Ken Mixter | 20d9e47 | 2010-08-12 10:58:46 -0700 | [diff] [blame] | 600 | getattr(self, '_test_' + test_name)() |