name: Tests

on:
  workflow_dispatch:
  pull_request:
    branches:
      - main
      - releases/**
  push:
    branches:
      - main
      - releases/**
      - fix/**
      - v4*

jobs:
  # End to end save and restore
  test-save:
    runs-on: runs-on,runner=2cpu-linux-arm64
    strategy:
      matrix:
        part_size: [32]
        queue_size: [4, 8]
      fail-fast: false
    steps:
    - name: Checkout
      uses: actions/checkout@v3
    - name: Generate files in working directory
      shell: bash
      run: |
        __tests__/create-cache-files.sh ${{ runner.os }} test-cache
        # 5GB
        # curl -o test-cache/ubuntu.iso https://releases.ubuntu.com/22.04.3/ubuntu-22.04.3-desktop-amd64.iso
        # 2Gb
        curl -o test-cache/ubuntu.iso https://releases.ubuntu.com/jammy/ubuntu-22.04.3-live-server-amd64.iso
    - name: Generate files outside working directory
      shell: bash
      run: __tests__/create-cache-files.sh ${{ runner.os }} ~/test-cache
    - name: Save cache
      uses: ./
      env:
        UPLOAD_PART_SIZE: ${{ matrix.part_size }}
        UPLOAD_QUEUE_SIZE: ${{ matrix.queue_size }}
      with:
        key: test-${{ runner.os }}-${{ github.run_id }}-${{ matrix.part_size }}-${{ matrix.queue_size }}
        path: |
          test-cache
          ~/test-cache
  test-restore:
    needs: test-save
    strategy:
      matrix:
        part_size: [8, 16]
        queue_size: [8, 12]
      fail-fast: false
    runs-on: runs-on,runner=2cpu-linux-arm64
    steps:
    - name: Checkout
      uses: actions/checkout@v3
    - name: Restore cache
      uses: ./
      env:
        DOWNLOAD_PART_SIZE: ${{ matrix.part_size }}
        DOWNLOAD_QUEUE_SIZE: ${{ matrix.queue_size }}
      with:
        key: test-${{ runner.os }}-${{ github.run_id }}-${{ matrix.part_size }}-${{ matrix.queue_size }}
        restore-keys: |
          test-${{ runner.os }}-${{ github.run_id }}-${{ matrix.part_size }}-
          test-${{ runner.os }}-${{ github.run_id }}-
        path: |
          test-cache
          ~/test-cache
    - name: Verify cache files in working directory
      shell: bash
      run: __tests__/verify-cache-files.sh ${{ runner.os }} test-cache
    - name: Verify cache files outside working directory
      shell: bash
      run: __tests__/verify-cache-files.sh ${{ runner.os }} ~/test-cache

  test-container:
    runs-on: runs-on,runner=2cpu-linux-arm64
    container:
      image: ubuntu:latest
    steps:
    - name: Checkout
      uses: actions/checkout@v3
    - name: Test in container
      run: |
        __tests__/create-cache-files.sh Linux test-cache
        __tests__/verify-cache-files.sh Linux test-cache
    - name: Save cache
      uses: ./
      with:
        key: test-container-${{ github.run_id }}
        path: test-cache