prole/tests/test_database_options.py
chrisfu dba8a2d1dc feat(installer): TDD stabilization for k3d install path; dual-cluster GKE TUI
Junie's session targeted the prompt "stabilize ./install.py -c conf/k3d.cfg
using strict TDD" — broad installer-side work, not the k3d-mirror Phase 3
brief I had filed (which she didn't pick up; phase-3 brief stays open). All
750 installer tests pass post-change.

What Junie produced:

  install.py                          (NEW) Top-level CLI entry point. Was
                                            imagined by the prompt but didn't
                                            exist; this commit makes it real.
  knoe/deployment.py                  (NEW) `KnoeDeployment` orchestrator for
                                            the k3s service-mode deploy pipeline.
                                            Wraps Ansible kubeconfig fetch,
                                            opentofu apply, init_*.sh post-apply
                                            scripts, and (optionally) supabase/
                                            deploy.sh.
  knoe/ui/screens/cluster.py          Dual-cluster GKE kubecontext UI: prod env
  knoe/ui/screens/cfg.py              now shows separate "App Cluster:" and
                                       "DB Cluster:" dropdowns instead of a
                                       single "Kubernetes Context:" combo.
                                       New _app_kubectx_combo + _db_kubectx_combo
                                       widgets; new app/db_cluster_kubecontext
                                       tk.StringVars.
  knoe/core/{actions,env,milestones}.py
  knoe/core/ops/storage.py
  knoe/config.py, knoe/knoe_conf.py   Plumbing changes for the dual-cluster
                                       kubecontext flow + storage-class topology
                                       detection cleanup.
  knoe/tools/cleanup_cnpg_storage.py  (NEW) Stand-alone cleanup utility.
  tools/dashboard.sh                  (NEW) Dashboard helper.
  conf/knoe.cfg                       (NEW) Master cfg generated by knoe_conf.
  conf/dev/                           (NEW) Dev-mode cfg directory.
  conf/port-mapping.cfg               Port mapping tweaks for k3d.
  tests/installer/* (8 files)         New + extended tests for the dual-cluster
  tests/test_database_options.py      TUI, kubecontext save flow, storage ops,
                                       topology detection, deploy helpers,
                                       database-options screen.

Issues found in Junie's working state and fixed here:

  1. install.py was a 11-line import shim with no shebang, no `chmod +x`,
     no `if __name__ == '__main__'` block. `./install.py -c conf/k3d.cfg`
     returned `Permission denied` and `python install.py` did nothing.
     Added `#!/usr/bin/env python3`, `chmod +x`, and a __main__ block
     that delegates to `knoe.ui.screens.main()`. `./install.py --help`
     now prints the canonical argparse help.

  2. knoe/deployment.py had FIVE `subprocess.run()` call sites with no
     `timeout=` argument (`_run_script`, `_run_cmd`, the Ansible playbook
     fetch, `tofu init`, `tofu apply`). A hung child process — typical
     failure mode is a script waiting on stdin or a stalled network
     call — would lock up the installer indefinitely. Added timeouts:
       - Ansible kubeconfig fetch: 120s
       - tofu init: 300s
       - tofu apply, _run_script, _run_cmd: bounded by new module
         constant `_MILESTONE_TIMEOUT` (default 1800s = 30 min, override
         via `KNOE_MILESTONE_TIMEOUT_SECONDS` env var).
     `subprocess.TimeoutExpired` is caught explicitly; on timeout the
     run helpers return exit code 124 (conventional timeout code).

  3. `conf/k3d.cfg` was corrupted with MagicMock string-reprs on disk:
        KNOE_CONF = <MagicMock name='Canvas().tk.call().strip()' id='4743999712'>
        argocd.node_selector = <MagicMock name='mock.StringVar().get().strip()' id='...'>
     Likely path: Junie ran `./install.py -c conf/k3d.cfg` interactively
     in a non-Tk environment (or with a partially-mocked widget set) and
     the installer's "save current state" path wrote the mock-objects'
     `__repr__` strings into the cfg file. This commit reverts the cfg
     to its pre-Junie state. **Followup: harden the cfg save path
     against non-string widget values** — track separately.

  4. The corrupted cfg caused the installer to call `os.makedirs()` on
     the mock-string values, producing 10 directories on disk literally
     named `<MagicMock name='Canvas().tk.call().strip()' id='4733210304'>/`
     etc., with 5–86 files of install artifacts inside each. Removed.

The "final step is timing out" the user reported was almost certainly
issue #2 above: install.py walked the milestone pipeline, hit one of
the unbounded subprocess.run calls, and the wrapped command (probably
supabase/deploy.sh, which Junie was reading for context when her
session timed out) hung. With the timeouts in place that path now
exits cleanly with rc=124 instead of locking up.

Verification:
  - pytest tests/installer/ -q                                   750 passed in ~25s
  - python3 -c "import knoe.deployment"                          imports clean
  - ./install.py --help                                          prints argparse help
  - find . -maxdepth 1 -type d -name '<MagicMock*' | wc -l       0
  - head -7 conf/k3d.cfg                                          clean (no MagicMock)

Out of scope for this commit (followups):
  - The cfg save-path that wrote mock-objects-as-strings (issue #3 root cause).
    Reproducer: launch the installer in an env where Tk widget vars are
    `unittest.mock.MagicMock` instances. The cfg save code should refuse to
    serialize non-str values rather than calling `str()` on a MagicMock.
  - The k3d-mirror Phase 3 brief (`docs/plans/junie/k3d-knoe-auth-pod-deploy.md`)
    is still open — Junie picked a different prompt this round.

Co-authored-by: Junie <junie@jetbrains.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 23:51:14 -07:00

238 lines
10 KiB
Python

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