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 os | |
8 import sys | |
9 | |
10 from pylib import constants | |
11 from pylib.base import environment | |
12 from pylib.remote.device import remote_device_helper | |
13 | |
14 sys.path.append(os.path.join( | |
15 constants.DIR_SOURCE_ROOT, 'third_party', 'appurify-python', 'src')) | |
16 import appurify.api | |
17 | |
18 | |
19 class RemoteDeviceEnvironment(environment.Environment): | |
20 """An environment for running on remote devices.""" | |
21 | |
22 def __init__(self, options, error_func): | |
23 """Constructor. | |
24 | |
25 Args: | |
26 options: Command line options. | |
27 error_func: error to show when using bad command line options. | |
28 """ | |
29 super(RemoteDeviceEnvironment, self).__init__() | |
30 if options.api_key_file: | |
jbudorick
2014/12/03 22:46:09
api_key_file and api_key should be mutually exclus
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
31 with open(options.api_key_file) as api_key_file: | |
32 self._api_key = api_key_file.read().strip() | |
33 if options.api_secret_file: | |
jbudorick
2014/12/03 22:46:09
Same re mutual exclusivity.
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
34 with open(options.api_secret_file) as api_secret_file: | |
35 self._api_secret = api_secret_file.read().strip() | |
36 if options.api_key: | |
37 self._api_key = options.api_key | |
38 if options.api_secret: | |
39 self._api_secret = options.api_secret | |
40 if not self._api_key: | |
41 error_func('Must set api key with --api-key or --api-key-file') | |
42 if not self._api_secret: | |
43 error_func('Must set api secret with --api-secret or --api-secret-file') | |
44 self._api_protocol = options.api_protocol | |
45 self._api_address = options.api_address | |
46 self._access_token = '' | |
47 self._results_path = options.results_path | |
48 self._remote_device = options.remote_device | |
49 self._remote_device_os = options.remote_device_os | |
50 self._runner_package = options.runner_package | |
51 self._runner_type = options.runner_type | |
52 self._device = '' | |
53 | |
54 def SetUp(self): | |
55 """Set up the test environment.""" | |
56 os.environ['APPURIFY_API_PROTO'] = self._api_protocol | |
57 os.environ['APPURIFY_API_HOST'] = self._api_address | |
58 os.environ['APPURIFY_API_PORT'] = '80' | |
jbudorick
2014/12/03 22:46:09
This shouldn't be hard-coded.
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
59 self._GetAccessToken() | |
60 self._device = self._SelectDevice() | |
61 | |
62 def TearDown(self): | |
63 """Teardown the test environment.""" | |
64 self._RevokeAccessToken() | |
65 | |
66 def __enter__(self): | |
67 """Runs when entering with with keyword.""" | |
68 self.SetUp() | |
69 return self | |
70 | |
71 def __exit__(self, exc_type, exc_val, exc_tb): | |
72 """Runs when exiting with with keyword.""" | |
73 self.TearDown() | |
74 | |
75 def _GetAccessToken(self): | |
76 """Generates access token for remote device service.""" | |
77 access_token_results = appurify.api.access_token_generate( | |
78 self._api_key, self._api_secret) | |
79 remote_device_helper.TestHttpResponse(access_token_results, | |
80 'Unable to generate access token.') | |
81 self._access_token = access_token_results.json()['response']['access_token'] | |
82 | |
83 def _RevokeAccessToken(self): | |
84 """Destroys access token for remote device service.""" | |
85 revoke_token_results = appurify.api.access_token_revoke(self._access_token) | |
86 remote_device_helper.TestHttpResponse(revoke_token_results, | |
87 'Unable to revoke access token.') | |
88 | |
89 def _SelectDevice(self): | |
90 """Select which device to use.""" | |
91 dev_list_res = appurify.api.devices_list(self._access_token) | |
92 remote_device_helper.TestHttpResponse(dev_list_res, | |
93 'Unable to generate access token.') | |
94 device_list = dev_list_res.json()['response'] | |
95 for device in device_list: | |
96 if (device['name'] == self._remote_device and | |
97 device['os_version'] == self._remote_device_os): | |
98 return device['device_type_id'] | |
99 raise remote_device_helper.RemoteDeviceError('No device found: %s %s' %( | |
100 self._remote_device, self._remote_device_os)) | |
101 | |
102 @property | |
103 def device(self): | |
104 return self._device | |
105 | |
106 @property | |
107 def token(self): | |
108 return self._access_token | |
109 | |
110 @property | |
111 def results_path(self): | |
112 return self._results_path | |
113 | |
114 @property | |
115 def runner_type(self): | |
116 return self._runner_type | |
117 | |
118 @property | |
119 def runner_package(self): | |
120 return self._runner_package | |
OLD | NEW |