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