prole/tests/installer/test_controller.py
chrisfu 5618b662dd Remove prole-db-manager; simplify deployment via prole-authority; fix pg18 downgrade & cluster name
Summary:
Removed the prole-db-manager microservice and simplified deployment to use
prole-authority as the internal management and authorization point. Fixed two
blocking bugs that prevented silent install from completing on knoe-dev-cluster.

Removed: prole-db-manager
- Deleted db-manager-deployment.yaml and db-manager-service.yaml from opentofu manifests
- Deleted src/db-manager/ (Dockerfile, server.js, package.json, tests)
- Removed prole-db-manager port-forward mapping from installer/core/env.py
- Removed init_db_manager.sh from Initialization Scripts (milestones.py, actions.py)
- Removed init_certmgr.sh and init_db_manager.sh tabs from services screen (services.py)
- Removed live k8s Deployment/Service from knoe-dev-cluster

Fixed: PostgreSQL version downgrade error (pg17 -> pg18)
- Created conf/postgresql/.version with value 18
- Updated k8s/prole/prole-db.yaml and prole-db-recovery.yaml.tpl imageName to prole-db:18-089
- Fixed _init_database_options_state() to restore saved version_type from prole.cfg
  so db_version_type defaults to v18 (pg18) instead of silently reverting to pg17
- Added database_options.* keys to _collect_input_snapshot() in cfg.py so
  distribution, version_type, and all extension toggles persist to prole.cfg

Fixed: Cluster name inconsistency
- Removed stale prole-dev-cluster references; all scripts now use knoe-dev-cluster
- Added knoe-dev-cluster to mode-detection case in etc/prole_cfg.sh

Config: conf/prole.cfg
- Set kerberos_config.enabled = False, KERBEROS_AUTO_ENABLED = False
- Added database_options.distribution = percona, version_type = v18
- Added all 13 extension flags set to True (postgis, pgvector, pgcrypto, pgaudit,
  pg_repack, pg_stat_statements, pg_buffercache, pg_freespacemap, pgrowlocks,
  postgres_fdw, dblink, pg_stat_monitor, pgbadger)

Verification:
./install.py -s -l -v -c conf/prole.cfg completed successfully.
CNPG deployed prole-db:18-089 to knoe-dev-cluster; all milestones passed.

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-01 20:40:44 -08:00

238 lines
8.5 KiB
Python

