From 45f3f7c30974db37fdbf7a3a644117a4241322c1 Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 17 May 2024 07:55:11 +0000 Subject: [PATCH] test: netns: Add must{,_not}_receive In test cases, it is common that we want to verify that a certain type of traffic reaches a namespace. This has, up to this point, been solved by the generic sniffer.Sniffer. Try to improve the two most common use-cases... 1. Test if a flow reaches a namespace 2. Ensure that a flow does not reach a namespace ...by always exiting as soon as the first packet is received. This saves time on positive tests, and the synchronous API allows it to compose well with infamy.parallel(). --- test/infamy/netns.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/infamy/netns.py b/test/infamy/netns.py index ff3388ea..3f3a9792 100644 --- a/test/infamy/netns.py +++ b/test/infamy/netns.py @@ -182,3 +182,22 @@ class IsolatedMacVlan: return raise Exception(res) + + def must_receive(self, expr, timeout=None, ifname=None, must=True): + ifname = ifname if ifname else self.ifname + timeout = timeout if timeout else self.ping_timeout + + tshark = self.run(["tshark", "-nl", f"-i{ifname}", + f"-aduration:{timeout}", "-c1", expr], + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, check=True) + + needle = "1 packet captured" if must else "0 packets captured" + + if needle not in tshark.stdout: + raise Exception(tshark) + + def must_not_receive(self, *args, **kwargs): + self.must_receive(*args, **kwargs, must=False)