[PATCH rdma-core v2 10/10] pyverbs: Unittest framework

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Noa Osherovich <noaos@xxxxxxxxxxxx>

Use Python's unittest framework to create unittests
using existing pyverbs code.

It can be executed as follows:
PYTHONPATH=build/python python3 ./pyverbs/run_tests.py

Signed-off-by: Noa Osherovich <noaos@xxxxxxxxxxxx>
Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxxxx>
---
 buildlib/pyverbs_functions.cmake | 24 +++++++++++++++++++
 pyverbs/CMakeLists.txt           | 10 ++++++++
 pyverbs/run_tests.py             | 22 +++++++++++++++++
 pyverbs/tests/__init__.py        |  0
 pyverbs/tests/device.py          | 41 ++++++++++++++++++++++++++++++++
 5 files changed, 97 insertions(+)
 create mode 100644 pyverbs/run_tests.py
 create mode 100644 pyverbs/tests/__init__.py
 create mode 100644 pyverbs/tests/device.py

diff --git a/buildlib/pyverbs_functions.cmake b/buildlib/pyverbs_functions.cmake
index abf35163..bd8e2028 100644
--- a/buildlib/pyverbs_functions.cmake
+++ b/buildlib/pyverbs_functions.cmake
@@ -34,3 +34,27 @@ function(rdma_python_module PY_MODULE)
       DESTINATION ${CMAKE_INSTALL_PYTHON_ARCH_LIB}/${PY_MODULE})
   endforeach()
 endfunction()
+
+function(rdma_python_test PY_MODULE)
+  foreach(PY_FILE ${ARGN})
+    get_filename_component(LINK "${CMAKE_CURRENT_SOURCE_DIR}/${PY_FILE}" ABSOLUTE)
+    rdma_create_symlink("${LINK}" "${BUILD_PYTHON}/${PY_MODULE}/${PY_FILE}")
+    install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PY_FILE}
+      DESTINATION ${CMAKE_INSTALL_PYTHON_ARCH_LIB}/${PY_MODULE})
+  endforeach()
+endfunction()
+
+# Make a python script runnable from the build/bin directory with all the
+# correct paths filled in
+function(rdma_internal_binary)
+  foreach(PY_FILE ${ARGN})
+    get_filename_component(ABS "${CMAKE_CURRENT_SOURCE_DIR}/${PY_FILE}" ABSOLUTE)
+    get_filename_component(FN "${CMAKE_CURRENT_SOURCE_DIR}/${PY_FILE}" NAME)
+    set(BIN_FN "${BUILD_BIN}/${FN}")
+
+    file(WRITE "${BIN_FN}" "#!/bin/sh
+PYTHONPATH='${BUILD_PYTHON}' exec '${PYTHON_EXECUTABLE}' '${ABS}' \"$@\"
+")
+    execute_process(COMMAND "chmod" "a+x" "${BIN_FN}")
+  endforeach()
+endfunction()
diff --git a/pyverbs/CMakeLists.txt b/pyverbs/CMakeLists.txt
index f7bc3eca..8d1f8030 100644
--- a/pyverbs/CMakeLists.txt
+++ b/pyverbs/CMakeLists.txt
@@ -11,4 +11,14 @@ rdma_cython_module(pyverbs
 rdma_python_module(pyverbs
   pyverbs_error.py
   __init__.py
+  run_tests.py
+  )
+
+rdma_python_test(pyverbs/tests
+  tests/__init__.py
+  tests/device.py
+  )
+
+rdma_internal_binary(
+  run_tests.py
   )
diff --git a/pyverbs/run_tests.py b/pyverbs/run_tests.py
new file mode 100644
index 00000000..dbee92d9
--- /dev/null
+++ b/pyverbs/run_tests.py
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB)
+# Copyright (c) 2018, Mellanox Technologies. All rights reserved.  See COPYING file
+import unittest,os,os.path,fnmatch
+import tests
+
+
+def test_all():
+    # FIXME: This implementation is for older Python versions, will
+    # be replaced with discover()
+    return test_suite
+
+module = __import__("tests")
+fns = [os.path.splitext(I)[0] for I in fnmatch.filter(os.listdir(module.__path__[0]),"*.py")]
+fns.remove("__init__")
+for I in fns:
+    __import__("tests." + I)
+test_suite = unittest.TestSuite(unittest.defaultTestLoader.loadTestsFromNames(fns,module))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest="test_all")
+
+
diff --git a/pyverbs/tests/__init__.py b/pyverbs/tests/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/pyverbs/tests/device.py b/pyverbs/tests/device.py
new file mode 100644
index 00000000..fde35968
--- /dev/null
+++ b/pyverbs/tests/device.py
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB)
+# Copyright (c) 2018, Mellanox Technologies. All rights reserved.  See COPYING file
+import unittest
+import pyverbs.device as d
+
+class device_test(unittest.TestCase):
+    """
+    Test various functionalities of the Device class.
+    """
+    def test_dev_list(self):
+        """
+        Verify that it's possible to get IB devices list.
+        """
+        lst = d.get_device_list()
+
+    def test_open_dev(self):
+        """
+        Test ibv_open_device()
+        """
+        lst = d.get_device_list()
+        for dev in lst:
+            ctx = d.Context(name=dev.name.decode())
+
+    def test_query_device(self):
+        """
+        Test ibv_query_device()
+        """
+        lst = d.get_device_list()
+        for dev in lst:
+            ctx = d.Context(name=dev.name.decode())
+            ctx.query_device()
+
+    def test_query_gid(self):
+        """
+        Test ibv_query_gid()
+        """
+        lst = d.get_device_list()
+        for dev in lst:
+            ctx = d.Context(name=dev.name.decode())
+            ctx.query_gid(port_num=1, index=0)
+
-- 
2.19.1




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Photo]     [Yosemite News]     [Yosemite Photos]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux