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 | |
|
jbudorick
2014/12/05 01:01:46
I wonder if we should define SetUp in here to do t
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 45 #override | |
| 46 def RunTests(self): | |
| 47 """Run the test.""" | |
| 48 | |
| 49 #trigger | |
|
jbudorick
2014/12/05 01:01:46
nit: this doesn't add anything. Remove it.
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 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 with open(self._env.trigger, 'w') as test_run_id_file: | |
|
jbudorick
2014/12/05 01:03:37
similar to below w.r.t. sanity-checking the type o
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 58 test_run_id_file.write(test_run_id) | |
| 59 | |
| 60 #collect | |
|
jbudorick
2014/12/05 01:01:46
Same.
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 61 if self._env.collect: | |
| 62 if not self._env.trigger: | |
| 63 with open(self._env.collect, 'r') as test_run_id_file: | |
|
jbudorick
2014/12/05 01:03:37
To follow up on my dynamic typing comment: we shou
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 64 test_run_id = test_run_id_file.read() | |
| 65 while self._GetTestStatus(test_run_id) != self.COMPLETE: | |
| 66 time.sleep(self.WAIT_TIME) | |
| 67 self._DownloadTestResults(self._env.results_path) | |
| 68 return self._ParseTestResults() | |
| 69 | |
| 70 #override | |
| 71 def TearDown(self): | |
| 72 """Tear down the test run.""" | |
| 73 pass | |
| 74 | |
| 75 def __enter__(self): | |
| 76 """Set up the test run when used as a context manager.""" | |
| 77 self.SetUp() | |
| 78 return self | |
| 79 | |
| 80 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 81 """Tear down the test run when used as a context manager.""" | |
| 82 self.TearDown() | |
| 83 | |
| 84 def _ParseTestResults(self): | |
| 85 raise NotImplementedError | |
| 86 | |
| 87 def _GetTestByName(self, test_name): | |
| 88 """Gets test_id for specific test. | |
| 89 | |
| 90 Args: | |
| 91 test_name: Test to find the ID of. | |
| 92 """ | |
| 93 test_list_res = appurify.api.tests_list(self._env.token) | |
| 94 remote_device_helper.TestHttpResponse(test_list_res, | |
| 95 'Unable to get tests list.') | |
| 96 for test in test_list_res.json()['response']: | |
| 97 if test['test_type'] == test_name: | |
| 98 return test['test_id'] | |
| 99 raise remote_device_helper.RemoteDeviceError( | |
| 100 'No test found with name %s' % (test_name)) | |
| 101 | |
| 102 def _DownloadTestResults(self, results_path): | |
| 103 """Download the test results from remote device service. | |
| 104 | |
| 105 Args: | |
| 106 results_path: path to download results to. | |
| 107 """ | |
| 108 if results_path: | |
| 109 if not os.path.exists(os.path.basename(results_path)): | |
| 110 os.makedirs(os.path.basename(results_path)) | |
| 111 appurify.utils.wget(self._results['results']['url'], results_path) | |
| 112 | |
| 113 def _GetTestStatus(self, test_run_id): | |
| 114 """Checks the state of the test, and sets self._results | |
| 115 | |
| 116 Args: | |
| 117 test_run_id: Id of test on on remote service. | |
| 118 """ | |
| 119 | |
| 120 test_check_res = appurify.api.tests_check_result(self._env.token, | |
| 121 test_run_id) | |
| 122 remote_device_helper.TestHttpResponse(test_check_res, | |
| 123 'Unable to get test status.') | |
| 124 self._results = test_check_res.json()['response'] | |
| 125 return test_check_res.json()['response']['status'] | |
| 126 | |
| 127 def _UploadAppToDevice(self, apk_path): | |
| 128 """Upload app to device.""" | |
| 129 apk_name = os.path.basename(apk_path) | |
| 130 with open(apk_path, 'rb') as apk_src: | |
| 131 upload_results = appurify.api.apps_upload(self._env.token, | |
| 132 apk_src, 'raw', name=apk_name) | |
| 133 remote_device_helper.TestHttpResponse(upload_results, | |
|
jbudorick
2014/12/05 01:01:46
Either indent subsequent lines to the depth of the
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 134 'Unable to upload %s.' %(apk_path)) | |
| 135 return upload_results.json()['response']['app_id'] | |
| 136 | |
| 137 def _UploadTestToDevice(self, test_type): | |
| 138 """Upload test to device | |
| 139 Args: | |
| 140 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | |
| 141 """ | |
| 142 with open(self._test_instance.apk, 'rb') as test_src: | |
| 143 upload_results = appurify.api.tests_upload( | |
| 144 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | |
| 145 remote_device_helper.TestHttpResponse(upload_results, | |
| 146 'Unable to upload %s.' %(self._test_instance.apk)) | |
| 147 return upload_results.json()['response']['test_id'] | |
| 148 | |
| 149 def _SetTestConfig(self, runner_type, body): | |
| 150 """Generates and uploads config file for test. | |
| 151 Args: | |
| 152 extras: Extra arguments to set in the config file. | |
| 153 """ | |
| 154 config = tempfile.TemporaryFile() | |
| 155 config_data = ['[appurify]', '[' + runner_type + ']'] | |
|
jbudorick
2014/12/05 01:01:46
nit: '[%s]' % runner_type (for consistency if noth
rnephew (Reviews Here)
2014/12/05 15:45:40
Done.
| |
| 156 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) | |
| 157 config.write(''.join('%s\n' % l for l in config_data)) | |
| 158 config.flush() | |
| 159 config.seek(0) | |
| 160 config_response = appurify.api.config_upload(self._env.token, | |
| 161 config, self._test_id) | |
| 162 config.close() | |
| 163 remote_device_helper.TestHttpResponse(config_response, | |
| 164 'Unable to upload test config.') | |
| OLD | NEW |