name: manual pytest execution against basic lab
env:
  # thirdparties
  DOCKER_SERVER: tip-tip-wlan-cloud-docker-repo.jfrog.io
  DOCKER_USER_NAME: ${{ secrets.DOCKER_USER_NAME }}
  DOCKER_USER_PASSWORD: ${{ secrets.DOCKER_USER_PASSWORD }}
  # AWS credentials
  AWS_EKS_NAME: tip-wlan-main
  AWS_DEFAULT_OUTPUT: json
  AWS_DEFAULT_REGION: ap-south-1
  AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
  AWS_ACCESS_KEY_ID: ${{ secrets.AWS_CLIENT_ID }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_CLIENT_KEY }}

on:
  workflow_dispatch:
    inputs:
      openwifi_revision:
        required: true
        default: 'main'
        description: 'revision of the Open Wifi Helm chart'
      ap_model:
        description: 'AP model to test, one of edgecore_ecw5410,edgecore_eap101,edgecore_ecw5211,cig_wf188n,edgecore_eap102,liteon_wpx8324,hfcl_ion4'
        required: true
      firmware:
        default: 'next-latest'
        description: 'Target firmware version to be specified <branch>-<commit | latest>'
        required: false
      marker_expression:
        description: "marker expression that will be passed to pytest's -m"
        required: true
      tests_release:
        required: false
        default: ""
        description: "Tests release branch to use (i.e. 'release/v2.8.0' or 'master'). If left empty, latest release branch is used"

defaults:
  run:
    shell: bash

