public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: Cleber Rosa <crosa@redhat.com>,
	David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 18/22] tasst: IP address allocation helpers
Date: Mon,  5 Aug 2024 22:36:57 +1000	[thread overview]
Message-ID: <20240805123701.1720730-19-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240805123701.1720730-1-david@gibson.dropbear.id.au>

A bunch of our test scenarious will require us to allocate IPv4 and IPv6
addresses in example networks.  Make helpers to do this easily.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 test/Makefile               |  2 +-
 test/tasst/address.py       | 79 +++++++++++++++++++++++++++++++++++++
 test/tasst/selftest/veth.py | 41 ++++++++++++++++++-
 test/tasst/transfer.py      |  6 +--
 4 files changed, 123 insertions(+), 5 deletions(-)
 create mode 100644 test/tasst/address.py

diff --git a/test/Makefile b/test/Makefile
index 584f56e9..f3a3cc58 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -72,7 +72,7 @@ EXETER_PY = build/build.py
 EXETER_JOBS = $(EXETER_SH:%.sh=%.json) $(EXETER_PY:%.py=%.json)
 AVOCADO_JOBS = $(EXETER_JOBS) avocado/static_checkers.json
 
-TASST_SRCS = __init__.py __main__.py nstool.py snh.py transfer.py \
+TASST_SRCS = __init__.py __main__.py address.py nstool.py snh.py transfer.py \
 	selftest/__init__.py selftest/static_ifup.py selftest/veth.py
 
 EXETER_META = meta/lint.json meta/tasst.json
diff --git a/test/tasst/address.py b/test/tasst/address.py
new file mode 100644
index 00000000..70899789
--- /dev/null
+++ b/test/tasst/address.py
@@ -0,0 +1,79 @@
+#! /usr/bin/env python3
+
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright Red Hat
+# Author: David Gibson <david@gibson.dropbear.id.au>
+
+"""
+Test A Simple Socket Transport
+
+address.py - Address allocation helpers
+"""
+
+import ipaddress
+
+import exeter
+
+# Loopback addresses, for convenience
+LOOPBACK4 = ipaddress.ip_address('127.0.0.1')
+LOOPBACK6 = ipaddress.ip_address('::1')
+
+# Documentation test networks defined by RFC 5737
+TEST_NET_1 = ipaddress.ip_network('192.0.2.0/24')
+TEST_NET_2 = ipaddress.ip_network('198.51.100.0/24')
+TEST_NET_3 = ipaddress.ip_network('203.0.113.0/24')
+
+# Documentation test network defined by RFC 3849
+TEST_NET6 = ipaddress.ip_network('2001:db8::/32')
+# Some subnets of that for our usage
+TEST_NET6_TASST_A = ipaddress.ip_network('2001:db8:9a55:aaaa::/64')
+TEST_NET6_TASST_B = ipaddress.ip_network('2001:db8:9a55:bbbb::/64')
+TEST_NET6_TASST_C = ipaddress.ip_network('2001:db8:9a55:cccc::/64')
+
+
+class IpiAllocator:
+    """IP address allocator"""
+
+    DEFAULT_NETS = [TEST_NET_1, TEST_NET6_TASST_A]
+
+    def __init__(self, *nets):
+        if not nets:
+            nets = self.DEFAULT_NETS
+
+        self.nets = [ipaddress.ip_network(n) for n in nets]
+        self.hostses = [n.hosts() for n in self.nets]
+
+    def next_ipis(self):
+        addrs = [next(h) for h in self.hostses]
+        return [ipaddress.ip_interface(f'{a}/{n.prefixlen}')
+                for a, n in zip(addrs, self.nets)]
+
+
+@exeter.test
+def ipa_test(nets=None, count=12):
+    if nets is None:
+        ipa = IpiAllocator()
+        nets = IpiAllocator.DEFAULT_NETS
+    else:
+        ipa = IpiAllocator(*nets)
+
+    addrsets = [set() for i in range(len(nets))]
+    for i in range(count):
+        addrs = ipa.next_ipis()
+        # Check we got as many addresses as expected
+        exeter.assert_eq(len(addrs), len(nets))
+        for s, a, n in zip(addrsets, addrs, nets):
+            # Check the addresses belong to the right network
+            exeter.assert_eq(a.network, ipaddress.ip_network(n))
+            s.add(a)
+
+    print(addrsets)
+    # Check the addresses are unique
+    for s in addrsets:
+        exeter.assert_eq(len(s), count)
+
+
+@exeter.test
+def ipa_test_custom():
+    ipa_test(nets=['10.55.0.0/16', '192.168.55.0/24', 'fd00:9a57:a000::/48'])
diff --git a/test/tasst/selftest/veth.py b/test/tasst/selftest/veth.py
index 24bbdc27..39ac947d 100644
--- a/test/tasst/selftest/veth.py
+++ b/test/tasst/selftest/veth.py
@@ -16,7 +16,7 @@ import ipaddress
 
 import exeter
 
