~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/kunit/kunit.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /tools/testing/kunit/kunit.py (Version linux-6.12-rc7) and /tools/testing/kunit/kunit.py (Version linux-5.8.18)


  1 #!/usr/bin/env python3                         !!   1 #!/usr/bin/python3
  2 # SPDX-License-Identifier: GPL-2.0                  2 # SPDX-License-Identifier: GPL-2.0
  3 #                                                   3 #
  4 # A thin wrapper on top of the KUnit Kernel         4 # A thin wrapper on top of the KUnit Kernel
  5 #                                                   5 #
  6 # Copyright (C) 2019, Google LLC.                   6 # Copyright (C) 2019, Google LLC.
  7 # Author: Felix Guo <felixguoxiuping@gmail.com>      7 # Author: Felix Guo <felixguoxiuping@gmail.com>
  8 # Author: Brendan Higgins <brendanhiggins@googl      8 # Author: Brendan Higgins <brendanhiggins@google.com>
  9                                                     9 
 10 import argparse                                    10 import argparse
 11 import os                                      << 
 12 import re                                      << 
 13 import shlex                                   << 
 14 import sys                                         11 import sys
                                                   >>  12 import os
 15 import time                                        13 import time
                                                   >>  14 import shutil
 16                                                    15 
 17 assert sys.version_info >= (3, 7), "Python ver !!  16 from collections import namedtuple
 18                                                << 
 19 from dataclasses import dataclass              << 
 20 from enum import Enum, auto                        17 from enum import Enum, auto
 21 from typing import Iterable, List, Optional, S << 
 22                                                    18 
 23 import kunit_json                              !!  19 import kunit_config
 24 import kunit_kernel                                20 import kunit_kernel
 25 import kunit_parser                                21 import kunit_parser
 26 from kunit_printer import stdout               !!  22 
                                                   >>  23 KunitResult = namedtuple('KunitResult', ['status','result','elapsed_time'])
                                                   >>  24 
                                                   >>  25 KunitConfigRequest = namedtuple('KunitConfigRequest',
                                                   >>  26                                 ['build_dir', 'make_options'])
                                                   >>  27 KunitBuildRequest = namedtuple('KunitBuildRequest',
                                                   >>  28                                ['jobs', 'build_dir', 'alltests',
                                                   >>  29                                 'make_options'])
                                                   >>  30 KunitExecRequest = namedtuple('KunitExecRequest',
                                                   >>  31                               ['timeout', 'build_dir', 'alltests'])
                                                   >>  32 KunitParseRequest = namedtuple('KunitParseRequest',
                                                   >>  33                                ['raw_output', 'input_data'])
                                                   >>  34 KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs',
                                                   >>  35                                            'build_dir', 'alltests',
                                                   >>  36                                            'make_options'])
                                                   >>  37 
                                                   >>  38 KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0]
 27                                                    39 
 28 class KunitStatus(Enum):                           40 class KunitStatus(Enum):
 29         SUCCESS = auto()                           41         SUCCESS = auto()
 30         CONFIG_FAILURE = auto()                    42         CONFIG_FAILURE = auto()
 31         BUILD_FAILURE = auto()                     43         BUILD_FAILURE = auto()
 32         TEST_FAILURE = auto()                      44         TEST_FAILURE = auto()
 33                                                    45 
 34 @dataclass                                     !!  46 def create_default_kunitconfig():
 35 class KunitResult:                             !!  47         if not os.path.exists(kunit_kernel.kunitconfig_path):
 36         status: KunitStatus                    !!  48                 shutil.copyfile('arch/um/configs/kunit_defconfig',
 37         elapsed_time: float                    !!  49                                 kunit_kernel.kunitconfig_path)
 38                                                !!  50 
 39 @dataclass                                     !!  51 def get_kernel_root_path():
 40 class KunitConfigRequest:                      !!  52         parts = sys.argv[0] if not __file__ else __file__
 41         build_dir: str                         !!  53         parts = os.path.realpath(parts).split('tools/testing/kunit')
 42         make_options: Optional[List[str]]      << 
 43                                                << 
 44 @dataclass                                     << 
 45 class KunitBuildRequest(KunitConfigRequest):   << 
 46         jobs: int                              << 
 47                                                << 
 48 @dataclass                                     << 
 49 class KunitParseRequest:                       << 
 50         raw_output: Optional[str]              << 
 51         json: Optional[str]                    << 
 52                                                << 
 53 @dataclass                                     << 
 54 class KunitExecRequest(KunitParseRequest):     << 
 55         build_dir: str                         << 
 56         timeout: int                           << 
 57         filter_glob: str                       << 
 58         filter: str                            << 
 59         filter_action: Optional[str]           << 
 60         kernel_args: Optional[List[str]]       << 
 61         run_isolated: Optional[str]            << 
 62         list_tests: bool                       << 
 63         list_tests_attr: bool                  << 
 64                                                << 
 65 @dataclass                                     << 
 66 class KunitRequest(KunitExecRequest, KunitBuil << 
 67         pass                                   << 
 68                                                << 
 69                                                << 
 70 def get_kernel_root_path() -> str:             << 
 71         path = sys.argv[0] if not __file__ els << 
 72         parts = os.path.realpath(path).split(' << 
 73         if len(parts) != 2:                        54         if len(parts) != 2:
 74                 sys.exit(1)                        55                 sys.exit(1)
 75         return parts[0]                            56         return parts[0]
 76                                                    57 
 77 def config_tests(linux: kunit_kernel.LinuxSour     58 def config_tests(linux: kunit_kernel.LinuxSourceTree,
 78                  request: KunitConfigRequest)      59                  request: KunitConfigRequest) -> KunitResult:
 79         stdout.print_with_timestamp('Configuri !!  60         kunit_parser.print_with_timestamp('Configuring KUnit Kernel ...')
 80                                                    61 
 81         config_start = time.time()                 62         config_start = time.time()
                                                   >>  63         create_default_kunitconfig()
 82         success = linux.build_reconfig(request     64         success = linux.build_reconfig(request.build_dir, request.make_options)
 83         config_end = time.time()                   65         config_end = time.time()
 84         status = KunitStatus.SUCCESS if succes !!  66         if not success:
 85         return KunitResult(status, config_end  !!  67                 return KunitResult(KunitStatus.CONFIG_FAILURE,
                                                   >>  68                                    'could not configure kernel',
                                                   >>  69                                    config_end - config_start)
                                                   >>  70         return KunitResult(KunitStatus.SUCCESS,
                                                   >>  71                            'configured kernel successfully',
                                                   >>  72                            config_end - config_start)
 86                                                    73 
 87 def build_tests(linux: kunit_kernel.LinuxSourc     74 def build_tests(linux: kunit_kernel.LinuxSourceTree,
 88                 request: KunitBuildRequest) ->     75                 request: KunitBuildRequest) -> KunitResult:
 89         stdout.print_with_timestamp('Building  !!  76         kunit_parser.print_with_timestamp('Building KUnit Kernel ...')
 90                                                    77 
 91         build_start = time.time()                  78         build_start = time.time()
 92         success = linux.build_kernel(request.j !!  79         success = linux.build_um_kernel(request.alltests,
 93                                      request.b !!  80                                         request.jobs,
 94                                      request.m !!  81                                         request.build_dir,
                                                   >>  82                                         request.make_options)
 95         build_end = time.time()                    83         build_end = time.time()
 96         status = KunitStatus.SUCCESS if succes !!  84         if not success:
 97         return KunitResult(status, build_end - !!  85                 return KunitResult(KunitStatus.BUILD_FAILURE,
 98                                                !!  86                                    'could not build kernel',
 99 def config_and_build_tests(linux: kunit_kernel !!  87                                    build_end - build_start)
100                            request: KunitBuild !!  88         if not success:
101         config_result = config_tests(linux, re !!  89                 return KunitResult(KunitStatus.BUILD_FAILURE,
102         if config_result.status != KunitStatus !!  90                                    'could not build kernel',
103                 return config_result           !!  91                                    build_end - build_start)
104                                                !!  92         return KunitResult(KunitStatus.SUCCESS,
105         return build_tests(linux, request)     !!  93                            'built kernel successfully',
106                                                !!  94                            build_end - build_start)
107 def _list_tests(linux: kunit_kernel.LinuxSourc !!  95 
108         args = ['kunit.action=list']           !!  96 def exec_tests(linux: kunit_kernel.LinuxSourceTree,
                                                   >>  97                request: KunitExecRequest) -> KunitResult:
                                                   >>  98         kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
                                                   >>  99         test_start = time.time()
                                                   >> 100         result = linux.run_kernel(
                                                   >> 101                 timeout=None if request.alltests else request.timeout,
                                                   >> 102                 build_dir=request.build_dir)
                                                   >> 103 
                                                   >> 104         test_end = time.time()
                                                   >> 105 
                                                   >> 106         return KunitResult(KunitStatus.SUCCESS,
                                                   >> 107                            result,
                                                   >> 108                            test_end - test_start)
109                                                   109 
110         if request.kernel_args:                !! 110 def parse_tests(request: KunitParseRequest) -> KunitResult:
111                 args.extend(request.kernel_arg << 
112                                                << 
113         output = linux.run_kernel(args=args,   << 
114                            timeout=request.tim << 
115                            filter_glob=request << 
116                            filter=request.filt << 
117                            filter_action=reque << 
118                            build_dir=request.b << 
119         lines = kunit_parser.extract_tap_lines << 
120         # Hack! Drop the dummy TAP version hea << 
121         lines.pop()                            << 
122                                                << 
123         # Filter out any extraneous non-test o << 
124         return [l for l in output if re.match( << 
125                                                << 
126 def _list_tests_attr(linux: kunit_kernel.Linux << 
127         args = ['kunit.action=list_attr']      << 
128                                                << 
129         if request.kernel_args:                << 
130                 args.extend(request.kernel_arg << 
131                                                << 
132         output = linux.run_kernel(args=args,   << 
133                            timeout=request.tim << 
134                            filter_glob=request << 
135                            filter=request.filt << 
136                            filter_action=reque << 
137                            build_dir=request.b << 
138         lines = kunit_parser.extract_tap_lines << 
139         # Hack! Drop the dummy TAP version hea << 
140         lines.pop()                            << 
141                                                << 
142         # Filter out any extraneous non-test o << 
143         return lines                           << 
144                                                << 
145 def _suites_from_test_list(tests: List[str]) - << 
146         """Extracts all the suites from an ord << 
147         suites = []  # type: List[str]         << 
148         for t in tests:                        << 
149                 parts = t.split('.', maxsplit= << 
150                 if len(parts) != 2:            << 
151                         raise ValueError(f'int << 
152                 suite, _ = parts               << 
153                 if not suites or suites[-1] != << 
154                         suites.append(suite)   << 
155         return suites                          << 
156                                                << 
157 def exec_tests(linux: kunit_kernel.LinuxSource << 
158         filter_globs = [request.filter_glob]   << 
159         if request.list_tests:                 << 
160                 output = _list_tests(linux, re << 
161                 for line in output:            << 
162                         print(line.rstrip())   << 
163                 return KunitResult(status=Kuni << 
164         if request.list_tests_attr:            << 
165                 attr_output = _list_tests_attr << 
166                 for line in attr_output:       << 
167                         print(line.rstrip())   << 
168                 return KunitResult(status=Kuni << 
169         if request.run_isolated:               << 
170                 tests = _list_tests(linux, req << 
171                 if request.run_isolated == 'te << 
172                         filter_globs = tests   << 
173                 elif request.run_isolated == ' << 
174                         filter_globs = _suites << 
175                         # Apply the test-part  << 
176                         if '.' in request.filt << 
177                                 test_glob = re << 
178                                 filter_globs = << 
179                                                << 
180         metadata = kunit_json.Metadata(arch=li << 
181                                                << 
182         test_counts = kunit_parser.TestCounts( << 
183         exec_time = 0.0                        << 
184         for i, filter_glob in enumerate(filter << 
185                 stdout.print_with_timestamp('S << 
186                                                << 
187                 test_start = time.time()       << 
188                 run_result = linux.run_kernel( << 
189                         args=request.kernel_ar << 
190                         timeout=request.timeou << 
191                         filter_glob=filter_glo << 
192                         filter=request.filter, << 
193                         filter_action=request. << 
194                         build_dir=request.buil << 
195                                                << 
196                 _, test_result = parse_tests(r << 
197                 # run_kernel() doesn't block o << 
198                 # That only happens after we g << 
199                 # So exec_time here actually c << 
200                 test_end = time.time()         << 
201                 exec_time += test_end - test_s << 
202                                                << 
203                 test_counts.add_subtest_counts << 
204                                                << 
205         if len(filter_globs) == 1 and test_cou << 
206                 bd = request.build_dir         << 
207                 print('The kernel seems to hav << 
208                 print('$ scripts/decode_stackt << 
209                                 bd, bd, kunit_ << 
210                                                << 
211         kunit_status = _map_to_overall_status( << 
212         return KunitResult(status=kunit_status << 
213                                                << 
214 def _map_to_overall_status(test_status: kunit_ << 
215         if test_status in (kunit_parser.TestSt << 
216                 return KunitStatus.SUCCESS     << 
217         return KunitStatus.TEST_FAILURE        << 
218                                                << 
219 def parse_tests(request: KunitParseRequest, me << 
220         parse_start = time.time()                 111         parse_start = time.time()
221                                                   112 
                                                   >> 113         test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
                                                   >> 114                                               [],
                                                   >> 115                                               'Tests not Parsed.')
222         if request.raw_output:                    116         if request.raw_output:
223                 # Treat unparsed results as on !! 117                 kunit_parser.raw_output(request.input_data)
224                 fake_test = kunit_parser.Test( !! 118         else:
225                 fake_test.status = kunit_parse !! 119                 test_result = kunit_parser.parse_run_tests(request.input_data)
226                 fake_test.counts.passed = 1    !! 120         parse_end = time.time()
227                                                !! 121 
228                 output: Iterable[str] = input_ !! 122         if test_result.status != kunit_parser.TestStatus.SUCCESS:
229                 if request.raw_output == 'all' !! 123                 return KunitResult(KunitStatus.TEST_FAILURE, test_result,
230                         pass                   !! 124                                    parse_end - parse_start)
231                 elif request.raw_output == 'ku << 
232                         output = kunit_parser. << 
233                 for line in output:            << 
234                         print(line.rstrip())   << 
235                 parse_time = time.time() - par << 
236                 return KunitResult(KunitStatus << 
237                                                << 
238                                                << 
239         # Actually parse the test results.     << 
240         test = kunit_parser.parse_run_tests(in << 
241         parse_time = time.time() - parse_start << 
242                                                << 
243         if request.json:                       << 
244                 json_str = kunit_json.get_json << 
245                                         test=t << 
246                                         metada << 
247                 if request.json == 'stdout':   << 
248                         print(json_str)        << 
249                 else:                          << 
250                         with open(request.json << 
251                                 f.write(json_s << 
252                         stdout.print_with_time << 
253                                 os.path.abspat << 
254                                                   125 
255         if test.status != kunit_parser.TestSta !! 126         return KunitResult(KunitStatus.SUCCESS, test_result,
256                 return KunitResult(KunitStatus !! 127                                 parse_end - parse_start)
257                                                   128 
258         return KunitResult(KunitStatus.SUCCESS << 
259                                                   129 
260 def run_tests(linux: kunit_kernel.LinuxSourceT    130 def run_tests(linux: kunit_kernel.LinuxSourceTree,
261               request: KunitRequest) -> KunitR    131               request: KunitRequest) -> KunitResult:
262         run_start = time.time()                   132         run_start = time.time()
263                                                   133 
264         config_result = config_tests(linux, re !! 134         config_request = KunitConfigRequest(request.build_dir,
                                                   >> 135                                             request.make_options)
                                                   >> 136         config_result = config_tests(linux, config_request)
265         if config_result.status != KunitStatus    137         if config_result.status != KunitStatus.SUCCESS:
266                 return config_result              138                 return config_result
267                                                   139 
268         build_result = build_tests(linux, requ !! 140         build_request = KunitBuildRequest(request.jobs, request.build_dir,
                                                   >> 141                                           request.alltests,
                                                   >> 142                                           request.make_options)
                                                   >> 143         build_result = build_tests(linux, build_request)
269         if build_result.status != KunitStatus.    144         if build_result.status != KunitStatus.SUCCESS:
270                 return build_result               145                 return build_result
271                                                   146 
272         exec_result = exec_tests(linux, reques !! 147         exec_request = KunitExecRequest(request.timeout, request.build_dir,
                                                   >> 148                                         request.alltests)
                                                   >> 149         exec_result = exec_tests(linux, exec_request)
                                                   >> 150         if exec_result.status != KunitStatus.SUCCESS:
                                                   >> 151                 return exec_result
                                                   >> 152 
                                                   >> 153         parse_request = KunitParseRequest(request.raw_output,
                                                   >> 154                                           exec_result.result)
                                                   >> 155         parse_result = parse_tests(parse_request)
273                                                   156 
274         run_end = time.time()                     157         run_end = time.time()
275                                                   158 
276         stdout.print_with_timestamp((          !! 159         kunit_parser.print_with_timestamp((
277                 'Elapsed time: %.3fs total, %.    160                 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
278                 'building, %.3fs running\n') %    161                 'building, %.3fs running\n') % (
279                                 run_end - run_    162                                 run_end - run_start,
280                                 config_result.    163                                 config_result.elapsed_time,
281                                 build_result.e    164                                 build_result.elapsed_time,
282                                 exec_result.el    165                                 exec_result.elapsed_time))
283         return exec_result                     !! 166         return parse_result
284                                                << 
285 # Problem:                                     << 
286 # $ kunit.py run --json                        << 
287 # works as one would expect and prints the par << 
288 # $ kunit.py run --json suite_name             << 
289 # would *not* pass suite_name as the filter_gl << 
290 # argparse will consider it to be another way  << 
291 # $ kunit.py run --json=suite_name             << 
292 # i.e. it would run all tests, and dump the js << 
293 # So we hackily automatically rewrite --json = << 
294 pseudo_bool_flag_defaults = {                  << 
295                 '--json': 'stdout',            << 
296                 '--raw_output': 'kunit',       << 
297 }                                              << 
298 def massage_argv(argv: Sequence[str]) -> Seque << 
299         def massage_arg(arg: str) -> str:      << 
300                 if arg not in pseudo_bool_flag << 
301                         return arg             << 
302                 return  f'{arg}={pseudo_bool_f << 
303         return list(map(massage_arg, argv))    << 
304                                                   167 
305 def get_default_jobs() -> int:                 !! 168 def add_common_opts(parser):
306         return len(os.sched_getaffinity(0))    << 
307                                                << 
308 def add_common_opts(parser: argparse.ArgumentP << 
309         parser.add_argument('--build_dir',        169         parser.add_argument('--build_dir',
310                             help='As in the ma    170                             help='As in the make command, it specifies the build '
311                             'directory.',         171                             'directory.',
312                             type=str, default= !! 172                             type=str, default='.kunit', metavar='build_dir')
313         parser.add_argument('--make_options',     173         parser.add_argument('--make_options',
314                             help='X=Y make opt    174                             help='X=Y make option, can be repeated.',
315                             action='append', m !! 175                             action='append')
316         parser.add_argument('--alltests',         176         parser.add_argument('--alltests',
317                             help='Run all KUni !! 177                             help='Run all KUnit tests through allyesconfig',
318                             action='store_true    178                             action='store_true')
319         parser.add_argument('--kunitconfig',   << 
320                              help='Path to Kco << 
321                              ' If given a dire << 
322                              'will get  automa << 
323                              'blindly concaten << 
324                              action='append',  << 
325         parser.add_argument('--kconfig_add',   << 
326                              help='Additional  << 
327                              '.kunitconfig, e. << 
328                             action='append', m << 
329                                                << 
330         parser.add_argument('--arch',          << 
331                             help=('Specifies t << 
332                                   'The archite << 
333                                   'string pass << 
334                                   'e.g. i386,  << 
335                                   'architectur << 
336                             type=str, default= << 
337                                                << 
338         parser.add_argument('--cross_compile', << 
339                             help=('Sets make\' << 
340                                   'be set to a << 
341                                   'of gcc and  << 
342                                   'example `sp << 
343                                   'sparc toolc << 
344                                   '`$HOME/tool << 
345                                   'if you have << 
346                                   'from the 0- << 
347                                   'home direct << 
348                             metavar='PREFIX')  << 
349                                                << 
350         parser.add_argument('--qemu_config',   << 
351                             help=('Takes a pat << 
352                                   'a QemuArchP << 
353                             type=str, metavar= << 
354                                                << 
355         parser.add_argument('--qemu_args',     << 
356                             help='Additional Q << 
357                             action='append', m << 
358                                                   179 
359 def add_build_opts(parser: argparse.ArgumentPa !! 180 def add_build_opts(parser):
360         parser.add_argument('--jobs',             181         parser.add_argument('--jobs',
361                             help='As in the ma    182                             help='As in the make command, "Specifies  the number of '
362                             'jobs (commands) t    183                             'jobs (commands) to run simultaneously."',
363                             type=int, default= !! 184                             type=int, default=8, metavar='jobs')
364                                                   185 
365 def add_exec_opts(parser: argparse.ArgumentPar !! 186 def add_exec_opts(parser):
366         parser.add_argument('--timeout',          187         parser.add_argument('--timeout',
367                             help='maximum numb    188                             help='maximum number of seconds to allow for all tests '
368                             'to run. This does    189                             'to run. This does not include time taken to build the '
369                             'tests.',             190                             'tests.',
370                             type=int,             191                             type=int,
371                             default=300,          192                             default=300,
372                             metavar='SECONDS') !! 193                             metavar='timeout')
373         parser.add_argument('filter_glob',     << 
374                             help='Filter which << 
375                             'boot-time, e.g. l << 
376                             type=str,          << 
377                             nargs='?',         << 
378                             default='',        << 
379                             metavar='filter_gl << 
380         parser.add_argument('--filter',        << 
381                             help='Filter KUnit << 
382                             'e.g. module=examp << 
383                             type=str,          << 
384                                 default='')    << 
385         parser.add_argument('--filter_action', << 
386                             help='If set to sk << 
387                                 'e.g. --filter << 
388                             type=str,          << 
389                                 choices=['skip << 
390         parser.add_argument('--kernel_args',   << 
391                             help='Kernel comma << 
392                              action='append',  << 
393         parser.add_argument('--run_isolated',  << 
394                             'individual suite/ << 
395                             'a non-hermetic te << 
396                             'what ran before i << 
397                             type=str,          << 
398                             choices=['suite',  << 
399         parser.add_argument('--list_tests', he << 
400                             'run.',            << 
401                             action='store_true << 
402         parser.add_argument('--list_tests_attr << 
403                             'attributes.',     << 
404                             action='store_true << 
405                                                << 
406 def add_parse_opts(parser: argparse.ArgumentPa << 
407         parser.add_argument('--raw_output', he << 
408                             'By default, filte << 
409                             '--raw_output=all  << 
410                              type=str, nargs=' << 
411         parser.add_argument('--json',          << 
412                             nargs='?',         << 
413                             help='Prints parse << 
414                             'a filename is spe << 
415                             type=str, const='s << 
416                                                << 
417                                                << 
418 def tree_from_args(cli_args: argparse.Namespac << 
419         """Returns a LinuxSourceTree based on  << 
420         # Allow users to specify multiple argu << 
421         qemu_args: List[str] = []              << 
422         if cli_args.qemu_args:                 << 
423                 for arg in cli_args.qemu_args: << 
424                         qemu_args.extend(shlex << 
425                                                << 
426         kunitconfigs = cli_args.kunitconfig if << 
427         if cli_args.alltests:                  << 
428                 # Prepend so user-specified op << 
429                 # --kunitconfig options to hav << 
430                 kunitconfigs = [kunit_kernel.A << 
431                                                << 
432         return kunit_kernel.LinuxSourceTree(cl << 
433                         kunitconfig_paths=kuni << 
434                         kconfig_add=cli_args.k << 
435                         arch=cli_args.arch,    << 
436                         cross_compile=cli_args << 
437                         qemu_config_path=cli_a << 
438                         extra_qemu_args=qemu_a << 
439                                                << 
440                                                << 
441 def run_handler(cli_args: argparse.Namespace)  << 
442         if not os.path.exists(cli_args.build_d << 
443                 os.mkdir(cli_args.build_dir)   << 
444                                                << 
445         linux = tree_from_args(cli_args)       << 
446         request = KunitRequest(build_dir=cli_a << 
447                                         make_o << 
448                                         jobs=c << 
449                                         raw_ou << 
450                                         json=c << 
451                                         timeou << 
452                                         filter << 
453                                         filter << 
454                                         filter << 
455                                         kernel << 
456                                         run_is << 
457                                         list_t << 
458                                         list_t << 
459         result = run_tests(linux, request)     << 
460         if result.status != KunitStatus.SUCCES << 
461                 sys.exit(1)                    << 
462                                                   194 
463                                                !! 195 def add_parse_opts(parser):
464 def config_handler(cli_args: argparse.Namespac !! 196         parser.add_argument('--raw_output', help='don\'t format output from kernel',
465         if cli_args.build_dir and (            !! 197                             action='store_true')
466                         not os.path.exists(cli << 
467                 os.mkdir(cli_args.build_dir)   << 
468                                                << 
469         linux = tree_from_args(cli_args)       << 
470         request = KunitConfigRequest(build_dir << 
471                                                << 
472         result = config_tests(linux, request)  << 
473         stdout.print_with_timestamp((          << 
474                 'Elapsed time: %.3fs\n') % (   << 
475                         result.elapsed_time))  << 
476         if result.status != KunitStatus.SUCCES << 
477                 sys.exit(1)                    << 
478                                                << 
479                                                << 
480 def build_handler(cli_args: argparse.Namespace << 
481         linux = tree_from_args(cli_args)       << 
482         request = KunitBuildRequest(build_dir= << 
483                                         make_o << 
484                                         jobs=c << 
485         result = config_and_build_tests(linux, << 
486         stdout.print_with_timestamp((          << 
487                 'Elapsed time: %.3fs\n') % (   << 
488                         result.elapsed_time))  << 
489         if result.status != KunitStatus.SUCCES << 
490                 sys.exit(1)                    << 
491                                                << 
492                                                << 
493 def exec_handler(cli_args: argparse.Namespace) << 
494         linux = tree_from_args(cli_args)       << 
495         exec_request = KunitExecRequest(raw_ou << 
496                                         build_ << 
497                                         json=c << 
498                                         timeou << 
499                                         filter << 
500                                         filter << 
501                                         filter << 
502                                         kernel << 
503                                         run_is << 
504                                         list_t << 
505                                         list_t << 
506         result = exec_tests(linux, exec_reques << 
507         stdout.print_with_timestamp((          << 
508                 'Elapsed time: %.3fs\n') % (re << 
509         if result.status != KunitStatus.SUCCES << 
510                 sys.exit(1)                    << 
511                                                << 
512                                                << 
513 def parse_handler(cli_args: argparse.Namespace << 
514         if cli_args.file is None:              << 
515                 sys.stdin.reconfigure(errors=' << 
516                 kunit_output = sys.stdin  # ty << 
517         else:                                  << 
518                 with open(cli_args.file, 'r',  << 
519                         kunit_output = f.read( << 
520         # We know nothing about how the result << 
521         metadata = kunit_json.Metadata()       << 
522         request = KunitParseRequest(raw_output << 
523                                         json=c << 
524         result, _ = parse_tests(request, metad << 
525         if result.status != KunitStatus.SUCCES << 
526                 sys.exit(1)                    << 
527                                                << 
528                                                << 
529 subcommand_handlers_map = {                    << 
530         'run': run_handler,                    << 
531         'config': config_handler,              << 
532         'build': build_handler,                << 
533         'exec': exec_handler,                  << 
534         'parse': parse_handler                 << 
535 }                                              << 
536                                                   198 
537                                                   199 
538 def main(argv: Sequence[str]) -> None:         !! 200 def main(argv, linux=None):
539         parser = argparse.ArgumentParser(         201         parser = argparse.ArgumentParser(
540                         description='Helps wri    202                         description='Helps writing and running KUnit tests.')
541         subparser = parser.add_subparsers(dest    203         subparser = parser.add_subparsers(dest='subcommand')
542                                                   204 
543         # The 'run' command will config, build    205         # The 'run' command will config, build, exec, and parse in one go.
544         run_parser = subparser.add_parser('run    206         run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
545         add_common_opts(run_parser)               207         add_common_opts(run_parser)
546         add_build_opts(run_parser)                208         add_build_opts(run_parser)
547         add_exec_opts(run_parser)                 209         add_exec_opts(run_parser)
548         add_parse_opts(run_parser)                210         add_parse_opts(run_parser)
549                                                   211 
550         config_parser = subparser.add_parser('    212         config_parser = subparser.add_parser('config',
551                                                   213                                                 help='Ensures that .config contains all of '
552                                                   214                                                 'the options in .kunitconfig')
553         add_common_opts(config_parser)            215         add_common_opts(config_parser)
554                                                   216 
555         build_parser = subparser.add_parser('b    217         build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
556         add_common_opts(build_parser)             218         add_common_opts(build_parser)
557         add_build_opts(build_parser)              219         add_build_opts(build_parser)
558                                                   220 
559         exec_parser = subparser.add_parser('ex    221         exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
560         add_common_opts(exec_parser)              222         add_common_opts(exec_parser)
561         add_exec_opts(exec_parser)                223         add_exec_opts(exec_parser)
562         add_parse_opts(exec_parser)               224         add_parse_opts(exec_parser)
563                                                   225 
564         # The 'parse' option is special, as it    226         # The 'parse' option is special, as it doesn't need the kernel source
565         # (therefore there is no need for a bu    227         # (therefore there is no need for a build_dir, hence no add_common_opts)
566         # and the '--file' argument is not rel    228         # and the '--file' argument is not relevant to 'run', so isn't in
567         # add_parse_opts()                        229         # add_parse_opts()
568         parse_parser = subparser.add_parser('p    230         parse_parser = subparser.add_parser('parse',
569                                             he    231                                             help='Parses KUnit results from a file, '
570                                             'a    232                                             'and parses formatted results.')
571         add_parse_opts(parse_parser)              233         add_parse_opts(parse_parser)
572         parse_parser.add_argument('file',         234         parse_parser.add_argument('file',
573                                   help='Specif    235                                   help='Specifies the file to read results from.',
574                                   type=str, na    236                                   type=str, nargs='?', metavar='input_file')
575                                                   237 
576         cli_args = parser.parse_args(massage_a !! 238         cli_args = parser.parse_args(argv)
577                                                << 
578         if get_kernel_root_path():             << 
579                 os.chdir(get_kernel_root_path( << 
580                                                << 
581         subcomand_handler = subcommand_handler << 
582                                                   239 
583         if subcomand_handler is None:          !! 240         if cli_args.subcommand == 'run':
                                                   >> 241                 if not os.path.exists(cli_args.build_dir):
                                                   >> 242                         os.mkdir(cli_args.build_dir)
                                                   >> 243 
                                                   >> 244                 if not linux:
                                                   >> 245                         linux = kunit_kernel.LinuxSourceTree()
                                                   >> 246 
                                                   >> 247                 request = KunitRequest(cli_args.raw_output,
                                                   >> 248                                        cli_args.timeout,
                                                   >> 249                                        cli_args.jobs,
                                                   >> 250                                        cli_args.build_dir,
                                                   >> 251                                        cli_args.alltests,
                                                   >> 252                                        cli_args.make_options)
                                                   >> 253                 result = run_tests(linux, request)
                                                   >> 254                 if result.status != KunitStatus.SUCCESS:
                                                   >> 255                         sys.exit(1)
                                                   >> 256         elif cli_args.subcommand == 'config':
                                                   >> 257                 if cli_args.build_dir:
                                                   >> 258                         if not os.path.exists(cli_args.build_dir):
                                                   >> 259                                 os.mkdir(cli_args.build_dir)
                                                   >> 260 
                                                   >> 261                 if not linux:
                                                   >> 262                         linux = kunit_kernel.LinuxSourceTree()
                                                   >> 263 
                                                   >> 264                 request = KunitConfigRequest(cli_args.build_dir,
                                                   >> 265                                              cli_args.make_options)
                                                   >> 266                 result = config_tests(linux, request)
                                                   >> 267                 kunit_parser.print_with_timestamp((
                                                   >> 268                         'Elapsed time: %.3fs\n') % (
                                                   >> 269                                 result.elapsed_time))
                                                   >> 270                 if result.status != KunitStatus.SUCCESS:
                                                   >> 271                         sys.exit(1)
                                                   >> 272         elif cli_args.subcommand == 'build':
                                                   >> 273                 if cli_args.build_dir:
                                                   >> 274                         if not os.path.exists(cli_args.build_dir):
                                                   >> 275                                 os.mkdir(cli_args.build_dir)
                                                   >> 276 
                                                   >> 277                 if not linux:
                                                   >> 278                         linux = kunit_kernel.LinuxSourceTree()
                                                   >> 279 
                                                   >> 280                 request = KunitBuildRequest(cli_args.jobs,
                                                   >> 281                                             cli_args.build_dir,
                                                   >> 282                                             cli_args.alltests,
                                                   >> 283                                             cli_args.make_options)
                                                   >> 284                 result = build_tests(linux, request)
                                                   >> 285                 kunit_parser.print_with_timestamp((
                                                   >> 286                         'Elapsed time: %.3fs\n') % (
                                                   >> 287                                 result.elapsed_time))
                                                   >> 288                 if result.status != KunitStatus.SUCCESS:
                                                   >> 289                         sys.exit(1)
                                                   >> 290         elif cli_args.subcommand == 'exec':
                                                   >> 291                 if cli_args.build_dir:
                                                   >> 292                         if not os.path.exists(cli_args.build_dir):
                                                   >> 293                                 os.mkdir(cli_args.build_dir)
                                                   >> 294 
                                                   >> 295                 if not linux:
                                                   >> 296                         linux = kunit_kernel.LinuxSourceTree()
                                                   >> 297 
                                                   >> 298                 exec_request = KunitExecRequest(cli_args.timeout,
                                                   >> 299                                                 cli_args.build_dir,
                                                   >> 300                                                 cli_args.alltests)
                                                   >> 301                 exec_result = exec_tests(linux, exec_request)
                                                   >> 302                 parse_request = KunitParseRequest(cli_args.raw_output,
                                                   >> 303                                                   exec_result.result)
                                                   >> 304                 result = parse_tests(parse_request)
                                                   >> 305                 kunit_parser.print_with_timestamp((
                                                   >> 306                         'Elapsed time: %.3fs\n') % (
                                                   >> 307                                 exec_result.elapsed_time))
                                                   >> 308                 if result.status != KunitStatus.SUCCESS:
                                                   >> 309                         sys.exit(1)
                                                   >> 310         elif cli_args.subcommand == 'parse':
                                                   >> 311                 if cli_args.file == None:
                                                   >> 312                         kunit_output = sys.stdin
                                                   >> 313                 else:
                                                   >> 314                         with open(cli_args.file, 'r') as f:
                                                   >> 315                                 kunit_output = f.read().splitlines()
                                                   >> 316                 request = KunitParseRequest(cli_args.raw_output,
                                                   >> 317                                             kunit_output)
                                                   >> 318                 result = parse_tests(request)
                                                   >> 319                 if result.status != KunitStatus.SUCCESS:
                                                   >> 320                         sys.exit(1)
                                                   >> 321         else:
584                 parser.print_help()               322                 parser.print_help()
585                 return                         << 
586                                                << 
587         subcomand_handler(cli_args)            << 
588                                                << 
589                                                   323 
590 if __name__ == '__main__':                        324 if __name__ == '__main__':
591         main(sys.argv[1:])                        325         main(sys.argv[1:])
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php