name: e2e

on:
  pull_request:
  push:
    branches:
      - master

jobs:
  kind:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Restore Go cache
        uses: actions/cache@v1
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
      - name: Setup Go
        uses: actions/setup-go@v2-beta
        with:
          go-version: 1.14.x
      - name: Setup Kubernetes
        uses: engineerd/setup-kind@v0.4.0
        with:
          image: kindest/node:v1.16.9
      - name: Run test
        run: make test
      - name: Check if working tree is dirty
        run: |
          if [[ $(git diff --stat) != '' ]]; then
            git diff
            echo 'run make test and commit changes'
            exit 1
          fi
      - name: Build
        run: sudo go build -o ./bin/gotk ./cmd/gotk
      - name: gotk check --pre
        run: |
          ./bin/gotk check --pre
      - name: gotk install --version
        run: |
          ./bin/gotk install --version=master --namespace=test --verbose --components="source-controller,kustomize-controller"
      - name: gotk uninstall
        run: |
          ./bin/gotk uninstall --namespace=test --crds --silent
      - name: gotk install --manifests
        run: |
          ./bin/gotk install --manifests ./manifests/install/ --version=""
      - name: gotk create source git
        run: |
          ./bin/gotk create source git podinfo \
            --url https://github.com/stefanprodan/podinfo  \
            --tag-semver=">=3.2.3"
      - name: gotk get sources git
        run: |
          ./bin/gotk get sources git
      - name: gotk create kustomization
        run: |
          ./bin/gotk create kustomization podinfo \
            --source=podinfo \
            --path="./deploy/overlays/dev" \
            --prune=true \
            --interval=5m \
            --validation=client \
            --health-check="Deployment/frontend.dev" \
            --health-check="Deployment/backend.dev" \
            --health-check-timeout=3m
      - name: gotk sync kustomization --with-source
        run: |
          ./bin/gotk reconcile kustomization podinfo --with-source
      - name: gotk get kustomizations
        run: |
          ./bin/gotk get kustomizations
      - name: gotk suspend kustomization
        run: |
          ./bin/gotk suspend kustomization podinfo
      - name: gotk resume kustomization
        run: |
          ./bin/gotk resume kustomization podinfo
      - name: gotk export
        run: |
          ./bin/gotk export source git --all
          ./bin/gotk export kustomization --all
      - name: gotk delete kustomization
        run: |
          ./bin/gotk delete kustomization podinfo --silent
      - name: gotk create source helm
        run: |
          ./bin/gotk create source helm podinfo \
            --url https://stefanprodan.github.io/podinfo
      - name: gotk create helmrelease --source=HelmRepository/podinfo
        run: |
          ./bin/gotk create hr podinfo-helm \
            --target-namespace=default \
            --source=HelmRepository/podinfo \
            --chart=podinfo \
            --chart-version=">4.0.0 <5.0.0"
      - name: gotk create helmrelease --source=GitRepository/podinfo
        run: |
          ./bin/gotk create hr podinfo-git \
            --target-namespace=default \
            --source=GitRepository/podinfo \
            --chart=./charts/podinfo
      - name: gotk get helmreleases
        run: |
          ./bin/gotk get helmreleases
      - name: gotk export helmrelease
        run: |
          ./bin/gotk export hr --all
      - name: gotk delete helmrelease podinfo-helm
        run: |
          ./bin/gotk delete hr podinfo-helm --silent
      - name: gotk delete helmrelease podinfo-git
        run: |
          ./bin/gotk delete hr podinfo-git --silent
      - name: gotk delete source helm
        run: |
          ./bin/gotk delete source helm podinfo --silent
      - name: gotk delete source git
        run: |
          ./bin/gotk delete source git podinfo --silent
      - name: gotk check
        run: |
          ./bin/gotk check
      - name: Debug failure
        if: failure()
        run: |
          kubectl version --client --short
          kubectl -n gitops-system get all
          kubectl -n gitops-system get kustomizations -oyaml
          kubectl -n gitops-system logs deploy/source-controller
          kubectl -n gitops-system logs deploy/kustomize-controller