name: Run tests with workflow_call

on:
  workflow_call:
    inputs:
      fluence-env:
        description: "Fluence enviroment to run tests agains"
        type: string
        default: "local"
      rust-peer-image:
        description: "rust-peer image tag"
        type: string
        default: "fluencelabs/fluence:minimal"
      aqua-version:
        description: "@fluencejs/aqua version"
        type: string
      ref:
        description: "GitHub ref to checkout to"
        type: string
        default: "main"

env:
  FORCE_COLOR: true
  RUST_PEER_IMAGE: "${{ inputs.rust-peer-image }}"
  FLUENCE_ENV: "${{ inputs.fluence-env }}"

jobs:
  aqua:
    name: "Run aqua tests"
    runs-on: builder

    permissions:
      contents: read
      id-token: write

    steps:
      - name: Import secrets
        uses: hashicorp/vault-action@v2.4.1
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          secrets: |
            kv/docker-registry/basicauth/ci username | DOCKER_USERNAME ;
            kv/docker-registry/basicauth/ci password | DOCKER_PASSWORD ;
            kv/npm-registry/basicauth/ci token | NODE_AUTH_TOKEN;

      - name: Checkout registry
        uses: actions/checkout@v3
        with:
          repository: fluencelabs/registry
          ref: ${{ inputs.ref }}

      - name: Setup node with self-hosted registry
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://npm.fluence.dev"
          cache: "npm"
          cache-dependency-path: "**/package-lock.json"

      - name: Run npm install in aqua
        run: npm install
        working-directory: aqua

      - name: Set aqua version from branch
        if: inputs.aqua-version != ''
        run: npm i --save-dev @fluencelabs/aqua@${{ inputs.aqua-version }}
        working-directory: aqua

      - name: Run npm install in aqua-tests
        run: npm install
        working-directory: aqua-tests

      - name: Set aqua version from branch
        if: inputs.aqua-version != ''
        run: npm i --save-dev @fluencelabs/aqua@${{ inputs.aqua-version }}
        working-directory: aqua-tests

      - name: Setup rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1

      - name: Install marine
        uses: baptiste0928/cargo-install@v1
        with:
          crate: marine

      - name: Build service
        run: ./build.sh
        working-directory: service

      - name: Create builtin distribution package
        run: ./builtin-package/package.sh

      - name: Unpack registry package
        run: tar xvf registry.tar.gz

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          registry: docker.fluence.dev
          username: ${{ env.DOCKER_USERNAME }}
          password: ${{ env.DOCKER_PASSWORD }}

      - name: Pull rust-peer image
        run: docker pull $RUST_PEER_IMAGE

      - name: Setup docker-compose
        uses: KengoTODA/actions-setup-docker-compose@v1.0.9
        with:
          version: 'v2.10.0'

      - name: Run rust-peer
        uses: isbang/compose-action@v1.1.0
        with:
          compose-file: ".github/e2e/docker-compose.yml"
          down-flags: "--volumes"

      - name: Setup python
        uses: actions/setup-python@v4
        with:
          python-version: "3.9"
          cache: "pip"
          cache-dependency-path: aqua-tests/requirements.txt

      - name: Install python requirements
        run: pip install -r requirements.txt
        working-directory: aqua-tests

      # TODO run an aqua script in a loop to check
      - name: Wait for registry to be deployed
        run: sleep 40

      - name: Run aqua tests
        run: pytest -n auto
        working-directory: aqua-tests

      - name: Dump rust-peer logs on failure
        if: failure()
        uses: jwalton/gh-docker-logs@v1

      - name: Remove registry directory
        if: always()
        run: sudo rm -rf registry
