forked from toolshed/alakazam
90 lines
3.5 KiB
Python
90 lines
3.5 KiB
Python
"""Tests for leaving single recipes out of a run."""
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
import alakazam
|
|
from alakazam import exclude_from_configs
|
|
|
|
CONFIGS = {
|
|
"a.org": {"authentik": {"app_domain": "login.a.org"}, "traefik": {"app_domain": "a.org"}},
|
|
"b.org": {"traefik": {"app_domain": "b.org"}, "nextcloud": {"app_domain": "cloud.b.org"}},
|
|
}
|
|
|
|
|
|
class TestExcludeFromConfigs:
|
|
def test_nothing_excluded_returns_the_configs_unchanged(self):
|
|
assert exclude_from_configs(CONFIGS, ()) is CONFIGS
|
|
|
|
def test_the_recipe_is_gone_from_every_instance(self):
|
|
result = exclude_from_configs(CONFIGS, ("traefik",))
|
|
assert sorted(result["a.org"]) == ["authentik"]
|
|
assert sorted(result["b.org"]) == ["nextcloud"]
|
|
|
|
def test_several_recipes(self):
|
|
result = exclude_from_configs(CONFIGS, ("traefik", "nextcloud"))
|
|
assert result["b.org"] == {}
|
|
|
|
def test_the_original_is_not_modified(self):
|
|
exclude_from_configs(CONFIGS, ("traefik",))
|
|
assert "traefik" in CONFIGS["a.org"]
|
|
|
|
def test_an_unknown_recipe_is_reported(self, caplog):
|
|
"""A typo would otherwise exclude nothing and look like it worked."""
|
|
with caplog.at_level(logging.WARNING):
|
|
exclude_from_configs(CONFIGS, ("treafik",))
|
|
assert "'treafik' is excluded but not configured" in caplog.text
|
|
|
|
def test_a_known_recipe_is_not_reported(self, caplog):
|
|
with caplog.at_level(logging.WARNING):
|
|
exclude_from_configs(CONFIGS, ("traefik",))
|
|
assert caplog.text == ""
|
|
|
|
|
|
class TestExcludeReachesTheCommands:
|
|
"""One filter in cli() has to cover every consumer of INSTANCE_CONFIGS."""
|
|
|
|
@pytest.fixture
|
|
def env(self, tmp_path, monkeypatch):
|
|
root = tmp_path / "root"
|
|
(root / "group").mkdir(parents=True)
|
|
(root / "alaka.yml").write_text(
|
|
"authentik:\n version: 1.0.0\ntraefik:\n version: 2.0.0\n")
|
|
(root / "group" / "example.com.yml").write_text("authentik:\ntraefik:\n")
|
|
(root / "alakazam.yml").write_text(f"root: {root}\n")
|
|
monkeypatch.setattr(alakazam, "get_settings_path", lambda: str(root / "alakazam.yml"))
|
|
monkeypatch.setattr(alakazam, "get_abra_dir", lambda: root / ".abra")
|
|
monkeypatch.setattr(alakazam, "fetch_recipes", lambda *a, **k: None)
|
|
monkeypatch.setattr(alakazam, "abra", lambda *a, **k: "")
|
|
return root
|
|
|
|
def invoke(self, root, args):
|
|
return CliRunner().invoke(alakazam.cli, args + [str(root / "group"), "ls"])
|
|
|
|
def test_without_the_flag_both_recipes_are_listed(self, env):
|
|
result = self.invoke(env, [])
|
|
assert result.exit_code == 0, result.output
|
|
assert "authentik" in result.output
|
|
assert "traefik" in result.output
|
|
|
|
def test_the_excluded_recipe_disappears(self, env):
|
|
result = self.invoke(env, ["-er", "traefik"])
|
|
assert result.exit_code == 0, result.output
|
|
assert "authentik" in result.output
|
|
assert "traefik" not in result.output
|
|
|
|
def test_the_long_option_works_too(self, env):
|
|
result = self.invoke(env, ["--exclude-recipe", "traefik"])
|
|
assert "traefik" not in result.output
|
|
|
|
def test_it_can_be_given_more_than_once(self, env):
|
|
result = self.invoke(env, ["-er", "traefik", "-er", "authentik"])
|
|
assert "traefik" not in result.output
|
|
assert "authentik" not in result.output
|