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 self._trigger_and_collect = (True if not self._env.trigger | |
jbudorick
2014/12/04 21:51:34
Why is this necessary? Why can't the "if not self.
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
42 and not self._env.collect else False) | |
43 | |
44 def TestPackage(self): | |
45 pass | |
46 | |
47 #override | |
48 def RunTests(self): | |
49 """Run the test.""" | |
50 | |
51 #trigger | |
52 if self._env.trigger or self._trigger_and_collect: | |
53 test_start_res = appurify.api.tests_run( | |
54 self._env.token, self._env.device, self._app_id, self._test_id) | |
55 remote_device_helper.TestHttpResponse( | |
56 test_start_res, 'Unable to run test.') | |
57 test_run_id = test_start_res.json()['response']['test_run_id'] | |
58 if not self._trigger_and_collect: | |
jbudorick
2014/12/04 21:51:34
(following from above: why can't this just be if n
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
59 with open(self._env.trigger, 'w+') as test_run_id_file: | |
jbudorick
2014/12/04 21:51:34
Why w+ and not just w? We don't need to read the f
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
60 test_run_id_file.write(test_run_id) | |
61 | |
62 #collect | |
63 if self._env.collect or self._trigger_and_collect: | |
64 if not self._trigger_and_collect: | |
65 with open(self._env.collect, 'r') as test_run_id_file: | |
66 test_run_id = test_run_id_file.read() | |
67 self.WaitForTest(test_run_id) | |
68 self.DownloadTestResults(self._env.results_path) | |
69 return self.PackageTestResults() | |
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 def PackageTestResults(self): | |
jbudorick
2014/12/04 21:51:34
I might name this "ParseTestResults" or similar.
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
86 raise NotImplementedError | |
87 | |
88 def GetTestByName(self, test_name): | |
89 """Gets test_id for specific test. | |
90 | |
91 Args: | |
92 test_name: Test to find the ID of. | |
93 """ | |
94 test_list_res = appurify.api.tests_list(self._env.token) | |
95 remote_device_helper.TestHttpResponse(test_list_res, | |
96 'Unable to get tests list.') | |
97 for test in test_list_res.json()['response']: | |
98 if test['test_type'] == test_name: | |
99 return test['test_id'] | |
100 raise remote_device_helper.RemoteDeviceError( | |
101 'No test found with name %s' % (test_name)) | |
102 | |
103 def DownloadTestResults(self, results_path): | |
jbudorick
2014/12/04 21:51:34
Do any of these functions (PackageTestResults, Get
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
104 """Download the test results from remote device service. | |
105 | |
106 Args: | |
107 results_path: path to download results to. | |
108 """ | |
109 if results_path: | |
jbudorick
2014/12/04 21:51:34
When would results_path be empty? Should that be a
rnephew (Reviews Here)
2014/12/04 23:16:50
This is only set when you want to download the ful
| |
110 if not os.path.exists(os.path.basename(results_path)): | |
111 os.makedirs(os.path.basename(results_path)) | |
112 appurify.utils.wget(self._results['url'], results_path) | |
113 | |
114 def WaitForTest(self, test_run_id): | |
115 """Wait for remote service to have results of test. | |
116 | |
117 Args: | |
118 test_run_id: id of test to wait for results of. | |
119 """ | |
120 test_status = 'in-progress' | |
121 while test_status != self.COMPLETE: | |
122 time.sleep(self.WAIT_TIME) | |
123 test_check_res = appurify.api.tests_check_result(self._env.token, | |
jbudorick
2014/12/04 21:51:34
We should check before the first sleep.
rnephew (Reviews Here)
2014/12/04 23:16:50
Done.
| |
124 test_run_id) | |
125 remote_device_helper.TestHttpResponse(test_check_res, | |
126 'Unable to get test status.') | |
127 test_status = test_check_res.json()['response']['status'] | |
128 self._results = test_check_res.json()['response']['results'] | |
129 | |
130 def UploadAppToDevice(self, apk_path): | |
131 """Upload app to device.""" | |
132 apk_name = os.path.basename(apk_path) | |
133 with open(apk_path, 'rb') as apk_src: | |
134 upload_results = appurify.api.apps_upload(self._env.token, | |
135 apk_src, 'raw', name=apk_name) | |
136 remote_device_helper.TestHttpResponse(upload_results, | |
137 'Unable to upload %s.' %(apk_path)) | |
138 return upload_results.json()['response']['app_id'] | |
139 | |
140 def UploadTestToDevice(self, test_type): | |
141 """Upload test to device | |
142 Args: | |
143 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | |
144 """ | |
145 with open(self._test_instance.apk, 'rb') as test_src: | |
146 upload_results = appurify.api.tests_upload( | |
147 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | |
148 remote_device_helper.TestHttpResponse(upload_results, | |
149 'Unable to upload %s.' %(self._test_instance.apk)) | |
150 return upload_results.json()['response']['test_id'] | |
151 | |
152 def SetTestConfig(self, runner_type, body): | |
153 """Generates and uploads config file for test. | |
154 Args: | |
155 extras: Extra arguments to set in the config file. | |
156 """ | |
157 config = tempfile.TemporaryFile() | |
158 config_data = ['[appurify]', '[' + runner_type + ']'] | |
159 config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) | |
160 config.write(''.join('%s\n' % l for l in config_data)) | |
161 config.flush() | |
162 config.seek(0) | |
163 config_response = appurify.api.config_upload(self._env.token, | |
164 config, self._test_id) | |
165 config.close() | |
166 remote_device_helper.TestHttpResponse(config_response, | |
167 'Unable to upload test config.') | |
OLD | NEW |