OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
jbudorick
2014/11/30 22:57:02
These need to be in build/android/pylib/remote/dev
rnephew (Reviews Here)
2014/12/02 19:47:48
Done.
| |
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.device import remote_device_utils | |
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 #TODO(rnephew): store only needed options | |
31 # list of files checked for this: self | |
32 if not options.api_key: | |
33 error_func('Must set api key') | |
34 if not options.api_secret: | |
35 error_func('Must set api secret') | |
36 self._api_key = options.api_key | |
37 self._api_secret = options.api_secret | |
38 self._api_protocol = options.api_protocol | |
39 self._api_address = options.api_address | |
40 self._device_list = '' | |
41 self._access_token = '' | |
42 self._results_path = options.results_path | |
43 self._remote_device = options.remote_device | |
44 self._remote_device_os = options.remote_device_os | |
45 self._runner_package = options.runner_package | |
46 self._runner_type = options.runner_type | |
47 self._device = '' | |
48 | |
49 def SetUp(self): | |
50 """Set up the test environment.""" | |
51 os.environ['APPURIFY_API_PROTO'] = self._api_protocol | |
52 os.environ['APPURIFY_API_HOST'] = self._api_address | |
53 os.environ['APPURIFY_API_PORT'] = '80' | |
54 self.GetAccessToken() | |
55 self.GetDeviceList() | |
56 self._device = self.SelectDevice() | |
57 | |
58 def TearDown(self): | |
59 """Teardown the test environment.""" | |
60 self.RevokeAccessToken() | |
61 | |
62 def __enter__(self): | |
63 """Runs when entering with with keyword.""" | |
64 self.SetUp() | |
65 return self | |
66 | |
67 def __exit__(self, exc_type, exc_val, exc_tb): | |
68 """Runs when exiting with with keyword.""" | |
69 self.TearDown() | |
70 | |
71 def GetAccessToken(self): | |
72 """Generates access token for remote device service.""" | |
73 access_token_results = appurify.api.access_token_generate( | |
74 self._api_key, self._api_secret) | |
75 remote_device_utils.TestHttpResponse(access_token_results, | |
76 'Unable to generate access token.') | |
77 self._access_token = access_token_results.json()['response']['access_token'] | |
78 | |
79 def RevokeAccessToken(self): | |
80 """Destroys access token for remote device service.""" | |
81 revoke_token_results = appurify.api.access_token_revoke(self._access_token) | |
82 remote_device_utils.TestHttpResponse(revoke_token_results, | |
83 'Unable to revoke access token.') | |
84 | |
85 def GetDeviceList(self): | |
jbudorick
2014/11/30 22:57:02
Why are GetDeviceList and SelectDevice separate? D
rnephew (Reviews Here)
2014/12/02 19:47:48
Done.
| |
86 """Get list devices available on remote device service.""" | |
87 dev_list_res = appurify.api.devices_list(self._access_token) | |
88 remote_device_utils.TestHttpResponse(dev_list_res, | |
89 'Unable to generate access token.') | |
90 self._device_list = dev_list_res.json()['response'] | |
91 | |
92 def SelectDevice(self): | |
93 """Select which device to use.""" | |
94 for device in self._device_list: | |
95 if (device['name'] == self._remote_device and | |
96 device['os_version'] == self._remote_device_os): | |
97 return device['device_type_id'] | |
98 raise remote_device_utils.RemoteDeviceError('No device found: %s %s' %( | |
99 self._remote_device, self._remote_device_os)) | |
100 | |
101 @property | |
102 def device(self): | |
103 return self._device | |
104 | |
105 @property | |
106 def token(self): | |
107 return self._access_token | |
108 | |
109 @property | |
110 def results_path(self): | |
111 return self._results_path | |
112 | |
113 @property | |
114 def runner_type(self): | |
115 return self._runner_type | |
116 | |
117 @property | |
118 def runner_package(self): | |
119 return self._runner_package | |
OLD | NEW |