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 | |
8 import json | |
9 import logging | 7 import logging |
10 import os | 8 import os |
11 import random | 9 import random |
12 import sys | 10 import sys |
13 | 11 |
14 from pylib import constants | 12 from pylib import constants |
15 from pylib.base import environment | 13 from pylib.base import environment |
16 from pylib.remote.device import appurify_sanitized | 14 from pylib.remote.device import appurify_sanitized |
17 from pylib.remote.device import remote_device_helper | 15 from pylib.remote.device import remote_device_helper |
18 | 16 |
19 class RemoteDeviceEnvironment(environment.Environment): | 17 class RemoteDeviceEnvironment(environment.Environment): |
20 """An environment for running on remote devices.""" | 18 """An environment for running on remote devices.""" |
21 | 19 |
22 _ENV_KEY = 'env' | 20 _ENV_KEY = 'env' |
23 _DEVICE_KEY = 'device' | 21 _DEVICE_KEY = 'device' |
24 | 22 |
25 def __init__(self, args, error_func): | 23 def __init__(self, args, error_func): |
26 """Constructor. | 24 """Constructor. |
27 | 25 |
28 Args: | 26 Args: |
29 args: Command line arguments. | 27 args: Command line arguments. |
30 error_func: error to show when using bad command line arguments. | 28 error_func: error to show when using bad command line arguments. |
31 """ | 29 """ |
32 super(RemoteDeviceEnvironment, self).__init__() | 30 super(RemoteDeviceEnvironment, self).__init__() |
33 self._access_token = None | 31 |
34 self._device = None | 32 if args.api_key_file: |
| 33 with open(args.api_key_file) as api_key_file: |
| 34 self._api_key = api_key_file.read().strip() |
| 35 elif args.api_key: |
| 36 self._api_key = args.api_key |
| 37 else: |
| 38 error_func('Must set api key with --api-key or --api-key-file') |
| 39 |
| 40 if args.api_secret_file: |
| 41 with open(args.api_secret_file) as api_secret_file: |
| 42 self._api_secret = api_secret_file.read().strip() |
| 43 elif args.api_secret: |
| 44 self._api_secret = args.api_secret |
| 45 else: |
| 46 error_func('Must set api secret with --api-secret or --api-secret-file') |
| 47 |
| 48 if not args.api_protocol: |
| 49 error_func('Must set api protocol with --api-protocol. Example: http') |
| 50 self._api_protocol = args.api_protocol |
| 51 |
| 52 if not args.api_address: |
| 53 error_func('Must set api address with --api-address') |
| 54 self._api_address = args.api_address |
| 55 |
| 56 if not args.api_port: |
| 57 error_func('Must set api port with --api-port.') |
| 58 self._api_port = args.api_port |
| 59 |
| 60 self._access_token = '' |
| 61 self._results_path = args.results_path |
| 62 self._remote_device = args.remote_device |
| 63 self._remote_device_os = args.remote_device_os |
| 64 self._runner_package = args.runner_package |
| 65 self._runner_type = args.runner_type |
| 66 self._device = '' |
| 67 self._verbose_count = args.verbose_count |
35 self._device_type = args.device_type | 68 self._device_type = args.device_type |
36 self._verbose_count = args.verbose_count | |
37 self._timeouts = { | 69 self._timeouts = { |
38 'queueing': 60 * 10, | 70 'queueing': 60 * 10, |
39 'installing': 60 * 10, | 71 'installing': 60 * 10, |
40 'in-progress': 60 * 30, | 72 'in-progress': 60 * 30, |
41 'unknown': 60 * 5 | 73 'unknown': 60 * 5 |
42 } | 74 } |
43 # Example config file: | |
44 # { | |
45 # "remote_device": ["Galaxy S4", "Galaxy S3"], | |
46 # "remote_device_os": ["4.4.2", "4.4.4"], | |
47 # "remote_device_minimum_os": "4.4.2", | |
48 # "api_address": "www.example.com", | |
49 # "api_port": "80", | |
50 # "api_protocol": "http", | |
51 # "api_secret": "apisecret", | |
52 # "api_key": "apikey", | |
53 # "timeouts": { | |
54 # "queueing": 600, | |
55 # "installing": 600, | |
56 # "in-progress": 1800, | |
57 # "unknown": 300 | |
58 # } | |
59 # } | |
60 if args.remote_device_file: | |
61 with open(args.remote_device_file) as device_file: | |
62 device_json = json.load(device_file) | |
63 else: | |
64 device_json = {} | |
65 | |
66 self._api_address = device_json.get('api_address', None) | |
67 self._api_key = device_json.get('api_key', None) | |
68 self._api_port = device_json.get('api_port', None) | |
69 self._api_protocol = device_json.get('api_protocol', None) | |
70 self._api_secret = device_json.get('api_secret', None) | |
71 self._remote_device = device_json.get('remote_device', None) | |
72 self._remote_device_minimum_os = device_json.get( | |
73 'remote_device_minimum_os', None) | |
74 self._remote_device_os = device_json.get('remote_device_os', None) | |
75 self._device_oem = device_json.get('device_oem', None) | |
76 self._device_type = device_json.get('device_type', 'Android') | |
77 self._results_path = device_json.get('results_path', None) | |
78 self._runner_package = device_json.get('runner_package', None) | |
79 self._runner_type = device_json.get('runner_type', None) | |
80 if 'timeouts' in device_json: | |
81 for key in device_json['timeouts']: | |
82 self._timeouts[key] = device_json['timeouts'][key] | |
83 | |
84 def command_line_override( | |
85 file_value, cmd_line_value, desc, print_value=True): | |
86 if cmd_line_value: | |
87 if file_value and file_value != cmd_line_value: | |
88 if print_value: | |
89 logging.info('Overriding %s from %s to %s', | |
90 desc, file_value, cmd_line_value) | |
91 else: | |
92 logging.info('overriding %s', desc) | |
93 return cmd_line_value | |
94 return file_value | |
95 | |
96 self._api_address = command_line_override( | |
97 self._api_address, args.api_address, 'api_address') | |
98 self._api_port = command_line_override( | |
99 self._api_port, args.api_port, 'api_port') | |
100 self._api_protocol = command_line_override( | |
101 self._api_protocol, args.api_protocol, 'api_protocol') | |
102 self._device_oem = command_line_override( | |
103 self._device_oem, args.device_oem, 'device_oem') | |
104 self._device_type = command_line_override( | |
105 self._device_type, args.device_type, 'device_type') | |
106 self._remote_device = command_line_override( | |
107 self._remote_device, args.remote_device, 'remote_device') | |
108 self._remote_device_minimum_os = command_line_override( | |
109 self._remote_device_minimum_os, args.remote_device_minimum_os, | |
110 'remote_device_minimum_os') | |
111 self._remote_device_os = command_line_override( | |
112 self._remote_device_os, args.remote_device_os, 'remote_device_os') | |
113 self._results_path = command_line_override( | |
114 self._results_path, args.results_path, 'results_path') | |
115 self._runner_package = command_line_override( | |
116 self._runner_package, args.runner_package, 'runner_package') | |
117 self._runner_type = command_line_override( | |
118 self._runner_type, args.runner_type, 'runner_type') | |
119 | |
120 if args.api_key_file: | |
121 with open(args.api_key_file) as api_key_file: | |
122 temp_key = api_key_file.read().strip() | |
123 self._api_key = command_line_override( | |
124 self._api_key, temp_key, 'api_key', print_value=False) | |
125 self._api_key = command_line_override( | |
126 self._api_key, args.api_key, 'api_key', print_value=False) | |
127 | |
128 if args.api_secret_file: | |
129 with open(args.api_secret_file) as api_secret_file: | |
130 temp_secret = api_secret_file.read().strip() | |
131 self._api_secret = command_line_override( | |
132 self._api_secret, temp_secret, 'api_secret', print_value=False) | |
133 self._api_secret = command_line_override( | |
134 self._api_secret, args.api_secret, 'api_secret', print_value=False) | |
135 | |
136 if not self._api_address: | |
137 error_func('Must set api address with --api-address' | |
138 ' or in --remote-device-file.') | |
139 if not self._api_key: | |
140 error_func('Must set api key with --api-key, --api-key-file' | |
141 ' or in --remote-device-file') | |
142 if not self._api_port: | |
143 error_func('Must set api port with --api-port' | |
144 ' or in --remote-device-file') | |
145 if not self._api_protocol: | |
146 error_func('Must set api protocol with --api-protocol' | |
147 ' or in --remote-device-file. Example: http') | |
148 if not self._api_secret: | |
149 error_func('Must set api secret with --api-secret, --api-secret-file' | |
150 ' or in --remote-device-file') | |
151 | |
152 logging.info('Api address: %s', self._api_address) | |
153 logging.info('Api port: %s', self._api_port) | |
154 logging.info('Api protocol: %s', self._api_protocol) | |
155 logging.info('Remote device: %s', self._remote_device) | |
156 logging.info('Remote device minimum OS: %s', | |
157 self._remote_device_minimum_os) | |
158 logging.info('Remote device OS: %s', self._remote_device_os) | |
159 logging.info('Remote device OEM: %s', self._device_oem) | |
160 logging.info('Remote device type: %s', self._device_type) | |
161 logging.info('Results Path: %s', self._results_path) | |
162 logging.info('Runner package: %s', self._runner_package) | |
163 logging.info('Runner type: %s', self._runner_type) | |
164 logging.info('Timeouts: %s', self._timeouts) | |
165 | 75 |
166 if not args.trigger and not args.collect: | 76 if not args.trigger and not args.collect: |
167 self._trigger = True | 77 self._trigger = True |
168 self._collect = True | 78 self._collect = True |
169 else: | 79 else: |
170 self._trigger = args.trigger | 80 self._trigger = args.trigger |
171 self._collect = args.collect | 81 self._collect = args.collect |
172 | 82 |
173 def SetUp(self): | 83 def SetUp(self): |
174 """Set up the test environment.""" | 84 """Set up the test environment.""" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 with appurify_sanitized.SanitizeLogging(self._verbose_count, | 143 with appurify_sanitized.SanitizeLogging(self._verbose_count, |
234 logging.WARNING): | 144 logging.WARNING): |
235 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) | 145 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) |
236 remote_device_helper.TestHttpResponse(dev_list_res, | 146 remote_device_helper.TestHttpResponse(dev_list_res, |
237 'Unable to generate access token.') | 147 'Unable to generate access token.') |
238 device_list = dev_list_res.json()['response'] | 148 device_list = dev_list_res.json()['response'] |
239 random.shuffle(device_list) | 149 random.shuffle(device_list) |
240 for device in device_list: | 150 for device in device_list: |
241 if device['os_name'] != self._device_type: | 151 if device['os_name'] != self._device_type: |
242 continue | 152 continue |
243 if self._remote_device and device['name'] not in self._remote_device: | 153 if self._remote_device and device['name'] != self._remote_device: |
244 continue | 154 continue |
245 if (self._remote_device_os | 155 if (self._remote_device_os |
246 and device['os_version'] not in self._remote_device_os): | 156 and device['os_version'] != self._remote_device_os): |
247 continue | |
248 if self._device_oem and device['brand'] not in self._device_oem: | |
249 continue | |
250 if (self._remote_device_minimum_os | |
251 and distutils.version.LooseVersion(device['os_version']) | |
252 < distutils.version.LooseVersion(self._remote_device_minimum_os)): | |
253 continue | 157 continue |
254 if ((self._remote_device and self._remote_device_os) | 158 if ((self._remote_device and self._remote_device_os) |
255 or device['available_devices_count']): | 159 or device['available_devices_count']): |
256 logging.info('Found device: %s %s', | 160 logging.info('Found device: %s %s', |
257 device['name'], device['os_version']) | 161 device['name'], device['os_version']) |
258 return device | 162 return device |
259 self._NoDeviceFound(device_list) | 163 self._NoDeviceFound(device_list) |
260 | 164 |
261 def _PrintAvailableDevices(self, device_list): | 165 def _PrintAvailableDevices(self, device_list): |
262 def compare_devices(a,b): | 166 def compare_devices(a,b): |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 def trigger(self): | 216 def trigger(self): |
313 return self._trigger | 217 return self._trigger |
314 | 218 |
315 @property | 219 @property |
316 def verbose_count(self): | 220 def verbose_count(self): |
317 return self._verbose_count | 221 return self._verbose_count |
318 | 222 |
319 @property | 223 @property |
320 def device_type(self): | 224 def device_type(self): |
321 return self._device_type | 225 return self._device_type |
OLD | NEW |