All of lore.kernel.org
 help / color / mirror / Atom feed
From: Noa Osherovich <noaos@mellanox.com>
To: dledford@redhat.com, jgg@mellanox.com, leonro@mellanox.com
Cc: linux-rdma@vger.kernel.org, Maxim Chicherin <maximc@mellanox.com>,
	Noa Osherovich <noaos@mellanox.com>
Subject: [PATCH rdma-core 05/14] tests: RDMATestCase
Date: Mon, 19 Aug 2019 09:58:18 +0300	[thread overview]
Message-ID: <20190819065827.26921-6-noaos@mellanox.com> (raw)
In-Reply-To: <20190819065827.26921-1-noaos@mellanox.com>

From: Maxim Chicherin <maximc@mellanox.com>

Base class for future tests which allows user-set parameters:
device name, IB port, GID index and PKey index can be provided to the
tests. If not, they're selected at random.

Signed-off-by: Maxim Chicherin <maximc@mellanox.com>
Signed-off-by: Noa Osherovich <noaos@mellanox.com>
---
 tests/base.py | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)

diff --git a/tests/base.py b/tests/base.py
index 88f00e07b326..e141b83dea0e 100644
--- a/tests/base.py
+++ b/tests/base.py
@@ -2,9 +2,11 @@
 # Copyright (c) 2019 Mellanox Technologies, Inc . All rights reserved. See COPYING file
 
 import unittest
+import random
 
 from pyverbs.device import Context
 import pyverbs.device as d
+import pyverbs.enums as e
 from pyverbs.pd import PD
 
 
@@ -26,6 +28,105 @@ class PyverbsAPITestCase(unittest.TestCase):
             tup[0].close()
 
 
+class RDMATestCase(unittest.TestCase):
+    """
+    A base class for test cases which provides the option for user parameters.
+    These can be provided by manually adding the test case to the runner:
+    suite = unittest.TestSuite()
+    ... # Regular auto-detection of test cases, no parameters used.
+    # Now follows your manual addition of test cases e.g:
+    suite.addTest(RDMATestCase.parametrize(<TestCaseName>, dev_name='..',
+                                           ib_port=1, gid_index=3,
+                                           pkey_index=42))
+    """
+    ZERO_GID = '0000:0000:0000:0000'
+
+    def __init__(self, methodName='runTest', dev_name=None, ib_port=None,
+                 gid_index=None, pkey_index=None):
+        super(RDMATestCase, self).__init__(methodName)
+        self.dev_name = dev_name
+        self.ib_port = ib_port
+        self.gid_index = gid_index
+        self.pkey_index = pkey_index
+
+    @staticmethod
+    def parametrize(testcase_klass, dev_name=None, ib_port=None, gid_index=None,
+                    pkey_index=None):
+        """
+        Create a test suite containing all the tests from the given subclass
+        with the given dev_name, port, gid index and pkey_index.
+        """
+        loader = unittest.TestLoader()
+        names = loader.getTestCaseNames(testcase_klass)
+        suite = unittest.TestSuite()
+        for n in names:
+            suite.addTest(testcase_klass(n, dev_name=dev_name, ib_port=ib_port,
+                                         gid_index=gid_index,
+                                         pkey_index=pkey_index))
+        return suite
+
+    def setUp(self):
+        """
+        Verify that the test case has dev_name, ib_port, gid_index and pkey index.
+        If not provided by the user, a random valid combination will be used.
+        """
+        if self.pkey_index is None:
+            # To avoid iterating the entire pkeys table, if a pkey index wasn't
+            # provided, use index 0 which is always valid
+            self.pkey_index = 0
+
+        self.args = []
+        if self.dev_name is not None:
+            ctx = d.Context(name=self.dev_name)
+            if self.ib_port is not None:
+                if self.gid_index is not None:
+                    # We have all we need, return
+                    return
+                else:
+                    # Add avaiable GIDs of the given dev_name + port
+                    self._add_gids_per_port(ctx, self.dev_name, self.ib_port)
+            else:
+                # Add available GIDs for each port of the given dev_name
+                self._add_gids_per_device(ctx, self.dev_name)
+        else:
+            # Iterate available devices, add available GIDs for each of
+            # their ports
+            lst = d.get_device_list()
+            for dev in lst:
+                dev_name = dev.name.decode()
+                ctx = d.Context(name=dev_name)
+                self._add_gids_per_device(ctx, dev_name)
+
+        if not self.args:
+            raise unittest.SkipTest('No dev/port/GID combinations, please check your setup and try again')
+        # Choose one combination and use it
+        args = random.choice(self.args)
+        self.dev_name = args[0]
+        self.ib_port = args[1]
+        self.gid_index = args[2]
+
+    def _add_gids_per_port(self, ctx, dev, port):
+        idx = 0
+        ll = ctx.query_port(port).link_layer
+        while True:
+            gid = ctx.query_gid(port, idx)
+            if gid.gid[-19:] == self.ZERO_GID:
+                # No point iterating on
+                break
+            if ll == e.IBV_LINK_LAYER_ETHERNET and gid.gid[0:4] == 'fe80':
+                # Use only IPv4/IPv6 GIDs
+                idx += 1
+                continue
+            else:
+                self.args.append([dev, port, idx])
+                idx += 1
+
+    def _add_gids_per_device(self, ctx, dev):
+        port_count = ctx.query_device().phys_port_cnt
+        for port in range(port_count):
+            self._add_gids_per_port(ctx, dev, port+1)
+
+
 class BaseResources(object):
     """
     BaseResources class is a base aggregator object which contains basic
-- 
2.21.0


  parent reply	other threads:[~2019-08-19  6:58 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-19  6:58 [PATCH rdma-core 00/14] rdma-core tests infrastructure Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 01/14] pyverbs/tests: Rename base class Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 02/14] pyverbs: Move tests to a stand-alone directory Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 03/14] build: Add pyverbs-based test to the build Noa Osherovich
2019-08-19 13:50   ` Jason Gunthorpe
2019-08-20 13:00     ` Noa Osherovich
2019-08-22 16:18       ` Leon Romanovsky
2019-08-22 16:52         ` Jason Gunthorpe
2019-09-01 13:30           ` Noa Osherovich
2019-09-09 10:29             ` Leon Romanovsky
2019-09-09 10:39               ` Noa Osherovich
2019-09-09 11:26                 ` Leon Romanovsky
2019-09-09 11:30                   ` Noa Osherovich
2019-09-09 11:38                     ` Leon Romanovsky
2019-09-09 11:34                   ` Leon Romanovsky
2019-08-19  6:58 ` [PATCH rdma-core 04/14] tests: BaseResources Class Noa Osherovich
2019-08-19  6:58 ` Noa Osherovich [this message]
2019-08-19  6:58 ` [PATCH rdma-core 06/14] tests: TrafficResources class Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 07/14] tests: RCResources and UDResources classes Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 08/14] tests: ODP requires decorator Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 09/14] tests: Add traffic helper methods Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 10/14] tests: Add ODP RC test Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 11/14] tests: Add ODP UD test Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 12/14] tests: Fix test locating process Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 13/14] Documentation: Add background for rdma-core tests Noa Osherovich
2019-08-19  6:58 ` [PATCH rdma-core 14/14] tests: Unify API tests' output Noa Osherovich

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=20190819065827.26921-6-noaos@mellanox.com \
    --to=noaos@mellanox.com \
    --cc=dledford@redhat.com \
    --cc=jgg@mellanox.com \
    --cc=leonro@mellanox.com \
    --cc=linux-rdma@vger.kernel.org \
    --cc=maximc@mellanox.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.