prole/tests/installer/test_cfg_save_kubecontext.py
chrisfu 92e4101403 installer: preflight DB secrets + bootstrap OpenTofu password
- Materialize required DB Kubernetes secrets from user-entered password before CNPG init

- Auto-generate a temporary OpenTofu admin password in non-interactive runs when none is available

- Update installer UI flows and tests to cover the new preflights/rotation

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

82 lines
2.3 KiB
Python

from __future__ import annotations
from pathlib import Path
from installer.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):
self._conf_dir = conf_dir
self._cfg_path_override = conf_dir / "prole.cfg"
# Minimal surface required by ConfigMixin._save_prole_cfg
self.cluster_env = _Var("dev")
self.selected_kubectx = _Var(kubectx)
self.db_username = _Var("prole-db")
self.db_password = _Var("secret")
self.db_namespace = _Var("knoe-system")
self.db_host_port = _Var("5432")
self.k3s_server_url = _Var("")
self.k3s_token = _Var("")
self.prod_artifacts_path = _Var(str(conf_dir))
self.service_namespace = _Var("knoe-system")
self.prole_cfg_data = {
"Global": {},
"Initialize Cluster": {},
"Dev Cluster (k3d)": {},
"Service Cluster (k3s)": {},
"Prod Cluster (k8s)": {},
}
def _resolve_prole_conf_dir(self) -> Path:
return self._conf_dir
def _deployment_mode(self) -> str:
return "k3d"
def _get_service_namespace(self) -> str:
return "knoe-system"
def _secret_cfg_value(self, _section: str, _key: str, value: str, *_args) -> str:
return value
def _save_ansible_prole_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_prole_cfg_persists_kubecontext(tmp_path, monkeypatch):
# Avoid touching real env activation during unit test.
import installer.ui.screens.cfg as cfg_mod
monkeypatch.setattr(cfg_mod.prole_conf, "activate_environment", lambda *_a, **_k: None)
app = _DummyCfgApp(conf_dir=tmp_path, kubectx="k3d-knoe-system")
app._save_prole_cfg()
text = (tmp_path / "prole.cfg").read_text(encoding="utf-8")
assert "KUBECONTEXT" in text
assert "k3d-knoe-system" in text