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 | |
7 import logging | 9 import logging |
8 import os | 10 import os |
9 import random | 11 import random |
10 import sys | 12 import sys |
11 | 13 |
12 from pylib import constants | 14 from pylib import constants |
13 from pylib.base import environment | 15 from pylib.base import environment |
14 from pylib.remote.device import appurify_sanitized | 16 from pylib.remote.device import appurify_sanitized |
15 from pylib.remote.device import remote_device_helper | 17 from pylib.remote.device import remote_device_helper |
16 | 18 |
17 class RemoteDeviceEnvironment(environment.Environment): | 19 class RemoteDeviceEnvironment(environment.Environment): |
18 """An environment for running on remote devices.""" | 20 """An environment for running on remote devices.""" |
19 | 21 |
20 _ENV_KEY = 'env' | 22 _ENV_KEY = 'env' |
21 _DEVICE_KEY = 'device' | 23 _DEVICE_KEY = 'device' |
22 | 24 |
23 def __init__(self, args, error_func): | 25 def __init__(self, args, error_func): |
24 """Constructor. | 26 """Constructor. |
25 | 27 |
26 Args: | 28 Args: |
27 args: Command line arguments. | 29 args: Command line arguments. |
28 error_func: error to show when using bad command line arguments. | 30 error_func: error to show when using bad command line arguments. |
29 """ | 31 """ |
30 super(RemoteDeviceEnvironment, self).__init__() | 32 super(RemoteDeviceEnvironment, self).__init__() |
31 | 33 self._access_token = None |
32 if args.api_key_file: | 34 self._device = None |
33 with open(args.api_key_file) as api_key_file: | 35 self._device_type = args.device_type |
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 | 36 self._verbose_count = args.verbose_count |
68 self._device_type = args.device_type | |
69 self._timeouts = { | 37 self._timeouts = { |
70 'queueing': 60 * 10, | 38 'queueing': 60 * 10, |
71 'installing': 60 * 10, | 39 'installing': 60 * 10, |
72 'in-progress': 60 * 30, | 40 'in-progress': 60 * 30, |
73 'unknown': 60 * 5 | 41 'unknown': 60 * 5 |
74 } | 42 } |
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) | |
jbudorick
2015/01/30 14:24:59
Shouldn't we only do the device_json parsing if --
rnephew (Wrong account)
2015/01/30 17:51:10
Talked offline about why Im doing this.
| |
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 if args.api_key_file: | |
85 with open(args.api_key_file) as api_key_file: | |
86 temp_key = api_key_file.read().strip() | |
87 if self._api_key and temp_key != self._api_key: | |
88 logging.info('Overriding api key.') | |
89 self._api_key = temp_key | |
90 elif args.api_key: | |
91 if self._api_key and self._api_key != args.api_key: | |
92 logging.info('Overriding api key.') | |
93 self._api_key = args.api_key | |
94 | |
95 if args.api_secret_file: | |
96 with open(args.api_secret_file) as api_secret_file: | |
97 temp_secret = api_secret_file.read().strip() | |
98 if self._api_secret and temp_secret != self._api_secret: | |
99 logging.info('Overriding api secret.') | |
100 self._api_secret = temp_secret | |
101 elif args.api_secret: | |
102 if self._api_secret and self._api_secret != args.api_secret: | |
103 logging.info('Overriding api secret.') | |
104 self._api_secret = args.api_secret | |
105 | |
106 if args.api_address: | |
107 if self._api_address and self._api_address != args.api_address: | |
jbudorick
2015/01/30 14:24:59
This blocok is pretty repetitive. Can we make it i
jbudorick
2015/01/30 14:26:32
Wow, "blocok" ... yikes.
rnephew (Wrong account)
2015/01/30 17:51:10
Done.
| |
108 logging.info('Overriding api address from %s to %s.', | |
109 self._api_address, args.api_address) | |
110 self._api_address = args.api_address | |
111 if args.api_port: | |
112 if self._api_port and self._api_port != args.api_port: | |
113 logging.info('Overriding api port from %s to %s.', | |
114 self._api_port, args.api_port) | |
115 self._api_port = args.api_port | |
116 if args.api_protocol: | |
117 if self._api_protocol and self._api_protocol != args.api_protocol: | |
118 logging.info('Overriding api protocol from %s to %s.', | |
119 self._api_protocol, args.api_protocol) | |
120 self._api_protocol = args.api_protocol | |
121 if args.remote_device: | |
122 if self._remote_device and self._remote_device != args.remote_device: | |
123 logging.info('Overriding remote device from %s to %s.', | |
124 self._remote_device, args.remote_device) | |
125 self._remote_device = args.remote_device | |
126 if args.remote_device_minimum_os: | |
127 if (self._remote_device_minimum_os | |
128 and self._remote_device_minimum_os != args.remote_device_minimum_os): | |
129 logging.info('Overriding remote device minimum os from %s ' | |
130 'to %s.', self._remote_device_minimum_os, | |
131 args.remote_device_minimum_os) | |
132 self._remote_device_minimum_os = args.remote_device_minimum_os | |
133 if args.remote_device_os: | |
134 if( self._remote_device_os | |
jbudorick
2015/01/30 14:24:59
nit: if (self._remote_device_os
rnephew (Wrong account)
2015/01/30 17:51:10
Done.
| |
135 and self._remote_device_os != args.remote_device_os): | |
136 logging.info('Overriding remote device os from %s to %s.', | |
137 self._remote_device_os, args.remote_device_os) | |
138 self._remote_device_os = args.remote_device_os | |
139 if args.device_oem: | |
140 if self._device_oem and self._device_oem != args.device_oem: | |
141 logging.info('Overriding device oem from %s to %s.', | |
jbudorick
2015/01/30 14:24:59
nit: one space after from
rnephew (Wrong account)
2015/01/30 17:51:10
Done.
| |
142 self._device_oem, args.device_oem) | |
143 self._device_oem = args.device_oem | |
144 if args.device_type: | |
145 if self._device_type and self._device_type != args.device_type: | |
146 logging.info('Overriding device type from %s to %s.', | |
147 self._device_type, args.device_type) | |
148 self._device_type = args.device_type | |
149 if args.results_path: | |
150 if self._results_path and self._results_path != args.results_path: | |
151 logging.info('Overriding results path from %s to %s.', | |
152 self._results_path, args.results_path) | |
153 self._results_path = args.results_path | |
154 if args.runner_package: | |
155 if self._runner_package and self._runner_package != args.runner_package: | |
156 logging.info('Overriding runner package from %s to %s.', | |
157 self._runner_package, args.runner_package) | |
158 self._runner_package = args.runner_package | |
159 if args.runner_type: | |
160 if self._runner_type and self._runner_type != args.runner_type: | |
161 logging.info('Overriding runner type from %s to %s.', | |
162 self._runner_type, args.runner_type) | |
163 self._runner_type = args.runner_type | |
164 | |
165 if not self._api_address: | |
166 error_func('Must set api address with --api-address' | |
167 ' or in --remote-device-file.') | |
168 if not self._api_key: | |
169 error_func('Must set api key with --api-key, --api-key-file' | |
170 ' or in --remote-device-file') | |
171 if not self._api_port: | |
172 error_func('Must set api port with --api-port' | |
173 ' or in --remote-device-file') | |
174 if not self._api_protocol: | |
175 error_func('Must set api protocol with --api-protocol' | |
176 ' or in --remote-device-file. Example: http') | |
177 if not self._api_secret: | |
178 error_func('Must set api secret with --api-secret, --api-secret-file' | |
179 ' or in --remote-device-file') | |
180 | |
181 logging.info('Api address: %s', self._api_address) | |
182 logging.info('Api port: %s', self._api_port) | |
183 logging.info('Api protocol: %s', self._api_protocol) | |
184 logging.info('Remote device: %s', self._remote_device) | |
185 logging.info('Remote device minimum os: %s', | |
jbudorick
2015/01/30 14:24:59
nit: OS
rnephew (Wrong account)
2015/01/30 17:51:10
Done.
| |
186 self._remote_device_minimum_os) | |
187 logging.info('Remote device os: %s', self._remote_device_os) | |
jbudorick
2015/01/30 14:24:59
same nit
rnephew (Wrong account)
2015/01/30 17:51:10
Done.
| |
188 logging.info('Remote device OEM: %s', self._device_oem) | |
189 logging.info('Remote device type: %s', self._device_type) | |
190 logging.info('Results Path: %s', self._results_path) | |
191 logging.info('Runner package: %s', self._runner_package) | |
192 logging.info('Runner type: %s', self._runner_type) | |
193 logging.info('Timeouts: %s', self._timeouts) | |
75 | 194 |
76 if not args.trigger and not args.collect: | 195 if not args.trigger and not args.collect: |
77 self._trigger = True | 196 self._trigger = True |
78 self._collect = True | 197 self._collect = True |
79 else: | 198 else: |
80 self._trigger = args.trigger | 199 self._trigger = args.trigger |
81 self._collect = args.collect | 200 self._collect = args.collect |
82 | 201 |
83 def SetUp(self): | 202 def SetUp(self): |
84 """Set up the test environment.""" | 203 """Set up the test environment.""" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 with appurify_sanitized.SanitizeLogging(self._verbose_count, | 262 with appurify_sanitized.SanitizeLogging(self._verbose_count, |
144 logging.WARNING): | 263 logging.WARNING): |
145 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) | 264 dev_list_res = appurify_sanitized.api.devices_list(self._access_token) |
146 remote_device_helper.TestHttpResponse(dev_list_res, | 265 remote_device_helper.TestHttpResponse(dev_list_res, |
147 'Unable to generate access token.') | 266 'Unable to generate access token.') |
148 device_list = dev_list_res.json()['response'] | 267 device_list = dev_list_res.json()['response'] |
149 random.shuffle(device_list) | 268 random.shuffle(device_list) |
150 for device in device_list: | 269 for device in device_list: |
151 if device['os_name'] != self._device_type: | 270 if device['os_name'] != self._device_type: |
152 continue | 271 continue |
153 if self._remote_device and device['name'] != self._remote_device: | 272 if self._remote_device and device['name'] not in self._remote_device: |
154 continue | 273 continue |
155 if (self._remote_device_os | 274 if (self._remote_device_os |
156 and device['os_version'] != self._remote_device_os): | 275 and device['os_version'] not in self._remote_device_os): |
276 continue | |
277 if self._device_oem and device['brand'] not in self._device_oem: | |
278 continue | |
279 if (self._remote_device_minimum_os | |
280 and distutils.version.LooseVersion(device['os_version']) | |
281 < distutils.version.LooseVersion(self._remote_device_minimum_os)): | |
157 continue | 282 continue |
158 if ((self._remote_device and self._remote_device_os) | 283 if ((self._remote_device and self._remote_device_os) |
159 or device['available_devices_count']): | 284 or device['available_devices_count']): |
160 logging.info('Found device: %s %s', | 285 logging.info('Found device: %s %s', |
161 device['name'], device['os_version']) | 286 device['name'], device['os_version']) |
162 return device | 287 return device |
163 self._NoDeviceFound(device_list) | 288 self._NoDeviceFound(device_list) |
164 | 289 |
165 def _PrintAvailableDevices(self, device_list): | 290 def _PrintAvailableDevices(self, device_list): |
166 def compare_devices(a,b): | 291 def compare_devices(a,b): |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 def trigger(self): | 341 def trigger(self): |
217 return self._trigger | 342 return self._trigger |
218 | 343 |
219 @property | 344 @property |
220 def verbose_count(self): | 345 def verbose_count(self): |
221 return self._verbose_count | 346 return self._verbose_count |
222 | 347 |
223 @property | 348 @property |
224 def device_type(self): | 349 def device_type(self): |
225 return self._device_type | 350 return self._device_type |
OLD | NEW |