implement env manager (#6)

* add EnvManager class

* holds all functions that are env file related

* integrates runner dependency resolution

* add integration and unit tests for EnvManager

Reviewed-on: local-it-infrastructure/e2e_tests#6
Co-authored-by: Daniel <d.brummerloh@gmail.com>
Co-committed-by: Daniel <d.brummerloh@gmail.com>
This commit is contained in:
Daniel 2023-12-04 17:09:01 +01:00 committed by dan
parent d3dc0f942a
commit 3fa10aaa69
18 changed files with 264 additions and 198 deletions

View file

@ -1,59 +1,101 @@
import sys
from pathlib import Path
from icecream import ic
import pytest
sys.path.append(Path(__file__).parent.parent.resolve().__str__())
# import pytest
# from prototyping.sorting_algo import Rule, is_rule_satisfied, sort_by_rules
from src.coordinator import Coordinator
from src.env_file_helper import DependencyRule, EnvFile, sort_env_files_by_rule
# @pytest.fixture
# def in_list():
# return ["a", "b", "c", "d", "e", "f", "g"]
# from src.env_file_helper import DependencyRule, EnvFile, sort_env_files_by_rule
from src.env_manager import DependencyRule, EnvFile, EnvManager
# @pytest.fixture
# def rules() -> list[Rule]:
# return [ # X depends on Y
# Rule("a", "e"),
# Rule("b", "e"),
# Rule("b", "f"),
# Rule("c", "e"),
# Rule("d", "e"),
# Rule("f", "e"),
# ]
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 has_rules_satisfied(in_list, rules):
# rule_satisfied: list[bool] = []
# for rule in rules:
# if is_rule_satisfied(in_list, rule):
# rule_satisfied.append(True)
# else:
# rule_satisfied.append(False)
# return all(rule_satisfied)
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_stuff(in_list, rules):
# sort_by_rules(in_list, rules)
# assert has_unique_elements(in_list)
# assert has_rules_satisfied(in_list, 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)
sorted_env_files = EnvManager.sort_env_files_by_rule(env_files, dependency_rules)
assert sorted_env_files[0].env_type == "authentik"
ENV_FILES = [
Path("envfiles/blog.test.dev.local-it.cloud.env"), # wordpress
Path("envfiles/login.test.dev.local-it.cloud.env"), # 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)
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"
env_files: list[EnvFile] = Coordinator._getn_env_files_list(ENV_FILES)
dependency_rules: list[DependencyRule] = Coordinator._get_dependency_rules(env_files)
def test_real_env_files_duplicate_six() -> None:
"""authentik should be first"""
ic(env_files)
sorted_env_files = sort_env_files_by_rule(env_files, dependency_rules)
ic(env_files)
ic(sorted_env_files)
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)
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)
assert ENV.env_files[0].env_type == "authentik"
assert ENV.env_files[1].env_type == "authentik"
assert ENV.env_files[2].env_type == "wordpress"