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 """Run specific test on specific environment.""" |
| 6 |
| 7 import logging |
| 8 import os |
| 9 import sys |
| 10 import tempfile |
| 11 import time |
| 12 |
| 13 from pylib import constants |
| 14 from pylib.base import test_run |
| 15 from pylib.remote.device import remote_device_helper |
| 16 |
| 17 sys.path.append(os.path.join( |
| 18 constants.DIR_SOURCE_ROOT, 'third_party', 'requests', 'src')) |
| 19 sys.path.append(os.path.join( |
| 20 constants.DIR_SOURCE_ROOT, 'third_party', 'appurify-python', 'src')) |
| 21 import appurify.api |
| 22 import appurify.utils |
| 23 |
| 24 class RemoteDeviceTestRun(test_run.TestRun): |
| 25 """Run gtests and uirobot tests on a remote device.""" |
| 26 |
| 27 WAIT_TIME = 5 |
| 28 COMPLETE = 'complete' |
| 29 |
| 30 def __init__(self, env, test_instance): |
| 31 """Constructor. |
| 32 |
| 33 Args: |
| 34 env: Environment the tests will run in. |
| 35 test_instance: The test that will be run. |
| 36 """ |
| 37 super(RemoteDeviceTestRun, self).__init__(env, test_instance) |
| 38 self._env = env |
| 39 self._test_instance = test_instance |
| 40 self._app_id = '' |
| 41 self._test_id = '' |
| 42 self._results = '' |
| 43 |
| 44 def TestPackage(self): |
| 45 pass |
| 46 |
| 47 #override |
| 48 def RunTests(self): |
| 49 """Run the test.""" |
| 50 if self._env.trigger: |
| 51 test_start_res = appurify.api.tests_run( |
| 52 self._env.token, self._env.device, self._app_id, self._test_id) |
| 53 remote_device_helper.TestHttpResponse( |
| 54 test_start_res, 'Unable to run test.') |
| 55 test_run_id = test_start_res.json()['response']['test_run_id'] |
| 56 if not self._env.collect: |
| 57 assert isinstance(self._env.trigger, basestring), ( |
| 58 'File for storing test_run_id must be a string.') |
| 59 with open(self._env.trigger, 'w') as test_run_id_file: |
| 60 test_run_id_file.write(test_run_id) |
| 61 |
| 62 if self._env.collect: |
| 63 if not self._env.trigger: |
| 64 assert isinstance(self._env.trigger, basestring), ( |
| 65 'File for storing test_run_id must be a string.') |
| 66 with open(self._env.collect, 'r') as test_run_id_file: |
| 67 test_run_id = test_run_id_file.read() |
| 68 while self._GetTestStatus(test_run_id) != self.COMPLETE: |
| 69 time.sleep(self.WAIT_TIME) |
| 70 self._DownloadTestResults(self._env.results_path) |
| 71 return self._ParseTestResults() |
| 72 |
| 73 #override |
| 74 def TearDown(self): |
| 75 """Tear down the test run.""" |
| 76 pass |
| 77 |
| 78 def __enter__(self): |
| 79 """Set up the test run when used as a context manager.""" |
| 80 self.SetUp() |
| 81 return self |
| 82 |
| 83 def __exit__(self, exc_type, exc_val, exc_tb): |
| 84 """Tear down the test run when used as a context manager.""" |
| 85 self.TearDown() |
| 86 |
| 87 #override |
| 88 def SetUp(self): |
| 89 """Set up a test run.""" |
| 90 if self._env.trigger: |
| 91 self._TriggerSetUp() |
| 92 |
| 93 def _TriggerSetUp(self): |
| 94 """Set up the triggering of a test run.""" |
| 95 raise NotImplementedError |
| 96 |
| 97 def _ParseTestResults(self): |
| 98 raise NotImplementedError |
| 99 |
| 100 def _GetTestByName(self, test_name): |
| 101 """Gets test_id for specific test. |
| 102 |
| 103 Args: |
| 104 test_name: Test to find the ID of. |
| 105 """ |
| 106 test_list_res = appurify.api.tests_list(self._env.token) |
| 107 remote_device_helper.TestHttpResponse(test_list_res, |
| 108 'Unable to get tests list.') |
| 109 for test in test_list_res.json()['response']: |
| 110 if test['test_type'] == test_name: |
| 111 return test['test_id'] |
| 112 raise remote_device_helper.RemoteDeviceError( |
| 113 'No test found with name %s' % (test_name)) |
| 114 |
| 115 def _DownloadTestResults(self, results_path): |
| 116 """Download the test results from remote device service. |
| 117 |
| 118 Args: |
| 119 results_path: path to download results to. |
| 120 """ |
| 121 if results_path: |
| 122 if not os.path.exists(os.path.basename(results_path)): |
| 123 os.makedirs(os.path.basename(results_path)) |
| 124 appurify.utils.wget(self._results['results']['url'], results_path) |
| 125 |
| 126 def _GetTestStatus(self, test_run_id): |
| 127 """Checks the state of the test, and sets self._results |
| 128 |
| 129 Args: |
| 130 test_run_id: Id of test on on remote service. |
| 131 """ |
| 132 |
| 133 test_check_res = appurify.api.tests_check_result(self._env.token, |
| 134 test_run_id) |
| 135 remote_device_helper.TestHttpResponse(test_check_res, |
| 136 'Unable to get test status.') |
| 137 self._results = test_check_res.json()['response'] |
| 138 return self._results['status'] |
| 139 |
| 140 def _UploadAppToDevice(self, apk_path): |
| 141 """Upload app to device.""" |
| 142 apk_name = os.path.basename(apk_path) |
| 143 with open(apk_path, 'rb') as apk_src: |
| 144 upload_results = appurify.api.apps_upload(self._env.token, |
| 145 apk_src, 'raw', name=apk_name) |
| 146 remote_device_helper.TestHttpResponse( |
| 147 upload_results, 'Unable to upload %s.' %(apk_path)) |
| 148 return upload_results.json()['response']['app_id'] |
| 149 |
| 150 def _UploadTestToDevice(self, test_type): |
| 151 """Upload test to device |
| 152 Args: |
| 153 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. |
| 154 """ |
| 155 with open(self._test_instance.apk, 'rb') as test_src: |
| 156 upload_results = appurify.api.tests_upload( |
| 157 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) |
| 158 remote_device_helper.TestHttpResponse(upload_results, |
| 159 'Unable to upload %s.' %(self._test_instance.apk)) |
| 160 return upload_results.json()['response']['test_id'] |
| 161 |
| 162 def _SetTestConfig(self, runner_type, body): |
| 163 """Generates and uploads config file for test. |
| 164 Args: |
| 165 extras: Extra arguments to set in the config file. |
| 166 """ |
| 167 with tempfile.TemporaryFile() as config: |
| 168 config_data = ['[appurify]', '[%s]' % runner_type] |
| 169 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) |
| 170 config.write(''.join('%s\n' % l for l in config_data)) |
| 171 config.flush() |
| 172 config.seek(0) |
| 173 config_response = appurify.api.config_upload(self._env.token, |
| 174 config, self._test_id) |
| 175 remote_device_helper.TestHttpResponse(config_response, |
| 176 'Unable to upload test config.') |
OLD | NEW |