name: Test

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  image:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18
      - name: Push image to docker hub
        run: |
          echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USER }} --password-stdin
          export VERSION=test
          make all-image
  linux:
    runs-on: ubuntu-latest
    needs: [ "image" ]
    steps:
      - uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18

      - name: Setup Minikube
        timeout-minutes: 30
        uses: medyagh/setup-minikube@master

      - name: Kubernetes info
        run: |
          kubectl cluster-info
          cat ~/.kube/config
          kubectl get pods -n kube-system -o wide
      - name: Install demo bookinfo
        run: kubectl apply -f https://raw.githubusercontent.com/wencaiwulue/kubevpn/master/samples/bookinfo.yaml

      - name: Build
        run: |
          export VERSION=test
          make kubevpn-linux-amd64

      - name: Wait for pods reviews to be ready
        run: |
          kubectl wait pods -l app=reviews --for=condition=Ready --timeout=3600s
          kubectl get all -o wide
          kubectl get nodes -o yaml
          ifconfig
          route -n

      - name: Test
        run: go test -v ./test/function_test.go

  macos:
    runs-on: macos-latest
    needs: [ "image" ]
    steps:
      - uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18
      - uses: docker-practice/actions-setup-docker@master
      - name: Pull image in advance
        run: |
          rm '/usr/local/bin/kubectl'
          set -x
          docker version
          tag=`echo ${{ github.ref }} | sed 's/refs\/tags\///' | sed 's/\(.*\)-.*/\1/' | sed 's/-[0-9]*$//' || true`
          docker pull naison/kubevpn:test   || true
          docker pull naison/kubevpn:latest || true
          docker pull naison/kubevpn:${tag} || true
          docker pull naison/kubevpn-mesh:test   || true
          docker pull naison/kubevpn-mesh:latest || true
          docker pull naison/kubevpn-mesh:${tag} || true
          docker pull naison/envoy-xds-server:test   || true
          docker pull naison/envoy-xds-server:latest || true
          docker pull naison/envoy-xds-server:${tag} || true

      - name: Install minikube
        run: |
          brew install minikube
          minikube start --driver=docker
          kubectl get po -A
          minikube kubectl -- get po -A

      - name: Kubernetes info
        run: |
          kubectl cluster-info
          cat ~/.kube/config
          kubectl get pods -n kube-system -o wide

      - name: Install demo bookinfo
        run: kubectl apply -f https://raw.githubusercontent.com/wencaiwulue/kubevpn/master/samples/bookinfo.yaml

      - name: Build
        run: |
          export VERSION=test
          make kubevpn-darwin-amd64

      - name: Wait for pods reviews to be ready
        run: |
          kubectl wait pods -l app=reviews --for=condition=Ready --timeout=3600s
          kubectl get all -o wide
          kubectl get nodes -o yaml
          ifconfig
          netstat -anr

      - name: Test
        run: go test -v ./test/function_test.go

#  windows:
#    runs-on: windows-latest
#    steps:
#      - uses: actions/checkout@v2
#
#      - name: Set up Go
#        uses: actions/setup-go@v2
#        with:
#          go-version: 1.18
#      #      - run: |
#      #          choco install docker-desktop
#      #          docker version
#      #          docker run --rm hello-world
#      - run: |
#          choco install virtualbox
#          choco install minikube
#          minikube start --driver=virtualbox
#          minikube kubectl -- get po -A
#          choco install make
#      - name: Kubernetes info
#        run: |
#          kubectl cluster-info dump
#          kubectl get pods -n kube-system -o wide
#      - name: Install demo bookinfo
#        run: kubectl apply -f https://raw.githubusercontent.com/wencaiwulue/kubevpn/master/samples/bookinfo.yaml
#
#      - name: Build
#        run: make kubevpn-windows
#
#      - name: Wait for pods reviews to be ready
#        run: |
#          kubectl wait pods -l app=reviews --for=condition=Ready --timeout=600s
#          kubectl get all -o wide
#          kubectl get nodes -o yaml
#          ipconfig
#
#      - name: Test
#        run: go test -v ./test/

