Skip to content

Commit f585948

Browse files
committed
selftests: drv-net: add a TCP ping test case (and useful helpers)
JIRA: https://issues.redhat.com/browse/RHEL-57764 commit 31611ce Author: Jakub Kicinski <kuba@kernel.org> Date: Fri Apr 19 19:52:36 2024 -0700 selftests: drv-net: add a TCP ping test case (and useful helpers) More complex tests often have to spawn a background process, like a server which will respond to requests or tcpdump. Add support for creating such processes using the with keyword: with bkg("my-daemon", ..): # my-daemon is alive in this block My initial thought was to add this support to cmd() directly but it runs the command in the constructor, so by the time we __enter__ it's too late to make sure we used "background=True". Second useful helper transplanted from net_helper.sh is wait_port_listen(). The test itself uses socat, which insists on v6 addresses being wrapped in [], it's not the only command which requires this format, so add the wrapped address to env. The hope is to save test code from checking if address is v6. Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://lore.kernel.org/r/20240420025237.3309296-7-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Ivan Vecera <ivecera@redhat.com>
1 parent 45e7b8d commit f585948

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

tools/testing/selftests/drivers/net/lib/py/env.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ def __init__(self, src_path):
110110
self.addr = self.v6 if self.v6 else self.v4
111111
self.remote_addr = self.remote_v6 if self.remote_v6 else self.remote_v4
112112

113+
self.addr_ipver = "6" if self.v6 else "4"
114+
# Bracketed addresses, some commands need IPv6 to be inside []
115+
self.baddr = f"[{self.v6}]" if self.v6 else self.v4
116+
self.remote_baddr = f"[{self.remote_v6}]" if self.remote_v6 else self.remote_v4
117+
113118
self.ifname = self.dev['ifname']
114119
self.ifindex = self.dev['ifindex']
115120

tools/testing/selftests/drivers/net/ping.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# SPDX-License-Identifier: GPL-2.0
33

44
from lib.py import ksft_run, ksft_exit
5+
from lib.py import ksft_eq
56
from lib.py import NetDrvEpEnv
6-
from lib.py import cmd
7+
from lib.py import bkg, cmd, wait_port_listen, rand_port
78

89

910
def test_v4(cfg) -> None:
@@ -16,6 +17,24 @@ def test_v6(cfg) -> None:
1617
cmd(f"ping -c 1 -W0.5 {cfg.v6}", host=cfg.remote)
1718

1819

20+
def test_tcp(cfg) -> None:
21+
port = rand_port()
22+
listen_cmd = f"socat -{cfg.addr_ipver} -t 2 -u TCP-LISTEN:{port},reuseport STDOUT"
23+
24+
with bkg(listen_cmd, exit_wait=True) as nc:
25+
wait_port_listen(port)
26+
27+
cmd(f"echo ping | socat -t 2 -u STDIN TCP:{cfg.baddr}:{port}",
28+
shell=True, host=cfg.remote)
29+
ksft_eq(nc.stdout.strip(), "ping")
30+
31+
with bkg(listen_cmd, host=cfg.remote, exit_wait=True) as nc:
32+
wait_port_listen(port, host=cfg.remote)
33+
34+
cmd(f"echo ping | socat -t 2 -u STDIN TCP:{cfg.remote_baddr}:{port}", shell=True)
35+
ksft_eq(nc.stdout.strip(), "ping")
36+
37+
1938
def main() -> None:
2039
with NetDrvEpEnv(__file__) as cfg:
2140
ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, ))

tools/testing/selftests/net/lib/py/utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
import json as _json
4+
import random
5+
import re
46
import subprocess
7+
import time
8+
59

610
class cmd:
711
def __init__(self, comm, shell=True, fail=True, ns=None, background=False, host=None):
@@ -37,6 +41,20 @@ def process(self, terminate=True, fail=None):
3741
raise Exception("Command failed: %s\n%s" % (self.proc.args, stderr))
3842

3943

44+
class bkg(cmd):
45+
def __init__(self, comm, shell=True, fail=True, ns=None, host=None,
46+
exit_wait=False):
47+
super().__init__(comm, background=True,
48+
shell=shell, fail=fail, ns=ns, host=host)
49+
self.terminate = not exit_wait
50+
51+
def __enter__(self):
52+
return self
53+
54+
def __exit__(self, ex_type, ex_value, ex_tb):
55+
return self.process(terminate=self.terminate)
56+
57+
4058
def ip(args, json=None, ns=None, host=None):
4159
cmd_str = "ip "
4260
if json:
@@ -46,3 +64,28 @@ def ip(args, json=None, ns=None, host=None):
4664
if json:
4765
return _json.loads(cmd_obj.stdout)
4866
return cmd_obj
67+
68+
69+
def rand_port():
70+
"""
71+
Get unprivileged port, for now just random, one day we may decide to check if used.
72+
"""
73+
return random.randint(1024, 65535)
74+
75+
76+
def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5):
77+
end = time.monotonic() + deadline
78+
79+
pattern = f":{port:04X} .* "
80+
if proto == "tcp": # for tcp protocol additionally check the socket state
81+
pattern += "0A"
82+
pattern = re.compile(pattern)
83+
84+
while True:
85+
data = cmd(f'cat /proc/net/{proto}*', ns=ns, host=host, shell=True).stdout
86+
for row in data.split("\n"):
87+
if pattern.search(row):
88+
return
89+
if time.monotonic() > end:
90+
raise Exception("Waiting for port listen timed out")
91+
time.sleep(sleep)

0 commit comments

Comments
 (0)