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 os | |
| 8 import sys | |
| 9 import tempfile | |
| 10 import time | |
| 11 | |
| 12 from pylib import constants | |
| 13 from pylib.base import test_run | |
| 14 from pylib.device import remote_device_utils | |
| 15 | |
| 16 sys.path.append(os.path.join( | |
| 17 constants.DIR_SOURCE_ROOT, 'third_party', 'appurify-python', 'src')) | |
| 18 import appurify.api | |
| 19 import appurify.utils | |
| 20 | |
| 21 class RemoteDeviceTestRun(test_run.TestRun): | |
| 22 """Run gtests and uirobot tests on a remote device.""" | |
| 23 | |
| 24 WAIT_TIME = 5 | |
| 25 COMPLETE = 'complete' | |
| 26 DEFAULT_GTEST_RUNNER_TYPE = 'robotium' | |
| 27 DEFAULT_UIROBOT_RUNNER_TYPE = 'android_robot' | |
| 28 DEFAULT_GTEST_RUNNER_PACKAGE = ( | |
| 29 'org.chromium.native_test.ChromiumNativeTestInstrumentationTestRunner') | |
| 30 | |
| 31 def __init__(self, env, test_instance): | |
| 32 """Constructor. | |
| 33 | |
| 34 Args: | |
| 35 env: Environment the tests will run in. | |
| 36 test_instance: The test that will be run. | |
| 37 """ | |
| 38 super(RemoteDeviceTestRun, self).__init__(env, test_instance) | |
| 39 self._env = env | |
| 40 self._test_instance = test_instance | |
| 41 self._app_id = '' | |
| 42 self._test_id = '' | |
| 43 self._results = '' | |
| 44 | |
| 45 def TestPackage(self): | |
| 46 pass | |
| 47 | |
| 48 #override | |
| 49 def SetUp(self): | |
| 50 """Setup the test run.""" | |
| 51 self._app_id = self.UploadAppToDevice() | |
| 52 | |
| 53 if self._test_instance.TestType() == 'gtest': | |
|
jbudorick
2014/11/30 22:57:02
Perhaps these should be in separate classes with a
rnephew (Reviews Here)
2014/12/02 19:47:48
Done.
| |
| 54 if not self._env.runner_type: | |
| 55 runner_type = self.DEFAULT_GTEST_RUNNER_TYPE | |
|
jbudorick
2014/11/30 22:57:02
Log a message here at info level about the default
rnephew (Reviews Here)
2014/12/02 19:47:48
Done.
| |
| 56 else: | |
| 57 runner_type = self._env.runner_type | |
| 58 if not self._env.runner_package: | |
| 59 runner_package = self.DEFAULT_GTEST_RUNNER_PACKAGE | |
|
jbudorick
2014/11/30 22:57:02
Same here.
rnephew (Reviews Here)
2014/12/02 19:47:48
Done.
| |
| 60 else: | |
| 61 runner_package = self._env.runner_package | |
| 62 self._test_id = self.UploadTestToDevice(runner_type) | |
| 63 config_body = {'runner': runner_package} | |
| 64 | |
| 65 elif self._test_instance.TestType() == 'uirobot': | |
| 66 if not self._env.runner_type: | |
| 67 runner_type = self.DEFAULT_UIROBOT_RUNNER_TYPE | |
| 68 else: | |
| 69 runner_type = self._env.runner_type | |
| 70 self._test_id = self.GetTestByName(runner_type) | |
| 71 config_body = {'duration': self._test_instance.minutes} | |
| 72 | |
| 73 else: | |
| 74 raise remote_device_utils.RemoteDeviceError('Unknown test type: %s' %( | |
| 75 self._test_instance.TestType())) | |
| 76 | |
| 77 self.SetTestConfig(runner_type, config_body) | |
| 78 | |
| 79 #override | |
| 80 def RunTest(self): | |
| 81 """Run the test.""" | |
| 82 test_start_res = appurify.api.tests_run( | |
| 83 self._env.token, self._env.device, self._app_id, self._test_id) | |
| 84 remote_device_utils.TestHttpResponse(test_start_res, 'Unable to run test.') | |
| 85 test_run_id = test_start_res.json()['response']['test_run_id'] | |
| 86 self.WaitForTest(test_run_id) | |
|
jbudorick
2014/11/30 22:57:02
We're going to have to figure out the trigger/coll
rnephew (Reviews Here)
2014/12/02 19:47:48
Agreed, for now I'll leave this here for the sake
| |
| 87 self.DownloadTestResults(self._env.results_path) | |
| 88 return self._results | |
| 89 | |
| 90 #override | |
| 91 def TearDown(self): | |
| 92 """Teardown the test run.""" | |
| 93 pass | |
| 94 | |
| 95 def __enter__(self): | |
| 96 """Runs when entering with with keyword.""" | |
| 97 self.SetUp() | |
| 98 return self | |
| 99 | |
| 100 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 101 """Runs when exiting with with keyword.""" | |
| 102 self.TearDown() | |
| 103 | |
| 104 def GetTestByName(self, test_name): | |
| 105 """Gets test_id for specific test. | |
| 106 | |
| 107 Args: | |
| 108 test_name: Test to find the ID of. | |
| 109 """ | |
| 110 test_list_res = appurify.api.tests_list(self._env.token) | |
| 111 remote_device_utils.TestHttpResponse(test_list_res, | |
| 112 'Unable to get tests list.') | |
| 113 for test in test_list_res.json()['response']: | |
| 114 if test['test_type'] == test_name: | |
| 115 return test['test_id'] | |
| 116 raise remote_device_utils.RemoteDeviceError('No test found with name %s' | |
| 117 %(test_name)) | |
|
jbudorick
2014/11/30 22:57:02
nit: space after %
You may want to just put the e
rnephew (Reviews Here)
2014/12/02 19:47:48
Done.
| |
| 118 | |
| 119 def DownloadTestResults(self, results_path): | |
| 120 """Download the test results from remote device service. | |
| 121 | |
| 122 Args: | |
| 123 results_path: path to download results to. | |
| 124 """ | |
| 125 if results_path: | |
| 126 if not os.path.exists(os.path.basename(results_path)): | |
| 127 os.makedirs(os.path.basename(results_path)) | |
| 128 appurify.utils.wget(self._results['url'], results_path) | |
| 129 | |
| 130 def WaitForTest(self, test_run_id): | |
| 131 """Wait for remote service to have results of test. | |
| 132 | |
| 133 Args: | |
| 134 test_run_id: id of test to wait for results of. | |
| 135 """ | |
| 136 test_status = 'in-progress' | |
| 137 while test_status != self.COMPLETE: | |
| 138 time.sleep(self.WAIT_TIME) | |
| 139 test_check_res = appurify.api.tests_check_result(self._env.token, | |
| 140 test_run_id) | |
| 141 remote_device_utils.TestHttpResponse(test_check_res, | |
| 142 'Unable to get test status.') | |
| 143 test_status = test_check_res.json()['response']['status'] | |
| 144 self._results = test_check_res.json()['response']['results'] | |
| 145 | |
| 146 def UploadAppToDevice(self): | |
| 147 """Upload app to device.""" | |
| 148 apk_name = os.path.basename(self._test_instance.apk) | |
| 149 with open(self._test_instance.apk_under_test, 'rb') as apk_src: | |
| 150 upload_results = appurify.api.apps_upload(self._env.token, | |
| 151 apk_src, 'raw', name=apk_name) | |
| 152 remote_device_utils.TestHttpResponse(upload_results, | |
| 153 'Unable to upload %s.' %(self._test_instance.apk_under_test)) | |
| 154 return upload_results.json()['response']['app_id'] | |
| 155 | |
| 156 def UploadTestToDevice(self, test_type): | |
| 157 """Upload test to device | |
| 158 Args: | |
| 159 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | |
| 160 """ | |
| 161 with open(self._test_instance.apk, 'rb') as test_src: | |
| 162 upload_results = appurify.api.tests_upload( | |
| 163 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | |
| 164 remote_device_utils.TestHttpResponse(upload_results, | |
| 165 'Unable to upload %s.' %(self._test_instance.apk)) | |
| 166 return upload_results.json()['response']['test_id'] | |
| 167 | |
| 168 def SetTestConfig(self, runner_type, body): | |
| 169 """Generates and uploads config file for test. | |
| 170 Args: | |
| 171 extras: Extra arguments to set in the config file. | |
| 172 """ | |
| 173 config = tempfile.TemporaryFile() | |
| 174 config_data = ['[appurify]\n', '[' + runner_type + ']\n'] | |
| 175 config_data.extend('%s=%s\n' % (k, v) for k, v in body.iteritems()) | |
| 176 config.writelines(config_data) | |
| 177 config.seek(0) | |
| 178 config_response = appurify.api.config_upload(self._env.token, | |
| 179 config, self._test_id) | |
| 180 config.close() | |
| 181 remote_device_utils.TestHttpResponse(config_response, | |
| 182 'Unable to upload test config.') | |
| OLD | NEW |