OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Environment setup and teardown for remote devices.""" | 5 """Environment setup and teardown for remote devices.""" |
6 | 6 |
7 import distutils.version | 7 import distutils.version |
8 import json | 8 import json |
9 import logging | 9 import logging |
10 import os | 10 import os |
11 import random | 11 import random |
12 import sys | 12 import sys |
13 | 13 |
14 from pylib import constants | 14 from pylib import constants |
15 from pylib.base import environment | 15 from pylib.base import environment |
16 from pylib.remote.device import appurify_sanitized | 16 from pylib.remote.device import appurify_sanitized |
17 from pylib.remote.device import remote_device_helper | 17 from pylib.remote.device import remote_device_helper |
| 18 from pylib.utils import timeout_retry |
| 19 from pylib.utils import reraiser_thread |
18 | 20 |
19 class RemoteDeviceEnvironment(environment.Environment): | 21 class RemoteDeviceEnvironment(environment.Environment): |
20 """An environment for running on remote devices.""" | 22 """An environment for running on remote devices.""" |
21 | 23 |
22 _ENV_KEY = 'env' | 24 _ENV_KEY = 'env' |
23 _DEVICE_KEY = 'device' | 25 _DEVICE_KEY = 'device' |
| 26 _DEFAULT_RETRIES = 0 |
24 | 27 |
25 def __init__(self, args, error_func): | 28 def __init__(self, args, error_func): |
26 """Constructor. | 29 """Constructor. |
27 | 30 |
28 Args: | 31 Args: |
29 args: Command line arguments. | 32 args: Command line arguments. |
30 error_func: error to show when using bad command line arguments. | 33 error_func: error to show when using bad command line arguments. |
31 """ | 34 """ |
32 super(RemoteDeviceEnvironment, self).__init__() | 35 super(RemoteDeviceEnvironment, self).__init__() |
33 self._access_token = None | 36 self._access_token = None |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 self._api_key = device_json.get('api_key', None) | 70 self._api_key = device_json.get('api_key', None) |
68 self._api_port = device_json.get('api_port', None) | 71 self._api_port = device_json.get('api_port', None) |
69 self._api_protocol = device_json.get('api_protocol', None) | 72 self._api_protocol = device_json.get('api_protocol', None) |
70 self._api_secret = device_json.get('api_secret', None) | 73 self._api_secret = device_json.get('api_secret', None) |
71 self._device_oem = device_json.get('device_oem', None) | 74 self._device_oem = device_json.get('device_oem', None) |
72 self._device_type = device_json.get('device_type', 'Android') | 75 self._device_type = device_json.get('device_type', 'Android') |
73 self._remote_device = device_json.get('remote_device', None) | 76 self._remote_device = device_json.get('remote_device', None) |
74 self._remote_device_minimum_os = device_json.get( | 77 self._remote_device_minimum_os = device_json.get( |
75 'remote_device_minimum_os', None) | 78 'remote_device_minimum_os', None) |
76 self._remote_device_os = device_json.get('remote_device_os', None) | 79 self._remote_device_os = device_json.get('remote_device_os', None) |
| 80 self._remote_device_timeout = device_json.get('remote_device_timeout', None) |
77 self._results_path = device_json.get('results_path', None) | 81 self._results_path = device_json.get('results_path', None) |
78 self._runner_package = device_json.get('runner_package', None) | 82 self._runner_package = device_json.get('runner_package', None) |
79 self._runner_type = device_json.get('runner_type', None) | 83 self._runner_type = device_json.get('runner_type', None) |
80 if 'timeouts' in device_json: | 84 if 'timeouts' in device_json: |
81 for key in device_json['timeouts']: | 85 for key in device_json['timeouts']: |
82 self._timeouts[key] = device_json['timeouts'][key] | 86 self._timeouts[key] = device_json['timeouts'][key] |
83 | 87 |
84 def command_line_override( | 88 def command_line_override( |
85 file_value, cmd_line_value, desc, print_value=True): | 89 file_value, cmd_line_value, desc, print_value=True): |
86 if cmd_line_value: | 90 if cmd_line_value: |
(...skipping 16 matching lines...) Expand all Loading... |
103 self._device_oem, args.device_oem, 'device_oem') | 107 self._device_oem, args.device_oem, 'device_oem') |
104 self._device_type = command_line_override( | 108 self._device_type = command_line_override( |
105 self._device_type, args.device_type, 'device_type') | 109 self._device_type, args.device_type, 'device_type') |
106 self._remote_device = command_line_override( | 110 self._remote_device = command_line_override( |
107 self._remote_device, args.remote_device, 'remote_device') | 111 self._remote_device, args.remote_device, 'remote_device') |
108 self._remote_device_minimum_os = command_line_override( | 112 self._remote_device_minimum_os = command_line_override( |
109 self._remote_device_minimum_os, args.remote_device_minimum_os, | 113 self._remote_device_minimum_os, args.remote_device_minimum_os, |
110 'remote_device_minimum_os') | 114 'remote_device_minimum_os') |
111 self._remote_device_os = command_line_override( | 115 self._remote_device_os = command_line_override( |
112 self._remote_device_os, args.remote_device_os, 'remote_device_os') | 116 self._remote_device_os, args.remote_device_os, 'remote_device_os') |
| 117 self._remote_device_timeout = command_line_override( |
| 118 self._remote_device_timeout, args.remote_device_timeout, |
| 119 'remote_device_timeout') |
113 self._results_path = command_line_override( | 120 self._results_path = command_line_override( |
114 self._results_path, args.results_path, 'results_path') | 121 self._results_path, args.results_path, 'results_path') |
115 self._runner_package = command_line_override( | 122 self._runner_package = command_line_override( |
116 self._runner_package, args.runner_package, 'runner_package') | 123 self._runner_package, args.runner_package, 'runner_package') |
117 self._runner_type = command_line_override( | 124 self._runner_type = command_line_override( |
118 self._runner_type, args.runner_type, 'runner_type') | 125 self._runner_type, args.runner_type, 'runner_type') |
119 | 126 |
120 if args.api_key_file: | 127 if args.api_key_file: |
121 with open(args.api_key_file) as api_key_file: | 128 with open(args.api_key_file) as api_key_file: |
122 temp_key = api_key_file.read().strip() | 129 temp_key = api_key_file.read().strip() |
(...skipping 28 matching lines...) Expand all Loading... |
151 | 158 |
152 logging.info('Api address: %s', self._api_address) | 159 logging.info('Api address: %s', self._api_address) |
153 logging.info('Api port: %s', self._api_port) | 160 logging.info('Api port: %s', self._api_port) |
154 logging.info('Api protocol: %s', self._api_protocol) | 161 logging.info('Api protocol: %s', self._api_protocol) |
155 logging.info('Remote device: %s', self._remote_device) | 162 logging.info('Remote device: %s', self._remote_device) |
156 logging.info('Remote device minimum OS: %s', | 163 logging.info('Remote device minimum OS: %s', |
157 self._remote_device_minimum_os) | 164 self._remote_device_minimum_os) |
158 logging.info('Remote device OS: %s', self._remote_device_os) | 165 logging.info('Remote device OS: %s', self._remote_device_os) |
159 logging.info('Remote device OEM: %s', self._device_oem) | 166 logging.info('Remote device OEM: %s', self._device_oem) |
160 logging.info('Remote device type: %s', self._device_type) | 167 logging.info('Remote device type: %s', self._device_type) |
| 168 logging.info('Remote device timout: %s', self._remote_device_timeout) |
161 logging.info('Results Path: %s', self._results_path) | 169 logging.info('Results Path: %s', self._results_path) |
162 logging.info('Runner package: %s', self._runner_package) | 170 logging.info('Runner package: %s', self._runner_package) |
163 logging.info('Runner type: %s', self._runner_type) | 171 logging.info('Runner type: %s', self._runner_type) |
164 logging.info('Timeouts: %s', self._timeouts) | 172 logging.info('Timeouts: %s', self._timeouts) |
165 | 173 |
166 if not args.trigger and not args.collect: | 174 if not args.trigger and not args.collect: |
167 self._trigger = True | 175 self._trigger = True |
168 self._collect = True | 176 self._collect = True |
169 else: | 177 else: |
170 self._trigger = args.trigger | 178 self._trigger = args.trigger |
171 self._collect = args.collect | 179 self._collect = args.collect |
172 | 180 |
173 def SetUp(self): | 181 def SetUp(self): |
174 """Set up the test environment.""" | 182 """Set up the test environment.""" |
175 os.environ['APPURIFY_API_PROTO'] = self._api_protocol | 183 os.environ['APPURIFY_API_PROTO'] = self._api_protocol |
176 os.environ['APPURIFY_API_HOST'] = self._api_address | 184 os.environ['APPURIFY_API_HOST'] = self._api_address |
177 os.environ['APPURIFY_API_PORT'] = self._api_port | 185 os.environ['APPURIFY_API_PORT'] = self._api_port |
178 self._GetAccessToken() | 186 self._GetAccessToken() |
179 if self._trigger: | 187 if self._trigger: |
180 self._device = self._SelectDevice() | 188 self._SelectDevice() |
181 | 189 |
182 def TearDown(self): | 190 def TearDown(self): |
183 """Teardown the test environment.""" | 191 """Teardown the test environment.""" |
184 self._RevokeAccessToken() | 192 self._RevokeAccessToken() |
185 | 193 |
186 def __enter__(self): | 194 def __enter__(self): |
187 """Set up the test run when used as a context manager.""" | 195 """Set up the test run when used as a context manager.""" |
188 try: | 196 try: |
189 self.SetUp() | 197 self.SetUp() |
190 return self | 198 return self |
(...skipping 30 matching lines...) Expand all Loading... |
221 """Destroys access token for remote device service.""" | 229 """Destroys access token for remote device service.""" |
222 logging.info('Revoking remote service access token') | 230 logging.info('Revoking remote service access token') |
223 with appurify_sanitized.SanitizeLogging(self._verbose_count, | 231 with appurify_sanitized.SanitizeLogging(self._verbose_count, |
224 logging.WARNING): | 232 logging.WARNING): |
225 revoke_token_results = appurify_sanitized.api.access_token_revoke( | 233 revoke_token_results = appurify_sanitized.api.access_token_revoke( |
226 self._access_token) | 234 self._access_token) |
227 remote_device_helper.TestHttpResponse(revoke_token_results, | 235 remote_device_helper.TestHttpResponse(revoke_token_results, |
228 'Unable to revoke access token.') | 236 'Unable to revoke access token.') |
229 | 237 |
230 def _SelectDevice(self): | 238 def _SelectDevice(self): |
231 """Select which device to use.""" | 239 if self._remote_device_timeout: |
| 240 try: |
| 241 timeout_retry.Run(self._FindDeviceWithTimeout, |
| 242 self._remote_device_timeout, self._DEFAULT_RETRIES) |
| 243 except reraiser_thread.TimeoutError: |
| 244 self._NoDeviceFound() |
| 245 else: |
| 246 if not self._FindDevice(): |
| 247 self._NoDeviceFound() |
| 248 |
| 249 def _FindDevice(self): |
| 250 """Find which device to use.""" |
232 logging.info('Finding device to run tests on.') | 251 logging.info('Finding device to run tests on.') |
233 with appurify_sanitized.SanitizeLogging(self._verbose_count, | 252 device_list = self._GetDeviceList() |
234 logging.WARNING): | |
235 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) | |
236 remote_device_helper.TestHttpResponse(dev_list_res, | |
237 'Unable to generate access token.') | |
238 device_list = dev_list_res.json()['response'] | |
239 random.shuffle(device_list) | 253 random.shuffle(device_list) |
240 for device in device_list: | 254 for device in device_list: |
241 if device['os_name'] != self._device_type: | 255 if device['os_name'] != self._device_type: |
242 continue | 256 continue |
243 if self._remote_device and device['name'] not in self._remote_device: | 257 if self._remote_device and device['name'] not in self._remote_device: |
244 continue | 258 continue |
245 if (self._remote_device_os | 259 if (self._remote_device_os |
246 and device['os_version'] not in self._remote_device_os): | 260 and device['os_version'] not in self._remote_device_os): |
247 continue | 261 continue |
248 if self._device_oem and device['brand'] not in self._device_oem: | 262 if self._device_oem and device['brand'] not in self._device_oem: |
249 continue | 263 continue |
250 if (self._remote_device_minimum_os | 264 if (self._remote_device_minimum_os |
251 and distutils.version.LooseVersion(device['os_version']) | 265 and distutils.version.LooseVersion(device['os_version']) |
252 < distutils.version.LooseVersion(self._remote_device_minimum_os)): | 266 < distutils.version.LooseVersion(self._remote_device_minimum_os)): |
253 continue | 267 continue |
254 if ((self._remote_device and self._remote_device_os) | 268 if device['has_available_device']: |
255 or device['available_devices_count']): | |
256 logging.info('Found device: %s %s', | 269 logging.info('Found device: %s %s', |
257 device['name'], device['os_version']) | 270 device['name'], device['os_version']) |
258 return device | 271 self._device = device |
259 self._NoDeviceFound(device_list) | 272 return True |
| 273 return False |
| 274 |
| 275 def _FindDeviceWithTimeout(self): |
| 276 """Find which device to use with timeout.""" |
| 277 timeout_retry.WaitFor(self._FindDevice, wait_period=1) |
260 | 278 |
261 def _PrintAvailableDevices(self, device_list): | 279 def _PrintAvailableDevices(self, device_list): |
262 def compare_devices(a,b): | 280 def compare_devices(a,b): |
263 for key in ('os_version', 'name'): | 281 for key in ('os_version', 'name'): |
264 c = cmp(a[key], b[key]) | 282 c = cmp(a[key], b[key]) |
265 if c: | 283 if c: |
266 return c | 284 return c |
267 return 0 | 285 return 0 |
268 | 286 |
269 logging.critical('Available %s Devices:', self._device_type) | 287 logging.critical('Available %s Devices:', self._device_type) |
| 288 logging.critical(' %s %s %s', 'OS'.ljust(7), |
| 289 'Device Name'.ljust(20), '# Available') |
270 devices = (d for d in device_list if d['os_name'] == self._device_type) | 290 devices = (d for d in device_list if d['os_name'] == self._device_type) |
271 for d in sorted(devices, compare_devices): | 291 for d in sorted(devices, compare_devices): |
272 logging.critical(' %s %s', d['os_version'].ljust(7), d['name']) | 292 logging.critical(' %s %s %s', d['os_version'].ljust(7), |
| 293 d['name'].ljust(20), d['available_devices_count']) |
273 | 294 |
274 def _NoDeviceFound(self, device_list): | 295 def _GetDeviceList(self): |
275 self._PrintAvailableDevices(device_list) | 296 with appurify_sanitized.SanitizeLogging(self._verbose_count, |
| 297 logging.WARNING): |
| 298 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) |
| 299 remote_device_helper.TestHttpResponse(dev_list_res, |
| 300 'Unable to generate access token.') |
| 301 return dev_list_res.json()['response'] |
| 302 |
| 303 def _NoDeviceFound(self): |
| 304 self._PrintAvailableDevices(self._GetDeviceList()) |
276 raise remote_device_helper.RemoteDeviceError('No device found.') | 305 raise remote_device_helper.RemoteDeviceError('No device found.') |
277 | 306 |
278 @property | 307 @property |
279 def collect(self): | 308 def collect(self): |
280 return self._collect | 309 return self._collect |
281 | 310 |
282 @property | 311 @property |
283 def device_type_id(self): | 312 def device_type_id(self): |
284 return self._device['device_type_id'] | 313 return self._device['device_type_id'] |
285 | 314 |
(...skipping 26 matching lines...) Expand all Loading... |
312 def trigger(self): | 341 def trigger(self): |
313 return self._trigger | 342 return self._trigger |
314 | 343 |
315 @property | 344 @property |
316 def verbose_count(self): | 345 def verbose_count(self): |
317 return self._verbose_count | 346 return self._verbose_count |
318 | 347 |
319 @property | 348 @property |
320 def device_type(self): | 349 def device_type(self): |
321 return self._device_type | 350 return self._device_type |
OLD | NEW |