xref: /freebsd/tests/atf_python/utils.py (revision c07d6445eb89d9dd3950361b065b7bd110e3a043)
1#!/usr/bin/env python3
2import os
3from ctypes import CDLL
4from ctypes import get_errno
5from ctypes.util import find_library
6from typing import List
7from typing import Optional
8
9import pytest
10
11
12class LibCWrapper(object):
13    def __init__(self):
14        path: Optional[str] = find_library("c")
15        if path is None:
16            raise RuntimeError("libc not found")
17        self._libc = CDLL(path, use_errno=True)
18
19    def modfind(self, mod_name: str) -> int:
20        if self._libc.modfind(bytes(mod_name, encoding="ascii")) == -1:
21            return get_errno()
22        return 0
23
24    def jail_attach(self, jid: int) -> int:
25        if self._libc.jail_attach(jid) != 0:
26            return get_errno()
27        return 0
28
29
30libc = LibCWrapper()
31
32
33class BaseTest(object):
34    REQUIRED_MODULES: List[str] = []
35
36    def _check_modules(self):
37        for mod_name in self.REQUIRED_MODULES:
38            error_code = libc.modfind(mod_name)
39            if error_code != 0:
40                err_str = os.strerror(error_code)
41                pytest.skip(
42                    "kernel module '{}' not available: {}".format(mod_name, err_str)
43                )
44
45    @property
46    def test_id(self):
47        # 'test_ip6_output.py::TestIP6Output::test_output6_pktinfo[ipandif] (setup)'
48        return os.environ.get("PYTEST_CURRENT_TEST").split(" ")[0]
49
50    def setup_method(self, method):
51        """Run all pre-requisits for the test execution"""
52        self._check_modules()
53