"""
Unit tests for installer/core/controller.py
Covers ProleController: check_docker_running, get_prole_db_version, run_script.
"""
from __future__ import annotations
import os
import subprocess
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from installer.core.controller import ProleController
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _make_controller(tmp_path: Path, **kwargs) -> ProleController:
return ProleController(project_root=tmp_path, **kwargs)
def _scaffold_project(tmp_path: Path) -> None:
"""Create minimum project structure expected by run_script."""
(tmp_path / "etc").mkdir(parents=True, exist_ok=True)
(tmp_path / "conf" / "postgresql").mkdir(parents=True, exist_ok=True)
(tmp_path / "prole-db").mkdir(parents=True, exist_ok=True)
# ---------------------------------------------------------------------------
# check_docker_running
# ---------------------------------------------------------------------------
def test_check_docker_running_success(tmp_path):
ctrl = _make_controller(tmp_path)
with patch("installer.core.controller.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
assert ctrl.check_docker_running() is True
def test_check_docker_running_called_process_error(tmp_path):
ctrl = _make_controller(tmp_path)
with patch("installer.core.controller.subprocess.run",
side_effect=subprocess.CalledProcessError(1, "docker")):
assert ctrl.check_docker_running() is False
def test_check_docker_running_file_not_found(tmp_path):
ctrl = _make_controller(tmp_path)
with patch("installer.core.controller.subprocess.run",
side_effect=FileNotFoundError("docker not found")):
assert ctrl.check_docker_running() is False
# ---------------------------------------------------------------------------
# get_prole_db_version
# ---------------------------------------------------------------------------
def test_get_prole_db_version_no_files(tmp_path):
ctrl = _make_controller(tmp_path)
version = ctrl.get_prole_db_version()
# Falls back to defaults: 17.7 and 43 (zero-padded to 3 digits → 043)
assert version == "17.7-043"
def test_get_prole_db_version_with_files(tmp_path):
(tmp_path / "conf" / "postgresql").mkdir(parents=True)
(tmp_path / "conf" / "postgresql" / ".version").write_text("18\n")
(tmp_path / "prole-db").mkdir(parents=True)
(tmp_path / "prole-db" / ".version").write_text("88\n")
ctrl = _make_controller(tmp_path)
version = ctrl.get_prole_db_version()
assert version == "18-088"
def test_get_prole_db_version_non_numeric_release(tmp_path):
(tmp_path / "conf" / "postgresql").mkdir(parents=True)
(tmp_path / "conf" / "postgresql" / ".version").write_text("18\n")
(tmp_path / "prole-db").mkdir(parents=True)
(tmp_path / "prole-db" / ".version").write_text("latest\n")
ctrl = _make_controller(tmp_path)
version = ctrl.get_prole_db_version()
# Non-numeric release is not zero-padded
assert version == "18-latest"
def test_get_prole_db_version_only_pg_version(tmp_path):
(tmp_path / "conf" / "postgresql").mkdir(parents=True)
(tmp_path / "conf" / "postgresql" / ".version").write_text("17\n")
ctrl = _make_controller(tmp_path)
version = ctrl.get_prole_db_version()
assert version.startswith("17-")
def test_get_prole_db_version_pads_single_digit_release(tmp_path):
(tmp_path / "prole-db").mkdir(parents=True)
(tmp_path / "prole-db" / ".version").write_text("5\n")
ctrl = _make_controller(tmp_path)
version = ctrl.get_prole_db_version()
assert version.endswith("-005")
# ---------------------------------------------------------------------------
# run_script
# ---------------------------------------------------------------------------
def test_run_script_basic(tmp_path):
"""run_script executes a bash script and captures stdout via on_line."""
_scaffold_project(tmp_path)
script = tmp_path / "etc" / "hello.sh"
script.write_text("#!/bin/bash\necho 'hello from script'\n")
ctrl = _make_controller(tmp_path)
lines: list[str] = []
rc = ctrl.run_script(
"hello.sh",
env={"PROLE_HOME": str(tmp_path)},
on_line=lambda l: lines.append(l),
)
assert rc == 0
assert any("hello from script" in l for l in lines)
def test_run_script_nonzero_exit(tmp_path):
_scaffold_project(tmp_path)
script = tmp_path / "etc" / "fail.sh"
script.write_text("#!/bin/bash\nexit 42\n")
ctrl = _make_controller(tmp_path)
rc = ctrl.run_script("fail.sh", env={"PROLE_HOME": str(tmp_path)})
assert rc == 42
def test_run_script_with_args(tmp_path):
_scaffold_project(tmp_path)
script = tmp_path / "etc" / "args.sh"
script.write_text("#!/bin/bash\necho \"ARG:$1\"\n")
ctrl = _make_controller(tmp_path)
lines: list[str] = []
rc = ctrl.run_script(
"args.sh",
args=["myvalue"],
env={"PROLE_HOME": str(tmp_path)},
on_line=lambda l: lines.append(l),
)
assert rc == 0
assert any("ARG:myvalue" in l for l in lines)
def test_run_script_copies_k8s_and_conf(tmp_path):
"""Covers the k8s and conf sync paths when source dirs exist."""
_scaffold_project(tmp_path)
# Create source k8s and conf dirs
(tmp_path / "k8s").mkdir()
(tmp_path / "k8s" / "test.yaml").write_text("kind: Pod\n")
(tmp_path / "conf" / "prole.cfg").write_text("[General]\n")
script = tmp_path / "etc" / "synced.sh"
script.write_text("#!/bin/bash\necho ok\n")
target = tmp_path / "prole_home_test"
target.mkdir()
ctrl = _make_controller(tmp_path)
rc = ctrl.run_script("synced.sh", env={"PROLE_HOME": str(target)})
assert rc == 0
# k8s and conf should have been synced
assert (target / "k8s").exists()
assert (target / "conf").exists()
def test_run_script_copies_db_version(tmp_path):
"""Covers the prole-db/.version copy path."""
_scaffold_project(tmp_path)
(tmp_path / "prole-db" / ".version").write_text("88\n")
script = tmp_path / "etc" / "dbver.sh"
script.write_text("#!/bin/bash\necho ok\n")
target = tmp_path / "prole_home_db"
target.mkdir()
ctrl = _make_controller(tmp_path)
rc = ctrl.run_script("dbver.sh", env={"PROLE_HOME": str(target)})
assert rc == 0
assert (target / "prole-db" / ".version").read_text().strip() == "88"
def test_run_script_default_prole_home(tmp_path):
"""Covers the PROLE_HOME fallback to ~/.prole when not in env."""
_scaffold_project(tmp_path)
script = tmp_path / "etc" / "home.sh"
script.write_text("#!/bin/bash\necho hometest\n")
ctrl = _make_controller(tmp_path)
# Provide env without PROLE_HOME so it falls back to Path.home() / ".prole"
fake_home = tmp_path / "fakehome"
fake_home.mkdir()
with patch("installer.core.controller.Path.home", return_value=fake_home):
rc = ctrl.run_script("home.sh", env={})
assert rc == 0
def test_run_script_stderr_to_stdout_false(tmp_path):
"""Covers the stderr_to_stdout=False thread path."""
_scaffold_project(tmp_path)
script = tmp_path / "etc" / "stderr.sh"
script.write_text("#!/bin/bash\necho 'out'\n>&2 echo 'err'\n")
ctrl = _make_controller(tmp_path)
stderr_lines: list[str] = []
rc = ctrl.run_script(
"stderr.sh",
env={"PROLE_HOME": str(tmp_path)},
stderr_to_stdout=False,
on_stderr_line=lambda l: stderr_lines.append(l),
)
assert rc == 0
assert any("err" in l for l in stderr_lines)
def test_run_script_verbose_sets_env(tmp_path):
"""Covers the verbose env var injection path."""
_scaffold_project(tmp_path)
script = tmp_path / "etc" / "verbose.sh"
script.write_text("#!/bin/bash\necho \"VERBOSE:${PROLE_VERBOSE:-unset}\"\n")
ctrl = _make_controller(tmp_path, verbose=True)
lines: list[str] = []
rc = ctrl.run_script(
"verbose.sh",
env={"PROLE_HOME": str(tmp_path)},
on_line=lambda l: lines.append(l),
)
assert rc == 0
assert any("VERBOSE:1" in l for l in lines)
def test_run_script_missing_source_script(tmp_path):
"""If the source script doesn't exist, target script also won't exist — graceful."""
_scaffold_project(tmp_path)
# Don't create the script — run_script should still try to run it
ctrl = _make_controller(tmp_path)
# Should return non-zero (bash can't find the file) but not raise
rc = ctrl.run_script("nonexistent.sh", env={"PROLE_HOME": str(tmp_path)})
assert rc != 0