1#!/usr/bin/python2 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 15logging_CrashSender 16login_BadAuthentication 17 18This is intended for use only with Chrome OS test suits that leverage the 19dynamic suite infrastructure in server/cros/dynamic_suite.py. 20""" 21 22from __future__ import absolute_import 23from __future__ import division 24from __future__ import print_function 25 26import logging 27import optparse, os, sys 28 29# Silence messages relating to imports of missing, unneeded 30# modules. 31logging.basicConfig(level=logging.INFO) 32 33import common 34import autotest_lib.client.common_lib.cros as cros_lib 35import autotest_lib.server.cros.dynamic_suite.suite as suite_lib 36 37 38def parse_options(): 39 """Parse command line for arguments including autotest directory, suite 40 name, if to list stable tests only, and if to list all available suites. 41 """ 42 usage = "usage: %prog [options] suite_name" 43 parser = optparse.OptionParser(usage=usage) 44 parser.add_option('-a', '--autotest_dir', dest='autotest_dir', 45 default=os.path.abspath( 46 os.path.join(os.path.dirname(__file__), 47 os.pardir)), 48 help='Directory under which to search for tests.'\ 49 ' (e.g. /usr/local/autotest)') 50 parser.add_option('-l', '--listall', 51 action='store_true', default=False, 52 help='Print a listing of all suites. Ignores all args.') 53 options, args = parser.parse_args() 54 return parser, options, args 55 56 57def main(): 58 """Entry point to run the suite enumerator command.""" 59 parser, options, args = parse_options() 60 if options.listall: 61 if args: 62 print('Cannot use suite_name with --listall') 63 parser.print_help() 64 elif not args or len(args) != 1: 65 parser.print_help() 66 return 67 68 fs_getter = suite_lib.create_fs_getter(options.autotest_dir) 69 devserver = cros_lib.dev_server.ImageServer('') 70 if options.listall: 71 for suite in suite_lib.list_all_suites('', devserver, fs_getter): 72 print(suite) 73 return 74 75 suite = suite_lib.Suite.create_from_name(args[0], {}, '', devserver, 76 fs_getter) 77 # If in test list, print firmware_FAFTSetup before other tests 78 # NOTE: the test.name value can be *different* from the directory 79 # name that appears in test.path 80 PRETEST_LIST = ['firmware_FAFTSetup',] 81 for test in [test for test in suite.tests if test.name in 82 PRETEST_LIST]: 83 print(test.path) 84 for test in [test for test in suite.tests if test.name not in 85 PRETEST_LIST]: 86 print(test.path) 87 88 # Check if test_suites/control.suite_name exists. 89 control_path = os.path.join(options.autotest_dir, 'test_suites', 90 'control.' + args[0]) 91 if not os.path.exists(control_path): 92 print('Warning! control file is missing: %s' % 93 control_path, file=sys.stderr) 94 95 96if __name__ == "__main__": 97 sys.exit(main()) 98