2019-09-23 02:02:43 -07:00
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0
#
# A thin wrapper on top of the KUnit Kernel
#
# Copyright (C) 2019, Google LLC.
# Author: Felix Guo <felixguoxiuping@gmail.com>
# Author: Brendan Higgins <brendanhiggins@google.com>
import argparse
import sys
import os
import time
2019-09-23 02:02:44 -07:00
import shutil
2019-09-23 02:02:43 -07:00
from collections import namedtuple
from enum import Enum , auto
import kunit_config
import kunit_kernel
import kunit_parser
KunitResult = namedtuple ( ' KunitResult ' , [ ' status ' , ' result ' ] )
2019-09-23 02:02:44 -07:00
KunitRequest = namedtuple ( ' KunitRequest ' , [ ' raw_output ' , ' timeout ' , ' jobs ' , ' build_dir ' , ' defconfig ' ] )
2019-09-23 02:02:43 -07:00
class KunitStatus ( Enum ) :
SUCCESS = auto ( )
CONFIG_FAILURE = auto ( )
BUILD_FAILURE = auto ( )
TEST_FAILURE = auto ( )
2019-09-23 02:02:44 -07:00
def create_default_kunitconfig ( ) :
2019-12-20 05:14:05 +00:00
if not os . path . exists ( kunit_kernel . kunitconfig_path ) :
2019-09-23 02:02:44 -07:00
shutil . copyfile ( ' arch/um/configs/kunit_defconfig ' ,
2019-12-20 05:14:05 +00:00
kunit_kernel . kunitconfig_path )
2019-09-23 02:02:44 -07:00
2019-09-23 02:02:43 -07:00
def run_tests ( linux : kunit_kernel . LinuxSourceTree ,
request : KunitRequest ) - > KunitResult :
config_start = time . time ( )
success = linux . build_reconfig ( request . build_dir )
config_end = time . time ( )
if not success :
return KunitResult ( KunitStatus . CONFIG_FAILURE , ' could not configure kernel ' )
kunit_parser . print_with_timestamp ( ' Building KUnit Kernel ... ' )
build_start = time . time ( )
success = linux . build_um_kernel ( request . jobs , request . build_dir )
build_end = time . time ( )
if not success :
return KunitResult ( KunitStatus . BUILD_FAILURE , ' could not build kernel ' )
kunit_parser . print_with_timestamp ( ' Starting KUnit Kernel ... ' )
test_start = time . time ( )
test_result = kunit_parser . TestResult ( kunit_parser . TestStatus . SUCCESS ,
[ ] ,
' Tests not Parsed. ' )
if request . raw_output :
kunit_parser . raw_output (
kunit: Fix '--build_dir' option
Running kunit with '--build_dir' option gives following error message:
```
$ ./tools/testing/kunit/kunit.py run --build_dir ../linux.out.kunit/
[00:57:24] Building KUnit Kernel ...
[00:57:29] Starting KUnit Kernel ...
Traceback (most recent call last):
File "./tools/testing/kunit/kunit.py", line 136, in <module>
main(sys.argv[1:])
File "./tools/testing/kunit/kunit.py", line 129, in main
result = run_tests(linux, request)
File "./tools/testing/kunit/kunit.py", line 68, in run_tests
test_result = kunit_parser.parse_run_tests(kunit_output)
File "/home/sjpark/linux/tools/testing/kunit/kunit_parser.py", line
283, in parse_run_tests
test_result =
parse_test_result(list(isolate_kunit_output(kernel_output)))
File "/home/sjpark/linux/tools/testing/kunit/kunit_parser.py", line
54, in isolate_kunit_output
for line in kernel_output:
File "/home/sjpark/linux/tools/testing/kunit/kunit_kernel.py", line
145, in run_kernel
process = self._ops.linux_bin(args, timeout, build_dir)
File "/home/sjpark/linux/tools/testing/kunit/kunit_kernel.py", line
69, in linux_bin
stderr=subprocess.PIPE)
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: './linux'
```
This error occurs because the '--build_dir' option value is not passed
to the 'run_kernel()' function. Consequently, the function assumes
the kernel image that built for the tests, which is under the
'--build_dir' directory, is in kernel source directory and finally raises
the 'FileNotFoundError'.
This commit fixes the problem by properly passing the '--build_dir'
option value to the 'run_kernel()'.
Signed-off-by: SeongJae Park <sj38.park@gmail.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Tested-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-09-07 01:11:54 +09:00
linux . run_kernel ( timeout = request . timeout ,
build_dir = request . build_dir ) )
2019-09-23 02:02:43 -07:00
else :
kunit: Fix '--build_dir' option
Running kunit with '--build_dir' option gives following error message:
```
$ ./tools/testing/kunit/kunit.py run --build_dir ../linux.out.kunit/
[00:57:24] Building KUnit Kernel ...
[00:57:29] Starting KUnit Kernel ...
Traceback (most recent call last):
File "./tools/testing/kunit/kunit.py", line 136, in <module>
main(sys.argv[1:])
File "./tools/testing/kunit/kunit.py", line 129, in main
result = run_tests(linux, request)
File "./tools/testing/kunit/kunit.py", line 68, in run_tests
test_result = kunit_parser.parse_run_tests(kunit_output)
File "/home/sjpark/linux/tools/testing/kunit/kunit_parser.py", line
283, in parse_run_tests
test_result =
parse_test_result(list(isolate_kunit_output(kernel_output)))
File "/home/sjpark/linux/tools/testing/kunit/kunit_parser.py", line
54, in isolate_kunit_output
for line in kernel_output:
File "/home/sjpark/linux/tools/testing/kunit/kunit_kernel.py", line
145, in run_kernel
process = self._ops.linux_bin(args, timeout, build_dir)
File "/home/sjpark/linux/tools/testing/kunit/kunit_kernel.py", line
69, in linux_bin
stderr=subprocess.PIPE)
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: './linux'
```
This error occurs because the '--build_dir' option value is not passed
to the 'run_kernel()' function. Consequently, the function assumes
the kernel image that built for the tests, which is under the
'--build_dir' directory, is in kernel source directory and finally raises
the 'FileNotFoundError'.
This commit fixes the problem by properly passing the '--build_dir'
option value to the 'run_kernel()'.
Signed-off-by: SeongJae Park <sj38.park@gmail.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Tested-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-09-07 01:11:54 +09:00
kunit_output = linux . run_kernel ( timeout = request . timeout ,
build_dir = request . build_dir )
2019-09-23 02:02:43 -07:00
test_result = kunit_parser . parse_run_tests ( kunit_output )
test_end = time . time ( )
kunit_parser . print_with_timestamp ( (
' Elapsed time: %.3f s total, %.3f s configuring, %.3f s ' +
' building, %.3f s running \n ' ) % (
test_end - config_start ,
config_end - config_start ,
build_end - build_start ,
test_end - test_start ) )
if test_result . status != kunit_parser . TestStatus . SUCCESS :
return KunitResult ( KunitStatus . TEST_FAILURE , test_result )
else :
return KunitResult ( KunitStatus . SUCCESS , test_result )
2019-09-23 02:02:44 -07:00
def main ( argv , linux = None ) :
2019-09-23 02:02:43 -07:00
parser = argparse . ArgumentParser (
description = ' Helps writing and running KUnit tests. ' )
subparser = parser . add_subparsers ( dest = ' subcommand ' )
run_parser = subparser . add_parser ( ' run ' , help = ' Runs KUnit tests. ' )
run_parser . add_argument ( ' --raw_output ' , help = ' don \' t format output from kernel ' ,
action = ' store_true ' )
run_parser . add_argument ( ' --timeout ' ,
help = ' maximum number of seconds to allow for all tests '
' to run. This does not include time taken to build the '
' tests. ' ,
type = int ,
default = 300 ,
metavar = ' timeout ' )
run_parser . add_argument ( ' --jobs ' ,
help = ' As in the make command, " Specifies the number of '
' jobs (commands) to run simultaneously. " ' ,
type = int , default = 8 , metavar = ' jobs ' )
run_parser . add_argument ( ' --build_dir ' ,
help = ' As in the make command, it specifies the build '
' directory. ' ,
type = str , default = None , metavar = ' build_dir ' )
2019-09-23 02:02:44 -07:00
run_parser . add_argument ( ' --defconfig ' ,
help = ' Uses a default kunitconfig. ' ,
action = ' store_true ' )
2019-09-23 02:02:43 -07:00
cli_args = parser . parse_args ( argv )
if cli_args . subcommand == ' run ' :
2019-12-20 05:14:05 +00:00
if cli_args . build_dir :
if not os . path . exists ( cli_args . build_dir ) :
os . mkdir ( cli_args . build_dir )
kunit_kernel . kunitconfig_path = os . path . join (
cli_args . build_dir ,
kunit_kernel . kunitconfig_path )
2019-09-23 02:02:44 -07:00
if cli_args . defconfig :
create_default_kunitconfig ( )
if not linux :
linux = kunit_kernel . LinuxSourceTree ( )
2019-09-23 02:02:43 -07:00
request = KunitRequest ( cli_args . raw_output ,
cli_args . timeout ,
cli_args . jobs ,
2019-09-23 02:02:44 -07:00
cli_args . build_dir ,
cli_args . defconfig )
2019-09-23 02:02:43 -07:00
result = run_tests ( linux , request )
if result . status != KunitStatus . SUCCESS :
sys . exit ( 1 )
else :
parser . print_help ( )
if __name__ == ' __main__ ' :
2019-09-23 02:02:44 -07:00
main ( sys . argv [ 1 : ] )