opnsense-src/tests/atf_python/utils.py
Alexander V. Chernikov 2419f06beb testing: improve python vnet wrapper.
* Derive jail name from class name and method name, instead of just
method name. This change reduces the chances of different tests
clashing.
 Old: 'jail_test_one'. New: 'pytest:TestExampleSimplest:test_one'
* Simplify vnetX_handler() method signature by skipping obj_map (unused)
 and pipe. The latter can be accessed as the vnet property.
* Add `send_object()` method as a pair to the `wait_object` inside the
 VnetTestTemplate class.
* Add `test_id` property to the BaseTest method. Previously it was
 provided only for the VnetTestTemplate class. This change makes
 the identifier easily accessible for all users.

MFC after:	2 weeks

(cherry picked from commit f63825ff21)
2023-01-23 22:09:04 +00:00

56 lines
1.5 KiB
Python

#!/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)
)
@property
def test_id(self):
# 'test_ip6_output.py::TestIP6Output::test_output6_pktinfo[ipandif] (setup)'
return os.environ.get("PYTEST_CURRENT_TEST").split(" ")[0]
def setup_method(self, method):
"""Run all pre-requisits for the test execution"""
self._check_modules()
def cleanup(self, test_id: str):
"""Cleanup all test resources here"""
pass