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 os | |
8 import time | |
9 | |
10 | |
11 # TODO(rnephew): When appurify is added to 3rd party switch these. | |
12 from pylib import appurify_api | |
13 from pylib import appurify_utils | |
14 from pylib.base import remote_device_config | |
15 from pylib.base import test_run | |
16 | |
17 class RemoteDeviceTestRun(test_run.TestRun): | |
18 """Run gtests and uirobot tests on a remote device.""" | |
19 | |
20 def __init__(self, env, test_instance): | |
21 super(RemoteDeviceTestRun, self).__init__(env, test_instance) | |
22 self._env = env | |
23 self._test_instance = test_instance | |
24 self._wait_time = 5 | |
jbudorick
2014/11/21 00:17:25
This should just be a constant (i.e. _WAIT_TIME),
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
25 self._device = self.SelectDevice() | |
jbudorick
2014/11/21 00:17:25
Both SelectDevice and UploadAppToDevice should be
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
26 self._app_id = self.UploadAppToDevice() | |
27 self._test_id = '' | |
28 self._results = '' | |
29 | |
30 def TestPackage(self): | |
31 pass | |
32 | |
33 #override | |
34 def SetUp(self): | |
35 if self._test_instance.TestType() == 'gtest': | |
36 self.UploadTestToDevice('robotium') | |
37 self.SetTestConfig() | |
38 | |
39 if self._test_instance.TestType() == 'uirobot': | |
jbudorick
2014/11/21 00:17:25
elif?
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
40 self.GetTestByName('android_robot') | |
41 self.SetTestConfig(extras={'duration': self._env.options.minutes}) | |
42 | |
43 #override | |
44 def RunTest(self): | |
45 test_start_res = appurify_api.tests_run( | |
46 self._env.token, self._device, self._app_id, self._test_id) | |
47 self.TestHttpResponse(test_start_res, 'Unable to run test.') | |
48 test_run_id = test_start_res.json()['response']['test_run_id'] | |
49 self.WaitForTest(test_run_id) | |
50 self.DownloadTestResults(self._env.options.results_path) | |
51 return self._results | |
52 | |
53 #override | |
54 def TearDown(self): | |
55 pass | |
56 | |
57 def __enter__(self): | |
58 self.SetUp() | |
59 return self | |
60 | |
61 def __exit__(self, exc_type, exc_val, exc_tb): | |
62 self.TearDown() | |
63 | |
64 def GetTestByName(self, test_name): | |
jbudorick
2014/11/21 00:17:25
All of these functions need docstrings.
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
65 test_list_res = appurify_api.tests_list(self._env.token) | |
66 self.TestHttpResponse(test_list_res, 'Unable to get tests list.') | |
67 for test in test_list_res.json()['response']: | |
68 if test['test_type'] == test_name: | |
69 self._test_id = test['test_id'] | |
70 return | |
71 | |
72 def DownloadTestResults(self, results_path): | |
73 if results_path: | |
74 if not os.path.exists(os.path.basename(results_path)): | |
75 os.makedirs(os.path.basename(results_path)) | |
76 appurify_utils.wget(self._results['url'], results_path) | |
77 | |
78 def WaitForTest(self, test_run_id): | |
79 test_status = 'in-progress' | |
80 while test_status != 'complete': | |
jbudorick
2014/11/21 00:17:25
'complete' should be const'd.
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
81 time.sleep(self._wait_time) | |
82 test_check_res = appurify_api.tests_check_result(self._env.token, | |
83 test_run_id) | |
84 self.TestHttpResponse(test_check_res, 'Unable to get test status.') | |
85 test_status = test_check_res.json()['response']['status'] | |
86 self._results = test_check_res.json()['response']['results'] | |
87 | |
88 def SelectDevice(self): | |
89 for device in self._env.devices: | |
jbudorick
2014/11/21 00:17:25
This would seem like an environment level function
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
90 if (device['name'] == self._env.options.remote_device and | |
91 device['os_version'] == self._env.options.remote_device_os): | |
92 return device['device_type_id'] | |
93 self._env.error_func('No device found: %s %s' %( | |
94 self._env.options.remote_device, self._env.options.remote_device_os)) | |
95 | |
96 def UploadAppToDevice(self): | |
97 apk_name = os.path.basename(self._test_instance.apk) | |
98 with open(self._test_instance.apk_under_test, 'rb') as apk_src: | |
99 upload_results = appurify_api.apps_upload(self._env.token, | |
100 apk_src, 'raw', name=apk_name) | |
jbudorick
2014/11/21 00:17:25
They need an open file instead of a path? That's .
rnephew (Reviews Here)
2014/11/21 18:26:47
Yep. Thats how they do it on their client too. I t
| |
101 self.TestHttpResponse(upload_results, 'Unable to upload Chrome.') | |
jbudorick
2014/11/21 00:17:25
s/Chrome/<apk under test name>/
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
102 return upload_results.json()['response']['app_id'] | |
103 | |
104 def UploadTestToDevice(self, test_type): | |
105 with open(self._test_instance.apk, 'rb') as test_src: | |
106 upload_results = appurify_api.tests_upload( | |
107 self._env.token, test_src, 'raw', test_type, | |
108 app_id=self._app_id, name=self._test_instance.TestType()) | |
109 self.TestHttpResponse(upload_results, 'Unable to upload test.') | |
jbudorick
2014/11/21 00:17:25
s/ test./ <test apk name>./
rnephew (Reviews Here)
2014/11/21 18:26:47
Done.
| |
110 self._test_id = upload_results.json()['response']['test_id'] | |
111 | |
112 def SetTestConfig(self, extras=None): | |
113 with remote_device_config.RemoteDeviceConfig( | |
jbudorick
2014/11/21 00:17:25
I'm wondering if we should just use tempfile and d
rnephew (Reviews Here)
2014/11/21 18:26:47
Working on this, wont be in the next upload; but w
| |
114 self._test_instance.TestType(), self._test_instance.apk_under_test, | |
115 extras=extras) as config: | |
116 with open(config.path, 'rb') as config_src: | |
117 config_response = appurify_api.config_upload(self._env.token, | |
118 config_src, self._test_id) | |
119 self.TestHttpResponse(config_response, 'Unable to upload test config.') | |
120 | |
121 def TestHttpResponse(self, response, error_msg): | |
122 if response.status_code != 200: | |
123 self._env.error_func(error_msg) | |
OLD | NEW |