From 8e2e0cd2d71e72465922aba7a6a8bd59eaab4cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicki=20K=C5=99=C3=AD=C5=BEek?= Date: Mon, 24 Jun 2024 16:31:02 +0200 Subject: [PATCH] WIP use user+net namespace --- bin/tests/system/conftest.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/bin/tests/system/conftest.py b/bin/tests/system/conftest.py index 5585d1e402..8a09b43cb1 100644 --- a/bin/tests/system/conftest.py +++ b/bin/tests/system/conftest.py @@ -14,17 +14,20 @@ import os from pathlib import Path import re import shutil +import socket import subprocess import tempfile import time from typing import Any, List, Optional +import pyroute2 # TODO confitional import pytest pytest.register_assert_rewrite("isctest") import isctest from isctest.vars.dirs import SYSTEM_TEST_DIR_GIT_PATH +from namespaces import Namespace # Silence warnings caused by passing a pytest fixture to another fixture. @@ -87,6 +90,7 @@ def pytest_configure(config): # Ensure this hook only runs on the main pytest instance if xdist is # used to spawn other workers. if not XDIST_WORKER: + Namespace("user").__enter__() if config.pluginmanager.has_plugin("xdist") and config.option.numprocesses: # system tests depend on module scope for setup & teardown # enforce use "loadscope" scheduler or disable paralelism @@ -459,12 +463,24 @@ def run_tests_sh(system_test_dir, shell): return run_tests +@pytest.fixture(scope="module") +def namespace(shell): + with Namespace("net"): + shell("ifconfig.sh", ["up"]) + with pyroute2.IPRoute() as ipr: + lo = ipr.link_lookup(ifname="lo")[0] + ipr.route("add", gateway="127.0.0.1", oif=lo) + ipr.route("add", family=socket.AF_INET6, gateway="::1", oif=lo) + yield + + @pytest.fixture(scope="module", autouse=True) def system_test( # pylint: disable=too-many-arguments,too-many-statements request, system_test_dir, shell, perl, + namespace, ): """ Driver of the test setup/teardown process. Used automatically for every test module.