# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This file is not auto-generated. Don't delete it.

# Boring.
import logging
import pprint
from autotest_lib.client.bin import utils

usage = """
1) To run agains a particular $DUT use
   test_that --args="module=CtsDeqpTestCases test=dEQP-GLES31.functional.image_load_store.2d_array.atomic#exchange_r32f_return_value" $DUT cheets_CTS_P.tradefed-run-test
   test_that --args="module=CtsViewTestCases test=android.view.cts.SurfaceViewSyncTest#testVideoSurfaceViewCornerCoverage retry=0 revision=9.0_r7" 100.90.29.152 cheets_CTS_P.tradefed-run-test

2) To run against a lab pool use
    run_suite.py --board=eve --build=$TRYJOB_BUILD --suite_name arc-cts-test --pool cts --no_wait True --priority CQ --timeout_mins 6160 --retry False --num 1 --suite_min_duts 1 --test_args="{'module' : 'CtsDeqpTestCases', 'test' : 'dEQP-GLES31.functional.image_load_store.2d_array.atomic#exchange_r32f_return_value'}"
"""

def usage_error():
    logging.info('Example usage:')
    logging.info(usage)
    raise SystemExit

pp = pprint.PrettyPrinter()
logging.info(
    '***********************************************************************')

# Define the variables that we are going to use and set sensible defaults.
cts_abi = 'arm'
cts_module = ''
cts_retry = 5
cts_revision = '9.0_r14'  # TODO(ihf): Set this default value from generator.
cts_test = ''
cts_timeout = 600

# Pull parameters either from run_suite or test_that.
if 'args_dict' in vars():
    logging.info('Raw test options from run_suite:')
    pp.pprint(args_dict)
elif args:
    logging.info('Raw test options from test_that:')
    pp.pprint(args)
    args_dict = utils.args_to_dict(args)
else:
    usage_error()

cts_abi = args_dict.get('abi', cts_abi)
cts_module = args_dict.get('module', cts_module)
cts_revision = args_dict.get('revision', cts_revision)
cts_test = args_dict.get('test', cts_test)
cts_timeout = float(args_dict.get('timeout', cts_timeout))
cts_retry = int(args_dict.get('max_retry', cts_retry))

# Basic checks for option validity.
logging.error('Running module %s with test %s on abi %s and revision %s',
              cts_module, cts_test, cts_abi, cts_revision)
if not cts_abi or not cts_module or not cts_revision or not cts_test:
    usage_error()

# And we are getting ready for tradefed.
uri = ('gs://chromeos-arc-images/cts/bundle/P/android-cts-' + cts_revision +
       '-linux_x86-' + cts_abi + '.zip')
run_template = ['run', 'commandAndExit', 'cts',
                '--include-filter', cts_module + ' ' + cts_test,
                '--logcat-on-failure']
retry_template = ['run', 'commandAndExit', 'retry',
                  '--retry', '{session_id}']
# Unfortunately super long test names can cause problems. Try to get the
# rightmost element and use that as a simplified name.
# TODO(ihf): fix pipeline so it works with super long names.
simplified_test = cts_test
if '#' in cts_test:
    simplified_test = cts_test.split('#')[-1]
elif '.' in cts_test:
    simplified_test = cts_test.split('.')[-1]
tag = 'tradefed-run-test.%s.%s' % (cts_module, simplified_test)

# The usual testing stanza. We are suppressing some DEPENDENCIES on purpose.
AUTHOR = 'ARC++ Team'
NAME = 'cheets_CTS_P.tradefed-run-test'
ATTRIBUTES = ''
DEPENDENCIES = 'arc'
JOB_RETRIES = 0
TEST_TYPE = 'server'
TIME = 'LONG'
MAX_RESULT_SIZE_KB = 256000
DOC = ('Run a test of the Android Compatibility Test Suite (CTS) in the ARC++ '
       'container.')

# And launch.
def run_TS(machine):
    host_list = [hosts.create_host(machine)]
    job.run_test(
        'cheets_CTS_P',
        hosts=host_list,
        iterations=1,
        max_retry=cts_retry,
        needs_push_media=True,
        tag=tag,
        test_name=NAME,
        run_template=run_template,
        retry_template=retry_template,
        target_module=None,
        target_plan=None,
        bundle=cts_abi,
        uri=uri,
        login_precondition_commands=[
            'lsblk -do NAME,RM | sed -n s/1$//p | xargs -n1 eject'
        ],
        precondition_commands=[
            'echo $(({0} % 2 * 2 + 1)) > /proc/sys/kernel/perf_event_paranoid',
            'modprobe configs'
        ],
        timeout=cts_timeout)

parallel_simple(run_TS, machines)