xref: /aosp_15_r20/external/autotest/site_utils/suite_enumerator.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
2#
3# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Tool for enumerating the tests in a given suite.
8
9Given an autotest root directory and a suite name (e.g., bvt, regression), this
10tool will print out the name of each test in that suite, one per line.
11
12Example:
13$ ./site_utils/suite_enumerator.py -a /usr/local/autotest bvt 2>/dev/null
14login_LoginSuccess
15login_BadAuthentication
16
17This is intended for use only with ChromeOS test suits that leverage the
18dynamic suite infrastructure in server/cros/dynamic_suite.py.
19"""
20
21from __future__ import absolute_import
22from __future__ import division
23from __future__ import print_function
24
25import logging
26import optparse, os, sys
27
28# Silence messages relating to imports of missing, unneeded
29# modules.
30logging.basicConfig(level=logging.INFO)
31
32import common
33import autotest_lib.client.common_lib.cros as cros_lib
34import autotest_lib.server.cros.dynamic_suite.suite as suite_lib
35
36
37def parse_options():
38    """Parse command line for arguments including autotest directory, suite
39    name, if to list stable tests only, and if to list all available suites.
40    """
41    usage = "usage: %prog [options] suite_name"
42    parser = optparse.OptionParser(usage=usage)
43    parser.add_option('-a', '--autotest_dir', dest='autotest_dir',
44                      default=os.path.abspath(
45                          os.path.join(os.path.dirname(__file__),
46                                       os.pardir)),
47                      help='Directory under which to search for tests.'\
48                           ' (e.g. /usr/local/autotest)')
49    parser.add_option('-l', '--listall',
50                      action='store_true', default=False,
51                      help='Print a listing of all suites. Ignores all args.')
52    options, args = parser.parse_args()
53    return parser, options, args
54
55
56def main():
57    """Entry point to run the suite enumerator command."""
58    parser, options, args = parse_options()
59    if options.listall:
60        if args:
61            print('Cannot use suite_name with --listall')
62            parser.print_help()
63    elif not args or len(args) != 1:
64        parser.print_help()
65        return
66
67    fs_getter = suite_lib.create_fs_getter(options.autotest_dir)
68    devserver = cros_lib.dev_server.ImageServer('')
69    if options.listall:
70        for suite in suite_lib.list_all_suites('', devserver, fs_getter):
71            print(suite)
72        return
73
74    suite = suite_lib.Suite.create_from_name(args[0], {}, '', devserver,
75                                             fs_getter)
76    # If in test list, print firmware_FAFTSetup before other tests
77    # NOTE: the test.name value can be *different* from the directory
78    # name that appears in test.path
79    PRETEST_LIST = ['firmware_FAFTSetup',]
80    for test in [test for test in suite.tests if test.name in
81                 PRETEST_LIST]:
82        print(test.path)
83    for test in [test for test in suite.tests if test.name not in
84                 PRETEST_LIST]:
85        print(test.path)
86
87    # Check if test_suites/control.suite_name exists.
88    control_path = os.path.join(options.autotest_dir, 'test_suites',
89                                'control.' + args[0])
90    if not os.path.exists(control_path):
91        print('Warning! control file is missing: %s' %
92              control_path, file=sys.stderr)
93
94
95if __name__ == "__main__":
96    sys.exit(main())
97