Index: build/android/pylib/remote/device/remote_device_test_run.py |
diff --git a/build/android/pylib/remote/device/remote_device_test_run.py b/build/android/pylib/remote/device/remote_device_test_run.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c5f7aa32413ddf0db280bd7c17877223cfcfb9b8 |
--- /dev/null |
+++ b/build/android/pylib/remote/device/remote_device_test_run.py |
@@ -0,0 +1,167 @@ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Run specific test on specific environment.""" |
+ |
+import logging |
+import os |
+import sys |
+import tempfile |
+import time |
+ |
+from pylib import constants |
+from pylib.base import test_run |
+from pylib.remote.device import remote_device_helper |
+ |
+sys.path.append(os.path.join( |
+ constants.DIR_SOURCE_ROOT, 'third_party', 'appurify-python', 'src')) |
+import appurify.api |
+import appurify.utils |
+ |
+class RemoteDeviceTestRun(test_run.TestRun): |
+ """Run gtests and uirobot tests on a remote device.""" |
+ |
+ WAIT_TIME = 5 |
+ COMPLETE = 'complete' |
+ |
+ def __init__(self, env, test_instance): |
+ """Constructor. |
+ |
+ Args: |
+ env: Environment the tests will run in. |
+ test_instance: The test that will be run. |
+ """ |
+ super(RemoteDeviceTestRun, self).__init__(env, test_instance) |
+ self._env = env |
+ self._test_instance = test_instance |
+ self._app_id = '' |
+ self._test_id = '' |
+ self._results = '' |
+ 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.
|
+ and not self._env.collect else False) |
+ |
+ def TestPackage(self): |
+ pass |
+ |
+ #override |
+ def RunTests(self): |
+ """Run the test.""" |
+ |
+ #trigger |
+ if self._env.trigger or self._trigger_and_collect: |
+ test_start_res = appurify.api.tests_run( |
+ self._env.token, self._env.device, self._app_id, self._test_id) |
+ remote_device_helper.TestHttpResponse( |
+ test_start_res, 'Unable to run test.') |
+ test_run_id = test_start_res.json()['response']['test_run_id'] |
+ 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.
|
+ 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.
|
+ test_run_id_file.write(test_run_id) |
+ |
+ #collect |
+ if self._env.collect or self._trigger_and_collect: |
+ if not self._trigger_and_collect: |
+ with open(self._env.collect, 'r') as test_run_id_file: |
+ test_run_id = test_run_id_file.read() |
+ self.WaitForTest(test_run_id) |
+ self.DownloadTestResults(self._env.results_path) |
+ return self.PackageTestResults() |
+ |
+ #override |
+ def TearDown(self): |
+ """Tear down the test run.""" |
+ pass |
+ |
+ def __enter__(self): |
+ """Set up the test run when used as a context manager.""" |
+ self.SetUp() |
+ return self |
+ |
+ def __exit__(self, exc_type, exc_val, exc_tb): |
+ """Tear down the test run when used as a context manager.""" |
+ self.TearDown() |
+ |
+ 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.
|
+ raise NotImplementedError |
+ |
+ def GetTestByName(self, test_name): |
+ """Gets test_id for specific test. |
+ |
+ Args: |
+ test_name: Test to find the ID of. |
+ """ |
+ test_list_res = appurify.api.tests_list(self._env.token) |
+ remote_device_helper.TestHttpResponse(test_list_res, |
+ 'Unable to get tests list.') |
+ for test in test_list_res.json()['response']: |
+ if test['test_type'] == test_name: |
+ return test['test_id'] |
+ raise remote_device_helper.RemoteDeviceError( |
+ 'No test found with name %s' % (test_name)) |
+ |
+ 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.
|
+ """Download the test results from remote device service. |
+ |
+ Args: |
+ results_path: path to download results to. |
+ """ |
+ 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
|
+ if not os.path.exists(os.path.basename(results_path)): |
+ os.makedirs(os.path.basename(results_path)) |
+ appurify.utils.wget(self._results['url'], results_path) |
+ |
+ def WaitForTest(self, test_run_id): |
+ """Wait for remote service to have results of test. |
+ |
+ Args: |
+ test_run_id: id of test to wait for results of. |
+ """ |
+ test_status = 'in-progress' |
+ while test_status != self.COMPLETE: |
+ time.sleep(self.WAIT_TIME) |
+ 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.
|
+ test_run_id) |
+ remote_device_helper.TestHttpResponse(test_check_res, |
+ 'Unable to get test status.') |
+ test_status = test_check_res.json()['response']['status'] |
+ self._results = test_check_res.json()['response']['results'] |
+ |
+ def UploadAppToDevice(self, apk_path): |
+ """Upload app to device.""" |
+ apk_name = os.path.basename(apk_path) |
+ with open(apk_path, 'rb') as apk_src: |
+ upload_results = appurify.api.apps_upload(self._env.token, |
+ apk_src, 'raw', name=apk_name) |
+ remote_device_helper.TestHttpResponse(upload_results, |
+ 'Unable to upload %s.' %(apk_path)) |
+ return upload_results.json()['response']['app_id'] |
+ |
+ def UploadTestToDevice(self, test_type): |
+ """Upload test to device |
+ Args: |
+ test_type: Type of test that is being uploaded. Ex. uirobot, gtest.. |
+ """ |
+ with open(self._test_instance.apk, 'rb') as test_src: |
+ upload_results = appurify.api.tests_upload( |
+ self._env.token, test_src, 'raw', test_type, app_id=self._app_id) |
+ remote_device_helper.TestHttpResponse(upload_results, |
+ 'Unable to upload %s.' %(self._test_instance.apk)) |
+ return upload_results.json()['response']['test_id'] |
+ |
+ def SetTestConfig(self, runner_type, body): |
+ """Generates and uploads config file for test. |
+ Args: |
+ extras: Extra arguments to set in the config file. |
+ """ |
+ config = tempfile.TemporaryFile() |
+ config_data = ['[appurify]', '[' + runner_type + ']'] |
+ config_data.extend('%s=%s' % (k, v) for k, v in body.iteritems()) |
+ config.write(''.join('%s\n' % l for l in config_data)) |
+ config.flush() |
+ config.seek(0) |
+ config_response = appurify.api.config_upload(self._env.token, |
+ config, self._test_id) |
+ config.close() |
+ remote_device_helper.TestHttpResponse(config_response, |
+ 'Unable to upload test config.') |