name: "Running tests"
on:
  push:
    branches:
      - main
  pull_request:
    branches: [main]
  release:
    types:
      - published

permissions: read-all

jobs:
  test-run-minimal:
    name: Running zot without extensions tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: Install go
        uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          cache: false
          check-latest: true
          go-version: 1.26.x
      - name: Cache go dependencies
        id: cache-go-dependencies
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-mod-
      - name: Install go dependencies
        if: steps.cache-go-dependencies.outputs.cache-hit != 'true'
        run: |
          cd $GITHUB_WORKSPACE
          go mod download
      - uses: ./.github/actions/setup-localstack
      - name: run zot minimal tests
        run: |
          cd $GITHUB_WORKSPACE
          make test-minimal
        env:
          S3MOCK_ENDPOINT: localhost:4566
          DYNAMODBMOCK_ENDPOINT: http://localhost:4566
          AWS_ACCESS_KEY_ID: fake
          AWS_SECRET_ACCESS_KEY: fake
      - name: upload coverage
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-minimal
          path: coverage-minimal.txt
      - uses: ./.github/actions/teardown-localstack
  test-run-extensions:
    name: Run zot with extensions tests
    runs-on: cncf-ubuntu-16-64-x86
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          cache: false
          check-latest: true
          go-version: 1.26.x
      - name: Cache go dependencies
        id: cache-go-dependencies
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-mod-
      - name: Install go dependencies
        if: steps.cache-go-dependencies.outputs.cache-hit != 'true'
        run: |
          cd $GITHUB_WORKSPACE
          go mod download
      - uses: ./.github/actions/setup-localstack
      - name: run zot extended tests
        run: |
          cd $GITHUB_WORKSPACE
          make test-extended
        env:
          S3MOCK_ENDPOINT: localhost:4566
          DYNAMODBMOCK_ENDPOINT: http://localhost:4566
          AWS_ACCESS_KEY_ID: fake
          AWS_SECRET_ACCESS_KEY: fake
      - name: upload coverage
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-extended
          path: coverage-extended.txt
      - uses: ./.github/actions/teardown-localstack
  test-run-devmode:
    name: Running development-mode tests on Linux
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          cache: false
          check-latest: true
          go-version: 1.26.x
      - name: Cache go dependencies
        id: cache-go-dependencies
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-mod-
      - name: Install go dependencies
        if: steps.cache-go-dependencies.outputs.cache-hit != 'true'
        run: |
            cd $GITHUB_WORKSPACE
            go mod download
      - name: run zot development-mode unit tests (possibly using failure injection)
        run: make test-devmode
      - name: upload coverage
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-devmode
          path: coverage-dev-*.txt
  test-run-privileged:
    name: Running privileged tests on Linux
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          cache: false
          check-latest: true
          go-version: 1.26.x
      - name: Cache go dependencies
        id: cache-go-dependencies
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-mod-
      - name: Install go dependencies
        if: steps.cache-go-dependencies.outputs.cache-hit != 'true'
        run: |
            cd $GITHUB_WORKSPACE
            go mod download
      - uses: ./.github/actions/setup-gcs-storage-testbench
      - name: run zot privileged tests
        run: >
          sudo env
          "PATH=$PATH"
          "GCSMOCK_ENDPOINT=$GCSMOCK_ENDPOINT"
          "STORAGE_EMULATOR_HOST=$STORAGE_EMULATOR_HOST"
          make privileged-test
        env:
          GCSMOCK_ENDPOINT: http://localhost:9000/
          STORAGE_EMULATOR_HOST: localhost:9000
      - name: upload coverage
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: coverage-needprivileges
          path: coverage-needprivileges-*.txt
      - uses: ./.github/actions/teardown-gcs-storage-testbench
  test-coverage:
    name: Collect all test coverage
    runs-on: ubuntu-latest
    if: always()
    needs: [test-run-minimal,test-run-extensions,test-run-devmode,test-run-privileged]
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          cache: false
          check-latest: true
          go-version: 1.26.x
      - name: download all workflow coverage files
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
      - name: merge code coverage
        run: |
          cd $GITHUB_WORKSPACE
          cp coverage-minimal/* .
          cp coverage-extended/* .
          cp coverage-devmode/* .
          cp coverage-needprivileges/* .
          make covhtml
          mkdir unified-coverage
          cp coverage.txt coverage.html unified-coverage/
      - name: upload unified-coverage as build artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: unified-coverage
          path: unified-coverage/*
      - name: upload code coverage
        uses: codecov/codecov-action@e79a6962e0d4c0c17b229090214935d2e33f8354 # v6.0.1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

