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 14 15from collections import namedtuple 16from enum import Enum, auto 17 18import kunit_config 19import kunit_kernel 20import kunit_parser 21 22KunitResult = namedtuple('KunitResult', ['status','result']) 23 24KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir']) 25 26class KunitStatus(Enum): 27 SUCCESS = auto() 28 CONFIG_FAILURE = auto() 29 BUILD_FAILURE = auto() 30 TEST_FAILURE = auto() 31 32def run_tests(linux: kunit_kernel.LinuxSourceTree, 33 request: KunitRequest) -> KunitResult: 34 config_start = time.time() 35 success = linux.build_reconfig(request.build_dir) 36 config_end = time.time() 37 if not success: 38 return KunitResult(KunitStatus.CONFIG_FAILURE, 'could not configure kernel') 39 40 kunit_parser.print_with_timestamp('Building KUnit Kernel ...') 41 42 build_start = time.time() 43 success = linux.build_um_kernel(request.jobs, request.build_dir) 44 build_end = time.time() 45 if not success: 46 return KunitResult(KunitStatus.BUILD_FAILURE, 'could not build kernel') 47 48 kunit_parser.print_with_timestamp('Starting KUnit Kernel ...') 49 test_start = time.time() 50 51 test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS, 52 [], 53 'Tests not Parsed.') 54 if request.raw_output: 55 kunit_parser.raw_output( 56 linux.run_kernel(timeout=request.timeout)) 57 else: 58 kunit_output = linux.run_kernel(timeout=request.timeout) 59 test_result = kunit_parser.parse_run_tests(kunit_output) 60 test_end = time.time() 61 62 kunit_parser.print_with_timestamp(( 63 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' + 64 'building, %.3fs running\n') % ( 65 test_end - config_start, 66 config_end - config_start, 67 build_end - build_start, 68 test_end - test_start)) 69 70 if test_result.status != kunit_parser.TestStatus.SUCCESS: 71 return KunitResult(KunitStatus.TEST_FAILURE, test_result) 72 else: 73 return KunitResult(KunitStatus.SUCCESS, test_result) 74 75def main(argv, linux): 76 parser = argparse.ArgumentParser( 77 description='Helps writing and running KUnit tests.') 78 subparser = parser.add_subparsers(dest='subcommand') 79 80 run_parser = subparser.add_parser('run', help='Runs KUnit tests.') 81 run_parser.add_argument('--raw_output', help='don\'t format output from kernel', 82 action='store_true') 83 84 run_parser.add_argument('--timeout', 85 help='maximum number of seconds to allow for all tests ' 86 'to run. This does not include time taken to build the ' 87 'tests.', 88 type=int, 89 default=300, 90 metavar='timeout') 91 92 run_parser.add_argument('--jobs', 93 help='As in the make command, "Specifies the number of ' 94 'jobs (commands) to run simultaneously."', 95 type=int, default=8, metavar='jobs') 96 97 run_parser.add_argument('--build_dir', 98 help='As in the make command, it specifies the build ' 99 'directory.', 100 type=str, default=None, metavar='build_dir') 101 102 cli_args = parser.parse_args(argv) 103 104 if cli_args.subcommand == 'run': 105 request = KunitRequest(cli_args.raw_output, 106 cli_args.timeout, 107 cli_args.jobs, 108 cli_args.build_dir) 109 result = run_tests(linux, request) 110 if result.status != KunitStatus.SUCCESS: 111 sys.exit(1) 112 else: 113 parser.print_help() 114 115if __name__ == '__main__': 116 main(sys.argv[1:], kunit_kernel.LinuxSourceTree()) 117