import sys import tkinter as tk from contextlib import ExitStack from pathlib import Path from unittest.mock import patch, MagicMock import pytest class MockVar: def __init__(self, value=None): self.value = value def get(self): return self.value def set(self, value): self.value = value def trace_add(self, mode, callback): pass # Mock tkinter and other GUI/macOS specific imports # We use patch.dict to avoid polluting other tests if possible, # but sys.modules is global. # Better: just use patch in the fixture. sys.modules["Foundation"] = MagicMock() sys.modules["AppKit"] = MagicMock() sys.modules["PIL"] = MagicMock() sys.modules["PIL.Image"] = MagicMock() sys.modules["PIL.ImageTk"] = MagicMock() from install import KnoeInstaller def _generated_dockerfile_path(installer, tmp_path): return tmp_path / "build" / installer._db_mode_key() / "knoe-db" / "Dockerfile" @pytest.fixture def mock_installer(tmp_path): root = MagicMock() # We need to patch the tk references in the modules that use them, # because they might have already been imported with a different mock. patches = [ patch("knoe.ui.screens.PROJECT_ROOT", tmp_path), patch("knoe.ui.screens.database_options.PROJECT_ROOT", tmp_path), patch("knoe.ui.screens.database_options.resolve_knoe_home", return_value=tmp_path), patch("knoe.ui.screens.database_options.get_resource_path", side_effect=lambda name: tmp_path / name), patch("knoe.core.controller.KnoeController.run_script"), patch.object(KnoeInstaller, "_load_database_versions"), patch("knoe.ui.screens.database_options.tk.BooleanVar", side_effect=lambda value=None: MockVar(value)), patch("knoe.ui.screens.database_options.tk.StringVar", side_effect=lambda value=None: MockVar(value)), patch("knoe.ui.screens.database_options.copy_build_context_dir", side_effect=lambda src, dst: dst.mkdir(parents=True, exist_ok=True)), patch("knoe.ui.screens.database_options.tk.ttk.Treeview"), patch("knoe.ui.screens.database_options.tk.ttk.Scrollbar"), patch("knoe.ui.screens.database_options.tk.ttk.Combobox"), patch("knoe.ui.screens.database_options.tk.Radiobutton"), patch("knoe.ui.screens.database_options.tk.Checkbutton"), patch("knoe.ui.screens.database_options.tk.Label"), # Also for the main class if it uses them patch("knoe.ui.screens.tk.BooleanVar", side_effect=lambda value=None: MockVar(value)), patch("knoe.ui.screens.tk.StringVar", side_effect=lambda value=None: MockVar(value)), ] with ExitStack() as stack: for p in patches: stack.enter_context(p) # Create necessary directories (tmp_path / "knoe-db").mkdir() (tmp_path / "conf" / "postgresql").mkdir(parents=True) (tmp_path / "etc").mkdir() app = KnoeInstaller(root) app.bg_canvas = MagicMock() app.root = root # Force knoe_home to tmp_path so _runtime_knoe_home() returns tmp_path. app.knoe_cfg_data.setdefault("System Environment", {})["KNOE_HOME"] = str(tmp_path) yield app def test_database_options_state_init(mock_installer): assert hasattr(mock_installer, "db_at_rest_encryption") assert mock_installer.db_at_rest_encryption.get() is True assert mock_installer.db_distribution.get() == "percona" # Default is Percona 18 assert mock_installer.db_version_type.get() == "v18" assert "pg_cron" in mock_installer.db_extensions assert "pg_tde" in mock_installer.db_extensions assert mock_installer.db_extensions["pg_cron"].get() is True assert mock_installer.db_extensions["pg_tde"].get() is True def test_encryption_toggle(mock_installer): # Initial state: Encryption ON -> Percona assert mock_installer.db_at_rest_encryption.get() is True assert mock_installer.db_distribution.get() == "percona" # Toggle OFF -> should switch to postgresql mock_installer.db_at_rest_encryption.set(False) # We need to manually call the command since we're setting the variable directly in test # In real UI, the command=on_encryption_toggle would handle it. # Find the encryption toggle callback (it's local to _render_database_options_page but let's see) # Actually it might be easier to just test the logic if I can access it. # Let's mock the render to get the callback with patch("knoe.ui.screens.database_options.tk.Checkbutton") as mock_cb: mock_installer._render_database_options_page() args, kwargs = mock_cb.call_args on_encryption_toggle = kwargs["command"] mock_installer.db_at_rest_encryption.set(False) on_encryption_toggle() assert mock_installer.db_distribution.get() == "postgresql" mock_installer.db_at_rest_encryption.set(True) on_encryption_toggle() assert mock_installer.db_distribution.get() == "percona" def test_version_selection(mock_installer): mock_installer.db_versions_data = { "postgresql": {"stable": "14", "current": "15", "latest": "16"}, "percona": {"stable": "15", "current": "16", "latest": "17", "v18": "18"} } mock_installer.db_distribution.set("percona") mock_installer._render_database_options_page() # Initially it should be Latest mock_installer._refresh_database_options_ui() assert mock_installer.db_selected_version.get() == "18 (Percona 18)" # Test switching to stable mock_installer.db_selected_version.set("15") # simulate partial string match or manual set mock_installer._refresh_database_options_ui() assert mock_installer.db_selected_version.get() == "15 (Stable)" def test_dockerfile_generation(mock_installer, tmp_path): mock_installer.db_distribution.set("percona") mock_installer.db_selected_version.set("17 (Latest)") template_path = tmp_path / "knoe-db" / "Dockerfile.percona.template" template_path.write_text("FROM percona:{{MAJOR_VERSION}}\n{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}") for ext in mock_installer.db_extensions.values(): ext.set(False) mock_installer.db_extensions["postgis"].set(True) success = mock_installer._generate_knoe_db_dockerfile() assert success is True dockerfile = _generated_dockerfile_path(mock_installer, tmp_path) assert dockerfile.exists() content = dockerfile.read_text() assert "FROM percona:17" in content assert "percona-postgresql-17-postgis-3" in content assert "CREATE EXTENSION IF NOT EXISTS postgis SCHEMA knoe;" in content assert "ALTER EXTENSION postgis SET SCHEMA knoe" in content assert "vector" not in content def test_dockerfile_generation_percona_18(mock_installer, tmp_path): mock_installer.db_distribution.set("percona") mock_installer.db_selected_version.set("18 (Latest)") template_path = tmp_path / "knoe-db" / "Dockerfile.percona.template" template_path.write_text("FROM percona:{{MAJOR_VERSION}}\n{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}") # Enable a contrib extension mock_installer.db_extensions["pgcrypto"].set(True) # Enable a separate package extension mock_installer.db_extensions["pg_repack"].set(True) # Enable pgvector (verify name 'vector') mock_installer.db_extensions["pgvector"].set(True) success = mock_installer._generate_knoe_db_dockerfile() assert success is True dockerfile = _generated_dockerfile_path(mock_installer, tmp_path) content = dockerfile.read_text() assert "FROM percona:18" in content # pgcrypto should NOT have an apt-get install line because it's in contrib assert "percona-postgresql-18-pgcrypto" not in content # pg_repack should use 'repack' instead of 'pg_repack' for Percona assert "percona-postgresql-18-repack" in content # pgvector should use 'pgvector' for package assert "percona-postgresql-18-pgvector" in content # Percona build flow should not install non-existent cron package variants assert "percona-postgresql-18-cron" not in content assert "percona-postgresql-18-pg_cron" not in content # pg_tde is handled by dedicated Percona package naming and must not use generic mapping assert "percona-postgresql-18-pg_tde" not in content # SQL creation steps assert "CREATE EXTENSION IF NOT EXISTS pgcrypto;" in content assert "CREATE EXTENSION IF NOT EXISTS pg_repack;" in content assert "CREATE EXTENSION IF NOT EXISTS vector;" in content assert "CREATE EXTENSION IF NOT EXISTS pg_cron;" in content def test_pg_cron_package_mapping_for_postgresql(mock_installer, tmp_path): mock_installer.db_distribution.set("postgresql") mock_installer.db_selected_version.set("17 (Latest)") template_path = tmp_path / "knoe-db" / "Dockerfile.postgresql.template" template_path.write_text("FROM postgres:{{MAJOR_VERSION}}\n{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}") # Keep scenario focused on pg_cron mapping for ext in mock_installer.db_extensions.values(): ext.set(False) mock_installer.db_extensions["pg_cron"].set(True) success = mock_installer._generate_knoe_db_dockerfile() assert success is True dockerfile = _generated_dockerfile_path(mock_installer, tmp_path) content = dockerfile.read_text() assert "postgresql-17-cron" in content assert "postgresql-17-pg_cron" not in content def test_pgbadger_handling(mock_installer, tmp_path): mock_installer.db_distribution.set("percona") mock_installer.db_selected_version.set("18") template_path = tmp_path / "knoe-db" / "Dockerfile.percona.template" template_path.write_text("{{EXTENSION_INSTALL_STEPS}}\n{{EXTENSION_CREATE_STEPS}}") for ext in mock_installer.db_extensions.values(): ext.set(False) mock_installer.db_extensions["pgbadger"].set(True) success = mock_installer._generate_knoe_db_dockerfile() assert success is True dockerfile = _generated_dockerfile_path(mock_installer, tmp_path) content = dockerfile.read_text() assert "apt-get install -y --no-install-recommends percona-pgbadger" in content assert "CREATE EXTENSION IF NOT EXISTS pgbadger" not in content