name: memcached Tests

on:
  workflow_call:

jobs:
  build_wolfssl:
    name: Build wolfSSL
    # Just to keep it the same as the testing target
    runs-on: ubuntu-latest
    steps:
      - name: Build wolfSSL
        uses: wolfSSL/actions-build-autotools-project@v1
        with:
          path: wolfssl
          configure: --enable-memcached
          install: true

      - name: Bundle Docker entry point
        run: cp wolfssl/.github/workflows/memcached.sh build-dir/bin

      - name: Upload built lib
        uses: actions/upload-artifact@v3
        with:
          name: wolf-install-memcached
          path: build-dir
          retention-days: 1

  memcached_check:
    strategy:
      fail-fast: false
      matrix:
        # List of releases to test
        include:
          - ref: 1.6.22
    name: ${{ matrix.ref }}
    runs-on: ubuntu-latest
    needs: build_wolfssl
    steps:
      - name: Download lib
        uses: actions/download-artifact@v3
        with:
          name: wolf-install-memcached
          path: build-dir

      - name: Checkout OSP
        uses: actions/checkout@v3
        with:
          repository: wolfssl/osp
          path: osp

      - name: Install dependencies
        run: |
            export DEBIAN_FRONTEND=noninteractive
            sudo apt-get update
            sudo apt-get install -y libevent-dev libevent-2.1-7 automake pkg-config make libio-socket-ssl-perl

      - name: Checkout memcached
        uses: actions/checkout@v3
        with:
          repository: memcached/memcached
          ref: 1.6.22
          path: memcached

      - name: Configure and build memcached
        run: |
            cd $GITHUB_WORKSPACE/memcached/
            patch -p1 < $GITHUB_WORKSPACE/osp/memcached/memcached_1.6.22.patch
            ./autogen.sh
            export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/build-dir/lib:$LD_LIBRARY_PATH
            PKG_CONFIG_PATH=$GITHUB_WORKSPACE/build-dir/lib/pkgconfig ./configure --enable-wolfssl
            make -j$(nproc)

      - name: Confirm memcached built with wolfSSL
        working-directory: ./memcached
        run: |
          export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/build-dir/lib:$LD_LIBRARY_PATH
          ldd memcached | grep wolfssl

      - name: Run memcached tests
        working-directory: ./memcached
        run: |
          # Retry up to three times
          # Using docker because interrupting the tests doesn't close running
          # background servers. They can become daemonized and then all re-runs
          # will always fail.
          chmod +x $GITHUB_WORKSPACE/build-dir/bin/memcached.sh
          for i in {1..3}; do
            echo "-------- RUNNING TESTS --------"
            MEMCACHED_RES=0 # Not set when command succeeds
            # Tests should usually take less than 4 minutes. If already taking
            # 5 minutes then they are probably stuck. Interrupt and re-run.
            timeout 5m docker run -v /:/host \
              -v $GITHUB_WORKSPACE/build-dir/bin/memcached.sh:/memcached.sh \
              -e GITHUB_WORKSPACE=$GITHUB_WORKSPACE \
              -e HOST_ROOT=/host \
              -e LD_LIBRARY_PATH=$GITHUB_WORKSPACE/build-dir/lib:$LD_LIBRARY_PATH \
              alpine:latest /memcached.sh || MEMCACHED_RES=$?

            if [ "$MEMCACHED_RES" -eq "0" ]; then
              break
            fi
          done
          echo "test ran $i times"
          if [ "$MEMCACHED_RES" -ne "0" ]; then
            exit $MEMCACHED_RES
          fi
