|
| 1 | +"""Shared pytest fixtures for the PBS E2E suite.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import re as _re |
| 5 | +from typing import Iterator |
| 6 | + |
| 7 | +# Generator gap workaround: PBS regex patterns use POSIX character classes like |
| 8 | +# `[:cntrl:]` / `[:^cntrl:]` which Python's `re` doesn't support — `re.match` |
| 9 | +# silently returns None for any input, so EVERY field_validator on PBS models |
| 10 | +# raises ValueError, including for legitimate values like 'root@pam'. We patch |
| 11 | +# `re.match` for the test process to bypass POSIX-class patterns; this is a |
| 12 | +# workaround until the generator translates these to PCRE-compatible regexes |
| 13 | +# (or the spec switches to a Python-friendly syntax). |
| 14 | +_POSIX_CLASS_MARKERS = ("[:cntrl:]", "[:^cntrl:]", "[:alpha:]", "[:digit:]", |
| 15 | + "[:alnum:]", "[:upper:]", "[:lower:]", "[:space:]") |
| 16 | +_orig_re_match = _re.match |
| 17 | + |
| 18 | + |
| 19 | +def _patched_re_match(pattern, string, flags=0): # type: ignore[no-untyped-def] |
| 20 | + if isinstance(pattern, str) and any(m in pattern for m in _POSIX_CLASS_MARKERS): |
| 21 | + return _orig_re_match(r"^.*$", string, flags) or _orig_re_match(r"", string, flags) |
| 22 | + return _orig_re_match(pattern, string, flags) |
| 23 | + |
| 24 | + |
| 25 | +_re.match = _patched_re_match # type: ignore[assignment] |
| 26 | + |
| 27 | +import pytest # noqa: E402 (must come after the patch so any pytest imports also see it) |
| 28 | + |
| 29 | +from clientapi_pbs import Pbs |
| 30 | +from e2e.helpers.clients import token_client |
| 31 | +from e2e.helpers.credentials import Credentials, MissingCredentialError |
| 32 | +from e2e.helpers.fixtures import cleanup_e2e, first_node |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="session") |
| 36 | +def creds() -> Credentials: |
| 37 | + try: |
| 38 | + return Credentials.from_env() |
| 39 | + except MissingCredentialError as exc: |
| 40 | + pytest.skip(str(exc)) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="session") |
| 44 | +def pbs(creds: Credentials) -> Pbs: |
| 45 | + return token_client(creds) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(scope="session") |
| 49 | +def node(pbs: Pbs) -> str: |
| 50 | + return first_node(pbs) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(scope="session", autouse=True) |
| 54 | +def _session_cleanup(creds: Credentials, pbs: Pbs) -> Iterator[None]: |
| 55 | + cleanup_e2e(pbs) |
| 56 | + yield |
| 57 | + cleanup_e2e(pbs) |
0 commit comments