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