from __future__ import annotations import subprocess from pathlib import Path from unittest.mock import patch from knoe.ops.context import KnoeContext from knoe.ops.k8s import apply_yaml, kubectl def test_kubectl_command_assembly(tmp_path: Path): ctx = KnoeContext(project_root=tmp_path, env={"FOO": "BAR"}) cp = subprocess.CompletedProcess(args=["kubectl"], returncode=0, stdout="ok\n", stderr="") with patch("knoe.ops.k8s.check", return_value=cp) as m: out = kubectl(ctx, "get", "pods", namespace="myns") assert out == "ok\n" (cmd,), kwargs = m.call_args assert cmd[:3] == ["kubectl", "get", "pods"] assert cmd[-2:] == ["-n", "myns"] assert kwargs["env"]["FOO"] == "BAR" def test_apply_yaml_passes_stdin(tmp_path: Path): ctx = KnoeContext(project_root=tmp_path, env={}) cp = subprocess.CompletedProcess(args=["kubectl"], returncode=0, stdout="", stderr="") with patch("knoe.ops.k8s.check", return_value=cp) as m: apply_yaml(ctx, "apiVersion: v1\nkind: Namespace\nmetadata:\n name: x\n") (cmd,), kwargs = m.call_args assert cmd[:4] == ["kubectl", "apply", "-f", "-"] assert "apiVersion" in (kwargs.get("input_text") or "")