Index: build/android/pylib/remote/device/remote_device_test_run.py.back |
diff --git a/build/android/pylib/remote/device/remote_device_test_run.py.back b/build/android/pylib/remote/device/remote_device_test_run.py.back |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1c00a0655765bf9d23553172eb345c14499e76d6 |
--- /dev/null |
+++ b/build/android/pylib/remote/device/remote_device_test_run.py.back |
@@ -0,0 +1,182 @@ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
jbudorick
2014/12/03 22:46:10
What is this file?
rnephew (Reviews Here)
2014/12/03 23:49:26
This shouldn't be here.
|
+# 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 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' |
+ DEFAULT_GTEST_RUNNER_TYPE = 'robotium' |
+ DEFAULT_UIROBOT_RUNNER_TYPE = 'android_robot' |
+ DEFAULT_GTEST_RUNNER_PACKAGE = ( |
+ 'org.chromium.native_test.ChromiumNativeTestInstrumentationTestRunner') |
+ |
+ 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 = '' |
+ |
+ def TestPackage(self): |
+ pass |
+ |
+ #override |
+ def SetUp(self): |
+ """Setup the test run.""" |
+ self._app_id = self.UploadAppToDevice() |
+ |
+ if self._test_instance.TestType() == 'gtest': |
+ if not self._env.runner_type: |
+ runner_type = self.DEFAULT_GTEST_RUNNER_TYPE |
+ else: |
+ runner_type = self._env.runner_type |
+ if not self._env.runner_package: |
+ runner_package = self.DEFAULT_GTEST_RUNNER_PACKAGE |
+ else: |
+ runner_package = self._env.runner_package |
+ self._test_id = self.UploadTestToDevice(runner_type) |
+ config_body = {'runner': runner_package} |
+ |
+ elif self._test_instance.TestType() == 'uirobot': |
+ if not self._env.runner_type: |
+ runner_type = self.DEFAULT_UIROBOT_RUNNER_TYPE |
+ else: |
+ runner_type = self._env.runner_type |
+ self._test_id = self.GetTestByName(runner_type) |
+ config_body = {'duration': self._test_instance.minutes} |
+ |
+ else: |
+ raise remote_device_helper.RemoteDeviceError('Unknown test type: %s' %( |
+ self._test_instance.TestType())) |
+ |
+ self.SetTestConfig(runner_type, config_body) |
+ |
+ #override |
+ def RunTest(self): |
+ """Run the test.""" |
+ 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'] |
+ self.WaitForTest(test_run_id) |
+ self.DownloadTestResults(self._env.results_path) |
+ return self._results |
+ |
+ #override |
+ def TearDown(self): |
+ """Teardown the test run.""" |
+ pass |
+ |
+ def __enter__(self): |
+ """Runs when entering with with keyword.""" |
+ self.SetUp() |
+ return self |
+ |
+ def __exit__(self, exc_type, exc_val, exc_tb): |
+ """Runs when exiting with with keyword.""" |
+ self.TearDown() |
+ |
+ 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): |
+ """Download the test results from remote device service. |
+ |
+ Args: |
+ results_path: path to download results to. |
+ """ |
+ if results_path: |
+ 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, |
+ 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): |
+ """Upload app to device.""" |
+ apk_name = os.path.basename(self._test_instance.apk) |
+ with open(self._test_instance.apk_under_test, '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.' %(self._test_instance.apk_under_test)) |
+ 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]\n', '[' + runner_type + ']\n'] |
+ config_data.extend('%s=%s\n' % (k, v) for k, v in body.iteritems()) |
+ config.writelines(config_data) |
+ 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.') |