#!/usr/bin/env bash

# Adapted from https://github.com/helm/chart-testing/blob/b8572749f073372c64323618ca13255677376e0d/e2e-kind.sh

set -o errexit
set -o nounset
set -o pipefail

CLUSTER_NAME=cnab-go-testing
readonly CLUSTER_NAME

K8S_VERSION=v1.21.1
readonly K8S_VERSION

KIND_KUBECONFIG="$PWD/kind-kubeconfig.yaml"
readonly KIND_KUBECONFIG

GO_TEST_COMMAND="go test -tags=integration -v -coverprofile=coverage.txt -covermode atomic ./..."
readonly GO_TEST_COMMAND

GO_TEST_LOG="go_test.log"
readonly GO_TEST_LOG

KIND_CONFIG_FILE="$PWD/kind.config.yaml"
readonly KIND_CONFIG_FILE

create_kind_cluster() {
    kind create cluster \
      --name "$CLUSTER_NAME" \
      --image "kindest/node:$K8S_VERSION" \
      --kubeconfig "$KIND_KUBECONFIG" \
      --wait 300s \
      --config "$KIND_CONFIG_FILE"

    kubectl cluster-info --kubeconfig $KIND_KUBECONFIG
    echo

    kubectl wait --for=condition=Ready nodes --all --kubeconfig $KIND_KUBECONFIG

    kubectl get nodes --kubeconfig $KIND_KUBECONFIG
    echo

    echo 'Cluster ready!'
    echo
}

test_e2e() {
    echo "Running $GO_TEST_COMMAND"

    KUBECONFIG="$KIND_KUBECONFIG" $GO_TEST_COMMAND >"$GO_TEST_LOG" 2>&1
    echo
}

print_versions() {
    echo "kind version: $(kind version)"
    echo "kubectl version: $(kubectl version --client)"
}

delete_kind_cluster() {
    kind delete cluster --name "$CLUSTER_NAME"
}

cleanup() {
    cat "$GO_TEST_LOG"
    echo

    cat "$GO_TEST_LOG" | go-junit-report > report.xml
    gocov convert coverage.txt > coverage.json
    gocov-xml < coverage.json > coverage.xml

    delete_kind_cluster

    echo 'Done!'
}

main() {
    trap cleanup EXIT

    print_versions
    create_kind_cluster
    test_e2e
}

"$@"
