1#!/usr/bin/env python3 2import argparse 3import multiprocessing 4import os 5import sys 6 7import nose2 8 9from infra.basetest import BRConfigTest 10 11 12def main(): 13 parser = argparse.ArgumentParser(description='Run Buildroot tests') 14 parser.add_argument('testname', nargs='*', 15 help='list of test cases to execute') 16 parser.add_argument('-l', '--list', action='store_true', 17 help='list of available test cases') 18 parser.add_argument('-a', '--all', action='store_true', 19 help='execute all test cases') 20 parser.add_argument('-s', '--stdout', action='store_true', 21 help='log everything to stdout') 22 parser.add_argument('-o', '--output', 23 help='output directory') 24 parser.add_argument('-d', '--download', 25 help='download directory') 26 parser.add_argument('-k', '--keep', 27 help='keep build directories', 28 action='store_true') 29 parser.add_argument('-t', '--testcases', type=int, default=1, 30 help='number of testcases to run simultaneously') 31 parser.add_argument('-j', '--jlevel', type=int, 32 help='BR2_JLEVEL to use for each testcase') 33 parser.add_argument('--timeout-multiplier', type=int, default=1, 34 help='increase timeouts (useful for slow machines)') 35 36 args = parser.parse_args() 37 38 script_path = os.path.realpath(__file__) 39 test_dir = os.path.dirname(script_path) 40 41 if args.stdout: 42 BRConfigTest.logtofile = False 43 44 if args.list: 45 print("List of tests") 46 nose2.discover(argv=[script_path, 47 "-s", test_dir, 48 "-v", 49 "--collect-only"], 50 plugins=["nose2.plugins.collect"]) 51 return 0 52 53 if args.download is None: 54 args.download = os.getenv("BR2_DL_DIR") 55 if args.download is None: 56 print("Missing download directory, please use -d/--download") 57 print("") 58 parser.print_help() 59 return 1 60 61 BRConfigTest.downloaddir = os.path.abspath(args.download) 62 63 if args.output is None: 64 print("Missing output directory, please use -o/--output") 65 print("") 66 parser.print_help() 67 return 1 68 69 if not os.path.exists(args.output): 70 os.mkdir(args.output) 71 72 BRConfigTest.outputdir = os.path.abspath(args.output) 73 74 if args.all is False and not args.testname: 75 print("No test selected") 76 print("") 77 parser.print_help() 78 return 1 79 80 BRConfigTest.keepbuilds = args.keep 81 82 if args.testcases != 1: 83 if args.testcases < 1: 84 print("Invalid number of testcases to run simultaneously") 85 print("") 86 parser.print_help() 87 return 1 88 # same default BR2_JLEVEL as package/Makefile.in 89 br2_jlevel = 1 + multiprocessing.cpu_count() 90 each_testcase = br2_jlevel / args.testcases 91 if each_testcase < 1: 92 each_testcase = 1 93 BRConfigTest.jlevel = each_testcase 94 95 if args.jlevel: 96 if args.jlevel < 0: 97 print("Invalid BR2_JLEVEL to use for each testcase") 98 print("") 99 parser.print_help() 100 return 1 101 # the user can override the auto calculated value 102 BRConfigTest.jlevel = args.jlevel 103 104 if args.timeout_multiplier < 1: 105 print("Invalid multiplier for timeout values") 106 print("") 107 parser.print_help() 108 return 1 109 BRConfigTest.timeout_multiplier = args.timeout_multiplier 110 111 nose2_args = ["-v", 112 "-N", str(args.testcases), 113 "-s", test_dir, 114 "-c", os.path.join(test_dir, "conf/unittest.cfg")] 115 116 if args.testname: 117 nose2_args += args.testname 118 119 nose2.discover(argv=nose2_args) 120 121 122if __name__ == "__main__": 123 sys.exit(main()) 124