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-8-R2-6955212.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
54        return cmd
55
56    def _get_default_bundle_url(self, bundle):
57        return _PARTNER_GTS_LOCATION
58
59    def _get_default_authkey(self):
60        return _PARTNER_GTS_AUTHKEY
61
62    def _get_tradefed_base_dir(self):
63        return 'android-gts'
64
65    def _tradefed_cmd_path(self):
66        return os.path.join(self._repository, 'tools', 'gts-tradefed')
67
68    def _tradefed_env(self):
69        if self._authkey:
70            return dict(os.environ, APE_API_KEY=self._authkey)
71        return None
72
73    def run_once(self,
74                 test_name,
75                 run_template,
76                 retry_template=None,
77                 target_module=None,
78                 target_plan=None,
79                 needs_push_media=False,
80                 enable_default_apps=False,
81                 executable_test_count=None,
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 needs_push_media: need to push test media streams.
102        @param executable_test_count: the known number of tests in the run.
103        @param timeout: time after which tradefed can be interrupted.
104        @param precondition_commands: a list of scripts to be run on the
105        dut before the test is run, the scripts must already be installed.
106        @param login_precondition_commands: a list of scripts to be run on the
107        dut before the log-in for the test is performed.
108        @param prerequisites: a list of prerequisites that identify rogue DUTs.
109        """
110        # Download the GTS auth key to the local temp directory.
111        tmpdir = tempfile.mkdtemp()
112        try:
113            self._authkey = self._download_to_dir(
114                authkey or self._get_default_authkey(), tmpdir)
115
116            self._run_tradefed_with_retries(
117                test_name=test_name,
118                run_template=run_template,
119                retry_template=retry_template,
120                timeout=timeout,
121                target_module=target_module,
122                target_plan=target_plan,
123                media_asset=tradefed_test.MediaAsset(
124                    _GTS_MEDIA_URI if needs_push_media else None,
125                    _GTS_MEDIA_LOCALPATH),
126                enable_default_apps=enable_default_apps,
127                executable_test_count=executable_test_count,
128                login_precondition_commands=login_precondition_commands,
129                precondition_commands=precondition_commands,
130                prerequisites=prerequisites)
131        finally:
132            shutil.rmtree(tmpdir)
133