name: GitHub Actions CI

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

jobs:

  build:
    name: build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        # Run on the latest minor release of Go 1.14:
        go-version: ^1.14
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - name: Ensure all files were formatted as per gofmt
      run: |
        [ "$(gofmt -l $(find . -name '*.go') 2>&1)" = "" ]

    - name: Go Vet
      run: |
        go vet

    - name: Build
      run: |
        go build -v ./cmd/...

  test:
    name: test
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        # Run on the latest minor release of Go 1.14:
        go-version: ^1.14
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - name: Test
      run: |
        go test -v -race ./internal/...

  integrationtest:
    name: integrationtest
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.x
      uses: actions/setup-go@v2
      with:
        # Run on the latest minor release of Go 1.14:
        go-version: ^1.14
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v2

    - name: Share cache with other actions
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - name: Build Docker container with the tools our tests require
      run: |
        docker build --pull --no-cache --rm -t=router7 -f travis/Dockerfile .

    - name: Run tests in Docker container
      run: |
        exit=0; for pkg in $(go list ./integration/...); do go test -c $pkg && docker run --privileged --net=host -v $PWD:/usr/src:ro router7 /bin/sh -c "./$(basename $pkg).test -test.v" || exit=1; done; [ $exit = 0 ]