netlink: connect netlink tests to the build

Reviewed By: ngie
Differential Revision: https://reviews.freebsd.org/D37708

(cherry picked from commit 3873bdc2f2)
This commit is contained in:
Alexander V. Chernikov 2022-12-16 12:02:17 +00:00
parent e121eaf737
commit 65f065ec71
6 changed files with 57 additions and 21 deletions

View File

@ -2,7 +2,7 @@
.PATH: ${.CURDIR}
FILES= __init__.py atf_pytest.py
FILES= __init__.py atf_pytest.py utils.py
SUBDIR= sys
.include <bsd.own.mk>

View File

@ -1,13 +1,6 @@
#!/usr/local/bin/python3
import json
import os
import socket
import time
from ctypes import cdll
from ctypes import get_errno
from ctypes.util import find_library
from typing import List
from typing import Optional
class ToolsHelper(object):

View File

@ -5,17 +5,14 @@
import socket
import sys
import time
from ctypes import cdll
from ctypes import get_errno
from ctypes.util import find_library
from multiprocessing import Pipe
from multiprocessing import Process
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Optional
from atf_python.sys.net.tools import ToolsHelper
from atf_python.utils import libc, BaseTest
def run_cmd(cmd: str, verbose=True) -> str:
@ -145,7 +142,7 @@ class IfaceFactory(object):
def __init__(self, test_name: str):
self.test_name = test_name
test_id = convert_test_name(test_name)
self.test_id = convert_test_name(test_name)
self.file_name = self.INTERFACES_FNAME
def _register_iface(self, iface_name: str):
@ -204,13 +201,9 @@ def set_subprocess(self, p):
@staticmethod
def attach_jid(jid: int):
_path: Optional[str] = find_library("c")
if _path is None:
raise Exception("libc not found")
path: str = _path
libc = cdll.LoadLibrary(path)
if libc.jail_attach(jid) != 0:
raise Exception("jail_attach() failed: errno {}".format(get_errno()))
error_code = libc.jail_attach(jid)
if error_code != 0:
raise Exception("jail_attach() failed: errno {}".format(error_code))
def attach(self):
self.attach_jid(self.jid)
@ -290,7 +283,7 @@ class SingleInterfaceMap(NamedTuple):
vnet_aliases: List[str]
class VnetTestTemplate(object):
class VnetTestTemplate(BaseTest):
TOPOLOGY = {}
def _get_vnet_handler(self, vnet_alias: str):
@ -395,6 +388,7 @@ def setup_method(self, method):
# 'test_ip6_output.py::TestIP6Output::test_output6_pktinfo[ipandif] (setup)'
test_id = os.environ.get("PYTEST_CURRENT_TEST").split(" ")[0]
test_name = test_id.split("::")[-1]
self.check_constraints()
topology = self.TOPOLOGY
# First, setup kernel objects - interfaces & vnets
obj_map = self.setup_topology(topology, test_name)

46
tests/atf_python/utils.py Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python3
import os
from ctypes import CDLL
from ctypes import get_errno
from ctypes.util import find_library
from typing import List
from typing import Optional
import pytest
class LibCWrapper(object):
def __init__(self):
path: Optional[str] = find_library("c")
if path is None:
raise RuntimeError("libc not found")
self._libc = CDLL(path, use_errno=True)
def modfind(self, mod_name: str) -> int:
if self._libc.modfind(bytes(mod_name, encoding="ascii")) == -1:
return get_errno()
return 0
def jail_attach(self, jid: int) -> int:
if self._libc.jail_attach(jid) != 0:
return get_errno()
return 0
libc = LibCWrapper()
class BaseTest(object):
REQUIRED_MODULES: List[str] = []
def _check_modules(self):
for mod_name in self.REQUIRED_MODULES:
error_code = libc.modfind(mod_name)
if error_code != 0:
err_str = os.strerror(error_code)
pytest.skip(
"kernel module '{}' not available: {}".format(mod_name, err_str)
)
def check_constraints(self):
self._check_modules()

View File

@ -24,6 +24,7 @@ TESTS_SUBDIRS+= ${_netgraph}
TESTS_SUBDIRS+= netinet
TESTS_SUBDIRS+= netinet6
TESTS_SUBDIRS+= netipsec
TESTS_SUBDIRS+= netlink
TESTS_SUBDIRS+= netmap
TESTS_SUBDIRS+= netpfil
TESTS_SUBDIRS+= opencrypto

View File

@ -22,6 +22,8 @@
class TestRtNlIface(SingleVnetTestTemplate):
REQUIRED_MODULES = ["netlink"]
def setup_method(self, method):
super().setup_method(method)
self.helper = NlHelper()