Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'linux-kselftest-kunit-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull Kunit updates from Shuah Khan:
"This consists of:

- Several config fragment fixes from Anders Roxell to improve test
coverage.

- Improvements to kunit run script to use defconfig as default and
restructure the code for config/build/exec/parse from Vitor Massaru
Iha and David Gow.

- Miscellaneous documentation warn fix"

* tag 'linux-kselftest-kunit-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest:
security: apparmor: default KUNIT_* fragments to KUNIT_ALL_TESTS
fs: ext4: default KUNIT_* fragments to KUNIT_ALL_TESTS
drivers: base: default KUNIT_* fragments to KUNIT_ALL_TESTS
lib: Kconfig.debug: default KUNIT_* fragments to KUNIT_ALL_TESTS
kunit: default KUNIT_* fragments to KUNIT_ALL_TESTS
kunit: Kconfig: enable a KUNIT_ALL_TESTS fragment
kunit: Fix TabError, remove defconfig code and handle when there is no kunitconfig
kunit: use KUnit defconfig by default
kunit: use --build_dir=.kunit as default
Documentation: test.h - fix warnings
kunit: kunit_tool: Separate out config/build/exec/parse

+348 -86
+8 -5
Documentation/dev-tools/kunit/start.rst
··· 32 32 options required by the tests. 33 33 34 34 A good starting point for a ``.kunitconfig`` is the KUnit defconfig: 35 + 35 36 .. code-block:: bash 36 37 37 38 cd $PATH_TO_LINUX_REPO 38 39 cp arch/um/configs/kunit_defconfig .kunitconfig 39 40 40 41 You can then add any other Kconfig options you wish, e.g.: 42 + 41 43 .. code-block:: none 42 44 43 - CONFIG_LIST_KUNIT_TEST=y 45 + CONFIG_LIST_KUNIT_TEST=y 44 46 45 47 :doc:`kunit_tool <kunit-tool>` will ensure that all config options set in 46 48 ``.kunitconfig`` are set in the kernel ``.config`` before running the tests. ··· 56 54 other tools (such as make menuconfig) to adjust other config options. 57 55 58 56 59 - Running the tests 60 - ----------------- 57 + Running the tests (KUnit Wrapper) 58 + --------------------------------- 61 59 62 60 To make sure that everything is set up correctly, simply invoke the Python 63 61 wrapper from your kernel repo: ··· 107 105 KUnit and KUnit tests can be compiled as modules: in this case the tests in a 108 106 module will be run when the module is loaded. 109 107 110 - Running the tests 111 - ----------------- 108 + 109 + Running the tests (w/o KUnit Wrapper) 110 + ------------------------------------- 112 111 113 112 Build and run your kernel as usual. Test output will be written to the kernel 114 113 log in `TAP <https://testanything.org/>`_ format.
+2 -2
Documentation/dev-tools/kunit/usage.rst
··· 595 595 KUnit debugfs representation 596 596 ============================ 597 597 When kunit test suites are initialized, they create an associated directory 598 - in /sys/kernel/debug/kunit/<test-suite>. The directory contains one file 598 + in ``/sys/kernel/debug/kunit/<test-suite>``. The directory contains one file 599 599 600 600 - results: "cat results" displays results of each test case and the results 601 601 of the entire suite for the last test run. ··· 604 604 run in a native environment, either as modules or builtin. Having a way 605 605 to display results like this is valuable as otherwise results can be 606 606 intermixed with other events in dmesg output. The maximum size of each 607 - results file is KUNIT_LOG_SIZE bytes (defined in include/kunit/test.h). 607 + results file is KUNIT_LOG_SIZE bytes (defined in ``include/kunit/test.h``).
+2 -1
drivers/base/Kconfig
··· 149 149 test this functionality. 150 150 151 151 config PM_QOS_KUNIT_TEST 152 - bool "KUnit Test for PM QoS features" 152 + bool "KUnit Test for PM QoS features" if !KUNIT_ALL_TESTS 153 153 depends on KUNIT=y 154 + default KUNIT_ALL_TESTS 154 155 155 156 config HMEM_REPORTING 156 157 bool
+2 -1
drivers/base/test/Kconfig
··· 9 9 10 10 If unsure say N. 11 11 config KUNIT_DRIVER_PE_TEST 12 - bool "KUnit Tests for property entry API" 12 + bool "KUnit Tests for property entry API" if !KUNIT_ALL_TESTS 13 13 depends on KUNIT=y 14 + default KUNIT_ALL_TESTS
+2 -1
fs/ext4/Kconfig
··· 102 102 using dynamic debug control for mb_debug() / ext_debug() msgs. 103 103 104 104 config EXT4_KUNIT_TESTS 105 - tristate "KUnit tests for ext4" 105 + tristate "KUnit tests for ext4" if !KUNIT_ALL_TESTS 106 106 select EXT4_FS 107 107 depends on KUNIT 108 + default KUNIT_ALL_TESTS 108 109 help 109 110 This builds the ext4 KUnit tests. 110 111
+6 -6
include/kunit/test.h
··· 175 175 void (*exit)(struct kunit *test); 176 176 struct kunit_case *test_cases; 177 177 178 - /* private - internal use only */ 178 + /* private: internal use only */ 179 179 struct dentry *debugfs; 180 180 char *log; 181 181 }; ··· 232 232 * kunit_test_suites() - used to register one or more &struct kunit_suite 233 233 * with KUnit. 234 234 * 235 - * @suites: a statically allocated list of &struct kunit_suite. 235 + * @suites_list...: a statically allocated list of &struct kunit_suite. 236 236 * 237 - * Registers @suites with the test framework. See &struct kunit_suite for 237 + * Registers @suites_list with the test framework. See &struct kunit_suite for 238 238 * more information. 239 239 * 240 - * When builtin, KUnit tests are all run as late_initcalls; this means 240 + * When builtin, KUnit tests are all run as late_initcalls; this means 241 241 * that they cannot test anything where tests must run at a different init 242 242 * phase. One significant restriction resulting from this is that KUnit 243 243 * cannot reliably test anything that is initialize in the late_init phase; ··· 253 253 * tests from the same place, and at the very least to do so after 254 254 * everything else is definitely initialized. 255 255 */ 256 - #define kunit_test_suites(...) \ 257 - static struct kunit_suite *suites[] = { __VA_ARGS__, NULL}; \ 256 + #define kunit_test_suites(suites_list...) \ 257 + static struct kunit_suite *suites[] = {suites_list, NULL}; \ 258 258 static int kunit_test_suites_init(void) \ 259 259 { \ 260 260 return __kunit_test_suites_init(suites); \
+4 -2
lib/Kconfig.debug
··· 2142 2142 If unsure, say N. 2143 2143 2144 2144 config SYSCTL_KUNIT_TEST 2145 - tristate "KUnit test for sysctl" 2145 + tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS 2146 2146 depends on KUNIT 2147 + default KUNIT_ALL_TESTS 2147 2148 help 2148 2149 This builds the proc sysctl unit test, which runs on boot. 2149 2150 Tests the API contract and implementation correctness of sysctl. ··· 2154 2153 If unsure, say N. 2155 2154 2156 2155 config LIST_KUNIT_TEST 2157 - tristate "KUnit Test for Kernel Linked-list structures" 2156 + tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS 2158 2157 depends on KUNIT 2158 + default KUNIT_ALL_TESTS 2159 2159 help 2160 2160 This builds the linked list KUnit test suite. 2161 2161 It tests that the API and basic functionality of the list_head type
+20 -3
lib/kunit/Kconfig
··· 15 15 if KUNIT 16 16 17 17 config KUNIT_DEBUGFS 18 - bool "KUnit - Enable /sys/kernel/debug/kunit debugfs representation" 18 + bool "KUnit - Enable /sys/kernel/debug/kunit debugfs representation" if !KUNIT_ALL_TESTS 19 + default KUNIT_ALL_TESTS 19 20 help 20 21 Enable debugfs representation for kunit. Currently this consists 21 22 of /sys/kernel/debug/kunit/<test_suite>/results files for each ··· 24 23 run that occurred. 25 24 26 25 config KUNIT_TEST 27 - tristate "KUnit test for KUnit" 26 + tristate "KUnit test for KUnit" if !KUNIT_ALL_TESTS 27 + default KUNIT_ALL_TESTS 28 28 help 29 29 Enables the unit tests for the KUnit test framework. These tests test 30 30 the KUnit test framework itself; the tests are both written using ··· 34 32 expected. 35 33 36 34 config KUNIT_EXAMPLE_TEST 37 - tristate "Example test for KUnit" 35 + tristate "Example test for KUnit" if !KUNIT_ALL_TESTS 36 + default KUNIT_ALL_TESTS 38 37 help 39 38 Enables an example unit test that illustrates some of the basic 40 39 features of KUnit. This test only exists to help new users understand ··· 43 40 itself, lib/kunit/example-test.c, for more information. This option 44 41 is intended for curious hackers who would like to understand how to 45 42 use KUnit for kernel development. 43 + 44 + config KUNIT_ALL_TESTS 45 + tristate "All KUnit tests with satisfied dependencies" 46 + help 47 + Enables all KUnit tests, if they can be enabled. 48 + KUnit tests run during boot and output the results to the debug log 49 + in TAP format (http://testanything.org/). Only useful for kernel devs 50 + running the KUnit test harness, and not intended for inclusion into a 51 + production build. 52 + 53 + For more information on KUnit and unit tests in general please refer 54 + to the KUnit documentation in Documentation/dev-tools/kunit/. 55 + 56 + If unsure, say N. 46 57 47 58 endif # KUNIT
+2 -1
security/apparmor/Kconfig
··· 70 70 the kernel message buffer. 71 71 72 72 config SECURITY_APPARMOR_KUNIT_TEST 73 - bool "Build KUnit tests for policy_unpack.c" 73 + bool "Build KUnit tests for policy_unpack.c" if !KUNIT_ALL_TESTS 74 74 depends on KUNIT=y && SECURITY_APPARMOR 75 + default KUNIT_ALL_TESTS 75 76 help 76 77 This builds the AppArmor KUnit tests. 77 78
+241 -60
tools/testing/kunit/kunit.py
··· 20 20 import kunit_kernel 21 21 import kunit_parser 22 22 23 - KunitResult = namedtuple('KunitResult', ['status','result']) 23 + KunitResult = namedtuple('KunitResult', ['status','result','elapsed_time']) 24 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']) 25 34 KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 26 - 'build_dir', 'defconfig', 27 - 'alltests', 'make_options']) 35 + 'build_dir', 'alltests', 36 + 'make_options']) 28 37 29 38 KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0] 30 39 ··· 55 46 sys.exit(1) 56 47 return parts[0] 57 48 58 - def run_tests(linux: kunit_kernel.LinuxSourceTree, 59 - request: KunitRequest) -> KunitResult: 49 + def config_tests(linux: kunit_kernel.LinuxSourceTree, 50 + request: KunitConfigRequest) -> KunitResult: 51 + kunit_parser.print_with_timestamp('Configuring KUnit Kernel ...') 52 + 60 53 config_start = time.time() 54 + create_default_kunitconfig() 61 55 success = linux.build_reconfig(request.build_dir, request.make_options) 62 56 config_end = time.time() 63 57 if not success: 64 - return KunitResult(KunitStatus.CONFIG_FAILURE, 'could not configure kernel') 58 + return KunitResult(KunitStatus.CONFIG_FAILURE, 59 + 'could not configure kernel', 60 + config_end - config_start) 61 + return KunitResult(KunitStatus.SUCCESS, 62 + 'configured kernel successfully', 63 + config_end - config_start) 65 64 65 + def build_tests(linux: kunit_kernel.LinuxSourceTree, 66 + request: KunitBuildRequest) -> KunitResult: 66 67 kunit_parser.print_with_timestamp('Building KUnit Kernel ...') 67 68 68 69 build_start = time.time() ··· 83 64 build_end = time.time() 84 65 if not success: 85 66 return KunitResult(KunitStatus.BUILD_FAILURE, 'could not build kernel') 67 + if not success: 68 + return KunitResult(KunitStatus.BUILD_FAILURE, 69 + 'could not build kernel', 70 + build_end - build_start) 71 + return KunitResult(KunitStatus.SUCCESS, 72 + 'built kernel successfully', 73 + build_end - build_start) 86 74 75 + def exec_tests(linux: kunit_kernel.LinuxSourceTree, 76 + request: KunitExecRequest) -> KunitResult: 87 77 kunit_parser.print_with_timestamp('Starting KUnit Kernel ...') 88 78 test_start = time.time() 89 - kunit_output = linux.run_kernel( 79 + result = linux.run_kernel( 90 80 timeout=None if request.alltests else request.timeout, 91 81 build_dir=request.build_dir) 92 - if request.raw_output: 93 - raw_output = kunit_parser.raw_output(kunit_output) 94 - isolated = list(kunit_parser.isolate_kunit_output(raw_output)) 95 - test_result = kunit_parser.parse_test_result(isolated) 96 - else: 97 - test_result = kunit_parser.parse_run_tests(kunit_output) 82 + 98 83 test_end = time.time() 84 + 85 + return KunitResult(KunitStatus.SUCCESS, 86 + result, 87 + test_end - test_start) 88 + 89 + def parse_tests(request: KunitParseRequest) -> KunitResult: 90 + parse_start = time.time() 91 + 92 + test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS, 93 + [], 94 + 'Tests not Parsed.') 95 + if request.raw_output: 96 + kunit_parser.raw_output(request.input_data) 97 + else: 98 + test_result = kunit_parser.parse_run_tests(request.input_data) 99 + parse_end = time.time() 100 + 101 + if test_result.status != kunit_parser.TestStatus.SUCCESS: 102 + return KunitResult(KunitStatus.TEST_FAILURE, test_result, 103 + parse_end - parse_start) 104 + 105 + return KunitResult(KunitStatus.SUCCESS, test_result, 106 + parse_end - parse_start) 107 + 108 + 109 + def run_tests(linux: kunit_kernel.LinuxSourceTree, 110 + request: KunitRequest) -> KunitResult: 111 + run_start = time.time() 112 + 113 + config_request = KunitConfigRequest(request.build_dir, 114 + request.make_options) 115 + config_result = config_tests(linux, config_request) 116 + if config_result.status != KunitStatus.SUCCESS: 117 + return config_result 118 + 119 + build_request = KunitBuildRequest(request.jobs, request.build_dir, 120 + request.alltests, 121 + request.make_options) 122 + build_result = build_tests(linux, build_request) 123 + if build_result.status != KunitStatus.SUCCESS: 124 + return build_result 125 + 126 + exec_request = KunitExecRequest(request.timeout, request.build_dir, 127 + request.alltests) 128 + exec_result = exec_tests(linux, exec_request) 129 + if exec_result.status != KunitStatus.SUCCESS: 130 + return exec_result 131 + 132 + parse_request = KunitParseRequest(request.raw_output, 133 + exec_result.result) 134 + parse_result = parse_tests(parse_request) 135 + 136 + run_end = time.time() 99 137 100 138 kunit_parser.print_with_timestamp(( 101 139 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' + 102 140 'building, %.3fs running\n') % ( 103 - test_end - config_start, 104 - config_end - config_start, 105 - build_end - build_start, 106 - test_end - test_start)) 141 + run_end - run_start, 142 + config_result.elapsed_time, 143 + build_result.elapsed_time, 144 + exec_result.elapsed_time)) 145 + return parse_result 107 146 108 - if test_result.status != kunit_parser.TestStatus.SUCCESS: 109 - return KunitResult(KunitStatus.TEST_FAILURE, test_result) 110 - else: 111 - return KunitResult(KunitStatus.SUCCESS, test_result) 147 + def add_common_opts(parser): 148 + parser.add_argument('--build_dir', 149 + help='As in the make command, it specifies the build ' 150 + 'directory.', 151 + type=str, default='.kunit', metavar='build_dir') 152 + parser.add_argument('--make_options', 153 + help='X=Y make option, can be repeated.', 154 + action='append') 155 + parser.add_argument('--alltests', 156 + help='Run all KUnit tests through allyesconfig', 157 + action='store_true') 158 + 159 + def add_build_opts(parser): 160 + parser.add_argument('--jobs', 161 + help='As in the make command, "Specifies the number of ' 162 + 'jobs (commands) to run simultaneously."', 163 + type=int, default=8, metavar='jobs') 164 + 165 + def add_exec_opts(parser): 166 + parser.add_argument('--timeout', 167 + help='maximum number of seconds to allow for all tests ' 168 + 'to run. This does not include time taken to build the ' 169 + 'tests.', 170 + type=int, 171 + default=300, 172 + metavar='timeout') 173 + 174 + def add_parse_opts(parser): 175 + parser.add_argument('--raw_output', help='don\'t format output from kernel', 176 + action='store_true') 177 + 112 178 113 179 def main(argv, linux=None): 114 180 parser = argparse.ArgumentParser( 115 181 description='Helps writing and running KUnit tests.') 116 182 subparser = parser.add_subparsers(dest='subcommand') 117 183 184 + # The 'run' command will config, build, exec, and parse in one go. 118 185 run_parser = subparser.add_parser('run', help='Runs KUnit tests.') 119 - run_parser.add_argument('--raw_output', help='don\'t format output from kernel', 120 - action='store_true') 186 + add_common_opts(run_parser) 187 + add_build_opts(run_parser) 188 + add_exec_opts(run_parser) 189 + add_parse_opts(run_parser) 121 190 122 - run_parser.add_argument('--timeout', 123 - help='maximum number of seconds to allow for all tests ' 124 - 'to run. This does not include time taken to build the ' 125 - 'tests.', 126 - type=int, 127 - default=300, 128 - metavar='timeout') 191 + config_parser = subparser.add_parser('config', 192 + help='Ensures that .config contains all of ' 193 + 'the options in .kunitconfig') 194 + add_common_opts(config_parser) 129 195 130 - run_parser.add_argument('--jobs', 131 - help='As in the make command, "Specifies the number of ' 132 - 'jobs (commands) to run simultaneously."', 133 - type=int, default=8, metavar='jobs') 196 + build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests') 197 + add_common_opts(build_parser) 198 + add_build_opts(build_parser) 134 199 135 - run_parser.add_argument('--build_dir', 136 - help='As in the make command, it specifies the build ' 137 - 'directory.', 138 - type=str, default='', metavar='build_dir') 200 + exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests') 201 + add_common_opts(exec_parser) 202 + add_exec_opts(exec_parser) 203 + add_parse_opts(exec_parser) 139 204 140 - run_parser.add_argument('--defconfig', 141 - help='Uses a default .kunitconfig.', 142 - action='store_true') 143 - 144 - run_parser.add_argument('--alltests', 145 - help='Run all KUnit tests through allyesconfig', 146 - action='store_true') 147 - 148 - run_parser.add_argument('--make_options', 149 - help='X=Y make option, can be repeated.', 150 - action='append') 205 + # The 'parse' option is special, as it doesn't need the kernel source 206 + # (therefore there is no need for a build_dir, hence no add_common_opts) 207 + # and the '--file' argument is not relevant to 'run', so isn't in 208 + # add_parse_opts() 209 + parse_parser = subparser.add_parser('parse', 210 + help='Parses KUnit results from a file, ' 211 + 'and parses formatted results.') 212 + add_parse_opts(parse_parser) 213 + parse_parser.add_argument('file', 214 + help='Specifies the file to read results from.', 215 + type=str, nargs='?', metavar='input_file') 151 216 152 217 cli_args = parser.parse_args(argv) 153 218 154 219 if cli_args.subcommand == 'run': 155 - if get_kernel_root_path(): 156 - os.chdir(get_kernel_root_path()) 220 + if not os.path.exists(cli_args.build_dir): 221 + os.mkdir(cli_args.build_dir) 222 + kunit_kernel.kunitconfig_path = os.path.join( 223 + cli_args.build_dir, 224 + kunit_kernel.kunitconfig_path) 157 225 158 - if cli_args.build_dir: 159 - if not os.path.exists(cli_args.build_dir): 160 - os.mkdir(cli_args.build_dir) 161 - kunit_kernel.kunitconfig_path = os.path.join( 162 - cli_args.build_dir, 163 - kunit_kernel.kunitconfig_path) 164 - 165 - if cli_args.defconfig: 226 + if not os.path.exists(kunit_kernel.kunitconfig_path): 166 227 create_default_kunitconfig() 167 228 168 229 if not linux: ··· 252 153 cli_args.timeout, 253 154 cli_args.jobs, 254 155 cli_args.build_dir, 255 - cli_args.defconfig, 256 156 cli_args.alltests, 257 157 cli_args.make_options) 258 158 result = run_tests(linux, request) 159 + if result.status != KunitStatus.SUCCESS: 160 + sys.exit(1) 161 + elif cli_args.subcommand == 'config': 162 + if cli_args.build_dir: 163 + if not os.path.exists(cli_args.build_dir): 164 + os.mkdir(cli_args.build_dir) 165 + kunit_kernel.kunitconfig_path = os.path.join( 166 + cli_args.build_dir, 167 + kunit_kernel.kunitconfig_path) 168 + 169 + if not os.path.exists(kunit_kernel.kunitconfig_path): 170 + create_default_kunitconfig() 171 + 172 + if not linux: 173 + linux = kunit_kernel.LinuxSourceTree() 174 + 175 + request = KunitConfigRequest(cli_args.build_dir, 176 + cli_args.make_options) 177 + result = config_tests(linux, request) 178 + kunit_parser.print_with_timestamp(( 179 + 'Elapsed time: %.3fs\n') % ( 180 + result.elapsed_time)) 181 + if result.status != KunitStatus.SUCCESS: 182 + sys.exit(1) 183 + elif cli_args.subcommand == 'build': 184 + if cli_args.build_dir: 185 + if not os.path.exists(cli_args.build_dir): 186 + os.mkdir(cli_args.build_dir) 187 + kunit_kernel.kunitconfig_path = os.path.join( 188 + cli_args.build_dir, 189 + kunit_kernel.kunitconfig_path) 190 + 191 + if not os.path.exists(kunit_kernel.kunitconfig_path): 192 + create_default_kunitconfig() 193 + 194 + if not linux: 195 + linux = kunit_kernel.LinuxSourceTree() 196 + 197 + request = KunitBuildRequest(cli_args.jobs, 198 + cli_args.build_dir, 199 + cli_args.alltests, 200 + cli_args.make_options) 201 + result = build_tests(linux, request) 202 + kunit_parser.print_with_timestamp(( 203 + 'Elapsed time: %.3fs\n') % ( 204 + result.elapsed_time)) 205 + if result.status != KunitStatus.SUCCESS: 206 + sys.exit(1) 207 + elif cli_args.subcommand == 'exec': 208 + if cli_args.build_dir: 209 + if not os.path.exists(cli_args.build_dir): 210 + os.mkdir(cli_args.build_dir) 211 + kunit_kernel.kunitconfig_path = os.path.join( 212 + cli_args.build_dir, 213 + kunit_kernel.kunitconfig_path) 214 + 215 + if not os.path.exists(kunit_kernel.kunitconfig_path): 216 + create_default_kunitconfig() 217 + 218 + if not linux: 219 + linux = kunit_kernel.LinuxSourceTree() 220 + 221 + exec_request = KunitExecRequest(cli_args.timeout, 222 + cli_args.build_dir, 223 + cli_args.alltests) 224 + exec_result = exec_tests(linux, exec_request) 225 + parse_request = KunitParseRequest(cli_args.raw_output, 226 + exec_result.result) 227 + result = parse_tests(parse_request) 228 + kunit_parser.print_with_timestamp(( 229 + 'Elapsed time: %.3fs\n') % ( 230 + exec_result.elapsed_time)) 231 + if result.status != KunitStatus.SUCCESS: 232 + sys.exit(1) 233 + elif cli_args.subcommand == 'parse': 234 + if cli_args.file == None: 235 + kunit_output = sys.stdin 236 + else: 237 + with open(cli_args.file, 'r') as f: 238 + kunit_output = f.read().splitlines() 239 + request = KunitParseRequest(cli_args.raw_output, 240 + kunit_output) 241 + result = parse_tests(request) 259 242 if result.status != KunitStatus.SUCCESS: 260 243 sys.exit(1) 261 244 else:
+59 -4
tools/testing/kunit/kunit_tool_test.py
··· 239 239 self.print_patch.stop() 240 240 pass 241 241 242 + def test_config_passes_args_pass(self): 243 + kunit.main(['config'], self.linux_source_mock) 244 + assert self.linux_source_mock.build_reconfig.call_count == 1 245 + assert self.linux_source_mock.run_kernel.call_count == 0 246 + 247 + def test_build_passes_args_pass(self): 248 + kunit.main(['build'], self.linux_source_mock) 249 + assert self.linux_source_mock.build_reconfig.call_count == 0 250 + self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, '', None) 251 + assert self.linux_source_mock.run_kernel.call_count == 0 252 + 253 + def test_exec_passes_args_pass(self): 254 + kunit.main(['exec'], self.linux_source_mock) 255 + assert self.linux_source_mock.build_reconfig.call_count == 0 256 + assert self.linux_source_mock.run_kernel.call_count == 1 257 + self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='', timeout=300) 258 + self.print_mock.assert_any_call(StrContains('Testing complete.')) 259 + 242 260 def test_run_passes_args_pass(self): 243 261 kunit.main(['run'], self.linux_source_mock) 244 262 assert self.linux_source_mock.build_reconfig.call_count == 1 ··· 264 246 self.linux_source_mock.run_kernel.assert_called_once_with( 265 247 build_dir='', timeout=300) 266 248 self.print_mock.assert_any_call(StrContains('Testing complete.')) 249 + 250 + def test_exec_passes_args_fail(self): 251 + self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 252 + with self.assertRaises(SystemExit) as e: 253 + kunit.main(['exec'], self.linux_source_mock) 254 + assert type(e.exception) == SystemExit 255 + assert e.exception.code == 1 267 256 268 257 def test_run_passes_args_fail(self): 269 258 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) ··· 282 257 assert self.linux_source_mock.run_kernel.call_count == 1 283 258 self.print_mock.assert_any_call(StrContains(' 0 tests run')) 284 259 260 + def test_exec_raw_output(self): 261 + self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 262 + kunit.main(['exec', '--raw_output'], self.linux_source_mock) 263 + assert self.linux_source_mock.run_kernel.call_count == 1 264 + for kall in self.print_mock.call_args_list: 265 + assert kall != mock.call(StrContains('Testing complete.')) 266 + assert kall != mock.call(StrContains(' 0 tests run')) 267 + 285 268 def test_run_raw_output(self): 286 269 self.linux_source_mock.run_kernel = mock.Mock(return_value=[]) 287 - with self.assertRaises(SystemExit) as e: 288 - kunit.main(['run', '--raw_output'], self.linux_source_mock) 289 - assert type(e.exception) == SystemExit 290 - assert e.exception.code == 1 270 + kunit.main(['run', '--raw_output'], self.linux_source_mock) 291 271 assert self.linux_source_mock.build_reconfig.call_count == 1 292 272 assert self.linux_source_mock.run_kernel.call_count == 1 273 + for kall in self.print_mock.call_args_list: 274 + assert kall != mock.call(StrContains('Testing complete.')) 275 + assert kall != mock.call(StrContains(' 0 tests run')) 276 + 277 + def test_exec_timeout(self): 278 + timeout = 3453 279 + kunit.main(['exec', '--timeout', str(timeout)], self.linux_source_mock) 280 + self.linux_source_mock.run_kernel.assert_called_once_with(build_dir='', timeout=timeout) 281 + self.print_mock.assert_any_call(StrContains('Testing complete.')) 293 282 294 283 def test_run_timeout(self): 295 284 timeout = 3453 ··· 319 280 assert self.linux_source_mock.build_reconfig.call_count == 1 320 281 self.linux_source_mock.run_kernel.assert_called_once_with( 321 282 build_dir=build_dir, timeout=300) 283 + self.print_mock.assert_any_call(StrContains('Testing complete.')) 284 + 285 + def test_config_builddir(self): 286 + build_dir = '.kunit' 287 + kunit.main(['config', '--build_dir', build_dir], self.linux_source_mock) 288 + assert self.linux_source_mock.build_reconfig.call_count == 1 289 + 290 + def test_build_builddir(self): 291 + build_dir = '.kunit' 292 + kunit.main(['build', '--build_dir', build_dir], self.linux_source_mock) 293 + self.linux_source_mock.build_um_kernel.assert_called_once_with(False, 8, build_dir, None) 294 + 295 + def test_exec_builddir(self): 296 + build_dir = '.kunit' 297 + kunit.main(['exec', '--build_dir', build_dir], self.linux_source_mock) 298 + self.linux_source_mock.run_kernel.assert_called_once_with(build_dir=build_dir, timeout=300) 322 299 self.print_mock.assert_any_call(StrContains('Testing complete.')) 323 300 324 301 if __name__ == '__main__':