xref: /freebsd/tests/atf_python/utils.py (revision 829f0bcb5fe24bb523c5a9e7bd3bb79412e06906)
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    def check_constraints(self):
46        self._check_modules()
47