prole/tests/installer/test_cluster_screen_layout.py
chrisfu 765aa0b926 Checkpoint: cluster env UI layout + k3s common services
- Tighten Cluster Environment screen layout; switch Service/Prod to kubectx context selection; keep namespace and key controls on one line; ensure Repair button remains reachable.

- Add UI layout regression test to render with large mock data and assert key widgets remain visible and console is scrollable.

- Make kubeconfig generation deterministic under tests by avoiding overwriting cert-based kubeconfigs; write token sidecar kubeconfig when needed.

- Update common-services init scripts and add k3s/Helm deployment bits (svc-check, Kong/CertMgr tasks).
2026-02-27 14:35:14 -08:00

152 lines
5.5 KiB
Python

import tkinter as tk
from pathlib import Path
import sys
import pytest
# Ensure `installer` is importable when tests are invoked directly via `pytest`
# (the project also provides `tests/run_tests.sh` which sets `PYTHONPATH=.`).
PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from installer.ui.screens.base import ScreenBaseMixin
from installer.ui.screens.cluster import ClusterScreenMixin
class _DummyClusterApp(ScreenBaseMixin, ClusterScreenMixin):
"""Minimal Tk app surface needed to render the Cluster Environment screen."""
def __init__(self, root: tk.Tk, canvas: tk.Canvas, kubectx_values: list[str]):
self.root = root
self.bg_canvas = canvas
self._canvas_items: list[int] = []
self._overlay_widgets: list[tk.Widget] = []
# Tk variables used by the screen
self.cluster_env = tk.StringVar(master=root, value="prod")
self.selected_k3d_cluster = tk.StringVar(master=root, value="")
self.selected_kubectx = tk.StringVar(master=root, value="")
self.service_namespace = tk.StringVar(master=root, value="common-services")
self.prod_artifacts_path = tk.StringVar(master=root, value="/tmp")
# Legacy variables still referenced by non-UI logic elsewhere
self.k3s_server_url = tk.StringVar(master=root, value="")
self.k3s_token = tk.StringVar(master=root, value="")
self._kubectx_values = kubectx_values
# ---- Stubs for callbacks / helpers invoked by rendering ----
def _on_cluster_env_change(self, *_args):
return None
def _get_k3d_cluster_list(self) -> list[str]:
return ["prole-dev"]
def _on_create_k3d_cluster(self):
return None
def _on_delete_k3d_cluster(self):
return None
def _on_k3d_cluster_select(self, *_args):
return None
def _on_kubectx_select(self, *_args):
return None
def _switch_kubectx(self, _context_name: str):
return None
def _get_kubectx_list(self) -> list[str]:
return list(self._kubectx_values)
def _validate_and_save_cluster_config(self):
return None
def _deploy_k3s_services(self):
return None
def _verify_k3s_services(self):
return None
def _canvas_texts(cnv: tk.Canvas) -> list[str]:
texts: list[str] = []
for item in cnv.find_all():
if cnv.type(item) == "text":
texts.append(cnv.itemcget(item, "text"))
return texts
@pytest.mark.parametrize("env", ["service", "prod"])
def test_cluster_environment_layout_large_mock_data(env: str):
root = tk.Tk()
root.withdraw()
try:
# Keep the canvas height realistic: the installer window is fixed-size and
# there is a navigation footer consuming vertical space.
canvas_height = 780
canvas = tk.Canvas(root, width=975, height=canvas_height, bg="white")
canvas.pack(fill="both", expand=False)
# In some headless/withdrawn runs, `winfo_height()` may remain `1` unless we
# force a full update cycle.
root.update()
kubectx_values = [f"ctx-{i:03d}" for i in range(120)]
app = _DummyClusterApp(root, canvas, kubectx_values=kubectx_values)
app.cluster_env.set(env)
app._render_init_cluster_page()
root.update()
# K3S connection details should not be rendered anymore (kubectx flow).
assert "K3s Connection:" not in _canvas_texts(canvas)
# Kubernetes context selector + description should be a single row.
label_item = None
for item in canvas.find_all():
if canvas.type(item) == "text" and canvas.itemcget(item, "text") == "Kubernetes Context:":
label_item = item
break
assert label_item is not None
label_y = canvas.coords(label_item)[1]
combo_y = canvas.coords(app._kubectx_combo_canvas_window)[1]
assert abs(combo_y - label_y) <= 10
# Common Core Services Namespace input should be a single row.
ns_label_item = None
for item in canvas.find_all():
if (
canvas.type(item) == "text"
and canvas.itemcget(item, "text") == "Common Core Services Namespace:"
):
ns_label_item = item
break
assert ns_label_item is not None
ns_label_y = canvas.coords(ns_label_item)[1]
ns_entry_y = canvas.coords(app._service_namespace_entry_canvas_window)[1]
assert abs(ns_entry_y - ns_label_y) <= 10
# The status notebook and Repair button must remain within the visible plane.
visible_h = max(canvas.winfo_height(), int(canvas.cget("height")))
for item_id in (
app._k3s_service_notebook_canvas_window,
app._k3s_deploy_btn_canvas_window,
):
bbox = canvas.bbox(item_id)
assert bbox is not None
assert bbox[3] <= visible_h
# Long output should be scrollable (TerminalConsole provides a vertical scrollbar).
assert getattr(app._k3s_service_status_console, "scroll", None) is not None
assert app._k3s_service_status_console.scroll.winfo_exists()
# Ensure large content doesn't crash the UI and the console remains read-only.
for i in range(300):
app._k3s_service_status_console.write(f"line {i}\n")
root.update()
assert app._k3s_service_status_console.text.cget("state") == "disabled"
finally:
root.destroy()