name: KBS e2e

on:
  workflow_call:
    inputs:
      tee:
        type: string
        required: true
      runs-on:
        type: string
        default: '["ubuntu-24.04"]'
        description: JSON representation of runner labels
      tarball:
        type: string
        description: Artifact containing checked out source from a prior job
        required: true

# Self-hosted runners do not set -o pipefail otherwise
defaults:
  run:
    shell: bash

jobs:
  build-binaries:
    runs-on: ubuntu-24.04
    env:
      RUSTC_VERSION: 1.85.0
    steps:
    - name: Download artifacts
      uses: actions/download-artifact@v4

    - name: Extract tarball
      run: tar xzf ./artifact/${{ inputs.tarball }}

    - name: Install Rust toolchain (${{ env.RUSTC_VERSION }})
      run: |
        rustup update --no-self-update ${{ env.RUSTC_VERSION }}
        rustup component add --toolchain ${{ env.RUSTC_VERSION }} rustc
        rustup default ${{ env.RUSTC_VERSION }}

    - name: Set up rust build cache
      uses: actions/cache@v4
      continue-on-error: false
      with:
        path: |
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          target/
        key: rust-${{ hashFiles('./Cargo.lock') }}
    
    - name: Install TDX dependencies
      run: |
        sudo curl -L https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add -
        sudo echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu jammy main' | sudo tee /etc/apt/sources.list.d/intel-sgx.list
        sudo apt-get update
        sudo apt-get install -y libtdx-attest-dev libsgx-dcap-quote-verify-dev

    - name: Build bins
      working-directory: kbs/test
      run: |
        make install-dev-dependencies
        make bins

    - name: Archive test folder
      run: tar czf test.tar.gz kbs/test

    - uses: actions/upload-artifact@v4
      with:
        path: test.tar.gz
        overwrite: true

  e2e-test:
    needs: build-binaries
    runs-on: ${{ fromJSON(inputs.runs-on) }}
    steps:
    - name: Download artifacts
      uses: actions/download-artifact@v4

    - name: Extract test folder
      run: tar xzf ./artifact/test.tar.gz

    - name: Set up SGX/TDX certificates cache
      uses: actions/cache@v4
      with:
        path: /root/.dcap-qcnl
        key: ${{ runner.os }}-dcap-qcnl

    - name: Install dependencies
      working-directory: kbs/test
      run: |
        sudo apt-get update
        sudo apt-get install -y make --no-install-recommends
        sudo make install-dependencies
    
    - name: Install TDX dependencies
      run: |
        sudo curl -L https://download.01.org/intel-sgx/sgx_repo/ubuntu/intel-sgx-deb.key | sudo apt-key add -
        sudo echo 'deb [arch=amd64] https://download.01.org/intel-sgx/sgx_repo/ubuntu jammy main' | sudo tee /etc/apt/sources.list.d/intel-sgx.list
        sudo apt-get update
        sudo apt-get install -y libtdx-attest libsgx-dcap-quote-verify libsgx-dcap-default-qpl

    - name: Set cc_kbc sample attester env
      if: inputs.tee == 'sample'
      run: echo "AA_SAMPLE_ATTESTER_TEST=1" >> "$GITHUB_ENV"
    
    - name: Run e2e test
      working-directory: kbs/test
      env:
        TEE: ${{ inputs.tee }}
        RUST_LOG: warn
      run: sudo -E make e2e-test
