Commit b74dd063 authored by rnephew's avatar rnephew Committed by Commit bot

Clean up logging and add device randomization.

If no device_name is picked and os_version is, will pick random device with
given os; and vice versa. If neither is picked, both are randomly picked.

Clean up logging. Disable appurify/requests logging if verbosity is
less than 2.

BUG=428729

Review URL: https://codereview.chromium.org/814653003

Cr-Commit-Position: refs/heads/master@{#310337}
parent 6f89af56
......@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import contextlib
import logging
import os
import sys
......@@ -25,3 +26,15 @@ while new_handler:
api = appurify.api
utils = appurify.utils
# This is not thread safe. If multiple threads are ever supported with appurify
# this may cause logging messages to go missing.
@contextlib.contextmanager
def SanitizeLogging(verbose_count, level):
if verbose_count < 2:
logging.disable(level)
yield True
logging.disable(logging.NOTSET)
else:
yield False
......@@ -6,6 +6,7 @@
import logging
import os
import random
import sys
from pylib import constants
......@@ -60,6 +61,8 @@ class RemoteDeviceEnvironment(environment.Environment):
self._runner_package = args.runner_package
self._runner_type = args.runner_type
self._device = ''
self._verbose_count = args.verbose_count
if not args.trigger and not args.collect:
self._trigger = True
self._collect = True
......@@ -96,6 +99,8 @@ class RemoteDeviceEnvironment(environment.Environment):
def _GetAccessToken(self):
"""Generates access token for remote device service."""
logging.info('Generating remote service access token')
with appurify_sanitized.SanitizeLogging(self._verbose_count,
logging.WARNING):
access_token_results = appurify_sanitized.api.access_token_generate(
self._api_key, self._api_secret)
remote_device_helper.TestHttpResponse(access_token_results,
......@@ -105,6 +110,8 @@ class RemoteDeviceEnvironment(environment.Environment):
def _RevokeAccessToken(self):
"""Destroys access token for remote device service."""
logging.info('Revoking remote service access token')
with appurify_sanitized.SanitizeLogging(self._verbose_count,
logging.WARNING):
revoke_token_results = appurify_sanitized.api.access_token_revoke(
self._access_token)
remote_device_helper.TestHttpResponse(revoke_token_results,
......@@ -112,15 +119,25 @@ class RemoteDeviceEnvironment(environment.Environment):
def _SelectDevice(self):
"""Select which device to use."""
logging.info('Finding %s with %s to run tests on.' %
(self._remote_device, self._remote_device_os))
logging.info('Finding device to run tests on.')
with appurify_sanitized.SanitizeLogging(self._verbose_count,
logging.WARNING):
dev_list_res = appurify_sanitized.api.devices_list(self._access_token)
remote_device_helper.TestHttpResponse(dev_list_res,
'Unable to generate access token.')
device_list = dev_list_res.json()['response']
random.shuffle(device_list)
for device in device_list:
if (device['name'] == self._remote_device
and device['os_version'] == self._remote_device_os):
if device['os_name'] != 'Android':
continue
if self._remote_device and device['name'] != self._remote_device:
continue
if (self._remote_device_os
and device['os_version'] != self._remote_device_os):
continue
if device['available_devices_count'] > 0:
logging.info('Found device: %s %s',
device['name'], device['os_version'])
return device['device_type_id']
self._NoDeviceFound(device_list)
......@@ -139,8 +156,7 @@ class RemoteDeviceEnvironment(environment.Environment):
def _NoDeviceFound(self, device_list):
self._PrintAvailableDevices(device_list)
raise remote_device_helper.RemoteDeviceError('No device found: %s %s' %
(self._remote_device, self._remote_device_os))
raise remote_device_helper.RemoteDeviceError('No device found.')
@property
def device(self):
......@@ -169,3 +185,7 @@ class RemoteDeviceEnvironment(environment.Environment):
@property
def collect(self):
return self._collect
@property
def verbose_count(self):
return self._verbose_count
......@@ -4,7 +4,6 @@
"""Common functions and Exceptions for remote_device_*"""
class RemoteDeviceError(Exception):
"""Exception to throw when problems occur with remote device service."""
pass
......
......@@ -37,11 +37,14 @@ class RemoteDeviceTestRun(test_run.TestRun):
self._test_id = ''
self._results = ''
self._test_run_id = ''
self._current_status = ''
#override
def RunTests(self):
"""Run the test."""
if self._env.trigger:
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
test_start_res = appurify_sanitized.api.tests_run(
self._env.token, self._env.device, self._app_id, self._test_id)
remote_device_helper.TestHttpResponse(
......@@ -68,8 +71,10 @@ class RemoteDeviceTestRun(test_run.TestRun):
#override
def TearDown(self):
"""Tear down the test run."""
if (self._GetTestStatus(self._test_run_id) != self.COMPLETE
and self._env.collect):
if (self._env.collect
and self._GetTestStatus(self._test_run_id) != self.COMPLETE):
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
test_abort_res = appurify_sanitized.api.tests_abort(
self._env.token, self._test_run_id, reason='Test runner exiting.')
remote_device_helper.TestHttpResponse(test_abort_res,
......@@ -103,6 +108,8 @@ class RemoteDeviceTestRun(test_run.TestRun):
Args:
test_name: Test to find the ID of.
"""
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
test_list_res = appurify_sanitized.api.tests_list(self._env.token)
remote_device_helper.TestHttpResponse(test_list_res,
'Unable to get tests list.')
......@@ -122,6 +129,8 @@ class RemoteDeviceTestRun(test_run.TestRun):
logging.info('Downloading results to %s.' % results_path)
if not os.path.exists(os.path.basename(results_path)):
os.makedirs(os.path.basename(results_path))
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
appurify_sanitized.utils.wget(self._results['results']['url'],
results_path)
......@@ -132,12 +141,16 @@ class RemoteDeviceTestRun(test_run.TestRun):
test_run_id: Id of test on on remote service.
"""
test_check_res = appurify_sanitized.api.tests_check_result(self._env.token,
test_run_id)
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
test_check_res = appurify_sanitized.api.tests_check_result(
self._env.token, test_run_id)
remote_device_helper.TestHttpResponse(test_check_res,
'Unable to get test status.')
self._results = test_check_res.json()['response']
logging.info('Test status: %s' % self._results['detailed_status'])
if self._results['detailed_status'] != self._current_status:
logging.info('Test status: %s', self._results['detailed_status'])
self._current_status = self._results['detailed_status']
return self._results['status']
def _AmInstrumentTestSetup(self, app_path, test_path, runner_package):
......@@ -170,9 +183,11 @@ class RemoteDeviceTestRun(test_run.TestRun):
def _UploadAppToDevice(self, app_path):
"""Upload app to device."""
logging.info('Upload %s to remote service.' % app_path)
logging.info('Uploading %s to remote service.', app_path)
apk_name = os.path.basename(app_path)
with open(app_path, 'rb') as apk_src:
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
upload_results = appurify_sanitized.api.apps_upload(
self._env.token, apk_src, 'raw', name=apk_name)
remote_device_helper.TestHttpResponse(
......@@ -186,6 +201,8 @@ class RemoteDeviceTestRun(test_run.TestRun):
"""
logging.info('Uploading %s to remote service.' % test_path)
with open(test_path, 'rb') as test_src:
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
upload_results = appurify_sanitized.api.tests_upload(
self._env.token, test_src, 'raw', test_type)
remote_device_helper.TestHttpResponse(upload_results,
......@@ -204,6 +221,8 @@ class RemoteDeviceTestRun(test_run.TestRun):
config.write(''.join('%s\n' % l for l in config_data))
config.flush()
config.seek(0)
with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
logging.WARNING):
config_response = appurify_sanitized.api.config_upload(
self._env.token, config, self._test_id)
remote_device_helper.TestHttpResponse(
......
......@@ -128,9 +128,9 @@ def AddRemoteDeviceOptions(parser):
group.add_argument('--collect', default='',
help=('Only collects the test results if set. '
'Gets test_run_id from given file path.'))
group.add_argument('--remote-device', default='Nexus 5',
group.add_argument('--remote-device', default='',
help=('Device type to run test on.'))
group.add_argument('--remote-device-os', default='4.4.2',
group.add_argument('--remote-device-os', default='',
help=('OS to have on the device.'))
group.add_argument('--results-path', default='',
help=('File path to download results to.'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment