name: interop testing

env:
  # AWS credentials
  AWS_EKS_NAME: tip-wlan-main
  AWS_DEFAULT_OUTPUT: json
  AWS_DEFAULT_REGION: us-east-2
  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:
      devices:
        description: comma-separated list of devices to test
        default: galaxy-s9,galaxy-s10,galaxy-s20,pixel-4,iphone-7,iphone-11,iphone-xr,iphone-12
        required: false
      marker_overwrite:
        description: overwrite the Pytest markers that will be selected, will disable report uploading
        required: false
  schedule:
    - cron: "30 20 * * *"

defaults:
  run:
    shell: bash

jobs:
  vars:
    runs-on: ubuntu-latest
    outputs:
      devices: ${{ steps.vars.outputs.devices }}
      marker_overwrite: ${{ steps.vars.outputs.marker_overwrite }}
      openwifi_revision: ${{ steps.vars.outputs.openwifi}}
      ap_models: ${{ steps.vars.outputs.ap_models}}
      ap_version: ${{ steps.vars.outputs.ap_version}}
      marker_expression: ${{ steps.vars.outputs.marker_expression }}
      existing_controller: ${{ steps.vars.outputs.existing_controller }}

    steps:
      - name: set variables
        id: vars
        run: |
          echo "::set-output name=marker_overwrite::${{ github.event.inputs.marker_overwrite || '' }}"

          DEVICES="${{ github.event.inputs.devices || 'galaxy-s9,galaxy-s10,galaxy-s20,pixel-4,iphone-7,iphone-11,iphone-xr,iphone-12' }}"
          DEVICES=$(echo $DEVICES | sed "s/,/\",\"/g" | sed 's/^/[\"/g' | sed 's/$/\"]/g')
          echo "::set-output name=devices::${DEVICES}"
          echo ::set-output name=openwifi::$(echo "${{ github.event.inputs.openwifi_revision || 'main' }}")
          echo ::set-output name=ap_models::$(echo "${{ github.event.inputs.ap_models || 'edgecore_ecw5410,edgecore_eap101,tp-link_ec420-g1,edgecore_ecw5211,cig_wf188n,edgecore_eap102,cig_wf194c,hfcl_ion4' }}")
          echo ::set-output name=ap_version::$(echo "${{ github.event.inputs.ap_version || 'next-latest' }}")
          echo ::set-output name=marker_expression::$(echo "${{ github.event.inputs.marker_expression || 'uc_sanity' }}")
          echo ::set-output name=existing_controller::$(echo "${{ github.event.inputs.existing_controller || 'qa01' }}")

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: build and push Docker image
        uses: ./.github/actions/build-and-push-docker
        with:
          registry: tip-tip-wlan-cloud-docker-repo.jfrog.io
          registry_user: wlan-testing-cicd
          registry_password: ${{ secrets.DOCKER_USER_PASSWORD }}

  # interop-01
  test-galaxy-s9:
    runs-on: ubuntu-latest
    needs: [vars, build]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'galaxy-s9')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
