1#!/usr/bin/python3 2# SPDX-License-Identifier: GPL-2.0 3# 4# A thin wrapper on top of the KUnit Kernel 5# 6# Copyright (C) 2019, Google LLC. 7# Author: Felix Guo <felixguoxiuping@gmail.com> 8# Author: Brendan Higgins <brendanhiggins@google.com> 9 10import argparse 11import sys 12import os 13import time 14import shutil 15 16from collections import namedtuple 17from enum import Enum, auto 18 19import kunit_config 20import kunit_kernel 21import kunit_parser 22 23KunitResult = namedtuple('KunitResult', ['status','result']) 24 25KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 26 'build_dir', 'defconfig', 27 'alltests', 'make_options']) 28 29KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0] 30 31class KunitStatus(Enum): 32 SUCCESS = auto() 33 CONFIG_FAILURE = auto() 34 BUILD_FAILURE = auto() 35 TEST_FAILURE = auto() 36 37def create_default_kunitconfig(): 38 if not os.path.exists(kunit_kernel.kunitconfig_path): 39 shutil.copyfile('arch/um/configs/kunit_defconfig', 40 kunit_kernel.kunitconfig_path) 41 42def get_kernel_root_path(): 43 parts = sys.argv[0] if not __file__ else __file__ 44 parts = os.path.realpath(parts).split('tools/testing/kunit') 45 if len(parts) != 2: 46 sys.exit(1) 47 return parts[0] 48 49def run_tests(linux: kunit_kernel.LinuxSourceTree, 50 request: KunitRequest) -> KunitResult: 51 config_start = time.time() 52 success = linux.build_reconfig(request.build_dir, request.make_options) 53 config_end = time.time() 54 if not success: 55 return KunitResult(KunitStatus.CONFIG_FAILURE, 'could not configure kernel') 56 57 kunit_parser.print_with_timestamp('Building KUnit Kernel ...') 58 59 build_start = time.time() 60 success = linux.build_um_kernel(request.alltests, 61 request.jobs, 62 request.build_dir, 63 request.make_options) 64 build_end = time.time() 65 if not success: 66 return KunitResult(KunitStatus.BUILD_FAILURE, 'could not build kernel') 67 68 kunit_parser.print_with_timestamp('Starting KUnit Kernel ...') 69 test_start = time.time() 70 kunit_output = linux.run_kernel( 71 timeout=None if request.alltests else request.timeout, 72 build_dir=request.build_dir) 73 if request.raw_output: 74 raw_output = kunit_parser.raw_output(kunit_output) 75 isolated = list(kunit_parser.isolate_kunit_output(raw_output)) 76 test_result = kunit_parser.parse_test_result(isolated) 77 else: 78 test_result = kunit_parser.parse_run_tests(kunit_output) 79 test_end = time.time() 80 81 kunit_parser.print_with_timestamp(( 82 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' + 83 'building, %.3fs running\n') % ( 84 test_end - config_start, 85 config_end - config_start, 86 build_end - build_start, 87 test_end - test_start)) 88 89 if test_result.status != kunit_parser.TestStatus.SUCCESS: 90 return KunitResult(KunitStatus.TEST_FAILURE, test_result) 91 else: 92 return KunitResult(KunitStatus.SUCCESS, test_result) 93 94def main(argv, linux=None): 95 parser = argparse.ArgumentParser( 96 description='Helps writing and running KUnit tests.') 97 subparser = parser.add_subparsers(dest='subcommand') 98 99 run_parser = subparser.add_parser('run', help='Runs KUnit tests.') 100 run_parser.add_argument('--raw_output', help='don\'t format output from kernel', 101 action='store_true') 102 103 run_parser.add_argument('--timeout', 104 help='maximum number of seconds to allow for all tests ' 105 'to run. This does not include time taken to build the ' 106 'tests.', 107 type=int, 108 default=300, 109 metavar='timeout') 110 111 run_parser.add_argument('--jobs', 112 help='As in the make command, "Specifies the number of ' 113 'jobs (commands) to run simultaneously."', 114 type=int, default=8, metavar='jobs') 115 116 run_parser.add_argument('--build_dir', 117 help='As in the make command, it specifies the build ' 118 'directory.', 119 type=str, default='', metavar='build_dir') 120 121 run_parser.add_argument('--defconfig', 122 help='Uses a default .kunitconfig.', 123 action='store_true') 124 125 run_parser.add_argument('--alltests', 126 help='Run all KUnit tests through allyesconfig', 127 action='store_true') 128 129 run_parser.add_argument('--make_options', 130 help='X=Y make option, can be repeated.', 131 action='append') 132 133 cli_args = parser.parse_args(argv) 134 135 if cli_args.subcommand == 'run': 136 if get_kernel_root_path(): 137 os.chdir(get_kernel_root_path()) 138 139 if cli_args.build_dir: 140 if not os.path.exists(cli_args.build_dir): 141 os.mkdir(cli_args.build_dir) 142 kunit_kernel.kunitconfig_path = os.path.join( 143 cli_args.build_dir, 144 kunit_kernel.kunitconfig_path) 145 146 if cli_args.defconfig: 147 create_default_kunitconfig() 148 149 if not linux: 150 linux = kunit_kernel.LinuxSourceTree() 151 152 request = KunitRequest(cli_args.raw_output, 153 cli_args.timeout, 154 cli_args.jobs, 155 cli_args.build_dir, 156 cli_args.defconfig, 157 cli_args.alltests, 158 cli_args.make_options) 159 result = run_tests(linux, request) 160 if result.status != KunitStatus.SUCCESS: 161 sys.exit(1) 162 else: 163 parser.print_help() 164 165if __name__ == '__main__': 166 main(sys.argv[1:]) 167