Gaurav Shah | a7fb896 | 2011-08-16 15:06:32 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Vadim Bendebury | e797092 | 2011-04-07 17:07:37 -0700 | [diff] [blame] | 5 | import logging, os, platform, re, tempfile, time |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import error |
Vadim Bendebury | e797092 | 2011-04-07 17:07:37 -0700 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import utils |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 8 | |
| 9 | |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 10 | class TimeoutError(error.TestError): |
| 11 | """Error raised when we time out when waiting on a condition.""" |
| 12 | |
| 13 | |
Vadim Bendebury | e797092 | 2011-04-07 17:07:37 -0700 | [diff] [blame] | 14 | class Crossystem(object): |
| 15 | """A wrapper for the crossystem utility.""" |
| 16 | |
| 17 | def __init__(self, client): |
| 18 | self.cros_system_data = {} |
| 19 | self._client = client |
| 20 | |
| 21 | def init(self): |
| 22 | self.cros_system_data = {} |
| 23 | (_, fname) = tempfile.mkstemp() |
| 24 | f = open(fname, 'w') |
| 25 | self._client.run('crossystem', stdout_tee=f) |
| 26 | f.close() |
| 27 | text = utils.read_file(fname) |
| 28 | for line in text.splitlines(): |
| 29 | assignment_string = line.split('#')[0] |
| 30 | if not assignment_string.count('='): |
| 31 | continue |
| 32 | (name, value) = assignment_string.split('=', 1) |
| 33 | self.cros_system_data[name.strip()] = value.strip() |
| 34 | os.remove(fname) |
| 35 | |
| 36 | def __getattr__(self, name): |
| 37 | """ |
| 38 | Retrieve a crosssystem attribute. |
| 39 | |
| 40 | The call crossystemobject.name() will return the crossystem reported |
| 41 | string. |
| 42 | """ |
| 43 | return lambda : self.cros_system_data[name] |
| 44 | |
| 45 | |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 46 | def poll_for_condition( |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 47 | condition, exception=None, timeout=10, sleep_interval=0.1, desc=None): |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 48 | """Poll until a condition becomes true. |
| 49 | |
| 50 | condition: function taking no args and returning bool |
| 51 | exception: exception to throw if condition doesn't become true |
| 52 | timeout: maximum number of seconds to wait |
| 53 | sleep_interval: time to sleep between polls |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 54 | desc: description of default TimeoutError used if 'exception' is None |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 55 | |
| 56 | Raises: |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 57 | 'exception' arg if supplied; site_utils.TimeoutError otherwise |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 58 | """ |
| 59 | start_time = time.time() |
| 60 | while True: |
| 61 | if condition(): |
| 62 | return |
| 63 | if time.time() + sleep_interval - start_time > timeout: |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 64 | if exception: |
Gaurav Shah | a7fb896 | 2011-08-16 15:06:32 -0700 | [diff] [blame] | 65 | logging.error(exception) |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 66 | raise exception |
| 67 | |
| 68 | if desc: |
| 69 | desc = 'Timed out waiting for condition: %s' % desc |
| 70 | else: |
| 71 | desc = 'Timed out waiting for unnamed condition' |
Gaurav Shah | a7fb896 | 2011-08-16 15:06:32 -0700 | [diff] [blame] | 72 | logging.error(desc) |
Mitsuru Oshima | 5d3e454 | 2010-08-18 13:46:06 -0700 | [diff] [blame] | 73 | raise TimeoutError, desc |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 74 | |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 75 | time.sleep(sleep_interval) |
Thieu Le | 1904d00 | 2010-11-30 17:10:24 -0800 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def save_vm_state(checkpoint): |
| 79 | """Saves the current state of the virtual machine. |
| 80 | |
| 81 | This function is a NOOP if the test is not running under a virtual machine |
| 82 | with the USB serial port redirected. |
| 83 | |
| 84 | Arguments: |
| 85 | checkpoint - Name used to identify this state |
| 86 | |
| 87 | Returns: |
| 88 | None |
| 89 | """ |
| 90 | # The QEMU monitor has been redirected to the guest serial port located at |
| 91 | # /dev/ttyUSB0. To save the state of the VM, we just send the 'savevm' |
| 92 | # command to the serial port. |
| 93 | proc = platform.processor() |
| 94 | if 'QEMU' in proc and os.path.exists('/dev/ttyUSB0'): |
| 95 | logging.info('Saving VM state "%s"' % checkpoint) |
| 96 | serial = open('/dev/ttyUSB0', 'w') |
| 97 | serial.write("savevm %s\r\n" % checkpoint) |
| 98 | logging.info('Done saving VM state "%s"' % checkpoint) |
Mandeep Singh Baines | 142ac8d | 2011-02-18 13:31:08 -0800 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def check_raw_dmesg(dmesg, message_level, whitelist): |
| 102 | """Checks dmesg for unexpected warnings. |
| 103 | |
| 104 | This function parses dmesg for message with message_level <= message_level |
| 105 | which do not appear in the whitelist. |
| 106 | |
| 107 | Arguments: |
| 108 | dmesg - string containing raw dmesg buffer |
| 109 | message_level - minimum message priority to check |
| 110 | whitelist - messages to ignore |
| 111 | |
| 112 | Returns: |
| 113 | List of unexpected warnings |
| 114 | """ |
Vadim Bendebury | e797092 | 2011-04-07 17:07:37 -0700 | [diff] [blame] | 115 | whitelist_re = re.compile(r'(%s)' % '|'.join(whitelist)) |
Mandeep Singh Baines | 142ac8d | 2011-02-18 13:31:08 -0800 | [diff] [blame] | 116 | unexpected = [] |
| 117 | for line in dmesg.splitlines(): |
| 118 | if int(line[1]) <= message_level: |
Vadim Bendebury | e797092 | 2011-04-07 17:07:37 -0700 | [diff] [blame] | 119 | stripped_line = line.split('] ', 1)[1] |
| 120 | if whitelist_re.search(stripped_line): |
| 121 | continue |
| 122 | unexpected.append(stripped_line) |
Mandeep Singh Baines | 142ac8d | 2011-02-18 13:31:08 -0800 | [diff] [blame] | 123 | return unexpected |
Vadim Bendebury | 29916f2 | 2011-04-13 10:54:47 -0700 | [diff] [blame] | 124 | |
| 125 | def verify_mesg_set(mesg, regex, whitelist): |
| 126 | """Verifies that the exact set of messages are present in a text. |
| 127 | |
| 128 | This function finds all strings in the text matching a certain regex, and |
| 129 | then verifies that all expected strings are present in the set, and no |
| 130 | unexpected strings are there. |
| 131 | |
| 132 | Arguments: |
| 133 | mesg - the mutiline text to be scanned |
| 134 | regex - regular expression to match |
| 135 | whitelist - messages to find in the output, a list of strings |
| 136 | (potentially regexes) to look for in the filtered output. All these |
| 137 | strings must be there, and no other strings should be present in the |
| 138 | filtered output. |
| 139 | |
| 140 | Returns: |
| 141 | string of inconsistent findings (i.e. an empty string on success). |
| 142 | """ |
| 143 | |
| 144 | rv = [] |
| 145 | |
| 146 | missing_strings = [] |
| 147 | present_strings = [] |
| 148 | for line in mesg.splitlines(): |
| 149 | if not re.search(r'%s' % regex, line): |
| 150 | continue |
| 151 | present_strings.append(line.split('] ', 1)[1]) |
| 152 | |
| 153 | for string in whitelist: |
| 154 | for present_string in list(present_strings): |
| 155 | if re.search(r'^%s$' % string, present_string): |
| 156 | present_strings.remove(present_string) |
| 157 | break |
| 158 | else: |
| 159 | missing_strings.append(string) |
| 160 | |
| 161 | if present_strings: |
| 162 | rv.append('unexpected strings:') |
| 163 | rv.extend(present_strings) |
| 164 | if missing_strings: |
| 165 | rv.append('missing strings:') |
| 166 | rv.extend(missing_strings) |
| 167 | |
| 168 | return '\n'.join(rv) |
Ahmad Sharif | f8e9262 | 2011-05-24 12:37:39 -0700 | [diff] [blame] | 169 | |
| 170 | |
| 171 | def target_is_x86_pie(): |
| 172 | """Returns whether the toolchain produces an x86 PIE (position independent |
| 173 | executable) by default. |
| 174 | |
| 175 | Arguments: |
| 176 | None |
| 177 | |
| 178 | Returns: |
| 179 | True if the target toolchain produces an x86 PIE by default. |
| 180 | False otherwise. |
| 181 | """ |
| 182 | |
| 183 | |
| 184 | command = "echo \"int main(){return 0;}\" | ${CC} -o /tmp/a.out -xc -" |
| 185 | command += "&& file /tmp/a.out" |
| 186 | result = utils.system_output(command, retain_output=True, |
| 187 | ignore_status=True) |
| 188 | if re.search("80\d86", result) and re.search("shared object", result): |
| 189 | return True |
| 190 | else: |
| 191 | return False |