1# Copyright 2016 The Chromium OS 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# repohooks/pre-upload.py currently does not run pylint. But for developers who 6# want to check their code manually we disable several harmless pylint warnings 7# which just distract from more serious remaining issues. 8# 9# The instance variable _android_gts is not defined in __init__(). 10# pylint: disable=attribute-defined-outside-init 11# 12# Many short variable names don't follow the naming convention. 13# pylint: disable=invalid-name 14 15import logging 16import os 17import shutil 18import tempfile 19 20from autotest_lib.server import utils 21from autotest_lib.server.cros import tradefed_test 22 23# Maximum default time allowed for each individual GTS module. 24_GTS_TIMEOUT_SECONDS = 3600 25_PARTNER_GTS_BUCKET = 'gs://chromeos-partner-gts/' 26_PARTNER_GTS_LOCATION = _PARTNER_GTS_BUCKET + 'gts-6.0_r4-5356336.zip' 27_PARTNER_GTS_AUTHKEY = _PARTNER_GTS_BUCKET + 'gts-arc.json' 28 29 30class cheets_GTS(tradefed_test.TradefedTest): 31 """Sets up tradefed to run GTS tests.""" 32 version = 1 33 34 _SHARD_CMD = '--shard-count' 35 36 def _tradefed_retry_command(self, template, session_id): 37 """Build tradefed 'retry' command from template.""" 38 cmd = [] 39 for arg in template: 40 cmd.append(arg.format(session_id=session_id)) 41 return cmd 42 43 def _tradefed_run_command(self, template): 44 """Build tradefed 'run' command from template.""" 45 cmd = template[:] 46 # If we are running outside of the lab we can collect more data. 47 if not utils.is_in_container(): 48 logging.info('Running outside of lab, adding extra debug options.') 49 cmd.append('--log-level-display=DEBUG') 50 return cmd 51 52 def _get_default_bundle_url(self, bundle): 53 return _PARTNER_GTS_LOCATION 54 55 def _get_default_authkey(self): 56 return _PARTNER_GTS_AUTHKEY 57 58 def _get_tradefed_base_dir(self): 59 return 'android-gts' 60 61 def _run_tradefed(self, commands): 62 """Kick off GTS. 63 64 @param commands: the command(s) to pass to GTS. 65 @return: The result object from utils.run. 66 """ 67 gts_tradefed = os.path.join(self._repository, 'tools', 'gts-tradefed') 68 env = None 69 if self._authkey: 70 env = dict(os.environ, APE_API_KEY=self._authkey) 71 with tradefed_test.adb_keepalive(self._get_adb_targets(), 72 self._install_paths): 73 for command in commands: 74 timeout = self._timeout * self._timeout_factor 75 logging.info('RUN(timeout=%d): ./gts-tradefed %s', timeout, 76 ' '.join(command)) 77 output = self._run( 78 gts_tradefed, 79 args=tuple(command), 80 env=env, 81 timeout=timeout, 82 verbose=True, 83 ignore_status=False, 84 # Make sure to tee tradefed stdout/stderr to autotest logs 85 # continuously during the test run. 86 stdout_tee=utils.TEE_TO_LOGS, 87 stderr_tee=utils.TEE_TO_LOGS, 88 # Also send the output to the test_that console. 89 stdout_level=logging.INFO) 90 logging.info('END: ./gts-tradefed %s\n', ' '.join(command)) 91 return output 92 93 def run_once(self, 94 test_name, 95 run_template, 96 retry_template=None, 97 target_module=None, 98 target_plan=None, 99 target_class=None, 100 target_method=None, 101 needs_push_media=False, 102 precondition_commands=[], 103 login_precondition_commands=[], 104 authkey=None, 105 timeout=_GTS_TIMEOUT_SECONDS): 106 """Runs the specified GTS once, but with several retries. 107 108 Run an arbitrary tradefed command. 109 110 @param test_name: the name of test. Used for logging. 111 @param run_template: the template to construct the run command. 112 Example: ['run', 'commandAndExit', 'cts', 113 '--skip-media-download'] 114 @param retry_template: the template to construct the retry command. 115 Example: ['run', 'commandAndExit', 'retry', 116 '--skip-media-download', '--retry', 117 '{session_id}'] 118 @param target_module: the name of test module to run. 119 @param target_plan: the name of the test plan to run. 120 @param target_class: the name of the class to be tested. 121 @param target_method: the name of the method to be tested. 122 @param needs_push_media: need to push test media streams. 123 @param timeout: time after which tradefed can be interrupted. 124 @param precondition_commands: a list of scripts to be run on the 125 dut before the test is run, the scripts must already be installed. 126 @param login_precondition_commands: a list of scripts to be run on the 127 dut before the log-in for the test is performed. 128 """ 129 # Download the GTS auth key to the local temp directory. 130 tmpdir = tempfile.mkdtemp() 131 try: 132 self._authkey = self._download_to_dir( 133 authkey or self._get_default_authkey(), tmpdir) 134 135 self._run_tradefed_with_retries( 136 test_name=test_name, 137 run_template=run_template, 138 retry_template=retry_template, 139 timeout=timeout, 140 target_module=target_module, 141 target_plan=target_plan, 142 needs_push_media=needs_push_media, 143 login_precondition_commands=login_precondition_commands, 144 precondition_commands=precondition_commands) 145 finally: 146 shutil.rmtree(tmpdir) 147