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 @@ ...@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
import contextlib
import logging import logging
import os import os
import sys import sys
...@@ -25,3 +26,15 @@ while new_handler: ...@@ -25,3 +26,15 @@ while new_handler:
api = appurify.api api = appurify.api
utils = appurify.utils 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 @@ ...@@ -6,6 +6,7 @@
import logging import logging
import os import os
import random
import sys import sys
from pylib import constants from pylib import constants
...@@ -60,6 +61,8 @@ class RemoteDeviceEnvironment(environment.Environment): ...@@ -60,6 +61,8 @@ class RemoteDeviceEnvironment(environment.Environment):
self._runner_package = args.runner_package self._runner_package = args.runner_package
self._runner_type = args.runner_type self._runner_type = args.runner_type
self._device = '' self._device = ''
self._verbose_count = args.verbose_count
if not args.trigger and not args.collect: if not args.trigger and not args.collect:
self._trigger = True self._trigger = True
self._collect = True self._collect = True
...@@ -96,8 +99,10 @@ class RemoteDeviceEnvironment(environment.Environment): ...@@ -96,8 +99,10 @@ class RemoteDeviceEnvironment(environment.Environment):
def _GetAccessToken(self): def _GetAccessToken(self):
"""Generates access token for remote device service.""" """Generates access token for remote device service."""
logging.info('Generating remote service access token') logging.info('Generating remote service access token')
access_token_results = appurify_sanitized.api.access_token_generate( with appurify_sanitized.SanitizeLogging(self._verbose_count,
self._api_key, self._api_secret) logging.WARNING):
access_token_results = appurify_sanitized.api.access_token_generate(
self._api_key, self._api_secret)
remote_device_helper.TestHttpResponse(access_token_results, remote_device_helper.TestHttpResponse(access_token_results,
'Unable to generate access token.') 'Unable to generate access token.')
self._access_token = access_token_results.json()['response']['access_token'] self._access_token = access_token_results.json()['response']['access_token']
...@@ -105,22 +110,34 @@ class RemoteDeviceEnvironment(environment.Environment): ...@@ -105,22 +110,34 @@ class RemoteDeviceEnvironment(environment.Environment):
def _RevokeAccessToken(self): def _RevokeAccessToken(self):
"""Destroys access token for remote device service.""" """Destroys access token for remote device service."""
logging.info('Revoking remote service access token') logging.info('Revoking remote service access token')
revoke_token_results = appurify_sanitized.api.access_token_revoke( with appurify_sanitized.SanitizeLogging(self._verbose_count,
self._access_token) logging.WARNING):
revoke_token_results = appurify_sanitized.api.access_token_revoke(
self._access_token)
remote_device_helper.TestHttpResponse(revoke_token_results, remote_device_helper.TestHttpResponse(revoke_token_results,
'Unable to revoke access token.') 'Unable to revoke access token.')
def _SelectDevice(self): def _SelectDevice(self):
"""Select which device to use.""" """Select which device to use."""
logging.info('Finding %s with %s to run tests on.' % logging.info('Finding device to run tests on.')
(self._remote_device, self._remote_device_os)) with appurify_sanitized.SanitizeLogging(self._verbose_count,
dev_list_res = appurify_sanitized.api.devices_list(self._access_token) logging.WARNING):
dev_list_res = appurify_sanitized.api.devices_list(self._access_token)
remote_device_helper.TestHttpResponse(dev_list_res, remote_device_helper.TestHttpResponse(dev_list_res,
'Unable to generate access token.') 'Unable to generate access token.')
device_list = dev_list_res.json()['response'] device_list = dev_list_res.json()['response']
random.shuffle(device_list)
for device in device_list: for device in device_list:
if (device['name'] == self._remote_device if device['os_name'] != 'Android':
and device['os_version'] == self._remote_device_os): 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'] return device['device_type_id']
self._NoDeviceFound(device_list) self._NoDeviceFound(device_list)
...@@ -139,8 +156,7 @@ class RemoteDeviceEnvironment(environment.Environment): ...@@ -139,8 +156,7 @@ class RemoteDeviceEnvironment(environment.Environment):
def _NoDeviceFound(self, device_list): def _NoDeviceFound(self, device_list):
self._PrintAvailableDevices(device_list) self._PrintAvailableDevices(device_list)
raise remote_device_helper.RemoteDeviceError('No device found: %s %s' % raise remote_device_helper.RemoteDeviceError('No device found.')
(self._remote_device, self._remote_device_os))
@property @property
def device(self): def device(self):
...@@ -169,3 +185,7 @@ class RemoteDeviceEnvironment(environment.Environment): ...@@ -169,3 +185,7 @@ class RemoteDeviceEnvironment(environment.Environment):
@property @property
def collect(self): def collect(self):
return self._collect return self._collect
@property
def verbose_count(self):
return self._verbose_count
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
"""Common functions and Exceptions for remote_device_*""" """Common functions and Exceptions for remote_device_*"""
class RemoteDeviceError(Exception): class RemoteDeviceError(Exception):
"""Exception to throw when problems occur with remote device service.""" """Exception to throw when problems occur with remote device service."""
pass pass
......
...@@ -37,13 +37,16 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -37,13 +37,16 @@ class RemoteDeviceTestRun(test_run.TestRun):
self._test_id = '' self._test_id = ''
self._results = '' self._results = ''
self._test_run_id = '' self._test_run_id = ''
self._current_status = ''
#override #override
def RunTests(self): def RunTests(self):
"""Run the test.""" """Run the test."""
if self._env.trigger: if self._env.trigger:
test_start_res = appurify_sanitized.api.tests_run( with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
self._env.token, self._env.device, self._app_id, self._test_id) 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( remote_device_helper.TestHttpResponse(
test_start_res, 'Unable to run test.') test_start_res, 'Unable to run test.')
self._test_run_id = test_start_res.json()['response']['test_run_id'] self._test_run_id = test_start_res.json()['response']['test_run_id']
...@@ -68,10 +71,12 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -68,10 +71,12 @@ class RemoteDeviceTestRun(test_run.TestRun):
#override #override
def TearDown(self): def TearDown(self):
"""Tear down the test run.""" """Tear down the test run."""
if (self._GetTestStatus(self._test_run_id) != self.COMPLETE if (self._env.collect
and self._env.collect): and self._GetTestStatus(self._test_run_id) != self.COMPLETE):
test_abort_res = appurify_sanitized.api.tests_abort( with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
self._env.token, self._test_run_id, reason='Test runner exiting.') 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, remote_device_helper.TestHttpResponse(test_abort_res,
'Unable to abort test.') 'Unable to abort test.')
...@@ -103,7 +108,9 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -103,7 +108,9 @@ class RemoteDeviceTestRun(test_run.TestRun):
Args: Args:
test_name: Test to find the ID of. test_name: Test to find the ID of.
""" """
test_list_res = appurify_sanitized.api.tests_list(self._env.token) 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, remote_device_helper.TestHttpResponse(test_list_res,
'Unable to get tests list.') 'Unable to get tests list.')
for test in test_list_res.json()['response']: for test in test_list_res.json()['response']:
...@@ -122,8 +129,10 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -122,8 +129,10 @@ class RemoteDeviceTestRun(test_run.TestRun):
logging.info('Downloading results to %s.' % results_path) logging.info('Downloading results to %s.' % results_path)
if not os.path.exists(os.path.basename(results_path)): if not os.path.exists(os.path.basename(results_path)):
os.makedirs(os.path.basename(results_path)) os.makedirs(os.path.basename(results_path))
appurify_sanitized.utils.wget(self._results['results']['url'], with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
results_path) logging.WARNING):
appurify_sanitized.utils.wget(self._results['results']['url'],
results_path)
def _GetTestStatus(self, test_run_id): def _GetTestStatus(self, test_run_id):
"""Checks the state of the test, and sets self._results """Checks the state of the test, and sets self._results
...@@ -132,12 +141,16 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -132,12 +141,16 @@ class RemoteDeviceTestRun(test_run.TestRun):
test_run_id: Id of test on on remote service. test_run_id: Id of test on on remote service.
""" """
test_check_res = appurify_sanitized.api.tests_check_result(self._env.token, with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
test_run_id) logging.WARNING):
test_check_res = appurify_sanitized.api.tests_check_result(
self._env.token, test_run_id)
remote_device_helper.TestHttpResponse(test_check_res, remote_device_helper.TestHttpResponse(test_check_res,
'Unable to get test status.') 'Unable to get test status.')
self._results = test_check_res.json()['response'] 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'] return self._results['status']
def _AmInstrumentTestSetup(self, app_path, test_path, runner_package): def _AmInstrumentTestSetup(self, app_path, test_path, runner_package):
...@@ -170,11 +183,13 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -170,11 +183,13 @@ class RemoteDeviceTestRun(test_run.TestRun):
def _UploadAppToDevice(self, app_path): def _UploadAppToDevice(self, app_path):
"""Upload app to device.""" """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) apk_name = os.path.basename(app_path)
with open(app_path, 'rb') as apk_src: with open(app_path, 'rb') as apk_src:
upload_results = appurify_sanitized.api.apps_upload( with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
self._env.token, apk_src, 'raw', name=apk_name) logging.WARNING):
upload_results = appurify_sanitized.api.apps_upload(
self._env.token, apk_src, 'raw', name=apk_name)
remote_device_helper.TestHttpResponse( remote_device_helper.TestHttpResponse(
upload_results, 'Unable to upload %s.' % app_path) upload_results, 'Unable to upload %s.' % app_path)
return upload_results.json()['response']['app_id'] return upload_results.json()['response']['app_id']
...@@ -186,8 +201,10 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -186,8 +201,10 @@ class RemoteDeviceTestRun(test_run.TestRun):
""" """
logging.info('Uploading %s to remote service.' % test_path) logging.info('Uploading %s to remote service.' % test_path)
with open(test_path, 'rb') as test_src: with open(test_path, 'rb') as test_src:
upload_results = appurify_sanitized.api.tests_upload( with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
self._env.token, test_src, 'raw', test_type) logging.WARNING):
upload_results = appurify_sanitized.api.tests_upload(
self._env.token, test_src, 'raw', test_type)
remote_device_helper.TestHttpResponse(upload_results, remote_device_helper.TestHttpResponse(upload_results,
'Unable to upload %s.' % test_path) 'Unable to upload %s.' % test_path)
return upload_results.json()['response']['test_id'] return upload_results.json()['response']['test_id']
...@@ -204,7 +221,9 @@ class RemoteDeviceTestRun(test_run.TestRun): ...@@ -204,7 +221,9 @@ class RemoteDeviceTestRun(test_run.TestRun):
config.write(''.join('%s\n' % l for l in config_data)) config.write(''.join('%s\n' % l for l in config_data))
config.flush() config.flush()
config.seek(0) config.seek(0)
config_response = appurify_sanitized.api.config_upload( with appurify_sanitized.SanitizeLogging(self._env.verbose_count,
self._env.token, config, self._test_id) logging.WARNING):
config_response = appurify_sanitized.api.config_upload(
self._env.token, config, self._test_id)
remote_device_helper.TestHttpResponse( remote_device_helper.TestHttpResponse(
config_response, 'Unable to upload test config.') config_response, 'Unable to upload test config.')
...@@ -128,9 +128,9 @@ def AddRemoteDeviceOptions(parser): ...@@ -128,9 +128,9 @@ def AddRemoteDeviceOptions(parser):
group.add_argument('--collect', default='', group.add_argument('--collect', default='',
help=('Only collects the test results if set. ' help=('Only collects the test results if set. '
'Gets test_run_id from given file path.')) '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.')) 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.')) help=('OS to have on the device.'))
group.add_argument('--results-path', default='', group.add_argument('--results-path', default='',
help=('File path to download results to.')) 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