jobs:
  vars:
    runs-on: ubuntu-latest
    outputs:
      tests_release: ${{ steps.vars.outputs.tests_release }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: set variables
        id: vars
        run: |
          LATEST_TESTS_RELEASE=$(git branch -r | grep 'release/v' | sed 's!\s*origin/!!' | tail -1)
          if [[ -z "${{ github.event.inputs.tests_release }}" ]]; then
            echo "Tests release was not passed, using branch $LATEST_TESTS_RELEASE"
            echo "tests_release=$LATEST_TESTS_RELEASE" >> $GITHUB_OUTPUT
          else
            echo "Tests release was passed - ${{ github.event.inputs.tests_release }}"
            echo "tests_release=${{ github.event.inputs.tests_release }}" >> $GITHUB_OUTPUT
          fi

  build:
    runs-on: ubuntu-latest
    needs: ["vars"]
    steps:
    # checkout needed repositories
    - name: Checkout Testing repo
      uses: actions/checkout@v3
      with:
        path: wlan-testing
        ref: ${{ needs.vars.outputs.tests_release }}

    - name: Checkout LANforge scripts
      uses: actions/checkout@v3
      with:
        path: wlan-lanforge-scripts
        repository: Telecominfraproject/wlan-lanforge-scripts

    - name: import LANforge scripts
      working-directory: wlan-testing
      run: ./sync_repos.bash

    # build and push docker image
    - name: docker login
      run: docker login ${{ env.DOCKER_SERVER }} -u ${{ env.DOCKER_USER_NAME }} -p ${{ env.DOCKER_USER_PASSWORD }}
    - name: build docker image
      working-directory: wlan-testing
      run: docker build -t ${{ env.DOCKER_SERVER }}/cloud-sdk-nightly:${{ github.run_id }} -f docker/Dockerfile .
    - name: push docker image
      run: docker push ${{ env.DOCKER_SERVER }}/cloud-sdk-nightly:${{ github.run_id }}

  test:
    runs-on: [ self-hosted, small ]
    needs: [ build ]
    timeout-minutes: 1440

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.8'

    - name: install dependencies
      run: pip install -r .quali/requirements.txt

    - name: start reservation
      env:
        CLOUDSHELL_URL: quali-cloudshell.lab.wlan.tip.build
        CLOUDSHELL_USER: ${{ secrets.CLOUDSHELL_USER }}
        CLOUDSHELL_PASSWORD: ${{ secrets.CLOUDSHELL_PASSWORD }}
      run: |
        python .quali/start_reservation.py --global-inputs '{"Chart Version":"${{ github.event.inputs.openwifi_revision }}","AP Model":"${{ github.event.inputs.ap_model }}"}' "Basic Lab"

    - name: set reservation outputs
      if: always()
      id: reservation
      run: |
        echo "identifier=$(cat ./reservation_id.txt)" >> $GITHUB_OUTPUT
        echo "namespace=$(cat ./reservation_id.txt | cut -d "-" -f 1)" >> $GITHUB_OUTPUT

    - name: install JRE
      run: |
        sudo apt-get update
        sudo apt-get install -y default-jre

    - name: install aws CLI tool
      run: |
        curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
        unzip awscliv2.zip
        sudo ./aws/install

    - name: get EKS access credentials
      run: aws eks update-kubeconfig --name ${{ env.AWS_EKS_NAME }}

    - name: install kubectl
      run: |
        # TODO WIFI-7839 revert to using stable when issue is resolved on AWS CLI side
        curl -s -LO "https://dl.k8s.io/release/v1.27.6/bin/linux/amd64/kubectl"
        sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

    - name: prepare namespace
      id: namespace
      run: |
        NAMESPACE="testing-${{ github.run_id }}"
        echo "name=${NAMESPACE}" >> $GITHUB_OUTPUT

    - name: prepare configuration
      env:
        CLOUDSHELL_URL: quali-cloudshell.lab.wlan.tip.build
        CLOUDSHELL_USER: ${{ secrets.CLOUDSHELL_USER }}
        CLOUDSHELL_PASSWORD: ${{ secrets.CLOUDSHELL_PASSWORD }}
      run: |
        cat << EOF > lab_info.json
        ${{ secrets.LAB_CONFIGURATION_JSON }}
        EOF

        DYN_CONFIG="$(python .quali/get_configuration.py --json ${{ steps.reservation.outputs.identifier }})"
        jq ".CONFIGURATION.basic=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json

    - name: run tests
      uses: ./.github/actions/run-tests
      with:
        namespace: ${{ steps.namespace.outputs.name }}
        testbed: basic
        marker_expression: ${{ github.event.inputs.marker_expression }}
        configuration_file: "./lab_info.json"
        testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
        additional_args: '-o firmware="${{ github.event.inputs.firmware }}"'
        allure_results_artifact_name: allure-results
        dns_records_to_resolve: "sec-${{ steps.reservation.outputs.namespace }}.cicd.lab.wlan.tip.build gw-${{ steps.reservation.outputs.namespace }}.cicd.lab.wlan.tip.build fms-${{ steps.reservation.outputs.namespace }}.cicd.lab.wlan.tip.build"

    # necessary because if conditionals in composite actions are currently not respected
    - name: get tests logs
      if: always()
      continue-on-error: true
      run: |
        podname=$(kubectl get pods -n ${{ steps.namespace.outputs.name }} --no-headers -o custom-columns=":metadata.name" -l job-name=testing | sed "s/pod\///")
        kubectl logs --timestamps -n ${{ steps.namespace.outputs.name }} $podname || true

    - name: delete namespace
      if: always()
      continue-on-error: true
      run: kubectl delete ns --ignore-not-found=true --wait ${{ steps.namespace.outputs.name }}

    - name: stop reservation
      if: always()
      run: python .quali/stop_reservation.py ${{ steps.reservation.outputs.identifier }}
      env:
        CLOUDSHELL_URL: quali-cloudshell.lab.wlan.tip.build
        CLOUDSHELL_USER: ${{ secrets.CLOUDSHELL_USER }}
        CLOUDSHELL_PASSWORD: ${{ secrets.CLOUDSHELL_PASSWORD }}

    - name: generate Allure report
      uses: ./.github/actions/generate-allure-report
      with:
        results_path: ./allure-results

    - name: upload Allure report as artifact
      uses: actions/upload-artifact@v3
      with:
        name: allure-report
        path: allure-report

  cleanup:
    needs: [ test ]
    runs-on: ubuntu-latest
    if: always()
    steps:
    - name: cleanup Docker image
      run: curl -u${{ env.DOCKER_USER_NAME }}:${{ env.DOCKER_USER_PASSWORD }} -X DELETE "https://tip.jfrog.io/artifactory/tip-wlan-cloud-docker-repo/cloud-sdk-nightly/${{ github.run_id }}"
