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