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