]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/atf_python/utils.py
Merge llvm-project main llvmorg-15-init-16436-g18a6ab5b8d1f
[FreeBSD/FreeBSD.git] / tests / atf_python / utils.py
1 #!/usr/bin/env python3
2 import os
3 from ctypes import CDLL
4 from ctypes import get_errno
5 from ctypes.util import find_library
6 from typing import List
7 from typing import Optional
8
9 import pytest
10
11
12 class 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
30 libc = LibCWrapper()
31
32
33 class 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()