| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Run specific test on specific environment.""" | 5 """Run specific test on specific environment.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import tempfile | 10 import tempfile |
| 11 import time | 11 import time |
| 12 import zipfile |
| 12 | 13 |
| 13 from pylib import constants | 14 from pylib import constants |
| 14 from pylib.base import test_run | 15 from pylib.base import test_run |
| 15 from pylib.remote.device import appurify_sanitized | 16 from pylib.remote.device import appurify_sanitized |
| 16 from pylib.remote.device import remote_device_helper | 17 from pylib.remote.device import remote_device_helper |
| 18 from pylib.utils import zip_utils |
| 17 | 19 |
| 18 class RemoteDeviceTestRun(test_run.TestRun): | 20 class RemoteDeviceTestRun(test_run.TestRun): |
| 19 """Run gtests and uirobot tests on a remote device.""" | 21 """Run gtests and uirobot tests on a remote device.""" |
| 20 | 22 |
| 21 WAIT_TIME = 5 | 23 WAIT_TIME = 5 |
| 22 COMPLETE = 'complete' | 24 COMPLETE = 'complete' |
| 23 | 25 |
| 24 def __init__(self, env, test_instance): | 26 def __init__(self, env, test_instance): |
| 25 """Constructor. | 27 """Constructor. |
| 26 | 28 |
| 27 Args: | 29 Args: |
| 28 env: Environment the tests will run in. | 30 env: Environment the tests will run in. |
| 29 test_instance: The test that will be run. | 31 test_instance: The test that will be run. |
| 30 """ | 32 """ |
| 31 super(RemoteDeviceTestRun, self).__init__(env, test_instance) | 33 super(RemoteDeviceTestRun, self).__init__(env, test_instance) |
| 32 self._env = env | 34 self._env = env |
| 33 self._test_instance = test_instance | 35 self._test_instance = test_instance |
| 34 self._app_id = '' | 36 self._app_id = '' |
| 35 self._test_id = '' | 37 self._test_id = '' |
| 36 self._results = '' | 38 self._results = '' |
| 37 self._test_run_id = '' | 39 self._test_run_id = '' |
| 40 self._current_status = '' |
| 38 | 41 |
| 39 #override | 42 #override |
| 40 def RunTests(self): | 43 def RunTests(self): |
| 41 """Run the test.""" | 44 """Run the test.""" |
| 42 if self._env.trigger: | 45 if self._env.trigger: |
| 43 test_start_res = appurify_sanitized.api.tests_run( | 46 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 44 self._env.token, self._env.device, self._app_id, self._test_id) | 47 logging.WARNING): |
| 48 test_start_res = appurify_sanitized.api.tests_run( |
| 49 self._env.token, self._env.device, self._app_id, self._test_id) |
| 45 remote_device_helper.TestHttpResponse( | 50 remote_device_helper.TestHttpResponse( |
| 46 test_start_res, 'Unable to run test.') | 51 test_start_res, 'Unable to run test.') |
| 47 self._test_run_id = test_start_res.json()['response']['test_run_id'] | 52 self._test_run_id = test_start_res.json()['response']['test_run_id'] |
| 48 logging.info('Test run id: %s' % self._test_run_id) | 53 logging.info('Test run id: %s' % self._test_run_id) |
| 49 if not self._env.collect: | 54 if not self._env.collect: |
| 50 assert isinstance(self._env.trigger, basestring), ( | 55 assert isinstance(self._env.trigger, basestring), ( |
| 51 'File for storing test_run_id must be a string.') | 56 'File for storing test_run_id must be a string.') |
| 52 with open(self._env.trigger, 'w') as test_run_id_file: | 57 with open(self._env.trigger, 'w') as test_run_id_file: |
| 53 test_run_id_file.write(self._test_run_id) | 58 test_run_id_file.write(self._test_run_id) |
| 54 | 59 |
| 55 if self._env.collect: | 60 if self._env.collect: |
| 56 if not self._env.trigger: | 61 if not self._env.trigger: |
| 57 assert isinstance(self._env.trigger, basestring), ( | 62 assert isinstance(self._env.trigger, basestring), ( |
| 58 'File for storing test_run_id must be a string.') | 63 'File for storing test_run_id must be a string.') |
| 59 with open(self._env.collect, 'r') as test_run_id_file: | 64 with open(self._env.collect, 'r') as test_run_id_file: |
| 60 self._test_run_id = test_run_id_file.read().strip() | 65 self._test_run_id = test_run_id_file.read().strip() |
| 61 while self._GetTestStatus(self._test_run_id) != self.COMPLETE: | 66 while self._GetTestStatus(self._test_run_id) != self.COMPLETE: |
| 62 time.sleep(self.WAIT_TIME) | 67 time.sleep(self.WAIT_TIME) |
| 63 self._DownloadTestResults(self._env.results_path) | 68 self._DownloadTestResults(self._env.results_path) |
| 64 return self._ParseTestResults() | 69 return self._ParseTestResults() |
| 65 | 70 |
| 66 #override | 71 #override |
| 67 def TearDown(self): | 72 def TearDown(self): |
| 68 """Tear down the test run.""" | 73 """Tear down the test run.""" |
| 69 if (self._GetTestStatus(self._test_run_id) != self.COMPLETE | 74 if (self._env.collect |
| 70 and self._env.collect): | 75 and self._GetTestStatus(self._test_run_id) != self.COMPLETE): |
| 71 test_abort_res = appurify_sanitized.api.tests_abort( | 76 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 72 self._env.token, self._test_run_id, reason='Test runner exiting.') | 77 logging.WARNING): |
| 78 test_abort_res = appurify_sanitized.api.tests_abort( |
| 79 self._env.token, self._test_run_id, reason='Test runner exiting.') |
| 73 remote_device_helper.TestHttpResponse(test_abort_res, | 80 remote_device_helper.TestHttpResponse(test_abort_res, |
| 74 'Unable to abort test.') | 81 'Unable to abort test.') |
| 75 | 82 |
| 76 def __enter__(self): | 83 def __enter__(self): |
| 77 """Set up the test run when used as a context manager.""" | 84 """Set up the test run when used as a context manager.""" |
| 78 self.SetUp() | 85 self.SetUp() |
| 79 return self | 86 return self |
| 80 | 87 |
| 81 def __exit__(self, exc_type, exc_val, exc_tb): | 88 def __exit__(self, exc_type, exc_val, exc_tb): |
| 82 """Tear down the test run when used as a context manager.""" | 89 """Tear down the test run when used as a context manager.""" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 | 101 |
| 95 def _ParseTestResults(self): | 102 def _ParseTestResults(self): |
| 96 raise NotImplementedError | 103 raise NotImplementedError |
| 97 | 104 |
| 98 def _GetTestByName(self, test_name): | 105 def _GetTestByName(self, test_name): |
| 99 """Gets test_id for specific test. | 106 """Gets test_id for specific test. |
| 100 | 107 |
| 101 Args: | 108 Args: |
| 102 test_name: Test to find the ID of. | 109 test_name: Test to find the ID of. |
| 103 """ | 110 """ |
| 104 test_list_res = appurify_sanitized.api.tests_list(self._env.token) | 111 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 112 logging.WARNING): |
| 113 test_list_res = appurify_sanitized.api.tests_list(self._env.token) |
| 105 remote_device_helper.TestHttpResponse(test_list_res, | 114 remote_device_helper.TestHttpResponse(test_list_res, |
| 106 'Unable to get tests list.') | 115 'Unable to get tests list.') |
| 107 for test in test_list_res.json()['response']: | 116 for test in test_list_res.json()['response']: |
| 108 if test['test_type'] == test_name: | 117 if test['test_type'] == test_name: |
| 109 return test['test_id'] | 118 return test['test_id'] |
| 110 raise remote_device_helper.RemoteDeviceError( | 119 raise remote_device_helper.RemoteDeviceError( |
| 111 'No test found with name %s' % (test_name)) | 120 'No test found with name %s' % (test_name)) |
| 112 | 121 |
| 113 def _DownloadTestResults(self, results_path): | 122 def _DownloadTestResults(self, results_path): |
| 114 """Download the test results from remote device service. | 123 """Download the test results from remote device service. |
| 115 | 124 |
| 116 Args: | 125 Args: |
| 117 results_path: path to download results to. | 126 results_path: path to download results to. |
| 118 """ | 127 """ |
| 119 if results_path: | 128 if results_path: |
| 120 logging.info('Downloading results to %s.' % results_path) | 129 logging.info('Downloading results to %s.' % results_path) |
| 121 if not os.path.exists(os.path.basename(results_path)): | 130 if not os.path.exists(os.path.basename(results_path)): |
| 122 os.makedirs(os.path.basename(results_path)) | 131 os.makedirs(os.path.basename(results_path)) |
| 123 appurify_sanitized.utils.wget(self._results['results']['url'], | 132 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 124 results_path) | 133 logging.WARNING): |
| 134 appurify_sanitized.utils.wget(self._results['results']['url'], |
| 135 results_path) |
| 125 | 136 |
| 126 def _GetTestStatus(self, test_run_id): | 137 def _GetTestStatus(self, test_run_id): |
| 127 """Checks the state of the test, and sets self._results | 138 """Checks the state of the test, and sets self._results |
| 128 | 139 |
| 129 Args: | 140 Args: |
| 130 test_run_id: Id of test on on remote service. | 141 test_run_id: Id of test on on remote service. |
| 131 """ | 142 """ |
| 132 | 143 |
| 133 test_check_res = appurify_sanitized.api.tests_check_result(self._env.token, | 144 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 134 test_run_id) | 145 logging.WARNING): |
| 146 test_check_res = appurify_sanitized.api.tests_check_result( |
| 147 self._env.token, test_run_id) |
| 135 remote_device_helper.TestHttpResponse(test_check_res, | 148 remote_device_helper.TestHttpResponse(test_check_res, |
| 136 'Unable to get test status.') | 149 'Unable to get test status.') |
| 137 self._results = test_check_res.json()['response'] | 150 self._results = test_check_res.json()['response'] |
| 138 logging.info('Test status: %s' % self._results['detailed_status']) | 151 if self._results['detailed_status'] != self._current_status: |
| 152 logging.info('Test status: %s', self._results['detailed_status']) |
| 153 self._current_status = self._results['detailed_status'] |
| 139 return self._results['status'] | 154 return self._results['status'] |
| 140 | 155 |
| 141 def _UploadAppToDevice(self, apk_path): | 156 def _AmInstrumentTestSetup(self, app_path, test_path, runner_package): |
| 157 config = {'runner': runner_package} |
| 158 |
| 159 self._app_id = self._UploadAppToDevice(app_path) |
| 160 |
| 161 data_deps = self._test_instance.GetDataDependencies() |
| 162 if data_deps: |
| 163 with tempfile.NamedTemporaryFile(suffix='.zip') as test_with_deps: |
| 164 sdcard_files = [] |
| 165 host_test = os.path.basename(test_path) |
| 166 with zipfile.ZipFile(test_with_deps.name, 'w') as zip_file: |
| 167 zip_file.write(test_path, host_test, zipfile.ZIP_DEFLATED) |
| 168 for h, _ in data_deps: |
| 169 zip_utils.WriteToZipFile(zip_file, h, '.') |
| 170 if os.path.isdir(h): |
| 171 sdcard_files.extend(os.listdir(h)) |
| 172 else: |
| 173 sdcard_files.extend(h) |
| 174 config['sdcard_files'] = ','.join(sdcard_files) |
| 175 config['host_test'] = host_test |
| 176 self._test_id = self._UploadTestToDevice( |
| 177 'robotium', test_with_deps.name) |
| 178 else: |
| 179 self._test_id = self._UploadTestToDevice('robotium', test_path) |
| 180 |
| 181 logging.info('Setting config: %s' % config) |
| 182 self._SetTestConfig('robotium', config) |
| 183 |
| 184 def _UploadAppToDevice(self, app_path): |
| 142 """Upload app to device.""" | 185 """Upload app to device.""" |
| 143 logging.info('Upload %s to remote service.' % apk_path) | 186 logging.info('Uploading %s to remote service.', app_path) |
| 144 apk_name = os.path.basename(apk_path) | 187 apk_name = os.path.basename(app_path) |
| 145 with open(apk_path, 'rb') as apk_src: | 188 with open(app_path, 'rb') as apk_src: |
| 146 upload_results = appurify_sanitized.api.apps_upload(self._env.token, | 189 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 147 apk_src, 'raw', name=apk_name) | 190 logging.WARNING): |
| 191 upload_results = appurify_sanitized.api.apps_upload( |
| 192 self._env.token, apk_src, 'raw', name=apk_name) |
| 148 remote_device_helper.TestHttpResponse( | 193 remote_device_helper.TestHttpResponse( |
| 149 upload_results, 'Unable to upload %s.' %(apk_path)) | 194 upload_results, 'Unable to upload %s.' % app_path) |
| 150 return upload_results.json()['response']['app_id'] | 195 return upload_results.json()['response']['app_id'] |
| 151 | 196 |
| 152 def _UploadTestToDevice(self, test_type): | 197 def _UploadTestToDevice(self, test_type, test_path): |
| 153 """Upload test to device | 198 """Upload test to device |
| 154 Args: | 199 Args: |
| 155 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | 200 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. |
| 156 """ | 201 """ |
| 157 logging.info('Uploading %s to remote service.' % self._test_instance.apk) | 202 logging.info('Uploading %s to remote service.' % test_path) |
| 158 with open(self._test_instance.apk, 'rb') as test_src: | 203 with open(test_path, 'rb') as test_src: |
| 159 upload_results = appurify_sanitized.api.tests_upload( | 204 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 160 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | 205 logging.WARNING): |
| 206 upload_results = appurify_sanitized.api.tests_upload( |
| 207 self._env.token, test_src, 'raw', test_type) |
| 161 remote_device_helper.TestHttpResponse(upload_results, | 208 remote_device_helper.TestHttpResponse(upload_results, |
| 162 'Unable to upload %s.' %(self._test_instance.apk)) | 209 'Unable to upload %s.' % test_path) |
| 163 return upload_results.json()['response']['test_id'] | 210 return upload_results.json()['response']['test_id'] |
| 164 | 211 |
| 165 def _SetTestConfig(self, runner_type, body): | 212 def _SetTestConfig(self, runner_type, body): |
| 166 """Generates and uploads config file for test. | 213 """Generates and uploads config file for test. |
| 167 Args: | 214 Args: |
| 168 extras: Extra arguments to set in the config file. | 215 extras: Extra arguments to set in the config file. |
| 169 """ | 216 """ |
| 170 logging.info('Generating config file for test.') | 217 logging.info('Generating config file for test.') |
| 171 with tempfile.TemporaryFile() as config: | 218 with tempfile.TemporaryFile() as config: |
| 172 config_data = ['[appurify]', '[%s]' % runner_type] | 219 config_data = ['[appurify]', '[%s]' % runner_type] |
| 173 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) | 220 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) |
| 174 config.write(''.join('%s\n' % l for l in config_data)) | 221 config.write(''.join('%s\n' % l for l in config_data)) |
| 175 config.flush() | 222 config.flush() |
| 176 config.seek(0) | 223 config.seek(0) |
| 177 config_response = appurify_sanitized.api.config_upload(self._env.token, | 224 with appurify_sanitized.SanitizeLogging(self._env.verbose_count, |
| 178 config, self._test_id) | 225 logging.WARNING): |
| 179 remote_device_helper.TestHttpResponse(config_response, | 226 config_response = appurify_sanitized.api.config_upload( |
| 180 'Unable to upload test config.') | 227 self._env.token, config, self._test_id) |
| 228 remote_device_helper.TestHttpResponse( |
| 229 config_response, 'Unable to upload test config.') |
| OLD | NEW |