from __future__ import annotations from subprocess import CompletedProcess import pytest from knoe.core.ops import _garage_common as garage_common def test_apply_garage_statefulset_with_recreate_on_immutable_error(monkeypatch): calls: list[tuple[list[str], bool]] = [] def _fake_kubectl(args, *, env=None, input_text=None, timeout=300, check=False): calls.append((args, check)) if len(calls) == 1: return CompletedProcess( args, 1, "", "The StatefulSet \"garage\" is invalid: spec: Forbidden: updates to statefulset spec for fields other than", ) return CompletedProcess(args, 0, "", "") monkeypatch.setattr(garage_common, "_kubectl", _fake_kubectl) logs: list[str] = [] garage_common._apply_garage_statefulset_with_recreate( namespace="knoe-system", manifest="/tmp/garage-statefulset.yaml", log=logs.append, ) assert len(calls) == 3 assert calls[0][1] is False assert calls[1][1] is True assert calls[2][1] is True assert calls[1][0] == [ "-n", "knoe-system", "delete", "statefulset", "garage", "--ignore-not-found=true", "--wait=true", ] assert any("recreating statefulset/garage" in line for line in logs) def test_apply_garage_statefulset_with_recreate_raises_on_other_errors(monkeypatch): calls: list[list[str]] = [] def _fake_kubectl(args, *, env=None, input_text=None, timeout=300, check=False): calls.append(args) return CompletedProcess(args, 1, "", "boom") monkeypatch.setattr(garage_common, "_kubectl", _fake_kubectl) with pytest.raises(RuntimeError, match="boom"): garage_common._apply_garage_statefulset_with_recreate( namespace="knoe-system", manifest="/tmp/garage-statefulset.yaml", ) assert len(calls) == 1 def test_apply_garage_statefulset_with_recreate_returns_on_success(monkeypatch): calls: list[tuple[list[str], bool]] = [] def _fake_kubectl(args, *, env=None, input_text=None, timeout=300, check=False): calls.append((args, check)) return CompletedProcess(args, 0, "", "") monkeypatch.setattr(garage_common, "_kubectl", _fake_kubectl) garage_common._apply_garage_statefulset_with_recreate( namespace="knoe-system", manifest="/tmp/garage-statefulset.yaml", ) assert len(calls) == 1 assert calls[0][1] is False