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, args, error_func): | |
23 """Constructor. | |
24 | |
25 Args: | |
26 args: Command line arguments. | |
27 error_func: error to show when using bad command line arguments. | |
28 """ | |
29 super(RemoteDeviceEnvironment, self).__init__() | |
30 | |
31 if args.api_key_file: | |
32 with open(args.api_key_file) as api_key_file: | |
33 self._api_key = api_key_file.read().strip() | |
34 elif args.api_key: | |
35 self._api_key = args.api_key | |
36 else: | |
37 error_func('Must set api key with --api-key or --api-key-file') | |
38 | |
39 if args.api_secret_file: | |
40 with open(args.api_secret_file) as api_secret_file: | |
41 self._api_secret = api_secret_file.read().strip() | |
42 elif args.api_secret: | |
43 self._api_secret = args.api_secret | |
44 else: | |
45 error_func('Must set api secret with --api-secret or --api-secret-file') | |
46 | |
47 self._api_protocol = args.api_protocol | |
48 self._api_address = args.api_address | |
49 self._api_port = args.api_port | |
50 self._access_token = '' | |
51 self._results_path = args.results_path | |
52 self._remote_device = args.remote_device | |
53 self._remote_device_os = args.remote_device_os | |
54 self._runner_package = args.runner_package | |
55 self._runner_type = args.runner_type | |
56 self._device = '' | |
57 self.trigger = args.trigger | |
jbudorick
2014/12/04 21:51:33
Why are these public? If other classes need access
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
58 self.collect = args.collect | |
59 | |
60 def SetUp(self): | |
61 """Set up the test environment.""" | |
62 os.environ['APPURIFY_API_PROTO'] = self._api_protocol | |
63 os.environ['APPURIFY_API_HOST'] = self._api_address | |
64 os.environ['APPURIFY_API_PORT'] = self._api_port | |
65 self._GetAccessToken() | |
66 if not self.collect: | |
67 self._device = self._SelectDevice() | |
68 | |
69 def TearDown(self): | |
70 """Teardown the test environment.""" | |
71 self._RevokeAccessToken() | |
72 | |
73 def __enter__(self): | |
74 """Set up the test run when used as a context manager.""" | |
75 self.SetUp() | |
76 return self | |
77 | |
78 def __exit__(self, exc_type, exc_val, exc_tb): | |
79 """Tears down the test run when used as a context manager.""" | |
80 self.TearDown() | |
81 | |
82 def _GetAccessToken(self): | |
83 """Generates access token for remote device service.""" | |
84 access_token_results = appurify.api.access_token_generate( | |
85 self._api_key, self._api_secret) | |
86 remote_device_helper.TestHttpResponse(access_token_results, | |
87 'Unable to generate access token.') | |
88 self._access_token = access_token_results.json()['response']['access_token'] | |
89 | |
90 def _RevokeAccessToken(self): | |
91 """Destroys access token for remote device service.""" | |
92 revoke_token_results = appurify.api.access_token_revoke(self._access_token) | |
93 remote_device_helper.TestHttpResponse(revoke_token_results, | |
94 'Unable to revoke access token.') | |
95 | |
96 def _SelectDevice(self): | |
97 """Select which device to use.""" | |
98 dev_list_res = appurify.api.devices_list(self._access_token) | |
99 remote_device_helper.TestHttpResponse(dev_list_res, | |
100 'Unable to generate access token.') | |
101 device_list = dev_list_res.json()['response'] | |
102 for device in device_list: | |
103 if (device['name'] == self._remote_device and | |
104 device['os_version'] == self._remote_device_os): | |
105 return device['device_type_id'] | |
106 raise remote_device_helper.RemoteDeviceError('No device found: %s %s' %( | |
107 self._remote_device, self._remote_device_os)) | |
108 | |
109 @property | |
110 def device(self): | |
111 return self._device | |
112 | |
113 @property | |
114 def token(self): | |
115 return self._access_token | |
116 | |
117 @property | |
118 def results_path(self): | |
119 return self._results_path | |
120 | |
121 @property | |
122 def runner_type(self): | |
123 return self._runner_type | |
124 | |
125 @property | |
126 def runner_package(self): | |
127 return self._runner_package | |
OLD | NEW |