prole/tests/test_database_options.py
chrisfu a069989315 Rename prole-db to knoe-db, add knoe-auth as cluster-internal KDC
Itemized changes:

1. knoe-auth: New cluster-internal KDC and SSO gateway service
   - Created etc/init_knoe_auth.sh based on init_kdc.sh with knoe-auth naming
   - Namespace defaults to SERVICE_NAMESPACE (knoe-system)
   - ConfigMap: knoe-auth-kdc-config, Secret: knoe-auth-secrets
   - Legacy cleanup removes old auth/dog/authority deployments

2. Orchestration: knoe-auth initializes before CloudNativePG
   - Updated prole.sh to insert init_knoe_auth.sh as step 2 (before CNPG)
   - Renumbered all subsequent initialization steps

3. Kong routing: Updated init_kong.sh to route to knoe-auth in SERVICE_NAMESPACE

4. Comment/reference updates for knoe-auth
   - Updated init_common_services.sh, init_service_layer.sh, init_kerberos.sh

5. prole-db renamed to knoe-db across the entire codebase
   - Renamed prole-db/ directory to knoe-db/
   - Renamed all prole-db Kubernetes manifests (deploy/opentofu, k8s/)
   - Renamed scripts: docker-root-knoe-db.sh, docker-run-knoe-db.sh, test-cnpg-knoe-db.sh
   - Renamed etc/init_prole-db-reset.sh to etc/init_knoe-db-reset.sh
   - Renamed etc/prole-db-passwwd.sh to etc/knoe-db-passwwd.sh
   - Renamed mock_val counterparts accordingly
   - Renamed tests/etc/test_init_prole-db-reset.sh to test_init_knoe-db-reset.sh
   - Renamed docs/prole-db-documentation-mcp-architecture.md to knoe-db variant
   - Renamed modes/k3d/prole-db/ to modes/k3d/knoe-db/
   - Renamed prole-db.iml to knoe-db.iml

6. Configuration updates
   - Updated conf/dev, conf/prod, conf/test, conf/service prole.cfg files
   - Updated conf/port-mapping.cfg
   - Updated etc/prole_cfg.sh and mock_val/prole_cfg.sh
   - Updated service/prole.cfg

7. Kubernetes manifests and deploy configuration
   - Updated deploy/opentofu/k3s ArgoCD application YAMLs
   - Updated kong-configmap.yaml and kustomization.yaml
   - Updated k3s/kong-config.yml and prole-resources.yaml
   - Updated prole-mssql-db deployment YAMLs
   - Updated supabase helm render and deploy scripts

8. Infrastructure and GCP Terraform
   - Updated deploy/gcp/terraform: folders, groups, IAM, service-projects

9. Python/installer code updates
   - Updated knoe/core: actions, build_context, controller, env, milestones
   - Updated knoe/milestone.py
   - Updated knoe/ui/screens: cfg, database, database_options, deploy, docker,
     navigation, security, services, validate
   - Updated knoe.spec, status.py

10. Shell script updates
    - Updated etc/: build_db, init_cloudnative_pg, init_cnpg_backup,
      init_db_manager, init_forgejo, init_gitlab, init_monitoring, init_openbao,
      init_port_forwards, init_postgrest, init_supabase_ports, status
    - Updated mock_val/ counterparts for all above scripts
    - Updated prole-net/init-prole-dns.sh
    - Updated bin/prole-kpf.sh, gitea/deploy.sh, supabase/deploy.sh

11. Test updates
    - Updated tests/etc/: test_init_cloudnative_pg*, test_init_cnpg_backup*,
      test_init_kdc*, test_init_kerberos*, test_init_kong*, test_prole_cfg*
    - Updated tests/installer/: test_actions_helpers, test_cfg_save_kubecontext,
      test_controller, test_core_classes, test_milestones, test_milestones_extended,
      test_namespace_propagation
    - Updated tests/: test_database_options, test_navigation,
      test_render_supabase_hostname, test_docker_build_fix,
      test_all_prole_home_fixes, silent_install_test, final_test

12. Documentation updates
    - Updated docs/: DOCKER-BUILD-FIX, PROLE-CFG-SECRETS, PROLE-HOME-DIRECTORY,
      build-system, patent
    - Updated scan/network_description.txt
    - Updated pom.xml

13. Miscellaneous script updates
    - Updated root-level: _adopt_replica_pvcs, _fix_replica_merlin, _import_pi,
      _patch_cluster, _prebind_pvcs, _rebind_d002, _rebind_d002b, test_resolve
    - Updated scripts/generate_spec.py

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-22 22:16:21 -07:00

194 lines
8.1 KiB
Python

import sys
from unittest.mock import patch, MagicMock
from contextlib import ExitStack
import pytest
from pathlib import Path
import tkinter as tk
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()
import install
from install import ProleInstaller
@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_prole_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.ProleController.run_script"),
patch.object(ProleInstaller, "_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.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 = ProleInstaller(root)
app.bg_canvas = MagicMock()
app.root = root
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"
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}}")
mock_installer.db_extensions["postgis"].set(True)
mock_installer.db_extensions["pgvector"].set(False)
success = mock_installer._generate_knoe_db_dockerfile()
assert success is True
dockerfile = tmp_path / "build" / "k3d" / "knoe-db" / "Dockerfile"
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;" 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 = tmp_path / "build" / "k3d" / "knoe-db" / "Dockerfile"
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
# 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
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}}")
mock_installer.db_extensions["pgbadger"].set(True)
success = mock_installer._generate_knoe_db_dockerfile()
assert success is True
dockerfile = tmp_path / "build" / "k3d" / "knoe-db" / "Dockerfile"
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