e2e_tests/tests/test_env_resolution.py
Daniel 8685688698 installable package (#9)
* turn repo into installable package (pip install -e .)

* add hatchling build packend

* call it pytest-abra

* add pytest entrypoint, so that it gets loaded automatically if installed (and pytest is run)

* make fixtures optional, so that pytest can still be used in other context

* add cli script -> you can now directly run "pytest-abra" in console

Reviewed-on: local-it-infrastructure/e2e_tests#9
Co-authored-by: Daniel <d.brummerloh@gmail.com>
Co-committed-by: Daniel <d.brummerloh@gmail.com>
2023-12-07 11:32:01 +01:00

104 lines
4.2 KiB
Python

from pathlib import Path
import pytest
from pytest_abra.coordinator import Coordinator
from pytest_abra.env_manager import DependencyRule, EnvFile, EnvManager
RECIPES_DIR = Path("./recipes").resolve()
RUNNER_DICT = Coordinator.create_runner_dict(RECIPES_DIR)
def test_complex_sorting() -> None:
demo_rules = [ # X depends on Y
DependencyRule("a", "e"),
DependencyRule("b", "e"),
DependencyRule("b", "f"),
DependencyRule("c", "e"),
DependencyRule("d", "e"),
DependencyRule("f", "e"),
]
demo_types = ["a", "b", "c", "d", "e", "f", "g"]
env_files = [EnvFile(env_type=t, env_path=Path(), config=dict()) for t in demo_types]
EnvManager.sort_env_files_by_rule
sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, demo_rules)
assert sorted_env_files[0].env_type == "e"
def test_circular_import() -> None:
"""This test will raise ValueError because the example input cannot be correctly ordered"""
demo_rules = [
DependencyRule("a", "b"),
DependencyRule("b", "c"),
DependencyRule("c", "a"),
]
demo_types = ["a", "b", "c"]
env_files = [EnvFile(env_type=t, env_path=Path(), config=dict()) for t in demo_types]
with pytest.raises(ValueError):
EnvManager.sort_env_files_by_rule(env_files, demo_rules)
def test_real_env_files() -> None:
"""authentik should be first"""
ENV_FILES = [
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
]
env_files: list[EnvFile] = EnvManager._get_env_files(ENV_FILES)
dependency_rules: list[DependencyRule] = EnvManager._get_dependency_rules(env_files, RUNNER_DICT)
sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules)
assert sorted_env_files[0].env_type == "authentik"
def test_real_env_files_duplicate() -> None:
"""authentik should be first"""
ENV_FILES = [
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
]
env_files: list[EnvFile] = EnvManager._get_env_files(ENV_FILES)
dependency_rules: list[DependencyRule] = EnvManager._get_dependency_rules(env_files, RUNNER_DICT)
sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules)
assert sorted_env_files[0].env_type == "authentik"
assert sorted_env_files[1].env_type == "authentik"
assert sorted_env_files[2].env_type == "wordpress"
def test_real_env_files_duplicate_six() -> None:
"""authentik should be first"""
ENV_FILES = [
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
]
env_files: list[EnvFile] = EnvManager._get_env_files(ENV_FILES)
dependency_rules: list[DependencyRule] = EnvManager._get_dependency_rules(env_files, RUNNER_DICT)
sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules)
assert sorted_env_files[0].env_type == "authentik"
assert sorted_env_files[1].env_type == "authentik"
assert sorted_env_files[2].env_type == "authentik"
assert sorted_env_files[3].env_type == "wordpress"
assert sorted_env_files[4].env_type == "wordpress"
assert sorted_env_files[5].env_type == "wordpress"
def test_env_manager() -> None:
env_paths_list = [
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
Path("envfiles/login.test.dev.local-it.cloud.env"), # authentik
]
ENV = EnvManager(env_paths_list, RUNNER_DICT)
assert ENV.env_files[0].env_type == "authentik"
assert ENV.env_files[1].env_type == "authentik"
assert ENV.env_files[2].env_type == "wordpress"