| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 The Chromium 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 """Environment setup and teardown for remote devices.""" | |
| 6 | |
| 7 import distutils.version | |
| 8 import json | |
| 9 import logging | |
| 10 import os | |
| 11 import random | |
| 12 import sys | |
| 13 | |
| 14 from devil.utils import reraiser_thread | |
| 15 from devil.utils import timeout_retry | |
| 16 from pylib.base import environment | |
| 17 from pylib.remote.device import appurify_sanitized | |
| 18 from pylib.remote.device import remote_device_helper | |
| 19 | |
| 20 class RemoteDeviceEnvironment(environment.Environment): | |
| 21 """An environment for running on remote devices.""" | |
| 22 | |
| 23 _ENV_KEY = 'env' | |
| 24 _DEVICE_KEY = 'device' | |
| 25 _DEFAULT_RETRIES = 0 | |
| 26 | |
| 27 def __init__(self, args, error_func): | |
| 28 """Constructor. | |
| 29 | |
| 30 Args: | |
| 31 args: Command line arguments. | |
| 32 error_func: error to show when using bad command line arguments. | |
| 33 """ | |
| 34 super(RemoteDeviceEnvironment, self).__init__() | |
| 35 self._access_token = None | |
| 36 self._device = None | |
| 37 self._device_type = args.device_type | |
| 38 self._verbose_count = args.verbose_count | |
| 39 self._timeouts = { | |
| 40 'queueing': 60 * 10, | |
| 41 'installing': 60 * 10, | |
| 42 'in-progress': 60 * 30, | |
| 43 'unknown': 60 * 5 | |
| 44 } | |
| 45 # Example config file: | |
| 46 # { | |
| 47 # "remote_device": ["Galaxy S4", "Galaxy S3"], | |
| 48 # "remote_device_os": ["4.4.2", "4.4.4"], | |
| 49 # "remote_device_minimum_os": "4.4.2", | |
| 50 # "api_address": "www.example.com", | |
| 51 # "api_port": "80", | |
| 52 # "api_protocol": "http", | |
| 53 # "api_secret": "apisecret", | |
| 54 # "api_key": "apikey", | |
| 55 # "timeouts": { | |
| 56 # "queueing": 600, | |
| 57 # "installing": 600, | |
| 58 # "in-progress": 1800, | |
| 59 # "unknown": 300 | |
| 60 # } | |
| 61 # } | |
| 62 if args.remote_device_file: | |
| 63 with open(args.remote_device_file) as device_file: | |
| 64 device_json = json.load(device_file) | |
| 65 else: | |
| 66 device_json = {} | |
| 67 | |
| 68 self._api_address = device_json.get('api_address', None) | |
| 69 self._api_key = device_json.get('api_key', None) | |
| 70 self._api_port = device_json.get('api_port', None) | |
| 71 self._api_protocol = device_json.get('api_protocol', None) | |
| 72 self._api_secret = device_json.get('api_secret', None) | |
| 73 self._device_oem = device_json.get('device_oem', None) | |
| 74 self._device_type = device_json.get('device_type', 'Android') | |
| 75 self._network_config = device_json.get('network_config', None) | |
| 76 self._remote_device = device_json.get('remote_device', None) | |
| 77 self._remote_device_minimum_os = device_json.get( | |
| 78 'remote_device_minimum_os', None) | |
| 79 self._remote_device_os = device_json.get('remote_device_os', None) | |
| 80 self._remote_device_timeout = device_json.get( | |
| 81 'remote_device_timeout', None) | |
| 82 self._results_path = device_json.get('results_path', None) | |
| 83 self._runner_package = device_json.get('runner_package', None) | |
| 84 self._runner_type = device_json.get('runner_type', None) | |
| 85 self._timeouts.update(device_json.get('timeouts', {})) | |
| 86 | |
| 87 def command_line_override( | |
| 88 file_value, cmd_line_value, desc, print_value=True): | |
| 89 if cmd_line_value: | |
| 90 if file_value and file_value != cmd_line_value: | |
| 91 if print_value: | |
| 92 logging.info('Overriding %s from %s to %s', | |
| 93 desc, file_value, cmd_line_value) | |
| 94 else: | |
| 95 logging.info('overriding %s', desc) | |
| 96 return cmd_line_value | |
| 97 return file_value | |
| 98 | |
| 99 self._api_address = command_line_override( | |
| 100 self._api_address, args.api_address, 'api_address') | |
| 101 self._api_port = command_line_override( | |
| 102 self._api_port, args.api_port, 'api_port') | |
| 103 self._api_protocol = command_line_override( | |
| 104 self._api_protocol, args.api_protocol, 'api_protocol') | |
| 105 self._device_oem = command_line_override( | |
| 106 self._device_oem, args.device_oem, 'device_oem') | |
| 107 self._device_type = command_line_override( | |
| 108 self._device_type, args.device_type, 'device_type') | |
| 109 self._network_config = command_line_override( | |
| 110 self._network_config, args.network_config, 'network_config') | |
| 111 self._remote_device = command_line_override( | |
| 112 self._remote_device, args.remote_device, 'remote_device') | |
| 113 self._remote_device_minimum_os = command_line_override( | |
| 114 self._remote_device_minimum_os, args.remote_device_minimum_os, | |
| 115 'remote_device_minimum_os') | |
| 116 self._remote_device_os = command_line_override( | |
| 117 self._remote_device_os, args.remote_device_os, 'remote_device_os') | |
| 118 self._remote_device_timeout = command_line_override( | |
| 119 self._remote_device_timeout, args.remote_device_timeout, | |
| 120 'remote_device_timeout') | |
| 121 self._results_path = command_line_override( | |
| 122 self._results_path, args.results_path, 'results_path') | |
| 123 self._runner_package = command_line_override( | |
| 124 self._runner_package, args.runner_package, 'runner_package') | |
| 125 self._runner_type = command_line_override( | |
| 126 self._runner_type, args.runner_type, 'runner_type') | |
| 127 self._timeouts["in-progress"] = command_line_override( | |
| 128 self._timeouts["in-progress"], args.test_timeout, 'test_timeout') | |
| 129 | |
| 130 if args.api_key_file: | |
| 131 with open(args.api_key_file) as api_key_file: | |
| 132 temp_key = api_key_file.read().strip() | |
| 133 self._api_key = command_line_override( | |
| 134 self._api_key, temp_key, 'api_key', print_value=False) | |
| 135 self._api_key = command_line_override( | |
| 136 self._api_key, args.api_key, 'api_key', print_value=False) | |
| 137 | |
| 138 if args.api_secret_file: | |
| 139 with open(args.api_secret_file) as api_secret_file: | |
| 140 temp_secret = api_secret_file.read().strip() | |
| 141 self._api_secret = command_line_override( | |
| 142 self._api_secret, temp_secret, 'api_secret', print_value=False) | |
| 143 self._api_secret = command_line_override( | |
| 144 self._api_secret, args.api_secret, 'api_secret', print_value=False) | |
| 145 | |
| 146 if not self._api_address: | |
| 147 error_func('Must set api address with --api-address' | |
| 148 ' or in --remote-device-file.') | |
| 149 if not self._api_key: | |
| 150 error_func('Must set api key with --api-key, --api-key-file' | |
| 151 ' or in --remote-device-file') | |
| 152 if not self._api_port: | |
| 153 error_func('Must set api port with --api-port' | |
| 154 ' or in --remote-device-file') | |
| 155 if not self._api_protocol: | |
| 156 error_func('Must set api protocol with --api-protocol' | |
| 157 ' or in --remote-device-file. Example: http') | |
| 158 if not self._api_secret: | |
| 159 error_func('Must set api secret with --api-secret, --api-secret-file' | |
| 160 ' or in --remote-device-file') | |
| 161 | |
| 162 logging.info('Api address: %s', self._api_address) | |
| 163 logging.info('Api port: %s', self._api_port) | |
| 164 logging.info('Api protocol: %s', self._api_protocol) | |
| 165 logging.info('Remote device: %s', self._remote_device) | |
| 166 logging.info('Remote device minimum OS: %s', | |
| 167 self._remote_device_minimum_os) | |
| 168 logging.info('Remote device OS: %s', self._remote_device_os) | |
| 169 logging.info('Remote device OEM: %s', self._device_oem) | |
| 170 logging.info('Remote device type: %s', self._device_type) | |
| 171 logging.info('Remote device timout: %s', self._remote_device_timeout) | |
| 172 logging.info('Results Path: %s', self._results_path) | |
| 173 logging.info('Runner package: %s', self._runner_package) | |
| 174 logging.info('Runner type: %s', self._runner_type) | |
| 175 logging.info('Timeouts: %s', self._timeouts) | |
| 176 | |
| 177 if not args.trigger and not args.collect: | |
| 178 self._trigger = True | |
| 179 self._collect = True | |
| 180 else: | |
| 181 self._trigger = args.trigger | |
| 182 self._collect = args.collect | |
| 183 | |
| 184 def SetUp(self): | |
| 185 """Set up the test environment.""" | |
| 186 os.environ['APPURIFY_API_PROTO'] = self._api_protocol | |
| 187 os.environ['APPURIFY_API_HOST'] = self._api_address | |
| 188 os.environ['APPURIFY_API_PORT'] = self._api_port | |
| 189 os.environ['APPURIFY_STATUS_BASE_URL'] = 'none' | |
| 190 self._GetAccessToken() | |
| 191 if self._trigger: | |
| 192 self._SelectDevice() | |
| 193 | |
| 194 def TearDown(self): | |
| 195 """Teardown the test environment.""" | |
| 196 self._RevokeAccessToken() | |
| 197 | |
| 198 def __enter__(self): | |
| 199 """Set up the test run when used as a context manager.""" | |
| 200 try: | |
| 201 self.SetUp() | |
| 202 return self | |
| 203 except: | |
| 204 self.__exit__(*sys.exc_info()) | |
| 205 raise | |
| 206 | |
| 207 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 208 """Tears down the test run when used as a context manager.""" | |
| 209 self.TearDown() | |
| 210 | |
| 211 def DumpTo(self, persisted_data): | |
| 212 env_data = { | |
| 213 self._DEVICE_KEY: self._device, | |
| 214 } | |
| 215 persisted_data[self._ENV_KEY] = env_data | |
| 216 | |
| 217 def LoadFrom(self, persisted_data): | |
| 218 env_data = persisted_data[self._ENV_KEY] | |
| 219 self._device = env_data[self._DEVICE_KEY] | |
| 220 | |
| 221 def _GetAccessToken(self): | |
| 222 """Generates access token for remote device service.""" | |
| 223 logging.info('Generating remote service access token') | |
| 224 with appurify_sanitized.SanitizeLogging(self._verbose_count, | |
| 225 logging.WARNING): | |
| 226 access_token_results = appurify_sanitized.api.access_token_generate( | |
| 227 self._api_key, self._api_secret) | |
| 228 remote_device_helper.TestHttpResponse(access_token_results, | |
| 229 'Unable to generate access token.') | |
| 230 self._access_token = access_token_results.json()['response']['access_token'] | |
| 231 | |
| 232 def _RevokeAccessToken(self): | |
| 233 """Destroys access token for remote device service.""" | |
| 234 logging.info('Revoking remote service access token') | |
| 235 with appurify_sanitized.SanitizeLogging(self._verbose_count, | |
| 236 logging.WARNING): | |
| 237 revoke_token_results = appurify_sanitized.api.access_token_revoke( | |
| 238 self._access_token) | |
| 239 remote_device_helper.TestHttpResponse(revoke_token_results, | |
| 240 'Unable to revoke access token.') | |
| 241 | |
| 242 def _SelectDevice(self): | |
| 243 if self._remote_device_timeout: | |
| 244 try: | |
| 245 timeout_retry.Run(self._FindDeviceWithTimeout, | |
| 246 self._remote_device_timeout, self._DEFAULT_RETRIES) | |
| 247 except reraiser_thread.TimeoutError: | |
| 248 self._NoDeviceFound() | |
| 249 else: | |
| 250 if not self._FindDevice(): | |
| 251 self._NoDeviceFound() | |
| 252 | |
| 253 def _FindDevice(self): | |
| 254 """Find which device to use.""" | |
| 255 logging.info('Finding device to run tests on.') | |
| 256 device_list = self._GetDeviceList() | |
| 257 random.shuffle(device_list) | |
| 258 for device in device_list: | |
| 259 if device['os_name'] != self._device_type: | |
| 260 continue | |
| 261 if self._remote_device and device['name'] not in self._remote_device: | |
| 262 continue | |
| 263 if (self._remote_device_os | |
| 264 and device['os_version'] not in self._remote_device_os): | |
| 265 continue | |
| 266 if self._device_oem and device['brand'] not in self._device_oem: | |
| 267 continue | |
| 268 if (self._remote_device_minimum_os | |
| 269 and distutils.version.LooseVersion(device['os_version']) | |
| 270 < distutils.version.LooseVersion(self._remote_device_minimum_os)): | |
| 271 continue | |
| 272 if device['has_available_device']: | |
| 273 logging.info('Found device: %s %s', | |
| 274 device['name'], device['os_version']) | |
| 275 self._device = device | |
| 276 return True | |
| 277 return False | |
| 278 | |
| 279 def _FindDeviceWithTimeout(self): | |
| 280 """Find which device to use with timeout.""" | |
| 281 timeout_retry.WaitFor(self._FindDevice, wait_period=1) | |
| 282 | |
| 283 def _PrintAvailableDevices(self, device_list): | |
| 284 def compare_devices(a, b): | |
| 285 for key in ('os_version', 'name'): | |
| 286 c = cmp(a[key], b[key]) | |
| 287 if c: | |
| 288 return c | |
| 289 return 0 | |
| 290 | |
| 291 logging.critical('Available %s Devices:', self._device_type) | |
| 292 logging.critical( | |
| 293 ' %s %s %s %s %s', | |
| 294 'OS'.ljust(10), | |
| 295 'Device Name'.ljust(30), | |
| 296 'Available'.ljust(10), | |
| 297 'Busy'.ljust(10), | |
| 298 'All'.ljust(10)) | |
| 299 devices = (d for d in device_list if d['os_name'] == self._device_type) | |
| 300 for d in sorted(devices, compare_devices): | |
| 301 logging.critical( | |
| 302 ' %s %s %s %s %s', | |
| 303 d['os_version'].ljust(10), | |
| 304 d['name'].ljust(30), | |
| 305 str(d['available_devices_count']).ljust(10), | |
| 306 str(d['busy_devices_count']).ljust(10), | |
| 307 str(d['all_devices_count']).ljust(10)) | |
| 308 | |
| 309 def _GetDeviceList(self): | |
| 310 with appurify_sanitized.SanitizeLogging(self._verbose_count, | |
| 311 logging.WARNING): | |
| 312 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) | |
| 313 remote_device_helper.TestHttpResponse(dev_list_res, | |
| 314 'Unable to generate access token.') | |
| 315 return dev_list_res.json()['response'] | |
| 316 | |
| 317 def _NoDeviceFound(self): | |
| 318 self._PrintAvailableDevices(self._GetDeviceList()) | |
| 319 raise remote_device_helper.RemoteDeviceError( | |
| 320 'No device found.', is_infra_error=True) | |
| 321 | |
| 322 @property | |
| 323 def collect(self): | |
| 324 return self._collect | |
| 325 | |
| 326 @property | |
| 327 def device_type_id(self): | |
| 328 return self._device['device_type_id'] | |
| 329 | |
| 330 @property | |
| 331 def network_config(self): | |
| 332 return self._network_config | |
| 333 | |
| 334 @property | |
| 335 def results_path(self): | |
| 336 return self._results_path | |
| 337 | |
| 338 @property | |
| 339 def runner_package(self): | |
| 340 return self._runner_package | |
| 341 | |
| 342 @property | |
| 343 def runner_type(self): | |
| 344 return self._runner_type | |
| 345 | |
| 346 @property | |
| 347 def timeouts(self): | |
| 348 return self._timeouts | |
| 349 | |
| 350 @property | |
| 351 def token(self): | |
| 352 return self._access_token | |
| 353 | |
| 354 @property | |
| 355 def trigger(self): | |
| 356 return self._trigger | |
| 357 | |
| 358 @property | |
| 359 def verbose_count(self): | |
| 360 return self._verbose_count | |
| 361 | |
| 362 @property | |
| 363 def device_type(self): | |
| 364 return self._device_type | |
| OLD | NEW |