Changeset View
Changeset View
Standalone View
Standalone View
tests/atf_python/utils.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
import os | import os | ||||
import pwd | |||||
from ctypes import CDLL | from ctypes import CDLL | ||||
from ctypes import get_errno | from ctypes import get_errno | ||||
from ctypes.util import find_library | from ctypes.util import find_library | ||||
from typing import Dict | |||||
from typing import List | from typing import List | ||||
from typing import Optional | from typing import Optional | ||||
import pytest | import pytest | ||||
class LibCWrapper(object): | class LibCWrapper(object): | ||||
def __init__(self): | def __init__(self): | ||||
path: Optional[str] = find_library("c") | path: Optional[str] = find_library("c") | ||||
Lint: PEP8 E701: multiple statements on one line (colon) | |||||
if path is None: | if path is None: | ||||
raise RuntimeError("libc not found") | raise RuntimeError("libc not found") | ||||
self._libc = CDLL(path, use_errno=True) | self._libc = CDLL(path, use_errno=True) | ||||
def modfind(self, mod_name: str) -> int: | def modfind(self, mod_name: str) -> int: | ||||
if self._libc.modfind(bytes(mod_name, encoding="ascii")) == -1: | if self._libc.modfind(bytes(mod_name, encoding="ascii")) == -1: | ||||
return get_errno() | return get_errno() | ||||
return 0 | return 0 | ||||
def jail_attach(self, jid: int) -> int: | def jail_attach(self, jid: int) -> int: | ||||
if self._libc.jail_attach(jid) != 0: | if self._libc.jail_attach(jid) != 0: | ||||
return get_errno() | return get_errno() | ||||
return 0 | return 0 | ||||
libc = LibCWrapper() | libc = LibCWrapper() | ||||
class BaseTest(object): | class BaseTest(object): | ||||
NEED_ROOT: bool = False # True if the class needs root privileges for the setup | |||||
Lint: PEP8 E701 multiple statements on one line (colon) Lint: PEP8 E701: multiple statements on one line (colon) | |||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
TARGET_USER = None # Set to the target user by the framework | |||||
REQUIRED_MODULES: List[str] = [] | REQUIRED_MODULES: List[str] = [] | ||||
Lint: PEP8 E701 multiple statements on one line (colon) Lint: PEP8 E701: multiple statements on one line (colon) | |||||
def _check_modules(self): | def _check_modules(self): | ||||
for mod_name in self.REQUIRED_MODULES: | for mod_name in self.REQUIRED_MODULES: | ||||
error_code = libc.modfind(mod_name) | error_code = libc.modfind(mod_name) | ||||
if error_code != 0: | if error_code != 0: | ||||
err_str = os.strerror(error_code) | err_str = os.strerror(error_code) | ||||
pytest.skip( | pytest.skip( | ||||
"kernel module '{}' not available: {}".format(mod_name, err_str) | "kernel module '{}' not available: {}".format(mod_name, err_str) | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
) | ) | ||||
@property | |||||
Lint: PEP8 E301 expected 1 blank line, found 0 Lint: PEP8 E301: expected 1 blank line, found 0 | |||||
def atf_vars(self) -> Dict[str, str]: | |||||
px = "_ATF_VAR_" | |||||
return {k[len(px):]: v for k, v in os.environ.items() if k.startswith(px)} | |||||
Lint: PEP8 E501 line too long (82 > 79 characters) Lint: PEP8 E501: line too long (82 > 79 characters) | |||||
def drop_privileges_user(self, user: str): | |||||
uid = pwd.getpwnam(user)[2] | |||||
print("Dropping privs to {}/{}".format(user, uid)) | |||||
os.setuid(uid) | |||||
def drop_privileges(self): | |||||
if self.TARGET_USER: | |||||
if self.TARGET_USER == "unprivileged": | |||||
user = self.atf_vars["unprivileged-user"] | |||||
else: | |||||
user = self.TARGET_USER | |||||
self.drop_privileges_user(user) | |||||
@property | @property | ||||
def test_id(self): | def test_id(self) -> str: | ||||
jlduran_gmail.comUnsubmitted Not Done Inline ActionsIs this return annotation for debugging, to print the commented line below (which also looks like a remnant from debugging)? jlduran_gmail.com: Is this return annotation for debugging, to print the commented line below (which also looks… | |||||
# 'test_ip6_output.py::TestIP6Output::test_output6_pktinfo[ipandif] (setup)' | # 'test_ip6_output.py::TestIP6Output::test_output6_pktinfo[ipandif] (setup)' | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
return os.environ.get("PYTEST_CURRENT_TEST").split(" ")[0] | return os.environ.get("PYTEST_CURRENT_TEST").split(" ")[0] | ||||
def setup_method(self, method): | def setup_method(self, method): | ||||
"""Run all pre-requisits for the test execution""" | """Run all pre-requisits for the test execution""" | ||||
self._check_modules() | self._check_modules() |
multiple statements on one line (colon)