All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zhang Xiaoxu <zhangxiaoxu@huaweicloud.com>
To: zhangxiaoxu5@huawei.com, weiyongjun1@huawei.com,
	linux-kernel@vger.kernel.org, broonie@kernel.org,
	rostedt@goodmis.org, mingo@redhat.com, frowand.list@gmail.com,
	linux-spi@vger.kernel.org
Subject: [PATCH -next 07/14] kddv/cmd: Add command to create/remove mockup device
Date: Sat, 18 Nov 2023 18:40:33 +0800	[thread overview]
Message-ID: <20231118104040.386381-8-zhangxiaoxu@huaweicloud.com> (raw)
In-Reply-To: <20231118104040.386381-1-zhangxiaoxu@huaweicloud.com>

From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>

Add command kddv.cmds.mock to create/remove mockup device which support
by the test framework. Usage:

Create device:
  $ python3 -m kddv.cmds.mock --bus spi --devid mchp23k256
  create spi device mchp23k256 success!

Then the mockup device can be accessed by exists user space tools.
  $ ls /dev/mtd0
  mtd0    mtd0ro
  $ hexdump /dev/mtd0
  0000000 0000 0000 0000 0000 0000 0000 0000 0000
  *
  0008000

Remove the mockup device:
 $ python3 -m kddv.cmds.mock --bus spi --devid mchp23k256 -r
 [  198.718172] Deleting MTD partitions on "spi0.0":
 remove spi device mchp23k256 success!

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>
---
 tools/testing/kddv/kddv/Makefile         |   1 +
 tools/testing/kddv/kddv/cmds/__init__.py |   0
 tools/testing/kddv/kddv/cmds/mock.py     | 105 +++++++++++++++++++++++
 tools/testing/kddv/kddv/cmds/utils.py    |  28 ++++++
 4 files changed, 134 insertions(+)
 create mode 100755 tools/testing/kddv/kddv/cmds/__init__.py
 create mode 100755 tools/testing/kddv/kddv/cmds/mock.py
 create mode 100755 tools/testing/kddv/kddv/cmds/utils.py

diff --git a/tools/testing/kddv/kddv/Makefile b/tools/testing/kddv/kddv/Makefile
index a68112154669..a5c91fcb0e9a 100644
--- a/tools/testing/kddv/kddv/Makefile
+++ b/tools/testing/kddv/kddv/Makefile
@@ -12,6 +12,7 @@ install:
 	$(INSTALL) -m 0755 -d $(INSTALL_PATH)
 	$(INSTALL) __init__.py $(INSTALL_PATH)
 	cp -rf core/ $(INSTALL_PATH)
+	cp -rf cmds/ $(INSTALL_PATH)
 	cp -rf tests/ $(INSTALL_PATH)
 
 clean:
