@@ -18,6 +18,7 @@ rdma_python_module(pyverbs
rdma_python_test(pyverbs/tests
tests/__init__.py
tests/device.py
+ tests/pd.py
)
rdma_internal_binary(
new file mode 100644
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB)
+# Copyright (c) 2019, Mellanox Technologies. All rights reserved. See COPYING file
+
+import unittest
+import random
+
+from pyverbs.base import PyverbsRDMAErrno
+import pyverbs.device as d
+from pyverbs.pd import PD
+
+
+class pd_test(unittest.TestCase):
+ """
+ Test various functionalities of the PD class.
+ """
+ def test_alloc_pd(self):
+ """
+ Test ibv_alloc_pd()
+ """
+ lst = d.get_device_list()
+ for dev in lst:
+ with d.Context(name=dev.name.decode()) as ctx:
+ with PD(ctx):
+ pass
+
+ def test_dealloc_pd(self):
+ """
+ Test ibv_dealloc_pd()
+ """
+ lst = d.get_device_list()
+ for dev in lst:
+ with d.Context(name=dev.name.decode()) as ctx:
+ with PD(ctx) as pd:
+ pd.close()
+
+ def test_multiple_pd_creation(self):
+ """
+ Test multiple creations and destructions of a PD object
+ """
+ lst = d.get_device_list()
+ for dev in lst:
+ with d.Context(name=dev.name.decode()) as ctx:
+ for i in range(random.randint(1, 200)):
+ with PD(ctx) as pd:
+ pd.close()
+
+ def test_create_pd_none_ctx(self):
+ """
+ Verify that PD can't be created with a None context
+ """
+ try:
+ pd = PD(None)
+ except TypeError as te:
+ assert 'expected pyverbs.device.Context' in te.args[0]
+ assert 'got NoneType' in te.args[0]
+ else:
+ raise PyverbsRDMAErrno('Created a PD with None context')
+
+ def test_destroy_pd_twice(self):
+ """
+ Test bad flow cases in destruction of a PD object
+ """
+ lst = d.get_device_list()
+ for dev in lst:
+ with d.Context(name=dev.name.decode()) as ctx:
+ with PD(ctx) as pd:
+ # Pyverbs supports multiple destruction of objects, we are
+ # not expecting an exception here.
+ pd.close()
+ pd.close()
This patch adds tests for the PD related API. Bad flow of PD creation using an already freed context is not possible since it generated a segmentation fault in rdma-core. Other bad flows are checked. Signed-off-by: Noa Osherovich <noaos@mellanox.com> --- pyverbs/CMakeLists.txt | 1 + pyverbs/tests/pd.py | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 pyverbs/tests/pd.py