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 def SetUp(self): | |
jbudorick
2014/12/03 22:46:09
This is unnecessary if it's not implemented.
rnephew (Reviews Here)
2014/12/03 23:49:25
This is the base class that uirobot_runner and gte
jbudorick
2014/12/03 23:54:39
This inherits from TestRun, which has already defi
| |
46 """Setup the test run.""" | |
47 raise NotImplementedError | |
48 | |
49 #override | |
50 def RunTest(self): | |
51 """Run the test.""" | |
52 test_start_res = appurify.api.tests_run( | |
53 self._env.token, self._env.device, self._app_id, self._test_id) | |
54 remote_device_helper.TestHttpResponse(test_start_res, 'Unable to run test.') | |
55 test_run_id = test_start_res.json()['response']['test_run_id'] | |
56 #TODO(rnephew): Need to seperate invoking from results gather. | |
jbudorick
2014/12/03 22:46:09
Here's what I'm thinking with this:
- Figure out
rnephew (Reviews Here)
2014/12/03 23:49:25
The only thing that should be required is the test
jbudorick
2014/12/03 23:54:39
Both will need to take a file parameter, but yeah,
| |
57 self.WaitForTest(test_run_id) | |
58 self.DownloadTestResults(self._env.results_path) | |
59 return self._results | |
60 | |
61 #override | |
62 def TearDown(self): | |
63 """Teardown the test run.""" | |
jbudorick
2014/12/03 22:46:09
nit: "Teardown" -> "Tear down"
rnephew (Reviews Here)
2014/12/03 23:49:26
Done.
| |
64 pass | |
65 | |
66 def __enter__(self): | |
67 """Runs when entering with with keyword.""" | |
jbudorick
2014/12/03 22:46:09
reword: "Set up the test run when used as a contex
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
68 self.SetUp() | |
69 return self | |
70 | |
71 def __exit__(self, exc_type, exc_val, exc_tb): | |
72 """Runs when exiting with with keyword.""" | |
jbudorick
2014/12/03 22:46:09
Similarly, "Tear down the test run when used as a
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
73 self.TearDown() | |
74 | |
75 def GetTestByName(self, test_name): | |
jbudorick
2014/12/03 22:46:09
Do we anticipate calling this beyond uirobot? If n
rnephew (Reviews Here)
2014/12/03 23:49:26
Currently I do not plan on it. But if we can someh
jbudorick
2014/12/03 23:54:39
Going to have to talk to you more about this one.
| |
76 """Gets test_id for specific test. | |
77 | |
78 Args: | |
79 test_name: Test to find the ID of. | |
80 """ | |
81 test_list_res = appurify.api.tests_list(self._env.token) | |
82 remote_device_helper.TestHttpResponse(test_list_res, | |
83 'Unable to get tests list.') | |
jbudorick
2014/12/03 22:46:09
nit: one more space
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
84 for test in test_list_res.json()['response']: | |
85 if test['test_type'] == test_name: | |
86 return test['test_id'] | |
jbudorick
2014/12/03 22:46:09
nit: only one space between "return" and "test"
I
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
87 raise remote_device_helper.RemoteDeviceError( | |
88 'No test found with name %s' % (test_name)) | |
89 | |
90 def DownloadTestResults(self, results_path): | |
91 """Download the test results from remote device service. | |
92 | |
93 Args: | |
94 results_path: path to download results to. | |
95 """ | |
96 if results_path: | |
97 if not os.path.exists(os.path.basename(results_path)): | |
98 os.makedirs(os.path.basename(results_path)) | |
99 appurify.utils.wget(self._results['url'], results_path) | |
100 | |
101 def WaitForTest(self, test_run_id): | |
102 """Wait for remote service to have results of test. | |
103 | |
104 Args: | |
105 test_run_id: id of test to wait for results of. | |
106 """ | |
107 test_status = 'in-progress' | |
108 while test_status != self.COMPLETE: | |
109 time.sleep(self.WAIT_TIME) | |
110 test_check_res = appurify.api.tests_check_result(self._env.token, | |
111 test_run_id) | |
112 remote_device_helper.TestHttpResponse(test_check_res, | |
113 'Unable to get test status.') | |
114 test_status = test_check_res.json()['response']['status'] | |
115 self._results = test_check_res.json()['response']['results'] | |
116 | |
117 def UploadAppToDevice(self): | |
118 """Upload app to device.""" | |
119 apk_name = os.path.basename(self._test_instance.apk) | |
120 with open(self._test_instance.apk_under_test, 'rb') as apk_src: | |
121 upload_results = appurify.api.apps_upload(self._env.token, | |
122 apk_src, 'raw', name=apk_name) | |
123 remote_device_helper.TestHttpResponse(upload_results, | |
124 'Unable to upload %s.' %(self._test_instance.apk_under_test)) | |
125 return upload_results.json()['response']['app_id'] | |
126 | |
127 def UploadTestToDevice(self, test_type): | |
128 """Upload test to device | |
129 Args: | |
130 test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. | |
131 """ | |
132 with open(self._test_instance.apk, 'rb') as test_src: | |
jbudorick
2014/12/03 22:46:09
yeesh. Do we have to have an open file handle? Can
rnephew (Reviews Here)
2014/12/03 23:49:25
Unfortunately that's how they do it. It fails if I
jbudorick
2014/12/03 23:54:39
:(
| |
133 upload_results = appurify.api.tests_upload( | |
134 self._env.token, test_src, 'raw', test_type, app_id=self._app_id) | |
135 remote_device_helper.TestHttpResponse(upload_results, | |
136 'Unable to upload %s.' %(self._test_instance.apk)) | |
137 return upload_results.json()['response']['test_id'] | |
138 | |
139 def SetTestConfig(self, runner_type, body): | |
140 """Generates and uploads config file for test. | |
141 Args: | |
142 extras: Extra arguments to set in the config file. | |
143 """ | |
144 config = tempfile.TemporaryFile() | |
145 config_data = ['[appurify]\n', '[' + runner_type + ']\n'] | |
jbudorick
2014/12/03 22:46:09
nit: leave off the '\n' here and below and just do
rnephew (Reviews Here)
2014/12/03 23:49:25
Done.
| |
146 config_data.extend('%s=%s\n' % (k, v) for k, v in body.iteritems()) | |
147 config.writelines(config_data) | |
148 config.seek(0) | |
149 config_response = appurify.api.config_upload(self._env.token, | |
jbudorick
2014/12/03 22:46:09
Again, do we have to pass an open file handle?
rnephew (Reviews Here)
2014/12/03 23:49:25
See above.
| |
150 config, self._test_id) | |
151 config.close() | |
152 remote_device_helper.TestHttpResponse(config_response, | |
153 'Unable to upload test config.') | |
OLD | NEW |