#          curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
#          unzip awscliv2.zip
#          sudo ./aws/install

      - 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 '{"Phone ID(s)":"1,2,5,6","AP ID(s)":"1,2,3,4","Optional Existing SDK Namespace":"${{ needs.vars.outputs.existing_controller }}","Include a LANForge?":"Yes","securityToken":"eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI3NzkzZGM0Ni1jZmU4LTQ4ODMtYjhiOS02ZWFlZGU2OTc2MDkifQ.eyJpYXQiOjE2MzI4Mzc2NDEsImp0aSI6IjAwZGRiYWY5LWQwYjMtNDRjNS1hYjVlLTkyNzFlNzc5ZGUzNiIsImlzcyI6Imh0dHBzOi8vYXV0aDIucGVyZmVjdG9tb2JpbGUuY29tL2F1dGgvcmVhbG1zL3RpcC1wZXJmZWN0b21vYmlsZS1jb20iLCJhdWQiOiJodHRwczovL2F1dGgyLnBlcmZlY3RvbW9iaWxlLmNvbS9hdXRoL3JlYWxtcy90aXAtcGVyZmVjdG9tb2JpbGUtY29tIiwic3ViIjoiODNkNjUxMWQtNTBmZS00ZWM5LThkNzAtYTA0ZjBkNTdiZDUyIiwidHlwIjoiT2ZmbGluZSIsImF6cCI6Im9mZmxpbmUtdG9rZW4tZ2VuZXJhdG9yIiwibm9uY2UiOiI2ZjE1YzYxNy01YTU5LTQyOWEtODc2Yi1jOTQxMTQ1ZDFkZTIiLCJzZXNzaW9uX3N0YXRlIjoiYmRjZTFmYTMtMjlkYi00MmFmLWI5YWMtYjZjZmJkMDEyOTFhIiwic2NvcGUiOiJvcGVuaWQgcHJvZmlsZSBvZmZsaW5lX2FjY2VzcyBlbWFpbCJ9.5R85_1R38ZFXv_wIjjCIsj8NJm1p66dCsLJI5DBEmks"}' --reservation-duration 480 "InterOp Lab - Specific Selection" 

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

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


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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."Galaxy S9".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi
          

      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-galaxy-s9
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and android and not wpa3_personal' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='Galaxy S9' --skip-lanforge"
          allure_results_artifact_name: allure-results-galaxy-s9


      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-galaxy-s9

  test-iphone-11:
    runs-on: ubuntu-latest
    needs: [vars, test-galaxy-s9]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'iphone-11')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
        #  unzip awscliv2.zip
        #  sudo ./aws/install

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


      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          

          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."iPhone-11".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi


      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-iphone-11
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and ios' }}"
          configuration_file: "$./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='iPhone-11' --skip-lanforge"
          allure_results_artifact_name: allure-results-iphone-11


      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-iphone-11

  # interop-02
  test-galaxy-s10:
    runs-on: ubuntu-latest
    needs: [vars, build]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'galaxy-s10')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  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 }}


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

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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."Galaxy S10.*".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi


      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-galaxy-s10
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and android' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='Galaxy S10.*' --skip-lanforge"
          allure_results_artifact_name: allure-results-galaxy-s10


      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-galaxy-s10

  test-iphone-7:
    runs-on: ubuntu-latest
    needs: [vars, test-galaxy-s10]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'iphone-7')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
        #  unzip awscliv2.zip
        #  sudo ./aws/install


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


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


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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."iPhone-7".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json

          

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi


      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-iphone-7
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and ios' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='iPhone-7' --skip-lanforge"
          allure_results_artifact_name: allure-results-iphone-7


      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-iphone-7

  # interop-03
  test-galaxy-s20:
    runs-on: ubuntu-latest
    needs: [vars, build]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'galaxy-s20')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  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 }}


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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."Galaxy S20".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi

      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-galaxy-s20
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and android' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='Galaxy S20' --skip-lanforge"
          allure_results_artifact_name: allure-results-galaxy-s20


      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-galaxy-s20

  test-iphone-xr:
    runs-on: ubuntu-latest
    needs: [vars, test-galaxy-s20]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'iphone-xr')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  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 }}

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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."iPhone-XR".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi


      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-iphone-xr
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and ios' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='iPhone-XR' --skip-lanforge"
          allure_results_artifact_name: allure-results-iphone-xr



      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-iphone-xr

  #interop-04
  test-pixel-4:
    runs-on: ubuntu-latest
    needs: [vars, build]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'pixel-4')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  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 }}

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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."Pixel 4".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json

          

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi



      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-pixel-4
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and android' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='Pixel 4' --skip-lanforge"
          allure_results_artifact_name: allure-results-pixel-4


      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-pixel-4

  test-iphone-12:
    runs-on: ubuntu-latest
    needs: [vars, test-pixel-4]
    if: "!cancelled() && contains(fromJSON(needs.vars.outputs.devices), 'iphone-12')"
    steps:
      - uses: actions/checkout@v2

      - name: install dependencies
        run: |
          pip install -r .quali/requirements.txt
        #  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 }}

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

      - 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.interop=$DYN_CONFIG" lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          
          jq --arg git_run "${{ github.run_number }}" '.PERFECTO_DETAILS."iPhone-12".jobNumber=$git_run' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json

          if [ ${{ needs.vars.outputs.existing_controller }} != '' ]
          then
            jq '.CONFIGURATION.interop.controller.url="https://sec-${{ needs.vars.outputs.existing_controller }}.cicd.lab.wlan.tip.build:16001"' lab_info.json > lab_info.json.tmp && mv lab_info.json.tmp lab_info.json
          fi


      - name: run tests
        uses: ./.github/actions/run-tests
        with:
          namespace: interop-${{ github.run_id }}-iphone-12
          testbed: interop
          marker_expression: "${{ needs.vars.outputs.marker_overwrite || 'interop_uc_sanity and client_connect and ios' }}"
          configuration_file: "./lab_info.json"
          testing_docker_image: tip-tip-wlan-cloud-docker-repo.jfrog.io/cloud-sdk-nightly:${{ github.run_id }}
          additional_args: "--device='iPhone-12' --skip-lanforge"
          allure_results_artifact_name: allure-results-iphone-12

      - 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 }}

      # necessary because if conditionals in composite actions are currently not respected
      - name: delete namespace
        if: always()
        continue-on-error: true
        run: kubectl delete ns --ignore-not-found=true --wait interop-${{ github.run_id }}-iphone-12

  report:
    needs: [vars, test-iphone-11, test-iphone-xr, test-iphone-12, test-iphone-7]
    if: "!cancelled()"
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        device: ${{ fromJson( needs.vars.outputs.devices ) }}

    steps:
      - name: checkout testing repo
        uses: actions/checkout@v2

      - uses: actions/download-artifact@v2
        with:
          name: allure-results-${{ matrix.device }}
          path: allure-results

      - name: download history of previous run
        continue-on-error: true
        run: |
          LAST_RUN_ID=$(aws s3api head-object --bucket openwifi-allure-reports --key interop/${{ matrix.device }}/latest/index.html | jq -r .Metadata.latest)
          aws s3 cp --recursive s3://openwifi-allure-reports/interop/${{ matrix.device }}/$LAST_RUN_ID/history history

      - name: generate Allure report
        uses: ./.github/actions/generate-allure-report
        with:
          results_path: ./allure-results
          history_path: ./history
          additional_metadata: |
            Device=${{ matrix.device }}

      - name: upload Allure report as artifact
        uses: actions/upload-artifact@v2
        with:
          name: allure-report-${{ matrix.device }}
          path: allure-report

      # doing this to be able to aggregate multiple reports together later on
      - name: copy results into report
        run: cp -r allure-results allure-report/results

      - name: upload to S3
        if: github.ref == 'refs/heads/master' && needs.vars.outputs.marker_overwrite == ''
        uses: ./.github/actions/allure-report-to-s3
        with:
          test_type: interop
          testbed: ${{ matrix.device }}
          report_path: allure-report
          s3_access_key_id: ${{ secrets.ALLURE_S3_ACCESS_KEY_ID }}
          s3_access_key_secret: ${{ secrets.ALLURE_S3_ACCESS_KEY_SECRET }}

  cleanup:
    needs: [test-iphone-11, test-iphone-xr, test-iphone-12, test-iphone-7]
    runs-on: ubuntu-latest
    if: always()
    steps:
      - uses: actions/checkout@v2
      - name: cleanup Docker image
        uses: ./.github/actions/cleanup-docker
        with:
          registry_user: wlan-testing-cicd
          registry_password: ${{ secrets.DOCKER_USER_PASSWORD }}
