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.') | |
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.') | |
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 """Set up a test run.""" | |
88 if self._env.trigger: | |
89 self._TriggerSetUp() | |
90 | |
91 def _TriggerSetUp(self): | |
92 """Set up the triggering of a test run.""" | |
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 remote_device_helper.TestHttpResponse(test_list_res, | |
106 'Unable to get tests list.') | |
107 for test in test_list_res.json()['response']: | |
108 if test['test_type'] == test_name: | |
109 return test['test_id'] | |
110 raise remote_device_helper.RemoteDeviceError( | |
111 'No test found with name %s' % (test_name)) | |
112 | |
113 def _DownloadTestResults(self, results_path): | |
114 """Download the test results from remote device service. | |
115 | |
116 Args: | |
117 results_path: path to download results to. | |
118 """ | |
119 if results_path: | |
120 if not os.path.exists(os.path.basename(results_path)): | |
121 os.makedirs(os.path.basename(results_path)) | |
122 appurify.utils.wget(self._results['results']['url'], results_path) | |
123 | |
124 def _GetTestStatus(self, test_run_id): | |
125 """Checks the state of the test, and sets self._results | |
126 | |
127 Args: | |
128 test_run_id: Id of test on on remote service. | |
129 """ | |
130 | |
131 test_check_res = appurify.api.tests_check_result(self._env.token, | |
132 test_run_id) | |
133 remote_device_helper.TestHttpResponse(test_check_res, | |
134 'Unable to get test status.') | |
135 self._results = test_check_res.json()['response'] | |
136 return self._results['status'] | |
137 | |
138 def _UploadAppToDevice(self, apk_path): | |
139 """Upload app to device.""" | |
140 apk_name = os.path.basename(apk_path) | |
141 with open(apk_path, 'rb') as apk_src: | |
142 upload_results = appurify.api.apps_upload(self._env.token, | |
143 apk_src, 'raw', name=apk_name) | |
144 remote_device_helper.TestHttpResponse( | |
145 upload_results, 'Unable to upload %s.' %(apk_path)) | |
146 return upload_results.json()['response']['app_id'] | |
147 | |
148 def _UploadTestToDevice(self, test_type): | |
149 """Upload test to device | |
150 Args: | |
151 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | |
152 """ | |
153 with open(self._test_instance.apk, 'rb') as test_src: | |
154 upload_results = appurify.api.tests_upload( | |
155 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | |
156 remote_device_helper.TestHttpResponse(upload_results, | |
157 'Unable to upload %s.' %(self._test_instance.apk)) | |
158 return upload_results.json()['response']['test_id'] | |
159 | |
160 def _SetTestConfig(self, runner_type, body): | |
161 """Generates and uploads config file for test. | |
162 Args: | |
163 extras: Extra arguments to set in the config file. | |
164 """ | |
165 with tempfile.TemporaryFile() as config: | |
166 config_data = ['[appurify]', '[%s]' % runner_type] | |
167 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) | |
168 config.write(''.join('%s\n' % l for l in config_data)) | |
169 config.flush() | |
170 config.seek(0) | |
171 config_response = appurify.api.config_upload(self._env.token, | |
172 config, self._test_id) | |
173 remote_device_helper.TestHttpResponse(config_response, | |
174 'Unable to upload test config.') | |
jbudorick
2014/12/05 21:53:15
nit: one more space
rnephew (Reviews Here)
2014/12/05 21:55:08
Done.
| |
OLD | NEW |