-from tasst import nstool
+from tasst import address, nstool, transfer
 
 
 @contextlib.contextmanager
@@ -65,3 +65,42 @@ def test_optimistic_dad():
 @exeter.test
 def test_no_dad():
     test_slaac(dad='disable')
+
+
+@contextlib.contextmanager
+def configured_veth(ip1, ip2):
+    with unconfigured_veth() as (ns1, ns2):
+        ns1.ifup('lo')
+        ns1.ifup('veth1', ip1, dad='disable')
+
+        ns2.ifup('lo')
+        ns2.ifup('veth2', ip2, dad='disable')
+
+        yield (ns1, ns2)
+
+
+@contextlib.contextmanager
+def veth_transfer(ip1, ip2):
+    with configured_veth(ip1, ip2) as (ns1, ns2):
+        yield transfer.TransferTestScenario(client=ns1, server=ns2,
+                                            connect_ip=ip2.ip,
+                                            connect_port=10000)
+
+
+ipa = address.IpiAllocator()
+NS1_IP4, NS1_IP6 = ipa.next_ipis()
+NS2_IP4, NS2_IP6 = ipa.next_ipis()
+
+
+def veth_transfer4():
+    return veth_transfer(NS1_IP4, NS2_IP4)
+
+
+transfer.transfer_tests(veth_transfer4)
+
+
+def veth_transfer6():
+    return veth_transfer(NS1_IP6, NS2_IP6)
+
+
+transfer.transfer_tests(veth_transfer6)
diff --git a/test/tasst/transfer.py b/test/tasst/transfer.py
index be3eebc2..a5aa0614 100644
--- a/test/tasst/transfer.py
+++ b/test/tasst/transfer.py
@@ -17,7 +17,7 @@ import time
 
 import exeter
 
-from . import nstool, snh
+from . import address, nstool, snh
 
 
 # HACK: how long to wait for the server to be ready and listening (s)
@@ -175,7 +175,7 @@ def local_transfer4():
     with nstool.unshare_snh('ns', '-Un') as ns:
         ns.ifup('lo')
         yield TransferTestScenario(client=ns, server=ns,
-                                   connect_ip=IPv4Address('127.0.0.1'),
+                                   connect_ip=address.LOOPBACK4,
                                    connect_port=10000)
 
 
@@ -187,7 +187,7 @@ def local_transfer6():
     with nstool.unshare_snh('ns', '-Un') as ns:
         ns.ifup('lo')
         yield TransferTestScenario(client=ns, server=ns,
-                                   connect_ip=IPv6Address('::1'),
+                                   connect_ip=address.LOOPBACK6,
                                    connect_port=10000)
 
 
-- 
@@ -17,7 +17,7 @@ import time
 
 import exeter
 
-from . import nstool, snh
+from . import address, nstool, snh
 
 
 # HACK: how long to wait for the server to be ready and listening (s)
