name: CI
on:
  push:
    branches:
      - master
      - 'release/**'
  pull_request:
    branches:
      - master
      - 'release/**'

jobs:
  #
  # golangci-lint
  #
  linters:
    name: Linters
    runs-on: ${{ matrix.os }}
    timeout-minutes: 10

    strategy:
      matrix:
        go-version: [1.16.2]
        os: [ubuntu-18.04, macos-10.15, windows-2019]

    steps:
      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd

      - name: Set env
        shell: bash
        run: |
          echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
          echo "${{ github.workspace }}/bin" >> $GITHUB_PATH

      - uses: golangci/golangci-lint-action@v2
        with:
          version: v1.36.0
          working-directory: src/github.com/containerd/containerd
          args: --timeout=5m

  #
  # Project checks
  #
  project:
    name: Project Checks
    runs-on: ubuntu-18.04
    timeout-minutes: 5

    steps:
      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd
          fetch-depth: 100

      - uses: containerd/project-checks@v1
        with:
          working-directory: src/github.com/containerd/containerd

  #
  # Protobuf checks
  #
  protos:
    name: Protobuf
    runs-on: ubuntu-18.04
    timeout-minutes: 5

    defaults:
      run:
        working-directory: src/github.com/containerd/containerd

    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.16.2'

      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd

      - name: Set env
        shell: bash
        run: |
          echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
          echo "GO111MODULE=off" >> $GITHUB_ENV
          echo "${{ github.workspace }}/bin" >> $GITHUB_PATH

      - name: Install protobuf
        run: |
          sudo env PATH=$PATH GOPATH=$GOPATH script/setup/install-protobuf
          sudo chmod +x /usr/local/bin/protoc
          sudo chmod og+rx /usr/local/include/google /usr/local/include/google/protobuf /usr/local/include/google/protobuf/compiler
          sudo chmod -R og+r /usr/local/include/google/protobuf/
          protoc --version

      - run: script/setup/install-dev-tools
      - run: make proto-fmt
      - run: make check-protos check-api-descriptors

  man:
    name: Manpages
    runs-on: ubuntu-18.04
    timeout-minutes: 5

    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.16.2'

      - name: Set env
        shell: bash
        run: |
          echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
          echo "${{ github.workspace }}/bin" >> $GITHUB_PATH

      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd

      - run: GO111MODULE=on go get github.com/cpuguy83/go-md2man/v2@v2.0.0

      - run: make man
        working-directory: src/github.com/containerd/containerd

  #
  # Build containerd binaries
  #
  binaries:
    name: Binaries
    runs-on: ${{ matrix.os }}
    timeout-minutes: 10
    needs: [project, linters, protos, man]

    strategy:
      matrix:
        os: [ubuntu-18.04, macos-10.15, windows-2019]

    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.16.2'

      - name: Set env
        shell: bash
        run: |
          echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
          echo "${{ github.workspace }}/bin" >> $GITHUB_PATH

      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd

      - name: Make
        run: |
          make build
          make binaries
        working-directory: src/github.com/containerd/containerd

      - name: Cross-compile
        if: startsWith(matrix.os, 'ubuntu')
        run : |
          GOOS=freebsd make build
          GOOS=freebsd make binaries
        working-directory: src/github.com/containerd/containerd

  #
  # Integration and CRI tests
  #
  integration-windows:
    name: Windows Integration
    runs-on: windows-2019
    timeout-minutes: 30
    needs: [project, linters, protos, man]

    defaults:
      run:
        shell: bash
        working-directory: src/github.com/containerd/containerd

    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.16.2'

      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd

      - uses: actions/checkout@v2
        with:
          repository: Microsoft/hcsshim
          path: src/github.com/Microsoft/hcsshim

      - name: Set env
        run: |
          echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
          echo "${{ github.workspace }}/src/github.com/containerd/containerd/bin" >> $GITHUB_PATH

      - run: script/setup/install-dev-tools

      - name: Binaries
        env:
          CGO_ENABLED: 1
        run: |
          set -o xtrace
          mingw32-make.exe binaries
          bindir="$(pwd)"
          SHIM_COMMIT=$(grep 'Microsoft/hcsshim ' go.mod | awk '{print $2}')
          cd ../../Microsoft/hcsshim
          git fetch --tags origin "${SHIM_COMMIT}"
          git checkout "${SHIM_COMMIT}"
          GO111MODULE=on go build -mod=vendor -o "${bindir}/integration/client/containerd-shim-runhcs-v1.exe" ./cmd/containerd-shim-runhcs-v1

      - name: Tests
        env:
          CGO_ENABLED: 1
        run: mingw32-make.exe test root-test

      - name: Integration 1
        env:
          CGO_ENABLED: 1
        run: mingw32-make.exe integration

      # Run the integration suite a second time. See discussion in github.com/containerd/containerd/pull/175
      - name: Integration 2
        env:
          TESTFLAGS_PARALLEL: 1
          CGO_ENABLED: 1
        run: mingw32-make.exe integration

  integration-linux:
    name: Linux Integration
    runs-on: ubuntu-18.04
    timeout-minutes: 25
    needs: [project, linters, protos, man]

    strategy:
      fail-fast: false
      matrix:
        runtime: [io.containerd.runtime.v1.linux, io.containerd.runc.v1, io.containerd.runc.v2]
        runc: [runc, crun]
        exclude:
          - runtime: io.containerd.runc.v1
            runc: crun
          - runtime: io.containerd.runtime.v1.linux
            runc: crun

    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: '1.16.2'

      - uses: actions/checkout@v2
        with:
          path: src/github.com/containerd/containerd

      - name: Set env
        run: |
          echo "GOPATH=${{ github.workspace }}" >> $GITHUB_ENV
          echo "${{ github.workspace }}/bin" >> $GITHUB_PATH

      - name: Install containerd dependencies
        env:
          RUNC_FLAVOR: ${{ matrix.runc }}
        run: |
          sudo -E PATH=$PATH script/setup/install-seccomp
          sudo -E PATH=$PATH script/setup/install-runc
          sudo -E PATH=$PATH script/setup/install-cni
          sudo -E PATH=$PATH script/setup/install-critools
        working-directory: src/github.com/containerd/containerd

      - name: Install criu
        run: |
          sudo apt-get install -y \
            libprotobuf-dev \
            libprotobuf-c-dev \
            protobuf-c-compiler \
            protobuf-compiler \
            python-protobuf \
            libnl-3-dev \
            libnet-dev \
            libcap-dev \
            python-future
          wget https://github.com/checkpoint-restore/criu/archive/v3.13.tar.gz -O criu.tar.gz
          tar -zxf criu.tar.gz
          cd criu-3.13
          sudo make install-criu

      - name: Install containerd
        env:
          CGO_ENABLED: 1
        run: |
          make binaries
          sudo make install
        working-directory: src/github.com/containerd/containerd

      - name: Tests
        env:
          GOPROXY: direct
        run: |
          make test
          sudo -E PATH=$PATH GOPATH=$GOPATH GOPROXY=$GOPROXY make root-test
        working-directory: src/github.com/containerd/containerd

      - name: Integration 1
        env:
          GOPROXY: direct
          TEST_RUNTIME: ${{ matrix.runtime }}
          RUNC_FLAVOR: ${{ matrix.runc }}
        run: |
          sudo GOPATH=$GOPATH GOPROXY=$GOPROXY TEST_RUNTIME=$TEST_RUNTIME RUNC_FLAVOR=$RUNC_FLAVOR make integration EXTRA_TESTFLAGS=-no-criu TESTFLAGS_RACE=-race
        working-directory: src/github.com/containerd/containerd

      # Run the integration suite a second time. See discussion in github.com/containerd/containerd/pull/1759
      - name: Integration 2
        env:
          GOPROXY: direct
          TEST_RUNTIME: ${{ matrix.runtime }}
          RUNC_FLAVOR: ${{ matrix.runc }}
        run: |
          sudo GOPATH=$GOPATH GOPROXY=$GOPROXY TEST_RUNTIME=$TEST_RUNTIME RUNC_FLAVOR=$RUNC_FLAVOR TESTFLAGS_PARALLEL=1 make integration EXTRA_TESTFLAGS=-no-criu
        working-directory: src/github.com/containerd/containerd

      # CRIU wouldn't work with overlay snapshotter yet.
      # See https://github.com/containerd/containerd/pull/4708#issuecomment-724322294.
      - name: CRIU Integration
        env:
          GOPROXY: direct
          TEST_RUNTIME: ${{ matrix.runtime }}
          RUNC_FLAVOR: ${{ matrix.runc }}
        # crun doesn't have "checkpoint" command.
        if: ${{ matrix.runc == 'runc' }}
        run: |
          sudo GOPATH=$GOPATH GOPROXY=$GOPROXY \
          TEST_RUNTIME=$TEST_RUNTIME RUNC_FLAVOR=$RUNC_FLAVOR TESTFLAGS_PARALLEL=1 \
          TEST_SNAPSHOTTER=native \
          make integration EXTRA_TESTFLAGS='-run TestCheckpoint'
        working-directory: src/github.com/containerd/containerd

      - name: CRI Integration Test
        env:
          TEST_RUNTIME: ${{ matrix.runtime }}
        run: |
          CONTAINERD_RUNTIME=$TEST_RUNTIME make cri-integration
        working-directory: src/github.com/containerd/containerd

      - name: cri-tools critest
        env:
          TEST_RUNTIME: ${{ matrix.runtime }}
        run: |
          BDIR="$(mktemp -d -p $PWD)"
          mkdir -p ${BDIR}/{root,state}
          cat > ${BDIR}/config.toml <<EOF
            [plugins.cri.containerd.default_runtime]
              runtime_type = "${TEST_RUNTIME}"
          EOF
          sudo ls /etc/cni/net.d
          sudo PATH=$PATH BDIR=$BDIR /usr/local/bin/containerd -a ${BDIR}/c.sock --config ${BDIR}/config.toml --root ${BDIR}/root --state ${BDIR}/state --log-level debug &> ${BDIR}/containerd-cri.log &
          sudo PATH=$PATH BDIR=$BDIR /usr/local/bin/ctr -a ${BDIR}/c.sock version
          sudo PATH=$PATH BDIR=$BDIR GOPATH=$GOPATH critest --runtime-endpoint=unix:///${BDIR}/c.sock --parallel=8
          TEST_RC=$?
          test $TEST_RC -ne 0 && cat ${BDIR}/containerd-cri.log
          sudo pkill containerd
          sudo BDIR=$BDIR rm -rf ${BDIR}
          test $TEST_RC -eq 0 || /bin/false

      # Log the status of this VM to investigate issues like
      # https://github.com/containerd/containerd/issues/4969
      - name: Host Status
        if: always()
        run: |
          set -x
          mount
          df
          losetup -l

  cgroup2:
    name: CGroupsV2 and SELinux Integration
    # nested virtualization is only available on macOS hosts
    runs-on: macos-10.15
    timeout-minutes: 45
    needs: [project, linters, protos, man]
    strategy:
      matrix:
        # Currently crun is disabled to decrease CI flakiness.
        # We can enable crun again when we get a better CI infra.
        runc: [runc]
    steps:
      - uses: actions/checkout@v2

      - name: "Cache ~/.vagrant.d/boxes"
        uses: actions/cache@v2
        with:
          path: ~/.vagrant.d/boxes
          key: vagrant-${{ hashFiles('Vagrantfile*') }}

      - name: Vagrant start
        run: |
          # Retry if it fails (download.fedoraproject.org returns 404 sometimes)
          vagrant up || vagrant up

      - name: Integration
        env:
          RUNC_FLAVOR: ${{ matrix.runc }}
          SELINUX: Enforcing
        run: vagrant up --provision-with=selinux,install-runc,test-integration

      - name: CRI test
        env:
          RUNC_FLAVOR: ${{ matrix.runc }}
          SELINUX: Enforcing
        run: vagrant up --provision-with=selinux,install-runc,test-cri
