netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
	shuah@kernel.org, petrm@nvidia.com,
	linux-kselftest@vger.kernel.org, Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 5/6] selftests: net: support use of NetdevSimDev under "with" in python
Date: Wed, 10 Apr 2024 18:28:14 -0700	[thread overview]
Message-ID: <20240411012815.174400-6-kuba@kernel.org> (raw)
In-Reply-To: <20240411012815.174400-1-kuba@kernel.org>

Using "with" on an entire driver test env is supported already,
but it's also useful to use "with" on an individual nsim.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/testing/selftests/net/lib/py/nsim.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py
index 97457aca7e08..94aa32f59fdb 100644
--- a/tools/testing/selftests/net/lib/py/nsim.py
+++ b/tools/testing/selftests/net/lib/py/nsim.py
@@ -84,6 +84,17 @@ from .utils import cmd, ip
         for port_index in range(port_count):
             self.nsims.append(self._make_port(port_index, ifnames[port_index]))
 
+        self.removed = False
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, ex_type, ex_value, ex_tb):
+        """
+        __exit__ gets called at the end of a "with" block.
+        """
+        self.remove()
+
     def _make_port(self, port_index, ifname):
         return NetdevSim(self, port_index, ifname, self.ns)
 
@@ -112,7 +123,9 @@ from .utils import cmd, ip
             raise Exception("netdevices did not appear within timeout")
 
     def remove(self):
-        self.ctrl_write("del_device", "%u" % (self.addr, ))
+        if not self.removed:
+            self.ctrl_write("del_device", "%u" % (self.addr, ))
+            self.removed = True
 
     def remove_nsim(self, nsim):
         self.nsims.remove(nsim)
-- 
2.44.0


  parent reply	other threads:[~2024-04-11  1:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11  1:28 [PATCH net-next 0/6] selftests: net: exercise page pool reporting via netlink Jakub Kicinski
2024-04-11  1:28 ` [PATCH net-next 1/6] net: netdevsim: add some fake page pool use Jakub Kicinski
2024-04-11  1:28 ` [PATCH net-next 2/6] tools: ynl: don't return None for dumps Jakub Kicinski
2024-04-11 10:12   ` Donald Hunter
2024-04-11  1:28 ` [PATCH net-next 3/6] selftests: net: print report check location in python tests Jakub Kicinski
2024-04-12  7:45   ` Petr Machata
2024-04-11  1:28 ` [PATCH net-next 4/6] selftests: net: print full exception on failure Jakub Kicinski
2024-04-12  8:02   ` Petr Machata
2024-04-11  1:28 ` Jakub Kicinski [this message]
2024-04-12  8:07   ` [PATCH net-next 5/6] selftests: net: support use of NetdevSimDev under "with" in python Petr Machata
2024-04-11  1:28 ` [PATCH net-next 6/6] selftests: net: exercise page pool reporting via netlink Jakub Kicinski
2024-04-12  8:20   ` Petr Machata
2024-04-12 13:48     ` Jakub Kicinski

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=20240411012815.174400-6-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=petrm@nvidia.com \
    --cc=shuah@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).