@@ -175,7 +175,7 @@ def local_transfer4():
     with nstool.unshare_snh('ns', '-Un') as ns:
         ns.ifup('lo')
         yield TransferTestScenario(client=ns, server=ns,
-                                   connect_ip=IPv4Address('127.0.0.1'),
+                                   connect_ip=address.LOOPBACK4,
                                    connect_port=10000)
 
 
@@ -187,7 +187,7 @@ def local_transfer6():
     with nstool.unshare_snh('ns', '-Un') as ns:
         ns.ifup('lo')
         yield TransferTestScenario(client=ns, server=ns,
-                                   connect_ip=IPv6Address('::1'),
+                                   connect_ip=address.LOOPBACK6,
                                    connect_port=10000)
 
 
-- 
2.45.2


  parent reply	other threads:[~2024-08-05 12:37 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-05 12:36 [PATCH v2 00/22] RFC: Proof-of-concept based exeter+Avocado tests David Gibson
2024-08-05 12:36 ` [PATCH v2 01/22] nstool: Fix some trivial typos David Gibson
2024-08-05 12:36 ` [PATCH v2 02/22] nstool: Propagate SIGTERM to processes executed in the namespace David Gibson
2024-08-07  7:23   ` Stefano Brivio
2024-08-05 12:36 ` [PATCH v2 03/22] test: run static checkers with Avocado and JSON definitions David Gibson
2024-08-05 12:36 ` [PATCH v2 04/22] test: Extend make targets to run Avocado tests David Gibson
2024-08-05 12:36 ` [PATCH v2 05/22] test: Exeter based static tests David Gibson
2024-08-05 12:36 ` [PATCH v2 06/22] test: Add exeter+Avocado based build tests David Gibson
2024-08-06 22:11   ` Stefano Brivio
2024-08-07 10:51     ` David Gibson
2024-08-07 13:06       ` Stefano Brivio
2024-08-08  1:28         ` David Gibson
2024-08-08 22:55           ` Stefano Brivio
2024-08-05 12:36 ` [PATCH v2 07/22] test: Add linters for Python code David Gibson
2024-08-05 12:36 ` [PATCH v2 08/22] tasst: Introduce library of common test helpers David Gibson
2024-08-05 12:36 ` [PATCH v2 09/22] tasst: "snh" module for simulated network hosts David Gibson
2024-08-05 12:36 ` [PATCH v2 10/22] tasst: Add helper to get network interface names for a site David Gibson
2024-08-05 12:36 ` [PATCH v2 11/22] tasst: Add helpers to run commands with nstool David Gibson
2024-08-05 12:36 ` [PATCH v2 12/22] tasst: Add ifup and network address helpers to SimNetHost David Gibson
2024-08-05 12:36 ` [PATCH v2 13/22] tasst: Helper for creating veth devices between namespaces David Gibson
2024-08-05 12:36 ` [PATCH v2 14/22] tasst: Add helper for getting MTU of a network interface David Gibson
2024-08-05 12:36 ` [PATCH v2 15/22] tasst: Add helper to wait for IP address to appear David Gibson
2024-08-05 12:36 ` [PATCH v2 16/22] tasst: Add helpers for getting a SimNetHost's routes David Gibson
2024-08-05 12:36 ` [PATCH v2 17/22] tasst: Helpers to test transferring data between sites David Gibson
2024-08-05 12:36 ` David Gibson [this message]
2024-08-05 12:36 ` [PATCH v2 19/22] tasst: Helpers for testing NDP behaviour David Gibson
2024-08-05 12:36 ` [PATCH v2 20/22] tasst: Helpers for testing DHCP & DHCPv6 behaviour David Gibson
2024-08-05 12:37 ` [PATCH v2 21/22] tasst: Helpers to construct a simple network environment for tests David Gibson
2024-08-05 12:37 ` [PATCH v2 22/22] avocado: Convert basic pasta tests David Gibson
2024-08-06 12:28 ` [PATCH v2 00/22] RFC: Proof-of-concept based exeter+Avocado tests David Gibson
2024-08-07  8:17   ` Stefano Brivio

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=20240805123701.1720730-19-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=crosa@redhat.com \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).