make-all-env-files-available (#4)

Before, a test had only access to it's own env file / configuration (wordpress could see wordpress env file). Now, all env files are available. Wordpress test can also read authentik env file, for example to get the authentik domain.

Reviewed-on: local-it-infrastructure/e2e_tests#4
Co-authored-by: Daniel <d.brummerloh@gmail.com>
Co-committed-by: Daniel <d.brummerloh@gmail.com>
This commit is contained in:
Daniel 2023-11-30 10:53:20 +01:00 committed by dan
parent 8172f685de
commit 2e33f8f014
5 changed files with 68 additions and 44 deletions

View file

@ -25,7 +25,9 @@ class DirManager:
def create_all_dirs(self): def create_all_dirs(self):
self.create_dirs(self._output_dir, exist_ok=True) self.create_dirs(self._output_dir, exist_ok=True)
self.create_dirs([self.SESSION, self.RECORDS, self.RECORDS / "html", self.STATES, self.RESULTS], exist_ok=True) self.create_dirs(
[self.SESSION, self.RECORDS, self.HTML, self.STATES, self.ENV_FILES, self.RESULTS], exist_ok=True
)
@property @property
def OUTPUT(self): def OUTPUT(self):
@ -37,15 +39,23 @@ class DirManager:
@property @property
def RECORDS(self): def RECORDS(self):
return self.SESSION / Path("records") return self.SESSION / "records"
@property
def HTML(self):
return self.RECORDS / "html"
@property @property
def STATES(self): def STATES(self):
return self.SESSION / Path("states") return self.SESSION / "states"
@property
def ENV_FILES(self):
return self.STATES / "env_files"
@property @property
def RESULTS(self): def RESULTS(self):
return self.SESSION / Path("results") return self.SESSION / "results"
@staticmethod @staticmethod
def create_dirs(dirs: Path | list[Path] | dict[str, Path], exist_ok=False): def create_dirs(dirs: Path | list[Path] | dict[str, Path], exist_ok=False):

View file

@ -33,28 +33,44 @@ class Runner:
assert self.test_dir_name assert self.test_dir_name
self.root_dir = Path(__file__).parent self.root_dir = Path(__file__).parent
def _run_main_setup_and_test(self): def run_tests(self):
# check if required dependencies have passed
self._assert_dependencies_passed()
# run main setup if available
if isinstance(self.main_setup_name, str): if isinstance(self.main_setup_name, str):
self._run_test_if_required( self._run_or_skip_test(
identifier_string=self.combine_names(self.name, self.main_setup_name), identifier_string=self.combine_names(self.name, self.main_setup_name),
test_path=self.root_dir / self.test_dir_name / self.main_setup_name, test_path=self.root_dir / self.test_dir_name / self.main_setup_name,
) )
# run main test if available
if isinstance(self.main_test_name, str): if isinstance(self.main_test_name, str):
self._run_test_if_required( self._run_or_skip_test(
identifier_string=self.combine_names(self.name, self.main_test_name), identifier_string=self.combine_names(self.name, self.main_test_name),
test_path=self.root_dir / self.test_dir_name / self.main_test_name, test_path=self.root_dir / self.test_dir_name / self.main_test_name,
) )
def _run_test_if_required(self, identifier_string: str, test_path: Path): # run sub tests if conditions are met
if not self.prevent_skip and self._test_already_passed(identifier_string, remove_existing=True): for sub_test in self.sub_tests:
condition_function = sub_test["condition"]
sub_test_name = sub_test["test_file"]
identifier_string = self.combine_names(self.name, sub_test_name)
if condition_function(self.config):
test_path = self.root_dir / self.test_dir_name / sub_test_name
self._run_or_skip_test(identifier_string=identifier_string, test_path=test_path)
else:
self._create_result_file(result=-1, identifier_string=identifier_string)
def _run_or_skip_test(self, identifier_string: str, test_path: Path):
if not self.prevent_skip and self._is_test_passed(identifier_string, remove_existing=True):
logger.info(f"skipping {identifier_string}") logger.info(f"skipping {identifier_string}")
else: else:
logger.info(f"running {identifier_string}") logger.info(f"running {identifier_string}")
result = self._call_pytest(test_path) result = self._call_pytest(test_path)
self._create_result_file(result=result, identifier_string=identifier_string) self._create_result_file(result=result, identifier_string=identifier_string)
def _test_already_passed(self, identifier_string: str, remove_existing: bool = False) -> bool: def _is_test_passed(self, identifier_string: str, remove_existing: bool = False) -> bool:
"""returns True if the selected test (matching test_name + sub_test_name) already passed """returns True if the selected test (matching test_name + sub_test_name) already passed
This is determined by the presence of a specific output file in the RESULTS folder that This is determined by the presence of a specific output file in the RESULTS folder that
@ -116,19 +132,6 @@ class Runner:
return pytest.main(command_arguments) return pytest.main(command_arguments)
def run_tests(self):
self._assert_dependencies_passed()
self._run_main_setup_and_test()
for sub_test in self.sub_tests:
condition_function = sub_test["condition"]
sub_test_name = sub_test["test_file"]
identifier_string = self.combine_names(self.name, sub_test_name)
if condition_function(self.config):
test_path = self.root_dir / self.test_dir_name / sub_test_name
self._run_test_if_required(identifier_string=identifier_string, test_path=test_path)
else:
self._create_result_file(result=-1, identifier_string=identifier_string)
def _create_result_file( def _create_result_file(
self, self,
result: int, result: int,
@ -141,6 +144,16 @@ class Runner:
with open(file_path, "w") as _: with open(file_path, "w") as _:
pass # create empty file pass # create empty file
def _assert_dependencies_passed(self):
"""assert that all dependencie setups passed before"""
passed_tests = [r.name for r in self.DIRS.RESULTS.glob("*") if "passed" in r.name]
for dependencie in self.dependencies:
dependencie_identifier = self.combine_names(dependencie.name, dependencie.main_setup_name)
assert any(
dependencie_identifier in f for f in passed_tests
), f"could not run {self.name} because {dependencie} did not run before"
@staticmethod @staticmethod
def result_int_to_str(result_int: int) -> str: def result_int_to_str(result_int: int) -> str:
match result_int: match result_int:
@ -154,13 +167,3 @@ class Runner:
@staticmethod @staticmethod
def combine_names(*names: str) -> str: def combine_names(*names: str) -> str:
return "-".join(names) return "-".join(names)
def _assert_dependencies_passed(self):
"""assert that all dependencie setups passed before"""
passed_tests = [r.name for r in self.DIRS.RESULTS.glob("*") if "passed" in r.name]
for dependencie in self.dependencies:
dependencie_identifier = self.combine_names(dependencie.name, dependencie.main_setup_name)
assert any(
dependencie_identifier in f for f in passed_tests
), f"could not run {self.name} because {dependencie} did not run before"

View file

@ -1,7 +1,8 @@
import json import json
import pytest import pytest
from playwright.sync_api import BrowserContext from dotenv import dotenv_values
from playwright.sync_api import BrowserContext, Page
from src.dirmanager import DirManager from src.dirmanager import DirManager
@ -17,6 +18,17 @@ def admin_context(context: BrowserContext, DIR: DirManager) -> BrowserContext:
return context return context
@pytest.fixture
def authentik_admin_page(admin_context: BrowserContext, DIR: DirManager) -> Page:
page = admin_context.new_page()
page.pause()
authentik_env_file = DIR.ENV_FILES / "authentik"
authentik_config: dict[str, str] = dotenv_values(authentik_env_file) # type: ignore
url = "https://" + authentik_config["DOMAIN"]
page.goto(url)
return page
@pytest.fixture @pytest.fixture
def user_context(context: BrowserContext, DIR: DirManager) -> BrowserContext: def user_context(context: BrowserContext, DIR: DirManager) -> BrowserContext:
state_file = DIR.STATES / "user_state.json" state_file = DIR.STATES / "user_state.json"

View file

@ -1 +1 @@
from src.tests_authentik.fixtures_authentik import admin_context, user_context from src.tests_authentik.fixtures_authentik import admin_context, authentik_admin_page, user_context

View file

@ -1,14 +1,13 @@
import re from playwright.sync_api import Page, expect
from playwright.sync_api import BrowserContext, expect
from src.dirmanager import DirManager
def test_visit_from_authentik(admin_context: BrowserContext, dotenv_config: dict[str, str], DIR: DirManager): def test_visit_from_authentik(authentik_admin_page: Page):
page_authentik = admin_context.new_page() with authentik_admin_page.expect_popup() as event_context:
with page_authentik.expect_popup() as event_context: authentik_admin_page.get_by_role("link", name="Wordpress").click()
page_authentik.get_by_role("link", name="Wordpress").click()
page_wordpress = event_context.value page_wordpress = event_context.value
# look for content wrapper
expect(page_wordpress.locator("#wpcontent")).to_be_visible() expect(page_wordpress.locator("#wpcontent")).to_be_visible()
# look for admin bar
expect(page_wordpress.locator("#wpadminbar")).to_be_visible()