authentik setup and tracing (#2)

* authentik sessions created successfully during setup without breaking tracing

* setup works on EN and DE localization by using regex patterns

* automated tracing with pytest --trace option, manual hook no longer needed

Reviewed-on: local-it-infrastructure/e2e_tests#2
Co-authored-by: Daniel <d.brummerloh@gmail.com>
Co-committed-by: Daniel <d.brummerloh@gmail.com>
This commit is contained in:
Daniel 2023-11-27 17:01:45 +01:00 committed by dan
parent 97ed87c79f
commit d2cd6ba47f
22 changed files with 519 additions and 304 deletions

View file

@ -5,16 +5,14 @@
# sys.path. It is thus good practise for projects to either put conftest.py under
# a package scope or to never import anything from a conftest.py file.
from pathlib import Path
import pytest
from dirmanager import DirManager
from dotenv import dotenv_values
pytest_plugins = [
"setup.setup_authentik",
]
from src.dirmanager import DirManager
TIMEOUT = 5000
def pytest_addoption(parser):
@ -23,7 +21,7 @@ def pytest_addoption(parser):
action="store",
)
parser.addoption(
"--tests_dir",
"--output_dir",
action="store",
)
parser.addoption(
@ -33,32 +31,49 @@ def pytest_addoption(parser):
@pytest.fixture(scope="session", autouse=True)
def dirmanager(request) -> DirManager:
tests_dir = request.config.getoption("--tests_dir")
tests_dir = Path(tests_dir)
def DIR(request) -> DirManager:
"""Fixture holding test directories
DIR.OUTPUT
DIR.SESSION
DIR.RECORDS
DIR.STATES
DIR.RESULTS
DIR.PROGRESS"""
output_dir = request.config.getoption("--output_dir")
assert output_dir is not None, "required pytest command line argument not given"
output_dir = Path(output_dir)
session_id = request.config.getoption("--session_id")
return DirManager(tests_dir=tests_dir, session_id=session_id)
assert session_id is not None, "required pytest command line argument not given"
dirmanager = DirManager(output_dir=output_dir, session_id=session_id)
dirmanager.create_all_dirs()
return dirmanager
@pytest.fixture(scope="session", autouse=True)
def dotenv_config(request) -> dict[str, str]:
dotenv_path = request.config.getoption("--env_file")
assert dotenv_path is not None, "required pytest command line argument not given"
dotenv_path = Path(dotenv_path)
assert dotenv_path.is_file()
return dotenv_values(dotenv_path)
return dotenv_values(dotenv_path) # type: ignore
@pytest.fixture(scope="session", autouse=True)
def RECORDS(dirmanager) -> Path:
assert isinstance(dirmanager, DirManager)
return dirmanager.dirs["records"]
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""saves traceback when test fails"""
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()
@pytest.fixture(scope="session", autouse=True)
def STATES(dirmanager) -> Path:
return dirmanager.dirs["states"]
@pytest.fixture(scope="session", autouse=True)
def RESULTS(dirmanager) -> Path:
return dirmanager.dirs["results"]
# we only look at actual failing test calls, not setup/teardown
if rep.when == "call" and rep.failed:
# saves traceback as .txt for failed test
filename = f"failed-{item.nodeid}.txt"
filename = filename.replace("/", "-")
filename = filename.replace("::", "-")
filepath = item.funcargs["DIR"].RESULTS / filename
with open(filepath, "a") as f:
f.write(rep.longreprtext + "\n")