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.tradefed 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-7_r3-6045416.zip' 27_PARTNER_GTS_AUTHKEY = _PARTNER_GTS_BUCKET + 'gts-arc.json' 28_GTS_MEDIA_URI = ('https://storage.googleapis.com/youtube-test-media/gts/' + 29 'GtsYouTubeTestCases-media-1.2.zip') 30_GTS_MEDIA_LOCALPATH = '/tmp/android-gts-media/GtsYouTubeTestCases' 31 32 33class cheets_GTS(tradefed_test.TradefedTest): 34 """Sets up tradefed to run GTS tests.""" 35 version = 1 36 37 _SHARD_CMD = '--shard-count' 38 39 def _tradefed_retry_command(self, template, session_id): 40 """Build tradefed 'retry' command from template.""" 41 cmd = [] 42 for arg in template: 43 cmd.append(arg.format(session_id=session_id)) 44 return cmd 45 46 def _tradefed_run_command(self, template): 47 """Build tradefed 'run' command from template.""" 48 cmd = template[:] 49 # If we are running outside of the lab we can collect more data. 50 if not utils.is_in_container(): 51 logging.info('Running outside of lab, adding extra debug options.') 52 cmd.append('--log-level-display=DEBUG') 53 return cmd 54 55 def _get_default_bundle_url(self, bundle): 56 return _PARTNER_GTS_LOCATION 57 58 def _get_default_authkey(self): 59 return _PARTNER_GTS_AUTHKEY 60 61 def _get_tradefed_base_dir(self): 62 return 'android-gts' 63 64 def _tradefed_cmd_path(self): 65 return os.path.join(self._repository, 'tools', 'gts-tradefed') 66 67 def _tradefed_env(self): 68 if self._authkey: 69 return dict(os.environ, APE_API_KEY=self._authkey) 70 return None 71 72 def run_once(self, 73 test_name, 74 run_template, 75 retry_template=None, 76 target_module=None, 77 target_plan=None, 78 target_class=None, 79 target_method=None, 80 needs_push_media=False, 81 enable_default_apps=False, 82 precondition_commands=[], 83 login_precondition_commands=[], 84 authkey=None, 85 prerequisites=[], 86 timeout=_GTS_TIMEOUT_SECONDS): 87 """Runs the specified GTS once, but with several retries. 88 89 Run an arbitrary tradefed command. 90 91 @param test_name: the name of test. Used for logging. 92 @param run_template: the template to construct the run command. 93 Example: ['run', 'commandAndExit', 'cts', 94 '--skip-media-download'] 95 @param retry_template: the template to construct the retry command. 96 Example: ['run', 'commandAndExit', 'retry', 97 '--skip-media-download', '--retry', 98 '{session_id}'] 99 @param target_module: the name of test module to run. 100 @param target_plan: the name of the test plan to run. 101 @param target_class: the name of the class to be tested. 102 @param target_method: the name of the method to be tested. 103 @param needs_push_media: need to push test media streams. 104 @param timeout: time after which tradefed can be interrupted. 105 @param precondition_commands: a list of scripts to be run on the 106 dut before the test is run, the scripts must already be installed. 107 @param login_precondition_commands: a list of scripts to be run on the 108 dut before the log-in for the test is performed. 109 @param prerequisites: a list of prerequisites that identify rogue DUTs. 110 """ 111 # Download the GTS auth key to the local temp directory. 112 tmpdir = tempfile.mkdtemp() 113 try: 114 self._authkey = self._download_to_dir( 115 authkey or self._get_default_authkey(), tmpdir) 116 117 self._run_tradefed_with_retries( 118 test_name=test_name, 119 run_template=run_template, 120 retry_template=retry_template, 121 timeout=timeout, 122 target_module=target_module, 123 target_plan=target_plan, 124 media_asset=tradefed_test.MediaAsset( 125 _GTS_MEDIA_URI if needs_push_media else None, 126 _GTS_MEDIA_LOCALPATH), 127 enable_default_apps=enable_default_apps, 128 login_precondition_commands=login_precondition_commands, 129 precondition_commands=precondition_commands, 130 prerequisites=prerequisites) 131 finally: 132 shutil.rmtree(tmpdir) 133