diff mbox series

[-next,08/14] kddv/cmd: Add command to run testcases

Message ID 20231118104040.386381-9-zhangxiaoxu@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series Implement a ligth weight device driver test framework | expand

Commit Message

huaweicloud Nov. 18, 2023, 10:40 a.m. UTC
From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>

Add command kddv.cmds.test, which can help to run the testcase
use the python unittest framework.
  $ python3 -m kddv.cmd.test
  or
  $ python3 -m kddv.cmd.test kddv.tests.hwmon.test_max31722

Also enable fault inject test or enable kmemleak check. Usage:

Enable fault inject test:
  $ python3 -m kddv.cmds.test --fault-inject=all
  or
  $ python3 -m kddv.cmds.test --fault-inject=all \
  kddv.tests.hwmon.test_max31722.TestMax31722.test_device_probe

Enable kmemleak check:
  $ python3 -m kddv.cmds.test --kmemleak

Both fault inject and kmemleak will increase the time times.

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>
---
 tools/testing/kddv/kddv/cmds/test.py | 75 ++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100755 tools/testing/kddv/kddv/cmds/test.py
diff mbox series

Patch

diff --git a/tools/testing/kddv/kddv/cmds/test.py b/tools/testing/kddv/kddv/cmds/test.py
new file mode 100755
index 000000000000..9be346983286
--- /dev/null
+++ b/tools/testing/kddv/kddv/cmds/test.py
@@ -0,0 +1,75 @@ 
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel device driver verification
+#
+# Copyright (C) 2022-2023 Huawei Technologies Co., Ltd
+# Author: Wei Yongjun <weiyongjun1@huawei.com>
+
+import os
+import sys
+import fnmatch
+import argparse
+import unittest
+
+from kddv.core.ddunit import DriverTest
+from kddv.core.environ import environ
+from . import utils
+
+ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+
+def _list_suite(suite, tfilter=None):
+    for t in suite:
+        if isinstance(t, unittest.TestSuite):
+            _list_suite(t, tfilter)
+        elif isinstance(t, DriverTest):
+            id = t.id()
+            if tfilter and not any(fnmatch.fnmatch(id, f) for f in tfilter):
+                continue
+            print(f"kddv.tests.{id}")
+        else:
+            return None
+
+def list_suite(args):
+    args.filter = [f"*{f}*" for f in args.filter]
+    loader = unittest.defaultTestLoader
+    suites = loader.discover(os.path.join(ROOT_DIR, 'tests'))
+    _list_suite(suites, args.filter)
+    return 0
+
+def main() -> None:
+    parser = argparse.ArgumentParser()
+
+    parser.add_argument(
+        "--log-level", type=str, default=None,
+        choices=utils.LOG_LEVELS, help="Log Level"
+    )
+    parser.add_argument(
+        "--list", action='store_true', default=False,
+        help="List test cases",
+    )
+    parser.add_argument(
+        "--kmemleak", action='store_true', default=False,
+        help="Enable kmemeleak check",
+    )
+    parser.add_argument(
+        "--fault-inject", type=str, default=None,
+        choices=["slab", "page", "all"],
+        help="Enable fault inject features",
+    )
+    parser.add_argument("--filter", nargs="+", default=[],)
+
+    args, argv = parser.parse_known_args(sys.argv)
+    if args.log_level:
+        utils.setup_logger(args.log_level)
+    if args.list:
+        return list_suite(args)
+    if args.kmemleak:
+        environ.enable_kmemleak()
+    if args.fault_inject:
+        environ.enable_fault_inject(args.fault_inject)
+
+    unittest.main(verbosity=2, module=None, argv=argv)
+
+if __name__ == "__main__":
+    main()