diff --git a/tools/testing/kddv/kddv/cmds/__init__.py b/tools/testing/kddv/kddv/cmds/__init__.py
new file mode 100755
index 000000000000..e69de29bb2d1
diff --git a/tools/testing/kddv/kddv/cmds/mock.py b/tools/testing/kddv/kddv/cmds/mock.py
new file mode 100755
index 000000000000..2ec5e45219a0
--- /dev/null
+++ b/tools/testing/kddv/kddv/cmds/mock.py
@@ -0,0 +1,105 @@
+#!/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 logging
+import argparse
+import unittest
+import subprocess
+
+from kddv.core.mockup import Mockup
+from kddv.core.ddunit import DriverTest
+from kddv.core.buses import *
+from . import utils
+
+ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+
+logger = logging.getLogger()
+
+def search(suite, bus: str, devid: str):
+    mdrv = None
+    for t in suite:
+        if isinstance(t, unittest.TestSuite):
+            driver = search(t, bus, devid)
+            if driver:
+                if driver.device_id == devid:
+                    return driver
+                mdrv = driver
+        elif isinstance(t, DriverTest):
+            if not hasattr(t, 'bus') or not hasattr(t, 'device_id'):
+                logger.debug(f"not a driver test case: {t}")
+                continue
+            if t.bus != bus:
+                continue
+            if t.device_id == devid:
+                return t
+            if  t.driver_name == devid:
+                mdrv = t
+        else:
+            return mdrv
+
+def do_mockup_device(t):
+    mock = Mockup.create(t.bus, t)
+    try:
+        subprocess.check_output(
+            ["/sbin/modprobe", t.module_name], stderr=subprocess.STDOUT
+        )
+    except:
+        logger.warning(f"Module {t.module_name} not found")
+        sys.exit(1)
+
+    mock.load()
+    logger.warning(f"create {t.bus} device {t.device_id} success!")
+
+def do_remove_device(t):
+    mock = Mockup.create(t.bus, t)
+    try:
+        mock.unload()
+        logger.warning(f"remove {t.bus} device {t.device_id} success!")
+    except:
+        logger.warning(f"{t.bus} device {t.device_id} not found")
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--bus", "-b", type=str, required=True,
+        choices=["i2c", "spi", "pci", "platform"], help="Bus Types"
+    )
+    parser.add_argument(
+        "--devid", "-d", type=str, required=True, help="Device ID"
+    )
+    parser.add_argument(
+        "--log-level", "-l", type=str, default=None,
+        choices=utils.LOG_LEVELS, help="Log level"
+    )
+    parser.add_argument(
+        "--remove", "-r", action='store_true', default=False,
+        help="Remove device",
+    )
+    args = parser.parse_args()
+
+    if args.log_level:
+        utils.setup_logger(args.log_level)
+
+    loader = unittest.defaultTestLoader
+    suites = loader.discover(os.path.join(ROOT_DIR, 'tests'))
+    driver = search(suites, args.bus, args.devid)
+    if driver is None:
+        logger.error(f"{args.bus} device {args.devid} not support")
+        sys.exit(1)
+
+    if not args.remove:
+        do_mockup_device(driver)
+    else:
+        do_remove_device(driver)
+
+    sys.exit(0)
+
+if __name__ == "__main__":
+    main()
diff --git a/tools/testing/kddv/kddv/cmds/utils.py b/tools/testing/kddv/kddv/cmds/utils.py
new file mode 100755
index 000000000000..8130d7a57a36
--- /dev/null
+++ b/tools/testing/kddv/kddv/cmds/utils.py
@@ -0,0 +1,28 @@
+#!/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 sys
+import logging
+
+logger = logging.getLogger()
+
+LOG_FORMAT = "%(asctime)-15s [%(levelname)-7s] %(message)s"
+LOG_LEVELS = {
+    'ERROR': logging.ERROR,
+    'WARN': logging.WARN,
+    'INFO': logging.INFO,
+    'DEBUG': logging.DEBUG
+}
+
+def setup_logger(level):
+    logger.setLevel(LOG_LEVELS.get(level))
+    handler = logging.StreamHandler(sys.stdout)
+    handler.setFormatter(logging.Formatter(
+        fmt=LOG_FORMAT, datefmt="%Y-%m-%d %H:%M:%S"
+    ))
+    logger.addHandler(handler)
-- 
2.34.1


  parent reply	other threads:[~2023-11-18 10:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-18 10:40 [PATCH -next 00/14] Implement a ligth weight device driver test framework Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 01/14] kddv/core: " Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 02/14] kddv/core: Allow test case config bpf program Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 03/14] kddv/core: Add io fault support to " Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 04/14] kddv/core: Check kmsg before return from test case Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 05/14] kddv/core: Support kernel memory leak detector Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 06/14] kddv/core: Add page and slab fault inject support Zhang Xiaoxu
2023-11-18 10:40 ` Zhang Xiaoxu [this message]
2023-11-18 10:40 ` [PATCH -next 08/14] kddv/cmd: Add command to run testcases Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 09/14] kddv/core: Add test support for SPI driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 10/14] kddv/tests: Add support for testing hwmon driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 11/14] kddv/tests/hwmon: Add test cases for max31722 driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 12/14] kddv/tests: Add support for testing mtd driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 13/14] kddv/tests/mtd: Add test cases for mchp23k256 driver Zhang Xiaoxu
2023-11-18 10:40 ` [PATCH -next 14/14] kddv: Add document for kddv Zhang Xiaoxu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231118104040.386381-8-zhangxiaoxu@huaweicloud.com \
    --to=zhangxiaoxu@huaweicloud.com \
    --cc=broonie@kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=weiyongjun1@huawei.com \
    --cc=zhangxiaoxu5@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.