| 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 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 env: Environment the tests will run in. | 28 env: Environment the tests will run in. |
| 29 test_instance: The test that will be run. | 29 test_instance: The test that will be run. |
| 30 """ | 30 """ |
| 31 super(RemoteDeviceTestRun, self).__init__(env, test_instance) | 31 super(RemoteDeviceTestRun, self).__init__(env, test_instance) |
| 32 self._env = env | 32 self._env = env |
| 33 self._test_instance = test_instance | 33 self._test_instance = test_instance |
| 34 self._app_id = '' | 34 self._app_id = '' |
| 35 self._test_id = '' | 35 self._test_id = '' |
| 36 self._results = '' | 36 self._results = '' |
| 37 self._test_run_id = '' | 37 self._test_run_id = '' |
| 38 self._current_status = '' |
| 38 | 39 |
| 39 #override | 40 #override |
| 40 def RunTests(self): | 41 def RunTests(self): |
| 41 """Run the test.""" | 42 """Run the test.""" |
| 42 if self._env.trigger: | 43 if self._env.trigger: |
| 44 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 43 test_start_res = appurify_sanitized.api.tests_run( | 45 test_start_res = appurify_sanitized.api.tests_run( |
| 44 self._env.token, self._env.device, self._app_id, self._test_id) | 46 self._env.token, self._env.device, self._app_id, self._test_id) |
| 47 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 45 remote_device_helper.TestHttpResponse( | 48 remote_device_helper.TestHttpResponse( |
| 46 test_start_res, 'Unable to run test.') | 49 test_start_res, 'Unable to run test.') |
| 47 self._test_run_id = test_start_res.json()['response']['test_run_id'] | 50 self._test_run_id = test_start_res.json()['response']['test_run_id'] |
| 48 logging.info('Test run id: %s' % self._test_run_id) | 51 logging.info('Test run id: %s' % self._test_run_id) |
| 49 if not self._env.collect: | 52 if not self._env.collect: |
| 50 assert isinstance(self._env.trigger, basestring), ( | 53 assert isinstance(self._env.trigger, basestring), ( |
| 51 'File for storing test_run_id must be a string.') | 54 'File for storing test_run_id must be a string.') |
| 52 with open(self._env.trigger, 'w') as test_run_id_file: | 55 with open(self._env.trigger, 'w') as test_run_id_file: |
| 53 test_run_id_file.write(self._test_run_id) | 56 test_run_id_file.write(self._test_run_id) |
| 54 | 57 |
| 55 if self._env.collect: | 58 if self._env.collect: |
| 56 if not self._env.trigger: | 59 if not self._env.trigger: |
| 57 assert isinstance(self._env.trigger, basestring), ( | 60 assert isinstance(self._env.trigger, basestring), ( |
| 58 'File for storing test_run_id must be a string.') | 61 'File for storing test_run_id must be a string.') |
| 59 with open(self._env.collect, 'r') as test_run_id_file: | 62 with open(self._env.collect, 'r') as test_run_id_file: |
| 60 self._test_run_id = test_run_id_file.read().strip() | 63 self._test_run_id = test_run_id_file.read().strip() |
| 61 while self._GetTestStatus(self._test_run_id) != self.COMPLETE: | 64 while self._GetTestStatus(self._test_run_id) != self.COMPLETE: |
| 62 time.sleep(self.WAIT_TIME) | 65 time.sleep(self.WAIT_TIME) |
| 63 self._DownloadTestResults(self._env.results_path) | 66 self._DownloadTestResults(self._env.results_path) |
| 64 return self._ParseTestResults() | 67 return self._ParseTestResults() |
| 65 | 68 |
| 66 #override | 69 #override |
| 67 def TearDown(self): | 70 def TearDown(self): |
| 68 """Tear down the test run.""" | 71 """Tear down the test run.""" |
| 69 if (self._GetTestStatus(self._test_run_id) != self.COMPLETE | 72 if (self._env.collect |
| 70 and self._env.collect): | 73 and self._GetTestStatus(self._test_run_id) != self.COMPLETE): |
| 74 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 71 test_abort_res = appurify_sanitized.api.tests_abort( | 75 test_abort_res = appurify_sanitized.api.tests_abort( |
| 72 self._env.token, self._test_run_id, reason='Test runner exiting.') | 76 self._env.token, self._test_run_id, reason='Test runner exiting.') |
| 77 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 73 remote_device_helper.TestHttpResponse(test_abort_res, | 78 remote_device_helper.TestHttpResponse(test_abort_res, |
| 74 'Unable to abort test.') | 79 'Unable to abort test.') |
| 75 | 80 |
| 76 def __enter__(self): | 81 def __enter__(self): |
| 77 """Set up the test run when used as a context manager.""" | 82 """Set up the test run when used as a context manager.""" |
| 78 self.SetUp() | 83 self.SetUp() |
| 79 return self | 84 return self |
| 80 | 85 |
| 81 def __exit__(self, exc_type, exc_val, exc_tb): | 86 def __exit__(self, exc_type, exc_val, exc_tb): |
| 82 """Tear down the test run when used as a context manager.""" | 87 """Tear down the test run when used as a context manager.""" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 | 99 |
| 95 def _ParseTestResults(self): | 100 def _ParseTestResults(self): |
| 96 raise NotImplementedError | 101 raise NotImplementedError |
| 97 | 102 |
| 98 def _GetTestByName(self, test_name): | 103 def _GetTestByName(self, test_name): |
| 99 """Gets test_id for specific test. | 104 """Gets test_id for specific test. |
| 100 | 105 |
| 101 Args: | 106 Args: |
| 102 test_name: Test to find the ID of. | 107 test_name: Test to find the ID of. |
| 103 """ | 108 """ |
| 109 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 104 test_list_res = appurify_sanitized.api.tests_list(self._env.token) | 110 test_list_res = appurify_sanitized.api.tests_list(self._env.token) |
| 111 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 105 remote_device_helper.TestHttpResponse(test_list_res, | 112 remote_device_helper.TestHttpResponse(test_list_res, |
| 106 'Unable to get tests list.') | 113 'Unable to get tests list.') |
| 107 for test in test_list_res.json()['response']: | 114 for test in test_list_res.json()['response']: |
| 108 if test['test_type'] == test_name: | 115 if test['test_type'] == test_name: |
| 109 return test['test_id'] | 116 return test['test_id'] |
| 110 raise remote_device_helper.RemoteDeviceError( | 117 raise remote_device_helper.RemoteDeviceError( |
| 111 'No test found with name %s' % (test_name)) | 118 'No test found with name %s' % (test_name)) |
| 112 | 119 |
| 113 def _DownloadTestResults(self, results_path): | 120 def _DownloadTestResults(self, results_path): |
| 114 """Download the test results from remote device service. | 121 """Download the test results from remote device service. |
| 115 | 122 |
| 116 Args: | 123 Args: |
| 117 results_path: path to download results to. | 124 results_path: path to download results to. |
| 118 """ | 125 """ |
| 119 if results_path: | 126 if results_path: |
| 120 logging.info('Downloading results to %s.' % results_path) | 127 logging.info('Downloading results to %s.' % results_path) |
| 121 if not os.path.exists(os.path.basename(results_path)): | 128 if not os.path.exists(os.path.basename(results_path)): |
| 122 os.makedirs(os.path.basename(results_path)) | 129 os.makedirs(os.path.basename(results_path)) |
| 130 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 123 appurify_sanitized.utils.wget(self._results['results']['url'], | 131 appurify_sanitized.utils.wget(self._results['results']['url'], |
| 124 results_path) | 132 results_path) |
| 133 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 125 | 134 |
| 126 def _GetTestStatus(self, test_run_id): | 135 def _GetTestStatus(self, test_run_id): |
| 127 """Checks the state of the test, and sets self._results | 136 """Checks the state of the test, and sets self._results |
| 128 | 137 |
| 129 Args: | 138 Args: |
| 130 test_run_id: Id of test on on remote service. | 139 test_run_id: Id of test on on remote service. |
| 131 """ | 140 """ |
| 132 | 141 |
| 142 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 133 test_check_res = appurify_sanitized.api.tests_check_result(self._env.token, | 143 test_check_res = appurify_sanitized.api.tests_check_result(self._env.token, |
| 134 test_run_id) | 144 test_run_id) |
| 145 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 135 remote_device_helper.TestHttpResponse(test_check_res, | 146 remote_device_helper.TestHttpResponse(test_check_res, |
| 136 'Unable to get test status.') | 147 'Unable to get test status.') |
| 137 self._results = test_check_res.json()['response'] | 148 self._results = test_check_res.json()['response'] |
| 138 logging.info('Test status: %s' % self._results['detailed_status']) | 149 if self._results['detailed_status'] != self._current_status: |
| 150 logging.info('Test status: %s' % self._results['detailed_status']) |
| 151 self._current_status = self._results['detailed_status'] |
| 139 return self._results['status'] | 152 return self._results['status'] |
| 140 | 153 |
| 141 def _UploadAppToDevice(self, apk_path): | 154 def _UploadAppToDevice(self, apk_path): |
| 142 """Upload app to device.""" | 155 """Upload app to device.""" |
| 143 logging.info('Upload %s to remote service.' % apk_path) | 156 logging.info('Uploading %s to remote service.' % apk_path) |
| 144 apk_name = os.path.basename(apk_path) | 157 apk_name = os.path.basename(apk_path) |
| 145 with open(apk_path, 'rb') as apk_src: | 158 with open(apk_path, 'rb') as apk_src: |
| 159 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 146 upload_results = appurify_sanitized.api.apps_upload(self._env.token, | 160 upload_results = appurify_sanitized.api.apps_upload(self._env.token, |
| 147 apk_src, 'raw', name=apk_name) | 161 apk_src, 'raw', name=apk_name) |
| 162 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 148 remote_device_helper.TestHttpResponse( | 163 remote_device_helper.TestHttpResponse( |
| 149 upload_results, 'Unable to upload %s.' %(apk_path)) | 164 upload_results, 'Unable to upload %s.' %(apk_path)) |
| 150 return upload_results.json()['response']['app_id'] | 165 return upload_results.json()['response']['app_id'] |
| 151 | 166 |
| 152 def _UploadTestToDevice(self, test_type): | 167 def _UploadTestToDevice(self, test_type): |
| 153 """Upload test to device | 168 """Upload test to device |
| 154 Args: | 169 Args: |
| 155 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | 170 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. |
| 156 """ | 171 """ |
| 157 logging.info('Uploading %s to remote service.' % self._test_instance.apk) | 172 logging.info('Uploading %s to remote service.' % self._test_instance.apk) |
| 158 with open(self._test_instance.apk, 'rb') as test_src: | 173 with open(self._test_instance.apk, 'rb') as test_src: |
| 174 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 159 upload_results = appurify_sanitized.api.tests_upload( | 175 upload_results = appurify_sanitized.api.tests_upload( |
| 160 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | 176 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) |
| 177 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 161 remote_device_helper.TestHttpResponse(upload_results, | 178 remote_device_helper.TestHttpResponse(upload_results, |
| 162 'Unable to upload %s.' %(self._test_instance.apk)) | 179 'Unable to upload %s.' %(self._test_instance.apk)) |
| 163 return upload_results.json()['response']['test_id'] | 180 return upload_results.json()['response']['test_id'] |
| 164 | 181 |
| 165 def _SetTestConfig(self, runner_type, body): | 182 def _SetTestConfig(self, runner_type, body): |
| 166 """Generates and uploads config file for test. | 183 """Generates and uploads config file for test. |
| 167 Args: | 184 Args: |
| 168 extras: Extra arguments to set in the config file. | 185 extras: Extra arguments to set in the config file. |
| 169 """ | 186 """ |
| 170 logging.info('Generating config file for test.') | 187 logging.info('Generating config file for test.') |
| 171 with tempfile.TemporaryFile() as config: | 188 with tempfile.TemporaryFile() as config: |
| 172 config_data = ['[appurify]', '[%s]' % runner_type] | 189 config_data = ['[appurify]', '[%s]' % runner_type] |
| 173 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) | 190 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)) | 191 config.write(''.join('%s\n' % l for l in config_data)) |
| 175 config.flush() | 192 config.flush() |
| 176 config.seek(0) | 193 config.seek(0) |
| 194 remote_device_helper.SetLogging(self._env.verbose_count, logging.WARNING) |
| 177 config_response = appurify_sanitized.api.config_upload(self._env.token, | 195 config_response = appurify_sanitized.api.config_upload(self._env.token, |
| 178 config, self._test_id) | 196 config, self._test_id) |
| 197 remote_device_helper.SetLogging(self._env.verbose_count, logging.NOTSET) |
| 179 remote_device_helper.TestHttpResponse(config_response, | 198 remote_device_helper.TestHttpResponse(config_response, |
| 180 'Unable to upload test config.') | 199 'Unable to upload test config.') |
| OLD | NEW |