OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Helper functions common to native test runners.""" |
| 7 |
| 8 import logging |
| 9 import optparse |
| 10 import os |
| 11 import subprocess |
| 12 import sys |
| 13 |
| 14 # TODO(michaelbai): Move constant definitions like below to a common file. |
| 15 FORWARDER_PATH = '/data/local/tmp/forwarder' |
| 16 |
| 17 CHROME_DIR = os.path.abspath(os.path.join(sys.path[0], '..', '..')) |
| 18 |
| 19 |
| 20 def IsRunningAsBuildbot(): |
| 21 """Returns True if we are currently running on buildbot; False otherwise.""" |
| 22 return bool(os.getenv('BUILDBOT_BUILDERNAME')) |
| 23 |
| 24 |
| 25 def ReportBuildbotLink(label, url): |
| 26 """Adds a link with name |label| linking to |url| to current buildbot step. |
| 27 |
| 28 Args: |
| 29 label: A string with the name of the label. |
| 30 url: A string of the URL. |
| 31 """ |
| 32 if IsRunningAsBuildbot(): |
| 33 print '@@@STEP_LINK@%s@%s@@@' % (label, url) |
| 34 |
| 35 |
| 36 def ReportBuildbotMsg(msg): |
| 37 """Appends |msg| to the current buildbot step text. |
| 38 |
| 39 Args: |
| 40 msg: String to be appended. |
| 41 """ |
| 42 if IsRunningAsBuildbot(): |
| 43 print '@@@STEP_TEXT@%s@@@' % msg |
| 44 |
| 45 def ReportBuildbotError(): |
| 46 """Marks the current step as failed.""" |
| 47 if IsRunningAsBuildbot(): |
| 48 print '@@@STEP_FAILURE@@@' |
| 49 |
| 50 |
| 51 def GetExpectations(file_name): |
| 52 """Returns a list of test names in the |file_name| test expectations file.""" |
| 53 if not file_name or not os.path.exists(file_name): |
| 54 return [] |
| 55 return [x for x in [x.strip() for x in file(file_name).readlines()] |
| 56 if x and x[0] != '#'] |
| 57 |
| 58 |
| 59 def SetLogLevel(verbose_count): |
| 60 """Sets log level as |verbose_count|.""" |
| 61 log_level = logging.WARNING # Default. |
| 62 if verbose_count == 1: |
| 63 log_level = logging.INFO |
| 64 elif verbose_count >= 2: |
| 65 log_level = logging.DEBUG |
| 66 logging.getLogger().setLevel(log_level) |
| 67 |
| 68 |
| 69 def CreateTestRunnerOptionParser(usage=None, default_timeout=60): |
| 70 """Returns a new OptionParser with arguments applicable to all tests.""" |
| 71 option_parser = optparse.OptionParser(usage=usage) |
| 72 option_parser.add_option('-t', dest='timeout', |
| 73 help='Timeout to wait for each test', |
| 74 type='int', |
| 75 default=default_timeout) |
| 76 option_parser.add_option('-c', dest='cleanup_test_files', |
| 77 help='Cleanup test files on the device after run', |
| 78 action='store_true', |
| 79 default=False) |
| 80 option_parser.add_option('-v', |
| 81 '--verbose', |
| 82 dest='verbose_count', |
| 83 default=0, |
| 84 action='count', |
| 85 help='Verbose level (multiple times for more)') |
| 86 option_parser.add_option('--tool', |
| 87 dest='tool', |
| 88 help='Run the test under a tool ' |
| 89 '(use --tool help to list them)') |
| 90 return option_parser |
| 91 |
| 92 |
| 93 def ForwardDevicePorts(adb, ports, host_name='127.0.0.1'): |
| 94 """Forwards a TCP port on the device back to the host. |
| 95 |
| 96 Works like adb forward, but in reverse. |
| 97 |
| 98 Args: |
| 99 adb: Instance of AndroidCommands for talking to the device. |
| 100 ports: A list of tuples (device_port, host_port) to forward. |
| 101 host_name: Optional. Address to forward to, must be addressable from the |
| 102 host machine. Usually this is omitted and loopback is used. |
| 103 |
| 104 Returns: |
| 105 subprocess instance connected to the forwarder process on the device. |
| 106 """ |
| 107 adb.PushIfNeeded( |
| 108 os.path.join(CHROME_DIR, 'out', 'Release', 'forwarder'), FORWARDER_PATH) |
| 109 forward_string = ['%d:%d:%s' % |
| 110 (device, host, host_name) for device, host in ports] |
| 111 logging.info("Forwarding ports: %s" % (forward_string)) |
| 112 |
| 113 return subprocess.Popen( |
| 114 ['adb', '-s', adb._adb.GetSerialNumber(), |
| 115 'shell', '%s -D %s' % (FORWARDER_PATH, ' '.join(forward_string))]) |
| 116 |
| 117 |
| 118 def IsDevicePortUsed(adb, device_port): |
| 119 """Checks whether the specified device port is used or not. |
| 120 |
| 121 Args: |
| 122 adb: Instance of AndroidCommands for talking to the device. |
| 123 device_port: Port on device we want to check. |
| 124 |
| 125 Returns: |
| 126 True if the port on device is already used, otherwise returns False. |
| 127 """ |
| 128 base_url = '127.0.0.1:%d' % device_port |
| 129 netstat_results = adb.RunShellCommand('netstat') |
| 130 for single_connect in netstat_results: |
| 131 # Column 3 is the local address which we want to check with. |
| 132 if single_connect.split()[3] == base_url: |
| 133 return True |
| 134 return False |
OLD | NEW |