1# Lint as: python2, python3 2# Copyright 2018 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Class for running test inside wrapper tests.""" 7 8from __future__ import absolute_import 9from __future__ import division 10from __future__ import print_function 11 12import json 13import logging 14import six 15 16from autotest_lib.client.common_lib import error 17from autotest_lib.server import autotest 18from autotest_lib.server.cros.dynamic_suite import suite 19 20 21class WrapperTestRunner(object): 22 """A helper class for running test inside wrapper tests. 23 24 This class takes the tagged test name and finds the test in the autotest 25 directory. It also runs the test on the given DUT. 26 """ 27 28 def __init__(self, config, test_dir): 29 """Init WrapperTestRunner. 30 31 The test to run inside the wrapper test is usually a client side 32 autotest. Look up the control file of the inner test, and then prepend 33 the args to to the control file to pass the args to the inner test. 34 35 @param config: the args argument from test_that in a dict, contains the 36 test to look up in the autotest directory and the args to 37 prepend to the control file. 38 required data: {'test': 'test_TestName.tag'} 39 @param test_dir: the directory to retrieve the test from. 40 """ 41 if not config: 42 msg = 'Wrapper test must run with args input.' 43 raise error.TestNAError(msg) 44 if 'test' not in config: 45 msg = 'User did not specify client side test to run in wrapper.' 46 raise error.TestNAError(msg) 47 # test_name is tagged test name. 48 self._test_name = config['test'] 49 50 # Find the test in autotest file system. 51 fs_getter = suite.create_fs_getter(test_dir) 52 predicate = suite.test_name_equals_predicate(self._test_name) 53 test = suite.find_and_parse_tests(fs_getter, predicate) 54 if not test: 55 msg = '%s is not a valid test name.' % self._test_name 56 raise error.TestNAError(msg) 57 58 # If multiple tests with the same name are found, run the first one. 59 if len(test) > 1: 60 logging.warning('Found %d tests with name %s, running the first ' 61 'one.', len(test), self._test_name) 62 control_file_no_args = test[0].text 63 64 # Prepend the args. 65 args_list = ['='.join((k, str(v))) for k, v in six.iteritems(config)] 66 args_string = 'args = ' + json.dumps(args_list) 67 args_dict_string = 'args_dict = ' + json.dumps(config) 68 control_file_list = [args_string, args_dict_string] 69 control_file_list.extend( 70 ['%s = "%s"' % (k, str(v)) for k, v in six.iteritems(config)]) 71 control_file_list.append(control_file_no_args) 72 73 self._test_control_file = '\n'.join(control_file_list) 74 75 def run_test(self, host): 76 """Run the autotest from its control file on the specified DUT. 77 78 @param host: CrosHost object representing the DUT. 79 """ 80 autotest_client = autotest.Autotest(host) 81 autotest_client.run(self._test_control_file) 82 83 def get_tagged_test_name(self): 84 """Return the tagged test name to be run inside the wrapper. 85 86 @return the tagged test name to be run inside the wrapper. 87 """ 88 return self._test_name 89