prole/tests/installer/test_cfg_save_kubecontext.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

178 lines
5.3 KiB
Python

from __future__ import annotations
from pathlib import Path
from knoe.ui.screens.cfg import ConfigMixin
class _Var:
def __init__(self, value: str):
self._value = value
def get(self) -> str:
return self._value
class _DummyCfgApp(ConfigMixin):
def __init__(
self,
conf_dir: Path,
kubectx: str,
*,
mode: str = "k3d",
cluster_env: str = "dev",
service_namespace_input: str = "knoe-system",
resolved_service_namespace: str = "knoe-system",
existing_global: dict | None = None,
):
self._conf_dir = conf_dir
self._cfg_path_override = conf_dir / "knoe.cfg"
self._mode = mode
self._resolved_service_namespace = resolved_service_namespace
# Minimal surface required by ConfigMixin._save_knoe_cfg
self.cluster_env = _Var(cluster_env)
self.selected_kubectx = _Var(kubectx)
self.db_username = _Var("knoe-db")
self.db_password = _Var("secret")
self.db_namespace = _Var("knoe-system")
self.cnpg_cluster_name = _Var("knoe-db")
self.db_host_port = _Var("5432")
self.supabase_pv_node = _Var("")
self.supabase_pv_base_dir = _Var("")
self.gitops_node_selector = _Var("")
self.argocd_node_selector = _Var("")
self.k3s_server_url = _Var("")
self.k3s_token = _Var("")
self.prod_artifacts_path = _Var(str(conf_dir))
self.service_namespace = _Var(service_namespace_input)
self.knoe_cfg_data = {
"Global": dict(existing_global or {}),
"Initialize Cluster": {},
"Dev Cluster (k3d)": {},
"Service Cluster (k3s)": {},
"Prod Cluster (k8s)": {},
}
def _resolve_knoe_conf_dir(self) -> Path:
return self._conf_dir
def _deployment_mode(self) -> str:
return self._mode
def _get_service_namespace(self) -> str:
return self._resolved_service_namespace
def _secret_cfg_value(self, _section: str, _key: str, value: str, *_args) -> str:
return value
def _save_ansible_knoe_vault(self, _db_password: str):
return None
def _sanitize_sections_for_cfg(self, sections: dict) -> dict:
return sections
def _sync_port_forward_mappings(self):
# Not relevant for this unit test; avoids requiring optional-feature vars.
return None
def _collect_input_snapshot(self) -> dict:
return {}
def test_save_knoe_cfg_persists_kubecontext(tmp_path, monkeypatch):
# Avoid touching real env activation during unit test.
import knoe.ui.screens.cfg as cfg_mod
monkeypatch.setattr(cfg_mod.knoe_conf, "activate_environment", lambda *_a, **_k: None)
app = _DummyCfgApp(conf_dir=tmp_path, kubectx="k3d-knoe-system")
app._save_knoe_cfg()
text = (tmp_path / "knoe.cfg").read_text(encoding="utf-8")
assert "KUBECONTEXT" in text
assert "k3d-knoe-system" in text
def test_save_knoe_cfg_service_mode_drops_generated_localhost_values(
tmp_path, monkeypatch
):
import knoe.ui.screens.cfg as cfg_mod
monkeypatch.setattr(cfg_mod.knoe_conf, "activate_environment", lambda *_a, **_k: None)
app = _DummyCfgApp(
conf_dir=tmp_path,
kubectx="",
mode="k3s",
cluster_env="service",
service_namespace_input="",
resolved_service_namespace="knoe-system",
existing_global={
"SERVICE_HOSTNAME": "k3d.localhost",
"API_SERVICE_ENDPOINT": "http://localhost:8081",
"PROLE_OPENTOFU_URL": "http://127.0.0.1:8080",
},
)
app._save_knoe_cfg()
text = (tmp_path / "knoe.cfg").read_text(encoding="utf-8")
assert "SERVICE_HOSTNAME" not in text
assert "API_SERVICE_ENDPOINT" not in text
assert "localhost" not in text
assert "SERVICE_NAMESPACE" not in text
def test_save_knoe_cfg_service_mode_keeps_explicit_service_namespace(
tmp_path, monkeypatch
):
import knoe.ui.screens.cfg as cfg_mod
monkeypatch.setattr(cfg_mod.knoe_conf, "activate_environment", lambda *_a, **_k: None)
app = _DummyCfgApp(
conf_dir=tmp_path,
kubectx="",
mode="k3s",
cluster_env="service",
service_namespace_input="explicit-ns",
resolved_service_namespace="explicit-ns",
)
app._save_knoe_cfg()
text = (tmp_path / "knoe.cfg").read_text(encoding="utf-8")
assert "SERVICE_NAMESPACE = explicit-ns" in text
def test_save_knoe_cfg_service_mode_repeated_saves_remain_clean(tmp_path, monkeypatch):
import knoe.ui.screens.cfg as cfg_mod
monkeypatch.setattr(cfg_mod.knoe_conf, "activate_environment", lambda *_a, **_k: None)
app = _DummyCfgApp(
conf_dir=tmp_path,
kubectx="",
mode="k3s",
cluster_env="service",
service_namespace_input="",
resolved_service_namespace="knoe-system",
existing_global={
"SERVICE_HOSTNAME": "k3d.localhost",
"API_SERVICE_ENDPOINT": "http://localhost:8081",
},
)
app._save_knoe_cfg()
first = (tmp_path / "knoe.cfg").read_text(encoding="utf-8")
app._save_knoe_cfg()
second = (tmp_path / "knoe.cfg").read_text(encoding="utf-8")
for text in (first, second):
assert "SERVICE_HOSTNAME" not in text
assert "API_SERVICE_ENDPOINT" not in text
assert "localhost" not in text