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', 'requests', 'src')) |
| 16 sys.path.append(os.path.join( |
| 17 constants.DIR_SOURCE_ROOT, 'third_party', 'appurify-python', 'src')) |
| 18 import appurify.api |
| 19 |
| 20 |
| 21 class RemoteDeviceEnvironment(environment.Environment): |
| 22 """An environment for running on remote devices.""" |
| 23 |
| 24 def __init__(self, args, error_func): |
| 25 """Constructor. |
| 26 |
| 27 Args: |
| 28 args: Command line arguments. |
| 29 error_func: error to show when using bad command line arguments. |
| 30 """ |
| 31 super(RemoteDeviceEnvironment, self).__init__() |
| 32 |
| 33 if args.api_key_file: |
| 34 with open(args.api_key_file) as api_key_file: |
| 35 self._api_key = api_key_file.read().strip() |
| 36 elif args.api_key: |
| 37 self._api_key = args.api_key |
| 38 else: |
| 39 error_func('Must set api key with --api-key or --api-key-file') |
| 40 |
| 41 if args.api_secret_file: |
| 42 with open(args.api_secret_file) as api_secret_file: |
| 43 self._api_secret = api_secret_file.read().strip() |
| 44 elif args.api_secret: |
| 45 self._api_secret = args.api_secret |
| 46 else: |
| 47 error_func('Must set api secret with --api-secret or --api-secret-file') |
| 48 |
| 49 if not args.api_protocol: |
| 50 error_func('Must set api protocol with --api-protocol. Example: http') |
| 51 self._api_protocol = args.api_protocol |
| 52 |
| 53 if not args.api_address: |
| 54 error_func('Must set api address with --api-address') |
| 55 self._api_address = args.api_address |
| 56 |
| 57 if not args.api_port: |
| 58 error_func('Must set api port with --api-port.') |
| 59 self._api_port = args.api_port |
| 60 |
| 61 self._access_token = '' |
| 62 self._results_path = args.results_path |
| 63 self._remote_device = args.remote_device |
| 64 self._remote_device_os = args.remote_device_os |
| 65 self._runner_package = args.runner_package |
| 66 self._runner_type = args.runner_type |
| 67 self._device = '' |
| 68 if not args.trigger and not args.collect: |
| 69 self._trigger = True |
| 70 self._collect = True |
| 71 else: |
| 72 self._trigger = args.trigger |
| 73 self._collect = args.collect |
| 74 |
| 75 def SetUp(self): |
| 76 """Set up the test environment.""" |
| 77 os.environ['APPURIFY_API_PROTO'] = self._api_protocol |
| 78 os.environ['APPURIFY_API_HOST'] = self._api_address |
| 79 os.environ['APPURIFY_API_PORT'] = self._api_port |
| 80 self._GetAccessToken() |
| 81 if self._trigger: |
| 82 self._device = self._SelectDevice() |
| 83 |
| 84 def TearDown(self): |
| 85 """Teardown the test environment.""" |
| 86 self._RevokeAccessToken() |
| 87 |
| 88 def __enter__(self): |
| 89 """Set up the test run when used as a context manager.""" |
| 90 self.SetUp() |
| 91 return self |
| 92 |
| 93 def __exit__(self, exc_type, exc_val, exc_tb): |
| 94 """Tears down the test run when used as a context manager.""" |
| 95 self.TearDown() |
| 96 |
| 97 def _GetAccessToken(self): |
| 98 """Generates access token for remote device service.""" |
| 99 access_token_results = appurify.api.access_token_generate( |
| 100 self._api_key, self._api_secret) |
| 101 remote_device_helper.TestHttpResponse(access_token_results, |
| 102 'Unable to generate access token.') |
| 103 self._access_token = access_token_results.json()['response']['access_token'] |
| 104 |
| 105 def _RevokeAccessToken(self): |
| 106 """Destroys access token for remote device service.""" |
| 107 revoke_token_results = appurify.api.access_token_revoke(self._access_token) |
| 108 remote_device_helper.TestHttpResponse(revoke_token_results, |
| 109 'Unable to revoke access token.') |
| 110 |
| 111 def _SelectDevice(self): |
| 112 """Select which device to use.""" |
| 113 dev_list_res = appurify.api.devices_list(self._access_token) |
| 114 remote_device_helper.TestHttpResponse(dev_list_res, |
| 115 'Unable to generate access token.') |
| 116 device_list = dev_list_res.json()['response'] |
| 117 for device in device_list: |
| 118 if (device['name'] == self._remote_device |
| 119 and device['os_version'] == self._remote_device_os): |
| 120 return device['device_type_id'] |
| 121 raise remote_device_helper.RemoteDeviceError( |
| 122 'No device found: %s %s' % (self._remote_device, |
| 123 self._remote_device_os)) |
| 124 |
| 125 @property |
| 126 def device(self): |
| 127 return self._device |
| 128 |
| 129 @property |
| 130 def token(self): |
| 131 return self._access_token |
| 132 |
| 133 @property |
| 134 def results_path(self): |
| 135 return self._results_path |
| 136 |
| 137 @property |
| 138 def runner_type(self): |
| 139 return self._runner_type |
| 140 |
| 141 @property |
| 142 def runner_package(self): |
| 143 return self._runner_package |
| 144 |
| 145 @property |
| 146 def trigger(self): |
| 147 return self._trigger |
| 148 |
| 149 @property |
| 150 def collect(self): |
| 151 return self._collect |
OLD | NEW |