#!/bin/sh
# Fake kubectl for hack/kubernetes-nodes-unpin-hook.bats. Mocks the apiserver
# boundary of the pre-delete unpin hook's embedded script. Env:
#   FAKE_MODE      named-object get behavior:
#                    absent       success, empty output (the hook passes
#                                 --ignore-not-found, so absence is exit 0)
#                    error-named  transient server error exit 1 (list still ok)
#                    error-named-notfoundish
#                                 exit 1 with a conversion-webhook message whose
#                                 text contains "not found" -- still a FAILURE
#                    pinned       jsonpath read returns "keep"
#                    unpinned     jsonpath read returns empty, exit 0
#   FAKE_LIST_FAIL when set, the KMT list query fails with a transient error
#   FAKE_WARN_STDERR
#                  when set, successful reads print a deprecation warning to
#                  stderr first, the way real kubectl does
#   FAKE_KMT_NAMES newline-separated names the KMT list query returns
#   FAKE_CMDLOG    path to append annotate calls to
log() { [ -n "${FAKE_CMDLOG:-}" ] && echo "$*" >> "$FAKE_CMDLOG"; return 0; }
warn() { [ -n "${FAKE_WARN_STDERR:-}" ] && echo "Warning: cluster.x-k8s.io/v1beta1 MachineDeployment is deprecated" >&2; return 0; }
args="$*"
mode="${FAKE_MODE:-absent}"
case "$args" in
  *" annotate "*)
    log "annotate $args"; exit 0 ;;
  *"get kubevirtmachinetemplate.infrastructure.cluster.x-k8s.io -o"*)
    # list query (no /name)
    if [ -n "${FAKE_LIST_FAIL:-}" ]; then
      echo "Error from server: etcdserver: request timed out" >&2; exit 1
    fi
    warn
    [ -n "${FAKE_KMT_NAMES:-}" ] && printf '%s\n' "$FAKE_KMT_NAMES"
    exit 0 ;;
esac
# named-object get
case "$mode" in
  pinned)      warn; printf 'keep'; exit 0 ;;
  unpinned)    warn; exit 0 ;;
  error-named) echo "Error from server: etcdserver: request timed out" >&2; exit 1 ;;
  error-named-notfoundish)
    echo 'Error from server: conversion webhook for cluster.x-k8s.io/v1beta1, Kind=MachineDeployment failed: service "capi-webhook-service" not found' >&2; exit 1 ;;
  *)           warn; exit 0 ;;  # absent: --ignore-not-found -> success, empty
esac
