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