1# Copyright 2020 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_cts 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
17
18from autotest_lib.server import utils
19from autotest_lib.server.cros.tradefed import tradefed_test
20
21# Maximum default time allowed for each individual CTS module.
22_CTS_TIMEOUT_SECONDS = 3600
23
24# Public download locations for android cts bundles.
25_PUBLIC_CTS = 'https://dl.google.com/dl/android/cts/'
26_CTS_URI = {
27    'arm': _PUBLIC_CTS + 'android-cts-11_r2-linux_x86-arm.zip',
28    'x86': _PUBLIC_CTS + 'android-cts-11_r2-linux_x86-x86.zip',
29}
30_CTS_MEDIA_URI = _PUBLIC_CTS + 'android-cts-media-1.5.zip'
31_CTS_MEDIA_LOCALPATH = '/tmp/android-cts-media'
32
33# Internal uprev for all CTS modules.
34_INTERNAL_CTS = 'gs://chromeos-arc-images/cts/bundle/R/'
35_CTS_LATEST_URI = {
36        'arm': _INTERNAL_CTS + 'android-cts-7050651-linux_x86-arm.zip',
37        'x86': _INTERNAL_CTS + 'android-cts-7050651-linux_x86-x86.zip',
38}
39
40
41class cheets_CTS_R(tradefed_test.TradefedTest):
42    """Sets up tradefed to run CTS tests."""
43    version = 1
44
45    def _tradefed_retry_command(self, template, session_id):
46        """Build tradefed 'retry' command from template."""
47        cmd = []
48        for arg in template:
49            cmd.append(arg.format(session_id=session_id))
50        return cmd
51
52    def _tradefed_run_command(self, template):
53        """Build tradefed 'run' command from template."""
54        cmd = template[:]
55        # If we are running outside of the lab we can collect more data.
56        if not utils.is_in_container():
57            logging.info('Running outside of lab, adding extra debug options.')
58            cmd.append('--log-level-display=DEBUG')
59        return cmd
60
61    def _get_default_bundle_url(self, bundle):
62        return _CTS_URI[bundle]
63
64    def _get_latest_bundle_url(self, bundle):
65        return _CTS_LATEST_URI[bundle]
66
67    def _get_tradefed_base_dir(self):
68        return 'android-cts'
69
70    def _tradefed_cmd_path(self):
71        return os.path.join(self._repository, 'tools', 'cts-tradefed')
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                 bundle=None,
83                 precondition_commands=[],
84                 login_precondition_commands=[],
85                 timeout=_CTS_TIMEOUT_SECONDS):
86        """Runs the specified CTS once, but with several retries.
87
88        Run an arbitrary tradefed command.
89
90        @param test_name: the name of test. Used for logging.
91        @param run_template: the template to construct the run command.
92                             Example: ['run', 'commandAndExit', 'cts',
93                                       '--skip-media-download']
94        @param retry_template: the template to construct the retry command.
95                               Example: ['run', 'commandAndExit', 'retry',
96                                         '--skip-media-download', '--retry',
97                                         '{session_id}']
98        @param target_module: the name of test module to run.
99        @param target_plan: the name of the test plan to run.
100        @param needs_push_media: need to push test media streams.
101        @param executable_test_count: the known number of tests in the run
102        @param bundle: the type of the CTS bundle: 'arm' or 'x86'
103        @param precondition_commands: a list of scripts to be run on the
104        dut before the test is run, the scripts must already be installed.
105        @param login_precondition_commands: a list of scripts to be run on the
106        dut before the log-in for the test is performed.
107        @param timeout: time after which tradefed can be interrupted.
108        """
109        self._run_tradefed_with_retries(
110                test_name=test_name,
111                run_template=run_template,
112                retry_template=retry_template,
113                timeout=timeout,
114                target_module=target_module,
115                target_plan=target_plan,
116                media_asset=tradefed_test.MediaAsset(
117                        _CTS_MEDIA_URI if needs_push_media else None,
118                        _CTS_MEDIA_LOCALPATH),
119                enable_default_apps=enable_default_apps,
120                executable_test_count=executable_test_count,
121                bundle=bundle,
122                cts_uri=_CTS_URI,
123                login_precondition_commands=login_precondition_commands,
124                precondition_commands=precondition_commands)
125