]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - tools/testing/kunit/kunit.py
2 # SPDX-License-Identifier: GPL-2.0
4 # A thin wrapper on top of the KUnit Kernel
6 # Copyright (C) 2019, Google LLC.
7 # Author: Felix Guo <felixguoxiuping@gmail.com>
8 # Author: Brendan Higgins <brendanhiggins@google.com>
16 from collections
import namedtuple
17 from enum
import Enum
, auto
23 KunitResult
= namedtuple('KunitResult', ['status','result'])
25 KunitRequest
= namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir', 'defconfig'])
27 KernelDirectoryPath
= sys
.argv
[0].split('tools/testing/kunit/')[0]
29 class KunitStatus(Enum
):
31 CONFIG_FAILURE
= auto()
32 BUILD_FAILURE
= auto()
35 def create_default_kunitconfig():
36 if not os
.path
.exists(kunit_kernel
.kunitconfig_path
):
37 shutil
.copyfile('arch/um/configs/kunit_defconfig',
38 kunit_kernel
.kunitconfig_path
)
40 def get_kernel_root_path():
41 parts
= sys
.argv
[0] if not __file__
else __file__
42 parts
= os
.path
.realpath(parts
).split('tools/testing/kunit')
47 def run_tests(linux
: kunit_kernel
.LinuxSourceTree
,
48 request
: KunitRequest
) -> KunitResult
:
49 config_start
= time
.time()
50 success
= linux
.build_reconfig(request
.build_dir
)
51 config_end
= time
.time()
53 return KunitResult(KunitStatus
.CONFIG_FAILURE
, 'could not configure kernel')
55 kunit_parser
.print_with_timestamp('Building KUnit Kernel ...')
57 build_start
= time
.time()
58 success
= linux
.build_um_kernel(request
.jobs
, request
.build_dir
)
59 build_end
= time
.time()
61 return KunitResult(KunitStatus
.BUILD_FAILURE
, 'could not build kernel')
63 kunit_parser
.print_with_timestamp('Starting KUnit Kernel ...')
64 test_start
= time
.time()
66 test_result
= kunit_parser
.TestResult(kunit_parser
.TestStatus
.SUCCESS
,
69 if request
.raw_output
:
70 kunit_parser
.raw_output(
71 linux
.run_kernel(timeout
=request
.timeout
,
72 build_dir
=request
.build_dir
))
74 kunit_output
= linux
.run_kernel(timeout
=request
.timeout
,
75 build_dir
=request
.build_dir
)
76 test_result
= kunit_parser
.parse_run_tests(kunit_output
)
77 test_end
= time
.time()
79 kunit_parser
.print_with_timestamp((
80 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
81 'building, %.3fs running\n') % (
82 test_end
- config_start
,
83 config_end
- config_start
,
84 build_end
- build_start
,
85 test_end
- test_start
))
87 if test_result
.status
!= kunit_parser
.TestStatus
.SUCCESS
:
88 return KunitResult(KunitStatus
.TEST_FAILURE
, test_result
)
90 return KunitResult(KunitStatus
.SUCCESS
, test_result
)
92 def main(argv
, linux
=None):
93 parser
= argparse
.ArgumentParser(
94 description
='Helps writing and running KUnit tests.')
95 subparser
= parser
.add_subparsers(dest
='subcommand')
97 run_parser
= subparser
.add_parser('run', help='Runs KUnit tests.')
98 run_parser
.add_argument('--raw_output', help='don\'t format output from kernel',
101 run_parser
.add_argument('--timeout',
102 help='maximum number of seconds to allow for all tests '
103 'to run. This does not include time taken to build the '
109 run_parser
.add_argument('--jobs',
110 help='As in the make command, "Specifies the number of '
111 'jobs (commands) to run simultaneously."',
112 type=int, default
=8, metavar
='jobs')
114 run_parser
.add_argument('--build_dir',
115 help='As in the make command, it specifies the build '
117 type=str, default
='', metavar
='build_dir')
119 run_parser
.add_argument('--defconfig',
120 help='Uses a default .kunitconfig.',
123 cli_args
= parser
.parse_args(argv
)
125 if cli_args
.subcommand
== 'run':
126 if get_kernel_root_path():
127 os
.chdir(get_kernel_root_path())
129 if cli_args
.build_dir
:
130 if not os
.path
.exists(cli_args
.build_dir
):
131 os
.mkdir(cli_args
.build_dir
)
132 kunit_kernel
.kunitconfig_path
= os
.path
.join(
134 kunit_kernel
.kunitconfig_path
)
136 if cli_args
.defconfig
:
137 create_default_kunitconfig()
140 linux
= kunit_kernel
.LinuxSourceTree()
142 request
= KunitRequest(cli_args
.raw_output
,
147 result
= run_tests(linux
, request
)
148 if result
.status
!= KunitStatus
.SUCCESS
:
153 if __name__
== '__main__':