# USER_Target
#   "" / all / ALL: main repo and all extensions
#   main: main repo
#   extensions: ALL extensions
#   vm ext-account: certain targets

name: CLI TEST RUN ${{ parameters.userTarget }} ${{ parameters.userLive }} ${{ parameters.userUsername }} $(Date:yyyyMMdd)$(Rev:.r)

trigger:
  branches:
    exclude:
    - '*'

variables:
- template: ${{ variables.Pipeline.Workspace }}/.azure-pipelines/templates/variables.yml

parameters:
- name: maxParallel
  displayName: Max Parallel Count
  type: number
  default: 5
- name: pythonVersion
  displayName: Python Version
  type: string
  default: '3.12'
- name: staticWebUrl
  displayName: Static Web URL
  type: string
  default: 'https://clilivetestsa.z13.web.core.windows.net/'
- name: userBranch
  displayName: User Branch
  type: string
  default: 'live-test'
- name: userBranchExt
  displayName: User Branch for Extensions
  type: string
  default: 'main'
- name: userLive
  displayName: User Live Test Flag
  type: string
  default: '--live'
- name: userParallelism
  displayName: User Parallelism
  type: number
  default: 8
- name: userRepo
  displayName: User Repo
  type: string
  default: 'https://github.com/Azure/azure-cli.git'
- name: userRepoExt
  displayName: User Repo for Extensions
  type: string
  default: 'https://github.com/Azure/azure-cli-extensions.git'
- name: userTarget
  displayName: User Target
  type: string
  default: 'all'
- name: userToken
  displayName: User Token
  type: string
  default: ' '
- name: userUsername
  displayName: User Username
  type: string
  default: ' '

#schedules:
#- cron: "0 18 * * 5"
#  displayName: Weekly Friday test
#  branches:
#    include:
#    - CLITest
#  always: true

stages:
# Azure DevOps limits the number of jobs to 256, so it must be split according to the platform and placed in different stages.
- stage: TestUbuntu
  displayName: Live Test on Ubuntu latest
  condition: succeededOrFailed()
  jobs:
  - job: LiveTest
    displayName: Live Test
    # Sometimes the live test will be stuck forever, so the timeout cannot be set to 0, it is recommended to set it to twice the maximum time
    timeoutInMinutes: 1200
    strategy:
      # If the maxParallel is too large, the resource group will reach the limit of 980, so it is recommended that the maxParallel should not exceed 30.
      maxParallel: ${{ parameters.maxParallel }}
      matrix:
        acr:
          Target: acr
        acs:
          Target: acs
        advisor:
          Target: advisor
        ams:
          Target: ams
        apim:
          Target: apim
        appconfig:
          Target: appconfig
        appservice:
          Target: appservice
        aro:
          Target: aro
        backup:
          Target: backup
        batch:
          Target: batch
        batchai:
          Target: batchai
        billing:
          Target: billing
        botservice:
          Target: botservice
        cdn:
          Target: cdn
        cloud:
          Target: cloud
        cognitiveservices:
          Target: cognitiveservices
        config:
          Target: config
        configure:
          Target: configure
        consumption:
          Target: consumption
        container:
          Target: container
        cosmosdb:
          Target: cosmosdb
        databoxedge:
          Target: databoxedge
        #deploymentmanager:
        #  Target: deploymentmanager
        dla:
          Target: dla
        dls:
          Target: dls
        dms:
          Target: dms
        eventgrid:
          Target: eventgrid
        eventhubs:
          Target: eventhubs
        extension:
          Target: extension
        feedback:
          Target: feedback
        find:
          Target: find
        hdinsight:
          Target: hdinsight
        identity:
          Target: identity
        iot:
          Target: iot
        keyvault:
          Target: keyvault
        kusto:
          Target: kusto
        lab:
          Target: lab
        managedservices:
          Target: managedservices
        maps:
          Target: maps
        marketplaceordering:
          Target: marketplaceordering
        monitor:
          Target: monitor
        mysql:
          Target: mysql
        #natgateway:
        #  Target: natgateway
        netappfiles:
          Target: netappfiles
        network:
          Target: network
        policyinsights:
          Target: policyinsights
        postgresql:
          Target: postgresql
        privatedns:
          Target: privatedns
        profile:
          Target: profile
        rdbms:
          Target: rdbms
        redis:
          Target: redis
        relay:
          Target: relay
        resource:
          Target: resource
        role:
          Target: role
        search:
          Target: search
        security:
          Target: security
        servicebus:
          Target: servicebus
        serviceconnector:
          Target: serviceconnector
        servicefabric:
          Target: servicefabric
        signalr:
          Target: signalr
        sql:
          Target: sql
        sqlvm:
          Target: sqlvm
        storage:
          Target: storage
        synapse:
          Target: synapse
        util:
          Target: util
        vm:
          Target: vm
        ext-account:
          Target: ext-account
        ext-acrquery:
          Target: ext-acrquery
        ext-acrtransfer:
          Target: ext-acrtransfer
        ext-ad:
          Target: ext-ad
        ext-adp:
          Target: ext-adp
        ext-aem:
          Target: ext-aem
        ext-ai-examples:
          Target: ext-ai-examples
        ext-aks-preview:
          Target: ext-aks-preview
        ext-alertsmanagement:
          Target: ext-alertsmanagement
        #ext-alias:
        #  Target: ext-alias
        ext-amg:
          Target: ext-amg
        ext-application-insights:
          Target: ext-application-insights
        ext-appservice-kube:
          Target: ext-appservice-kube
        #ext-attestation:
        #  Target: ext-attestation
        ext-authV2:
          Target: ext-authV2
        ext-automanage:
          Target: ext-automanage
        ext-automation:
          Target: ext-automation
        ext-azure-firewall:
          Target: ext-azure-firewall
        ext-bastion:
          Target: ext-bastion
        ext-billing-benefits:
          Target: ext-billing-benefits
        ext-blockchain:
          Target: ext-blockchain
        ext-blueprint:
          Target: ext-blueprint
        ext-change-analysis:
          Target: ext-change-analysis
        ext-cli-translator:
          Target: ext-cli-translator
        #ext-cloudservice:
        #  Target: ext-cloudservice
        ext-communication:
          Target: ext-communication
        ext-confcom:
          Target: ext-confcom
        ext-confidentialledger:
          Target: ext-confidentialledger
        ext-confluent:
          Target: ext-confluent
        ext-connectedk8s:
          Target: ext-connectedk8s
        ext-connectedmachine:
          Target: ext-connectedmachine
        ext-connectedvmware:
          Target: ext-connectedvmware
        ext-connection-monitor-preview:
          Target: ext-connection-monitor-preview
        ext-containerapp:
          Target: ext-containerapp
        #ext-containerapp-preview:
        #  Target: ext-containerapp-preview
        ext-cosmosdb-preview:
          Target: ext-cosmosdb-preview
        ext-costmanagement:
          Target: ext-costmanagement
        ext-custom-providers:
          Target: ext-custom-providers
        ext-databox:
          Target: ext-databox
        ext-databricks:
          Target: ext-databricks
        ext-datadog:
          Target: ext-datadog
        ext-datafactory:
          Target: ext-datafactory
        ext-datamigration:
          Target: ext-datamigration
        ext-dataprotection:
          Target: ext-dataprotection
        ext-datashare:
          Target: ext-datashare
        ext-db-up:
          Target: ext-db-up
        ext-desktopvirtualization:
          Target: ext-desktopvirtualization
        #ext-dev-spaces:
        #  Target: ext-dev-spaces
        ext-devcenter:
          Target: ext-devcenter
        ext-diskpool:
          Target: ext-diskpool
        ext-dms-preview:
          Target: ext-dms-preview
        ext-dnc:
          Target: ext-dnc
        ext-dns-resolver:
          Target: ext-dns-resolver
        ext-dynatrace:
          Target: ext-dynatrace
        ext-edgeorder:
          Target: ext-edgeorder
        ext-elastic:
          Target: ext-elastic
        ext-elastic-san:
          Target: ext-elastic-san
        #ext-eventgrid:
        #  Target: ext-eventgrid
        #ext-express-route-cross-connection:
        #  Target: ext-express-route-cross-connection
        ext-fleet:
          Target: ext-fleet
        ext-fluid-relay:
          Target: ext-fluid-relay
        ext-footprint:
          Target: ext-footprint
        ext-front-door:
          Target: ext-front-door
        #ext-functionapp:
        #  Target: ext-functionapp
        ext-guestconfig:
          Target: ext-guestconfig
        #ext-hack:
        #  Target: ext-hack
        ext-hardware-security-modules:
          Target: ext-hardware-security-modules
        ext-healthbot:
          Target: ext-healthbot
        ext-healthcareapis:
          Target: ext-healthcareapis
        ext-hpc-cache:
          Target: ext-hpc-cache
        #ext-image-copy-extension:
        #  Target: ext-image-copy-extension
        ext-image-gallery:
          Target: ext-image-gallery
        ext-import-export:
          Target: ext-import-export
        ext-init:
          Target: ext-init
        ext-interactive:
          Target: ext-interactive
        ext-internet-analyzer:
          Target: ext-internet-analyzer
        ext-ip-group:
          Target: ext-ip-group
        ext-k8s-configuration:
          Target: ext-k8s-configuration
        ext-k8s-extension:
          Target: ext-k8s-extension
        #ext-kusto:
        #  Target: ext-kusto
        ext-load:
          Target: ext-load
        ext-log-analytics:
          Target: ext-log-analytics
        ext-log-analytics-solution:
          Target: ext-log-analytics-solution
        ext-logic:
          Target: ext-logic
        ext-logz:
          Target: ext-logz
        ext-maintenance:
          Target: ext-maintenance
        ext-managedccfs:
          Target: ext-managedccfs
        ext-managementpartner:
          Target: ext-managementpartner
        ext-mesh:
          Target: ext-mesh
        ext-mixed-reality:
          Target: ext-mixed-reality
        ext-mobile-network:
          Target: ext-mobile-network
        ext-monitor-control-service:
          Target: ext-monitor-control-service
        ext-netappfiles-preview:
          Target: ext-netappfiles-preview
        ext-network-manager:
          Target: ext-network-manager
        ext-next:
          Target: ext-next
        ext-nginx:
          Target: ext-nginx
        ext-notification-hub:
          Target: ext-notification-hub
        ext-nsp:
          Target: ext-nsp
        ext-offazure:
          Target: ext-offazure
        ext-orbital:
          Target: ext-orbital
        ext-peering:
          Target: ext-peering
        ext-portal:
          Target: ext-portal
        ext-powerbidedicated:
          Target: ext-powerbidedicated
        ext-providerhub:
          Target: ext-providerhub
        ext-purview:
          Target: ext-purview
        ext-quantum:
          Target: ext-quantum
        ext-quota:
          Target: ext-quota
        #ext-rdbms-connect:
        #  Target: ext-rdbms-connect
        ext-redisenterprise:
          Target: ext-redisenterprise
        ext-reservation:
          Target: ext-reservation
        ext-resource-graph:
          Target: ext-resource-graph
        ext-resource-mover:
          Target: ext-resource-mover
        ext-scenario-guide:
          Target: ext-scenario-guide
        ext-scheduled-query:
          Target: ext-scheduled-query
        ext-scvmm:
          Target: ext-scvmm
        ext-securityinsight:
          Target: ext-securityinsight
        ext-self-help:
          Target: ext-self-help
        ext-serial-console:
          Target: ext-serial-console
        ext-serviceconnector-passwordless:
          Target: ext-serviceconnector-passwordless
        ext-spring:
          Target: ext-spring
        ext-spring-cloud:
          Target: ext-spring-cloud
        ext-ssh:
          Target: ext-ssh
        ext-stack-hci:
          Target: ext-stack-hci
        ext-staticwebapp:
          Target: ext-staticwebapp
        ext-storage-blob-preview:
          Target: ext-storage-blob-preview
        ext-storage-mover:
          Target: ext-storage-mover
        ext-storage-preview:
          Target: ext-storage-preview
        ext-storagesync:
          Target: ext-storagesync
        ext-stream-analytics:
          Target: ext-stream-analytics
        ext-subscription:
          Target: ext-subscription
        ext-support:
          Target: ext-support
        ext-swiftlet:
          Target: ext-swiftlet
        ext-timeseriesinsights:
          Target: ext-timeseriesinsights
        ext-traffic-collector:
          Target: ext-traffic-collector
        #ext-virtual-network-tap:
        #  Target: ext-virtual-network-tap
        ext-virtual-wan:
          Target: ext-virtual-wan
        ext-vm-repair:
          Target: ext-vm-repair
        ext-vmware:
          Target: ext-vmware
        ext-voice-service:
          Target: ext-voice-service
        #ext-webapp:
        #  Target: ext-webapp
        ext-webpubsub:
          Target: ext-webpubsub
    pool:
      name: ${{ variables.ubuntu_pool }}
    steps:
      - bash: |
          if [[ "${{ parameters.userTarget }}" =~ "$(Target)" || ("${{ parameters.userTarget }}" == "main" && ! "$(Target)" =~ ^ext-.*) || ("${{ parameters.userTarget }}" == "extensions" && "$(Target)" =~ ^ext-.*) || ("${{ parameters.userTarget }}" == "ALL" || "${{ parameters.userTarget }}" == "all" || "${{ parameters.userTarget }}" == "") ]]; then
            echo "Match!"
            echo "##vso[task.setvariable variable=Match]1"
            echo "##vso[task.setvariable variable=platform]ubuntulatest"
          else
            echo "Not match!"
            echo "##vso[task.setvariable variable=Match]0"
          fi
      - task: UsePythonVersion@0
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          versionSpec: '${{ parameters.pythonVersion }}'
          addToPath: true
          architecture: 'x64'
      - task: AzureCLI@2
        displayName: 'Run live test'
        inputs:
          connectedServiceNameARM: $(azure-cli-live-test-bami-connected-service)
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            cd ..
            pwd
            workDir=$(pwd)
            echo "##vso[task.setvariable variable=workDir]$workDir"
            ls
            # ssh-keygen -f ~/.ssh/id_rsa -t rsa -N ''
            git config --global user.email "AzurePipelines"
            git config --global user.name "AzurePipelines"
    
            # print variables
            echo "PLATFORM: $(PLATFORM)"
            echo "USER_TARGET: ${{ parameters.userTarget }}"
            echo "USER_REPO: ${{ parameters.userRepo }}"
            echo "USER_REPO_EXT: ${{ parameters.userRepoExt }}"
            echo "USER_BRANCH: ${{ parameters.userBranch }}"
            echo "USER_BRANCH_EXT: ${{ parameters.userBranchExt }}"
            echo "USER_USERNAME: ${{ parameters.userUsername }}"
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " ]]; then
              REPO="${{ parameters.userRepo }}"
              EXT_REPO="${{ parameters.userRepoExt }}"
              # Pass username and token so that we can commit changes
              git clone -b ${{ parameters.userBranch }} ${REPO:0:8}${{ parameters.userUsername }}:${{ parameters.userToken }}@${REPO:8}
              git clone -b ${{ parameters.userBranchExt }} ${EXT_REPO:0:8}${{ parameters.userUsername }}:${{ parameters.userToken }}@${EXT_REPO:8}
            else
              git clone -b ${{ parameters.userBranch }} ${{ parameters.userRepo }}
              git clone -b ${{ parameters.userBranchExt }} ${{ parameters.userRepoExt }}
            fi
    
            python -m venv env
            source env/bin/activate
    
            # TODO: support pytest-html >= 4.0.0
            pip install azdev pytest-json-report pytest-html==3.2.0 pytest-rerunfailures
    
            azdev setup -c azure-cli -r azure-cli-extensions
            
            # Remove ext- prefix
            FinalTarget="$(Target)"
            if [[ "${FinalTarget}" =~ ^ext-.* ]]; then
              echo "It is an extention"
              echo $(Target)
              FinalTarget=${FinalTarget:4}
              azdev extension add ${FinalTarget}
            fi
            echo ${FinalTarget}
            
            serial_modules="appservice botservice cloud network azure-cli-core azure-cli-telemetry"
    
            # This helps detect issues in CI if a used SDK API version is deleted by the below script.
            python $workDir/s/scripts/trim_sdk.py
    
            # Compact aaz folders of modules
            python $workDir/s/scripts/compact_aaz.py
    
            az -v
            az account set -s $(azure-cli-live-test-bami-sub-id)
    
            # Clean policy
            python $workDir/s/scripts/live_test/clean_policy.py
            
            echo "Run tests"
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " ]]; then
              echo "Commit mode"
              azdev test ${FinalTarget} --no-exitfirst -a "-n ${{ parameters.userParallelism }}"
              azdev test ${FinalTarget} --live --lf --xml-path test_results.parallel.xml --no-exitfirst -a "-n ${{ parameters.userParallelism }} --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.parallel.json --html=$(Target).$(platform).report.parallel.html --self-contained-html --capture=sys"
            elif [[ "$serial_modules" =~ "$FinalTarget" ]]; then
              echo "Series mode"
              azdev test ${FinalTarget} ${{ parameters.userLive }} --xml-path test_results.sequential.xml --no-exitfirst -a "-n 1 --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.sequential.json --html=$(Target).$(platform).report.sequential.html --self-contained-html --capture=sys"
            else
              echo "Normal mode"
              # Sequential
              azdev test ${FinalTarget} ${{ parameters.userLive }} --mark serial --xml-path test_results.sequential.xml --no-exitfirst -a "-n 1 --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.sequential.json --html=$(Target).$(platform).report.sequential.html --self-contained-html --capture=sys"
              # Parallel
              azdev test ${FinalTarget} ${{ parameters.userLive }} --mark "not serial" --xml-path test_results.parallel.xml --no-exitfirst -a "-n ${{ parameters.userParallelism }} --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.parallel.json --html=$(Target).$(platform).report.parallel.html --self-contained-html --capture=sys"
            fi
    
            pwd
            ls
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " && ! "${{ parameters.userTarget }}" =~ ^ext-.* && "${{ parameters.userTarget }}" != "extensions" && "${{ parameters.userTarget }}" != ""  && "${{ parameters.userTarget }}" != "all" && "${{ parameters.userTarget }}" != "ALL" ]]; then
              cd azure-cli
              git status
              git add .
              git commit -m "Upload recording files"
              git push origin ${{ parameters.userBranch }}
            elif [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " && "${{ parameters.userTarget }}" =~ ^ext-.* ]]; then
              cd azure-cli-extensions
              git status
              git add .
              git commit -m "Upload recording files"
              git push origin ${{ parameters.userBranchExt }}
            fi
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
      - task: PublishTestResults@2
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          testResultsFiles: '$(workDir)/test_results.*.xml'
          testRunTitle: 'Azure CLI live test results of $(Target)'
          mergeTestResults: true
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.sequential.json'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.sequential.html'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.parallel.json'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.parallel.html'
          ArtifactName: 'livetest'
          publishLocation: 'Container'

- stage: TestMacos
  displayName: Live Test on Macos 12
  condition: succeededOrFailed()
  jobs:
  - job: LiveTest
    displayName: Live Test
    # Sometimes the live test will be stuck forever, so the timeout cannot be set to 0, it is recommended to set it to twice the maximum time
    # For 360 minutes (6 hours) on Microsoft-hosted agents with a public project and public repository
    timeoutInMinutes: 1200
    strategy:
      # If the maxParallel is too large, the resource group will reach the limit of 980, so it is recommended that the maxParallel should not exceed 30.
      maxParallel: ${{ parameters.maxParallel }}
      matrix:
        acr:
          Target: acr
        acs:
          Target: acs
        advisor:
          Target: advisor
        ams:
          Target: ams
        apim:
          Target: apim
        appconfig:
          Target: appconfig
        appservice:
          Target: appservice
        aro:
          Target: aro
        backup:
          Target: backup
        batch:
          Target: batch
        batchai:
          Target: batchai
        billing:
          Target: billing
        botservice:
          Target: botservice
        cdn:
          Target: cdn
        cloud:
          Target: cloud
        cognitiveservices:
          Target: cognitiveservices
        config:
          Target: config
        configure:
          Target: configure
        consumption:
          Target: consumption
        container:
          Target: container
        cosmosdb:
          Target: cosmosdb
        databoxedge:
          Target: databoxedge
        #deploymentmanager:
        #  Target: deploymentmanager
        dla:
          Target: dla
        dls:
          Target: dls
        dms:
          Target: dms
        eventgrid:
          Target: eventgrid
        eventhubs:
          Target: eventhubs
        extension:
          Target: extension
        feedback:
          Target: feedback
        find:
          Target: find
        hdinsight:
          Target: hdinsight
        identity:
          Target: identity
        iot:
          Target: iot
        keyvault:
          Target: keyvault
        kusto:
          Target: kusto
        lab:
          Target: lab
        managedservices:
          Target: managedservices
        maps:
          Target: maps
        marketplaceordering:
          Target: marketplaceordering
        monitor:
          Target: monitor
        mysql:
          Target: mysql
        #natgateway:
        #  Target: natgateway
        netappfiles:
          Target: netappfiles
        network:
          Target: network
        policyinsights:
          Target: policyinsights
        postgresql:
          Target: postgresql
        privatedns:
          Target: privatedns
        profile:
          Target: profile
        rdbms:
          Target: rdbms
        redis:
          Target: redis
        relay:
          Target: relay
        resource:
          Target: resource
        role:
          Target: role
        search:
          Target: search
        security:
          Target: security
        servicebus:
          Target: servicebus
        serviceconnector:
          Target: serviceconnector
        servicefabric:
          Target: servicefabric
        signalr:
          Target: signalr
        sql:
          Target: sql
        sqlvm:
          Target: sqlvm
        storage:
          Target: storage
        synapse:
          Target: synapse
        util:
          Target: util
        vm:
          Target: vm
        ext-account:
          Target: ext-account
        ext-acrquery:
          Target: ext-acrquery
        ext-acrtransfer:
          Target: ext-acrtransfer
        ext-ad:
          Target: ext-ad
        ext-adp:
          Target: ext-adp
        ext-aem:
          Target: ext-aem
        ext-ai-examples:
          Target: ext-ai-examples
        ext-aks-preview:
          Target: ext-aks-preview
        ext-alertsmanagement:
          Target: ext-alertsmanagement
        #ext-alias:
        #  Target: ext-alias
        ext-amg:
          Target: ext-amg
        ext-application-insights:
          Target: ext-application-insights
        ext-appservice-kube:
          Target: ext-appservice-kube
        #ext-attestation:
        #  Target: ext-attestation
        ext-authV2:
          Target: ext-authV2
        ext-automanage:
          Target: ext-automanage
        ext-automation:
          Target: ext-automation
        ext-azure-firewall:
          Target: ext-azure-firewall
        ext-bastion:
          Target: ext-bastion
        ext-billing-benefits:
          Target: ext-billing-benefits
        ext-blockchain:
          Target: ext-blockchain
        ext-blueprint:
          Target: ext-blueprint
        ext-change-analysis:
          Target: ext-change-analysis
        ext-cli-translator:
          Target: ext-cli-translator
        #ext-cloudservice:
        #  Target: ext-cloudservice
        ext-communication:
          Target: ext-communication
        ext-confcom:
          Target: ext-confcom
        ext-confidentialledger:
          Target: ext-confidentialledger
        ext-confluent:
          Target: ext-confluent
        ext-connectedk8s:
          Target: ext-connectedk8s
        ext-connectedmachine:
          Target: ext-connectedmachine
        ext-connectedvmware:
          Target: ext-connectedvmware
        ext-connection-monitor-preview:
          Target: ext-connection-monitor-preview
        ext-containerapp:
          Target: ext-containerapp
        #ext-containerapp-preview:
        #  Target: ext-containerapp-preview
        ext-cosmosdb-preview:
          Target: ext-cosmosdb-preview
        ext-costmanagement:
          Target: ext-costmanagement
        ext-custom-providers:
          Target: ext-custom-providers
        ext-databox:
          Target: ext-databox
        ext-databricks:
          Target: ext-databricks
        ext-datadog:
          Target: ext-datadog
        ext-datafactory:
          Target: ext-datafactory
        ext-datamigration:
          Target: ext-datamigration
        ext-dataprotection:
          Target: ext-dataprotection
        ext-datashare:
          Target: ext-datashare
        ext-db-up:
          Target: ext-db-up
        ext-desktopvirtualization:
          Target: ext-desktopvirtualization
        #ext-dev-spaces:
        #  Target: ext-dev-spaces
        ext-devcenter:
          Target: ext-devcenter
        ext-diskpool:
          Target: ext-diskpool
        ext-dms-preview:
          Target: ext-dms-preview
        ext-dnc:
          Target: ext-dnc
        ext-dns-resolver:
          Target: ext-dns-resolver
        ext-dynatrace:
          Target: ext-dynatrace
        ext-edgeorder:
          Target: ext-edgeorder
        ext-elastic:
          Target: ext-elastic
        ext-elastic-san:
          Target: ext-elastic-san
        #ext-eventgrid:
        #  Target: ext-eventgrid
        #ext-express-route-cross-connection:
        #  Target: ext-express-route-cross-connection
        ext-fleet:
          Target: ext-fleet
        ext-fluid-relay:
          Target: ext-fluid-relay
        ext-footprint:
          Target: ext-footprint
        ext-front-door:
          Target: ext-front-door
        #ext-functionapp:
        #  Target: ext-functionapp
        ext-guestconfig:
          Target: ext-guestconfig
        #ext-hack:
        #  Target: ext-hack
        ext-hardware-security-modules:
          Target: ext-hardware-security-modules
        ext-healthbot:
          Target: ext-healthbot
        ext-healthcareapis:
          Target: ext-healthcareapis
        ext-hpc-cache:
          Target: ext-hpc-cache
        #ext-image-copy-extension:
        #  Target: ext-image-copy-extension
        ext-image-gallery:
          Target: ext-image-gallery
        ext-import-export:
          Target: ext-import-export
        ext-init:
          Target: ext-init
        ext-interactive:
          Target: ext-interactive
        ext-internet-analyzer:
          Target: ext-internet-analyzer
        ext-ip-group:
          Target: ext-ip-group
        ext-k8s-configuration:
          Target: ext-k8s-configuration
        ext-k8s-extension:
          Target: ext-k8s-extension
        #ext-kusto:
        #  Target: ext-kusto
        ext-load:
          Target: ext-load
        ext-log-analytics:
          Target: ext-log-analytics
        ext-log-analytics-solution:
          Target: ext-log-analytics-solution
        ext-logic:
          Target: ext-logic
        ext-logz:
          Target: ext-logz
        ext-maintenance:
          Target: ext-maintenance
        ext-managedccfs:
          Target: ext-managedccfs
        ext-managementpartner:
          Target: ext-managementpartner
        ext-mesh:
          Target: ext-mesh
        ext-mixed-reality:
          Target: ext-mixed-reality
        ext-mobile-network:
          Target: ext-mobile-network
        ext-monitor-control-service:
          Target: ext-monitor-control-service
        ext-netappfiles-preview:
          Target: ext-netappfiles-preview
        ext-network-manager:
          Target: ext-network-manager
        ext-next:
          Target: ext-next
        ext-nginx:
          Target: ext-nginx
        ext-notification-hub:
          Target: ext-notification-hub
        ext-nsp:
          Target: ext-nsp
        ext-offazure:
          Target: ext-offazure
        ext-orbital:
          Target: ext-orbital
        ext-peering:
          Target: ext-peering
        ext-portal:
          Target: ext-portal
        ext-powerbidedicated:
          Target: ext-powerbidedicated
        ext-providerhub:
          Target: ext-providerhub
        ext-purview:
          Target: ext-purview
        ext-quantum:
          Target: ext-quantum
        ext-quota:
          Target: ext-quota
        #ext-rdbms-connect:
        #  Target: ext-rdbms-connect
        ext-redisenterprise:
          Target: ext-redisenterprise
        ext-reservation:
          Target: ext-reservation
        ext-resource-graph:
          Target: ext-resource-graph
        ext-resource-mover:
          Target: ext-resource-mover
        ext-scenario-guide:
          Target: ext-scenario-guide
        ext-scheduled-query:
          Target: ext-scheduled-query
        ext-scvmm:
          Target: ext-scvmm
        ext-securityinsight:
          Target: ext-securityinsight
        ext-self-help:
          Target: ext-self-help
        ext-serial-console:
          Target: ext-serial-console
        ext-serviceconnector-passwordless:
          Target: ext-serviceconnector-passwordless
        ext-spring:
          Target: ext-spring
        ext-spring-cloud:
          Target: ext-spring-cloud
        ext-ssh:
          Target: ext-ssh
        ext-stack-hci:
          Target: ext-stack-hci
        ext-staticwebapp:
          Target: ext-staticwebapp
        ext-storage-blob-preview:
          Target: ext-storage-blob-preview
        ext-storage-mover:
          Target: ext-storage-mover
        ext-storage-preview:
          Target: ext-storage-preview
        ext-storagesync:
          Target: ext-storagesync
        ext-stream-analytics:
          Target: ext-stream-analytics
        ext-subscription:
          Target: ext-subscription
        ext-support:
          Target: ext-support
        ext-swiftlet:
          Target: ext-swiftlet
        ext-timeseriesinsights:
          Target: ext-timeseriesinsights
        ext-traffic-collector:
          Target: ext-traffic-collector
        #ext-virtual-network-tap:
        #  Target: ext-virtual-network-tap
        ext-virtual-wan:
          Target: ext-virtual-wan
        ext-vm-repair:
          Target: ext-vm-repair
        ext-vmware:
          Target: ext-vmware
        ext-voice-service:
          Target: ext-voice-service
        #ext-webapp:
        #  Target: ext-webapp
        ext-webpubsub:
          Target: ext-webpubsub
    pool:
      vmImage: ${{ variables.macos_pool }}
    steps:
      - bash: |
          if [[ "${{ parameters.userTarget }}" =~ "$(Target)" || ("${{ parameters.userTarget }}" == "main" && ! "$(Target)" =~ ^ext-.*) || ("${{ parameters.userTarget }}" == "extensions" && "$(Target)" =~ ^ext-.*) || ("${{ parameters.userTarget }}" == "ALL" || "${{ parameters.userTarget }}" == "all" || "${{ parameters.userTarget }}" == "") ]]; then
            echo "Match!"
            echo "##vso[task.setvariable variable=Match]1"
            echo "##vso[task.setvariable variable=platform]macos12"
          else
            echo "Not match!"
            echo "##vso[task.setvariable variable=Match]0"
          fi
      - task: UsePythonVersion@0
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          versionSpec: '${{ parameters.pythonVersion }}'
          addToPath: true
          architecture: 'x64'
      - task: AzureCLI@2
        displayName: 'Run live test on Macos'
        inputs:
          connectedServiceNameARM: $(azure-cli-live-test-bami-connected-service)
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            cd ..
            pwd
            workDir=$(pwd)
            echo "##vso[task.setvariable variable=workDir]$workDir"
            ls
            # ssh-keygen -f ~/.ssh/id_rsa -t rsa -N ''
            git config --global user.email "AzurePipelines"
            git config --global user.name "AzurePipelines"
    
            # print variables
            echo "PLATFORM: $(PLATFORM)"
            echo "USER_TARGET: ${{ parameters.userTarget }}"
            echo "USER_REPO: ${{ parameters.userRepo }}"
            echo "USER_REPO_EXT: ${{ parameters.userRepoExt }}"
            echo "USER_BRANCH: ${{ parameters.userBranch }}"
            echo "USER_BRANCH_EXT: ${{ parameters.userBranchExt }}"
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " ]]; then
              REPO="${{ parameters.userRepo }}"
              EXT_REPO="${{ parameters.userRepoExt }}"
              # Pass username and token so that we can commit changes
              git clone -b ${{ parameters.userBranch }} ${REPO:0:8}${{ parameters.userUsername }}:${{ parameters.userToken }}@${REPO:8}
              git clone -b ${{ parameters.userBranchExt }} ${EXT_REPO:0:8}${{ parameters.userUsername }}:${{ parameters.userToken }}@${EXT_REPO:8}
            else
              git clone -b ${{ parameters.userBranch }} ${{ parameters.userRepo }}
              git clone -b ${{ parameters.userBranchExt }} ${{ parameters.userRepoExt }}
            fi
    
            python -m venv env
            source env/bin/activate
            export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
            
            # TODO: support pytest-html >= 4.0.0
            pip install azdev pytest-json-report pytest-html==3.2.0 pytest-rerunfailures
    
            azdev setup -c azure-cli -r azure-cli-extensions
            
            # Remove ext- prefix
            FinalTarget="$(Target)"
            if [[ "${FinalTarget}" =~ ^ext-.* ]]; then
              echo "It is an extention"
              echo $(Target)
              FinalTarget=${FinalTarget:4}
              azdev extension add ${FinalTarget}
            fi
            echo ${FinalTarget}
            
            serial_modules="appservice botservice cloud network azure-cli-core azure-cli-telemetry"
    
            # This helps detect issues in CI if a used SDK API version is deleted by the below script.
            python $workDir/s/scripts/trim_sdk.py
    
            # Compact aaz folders of modules
            python $workDir/s/scripts/compact_aaz.py
    
            az -v
            az account set -s $(azure-cli-live-test-bami-sub-id)
    
            # Clean policy
            python $workDir/s/scripts/live_test/clean_policy.py
            
            echo "Run tests"
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " ]]; then
              echo "Commit mode"
              azdev test ${FinalTarget} --no-exitfirst -a "-n ${{ parameters.userParallelism }}"
              azdev test ${FinalTarget} --live --lf --xml-path test_results.parallel.xml --no-exitfirst -a "-n ${{ parameters.userParallelism }} --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.parallel.json --html=$(Target).$(platform).report.parallel.html --self-contained-html --capture=sys"
            elif [[ "$serial_modules" =~ "$FinalTarget" ]]; then
              echo "Series mode"
              azdev test ${FinalTarget} ${{ parameters.userLive }} --xml-path test_results.sequential.xml --no-exitfirst -a "-n 1 --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.sequential.json --html=$(Target).$(platform).report.sequential.html --self-contained-html --capture=sys"
            else
              echo "Normal mode"
              # Sequential
              azdev test ${FinalTarget} ${{ parameters.userLive }} --mark serial --xml-path test_results.sequential.xml --no-exitfirst -a "-n 1 --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.sequential.json --html=$(Target).$(platform).report.sequential.html --self-contained-html --capture=sys"
              # Parallel
              azdev test ${FinalTarget} ${{ parameters.userLive }} --mark "not serial" --xml-path test_results.parallel.xml --no-exitfirst -a "-n ${{ parameters.userParallelism }} --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.parallel.json --html=$(Target).$(platform).report.parallel.html --self-contained-html --capture=sys"
            fi
    
            pwd
            ls
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " && ! "${{ parameters.userTarget }}" =~ ^ext-.* && "${{ parameters.userTarget }}" != "extensions" && "${{ parameters.userTarget }}" != ""  && "${{ parameters.userTarget }}" != "all" && "${{ parameters.userTarget }}" != "ALL" ]]; then
              cd azure-cli
              git status
              git add .
              git commit -m "Upload recording files"
              git push origin ${{ parameters.userBranch }}
            elif [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " && "${{ parameters.userTarget }}" =~ ^ext-.* ]]; then
              cd azure-cli-extensions
              git status
              git add .
              git commit -m "Upload recording files"
              git push origin ${{ parameters.userBranchExt }}
            fi
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
      - task: PublishTestResults@2
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          testResultsFiles: '$(workDir)/test_results.*.xml'
          testRunTitle: 'Azure CLI live test results of $(Target)'
          mergeTestResults: true
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.sequential.json'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.sequential.html'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.parallel.json'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.parallel.html'
          ArtifactName: 'livetest'
          publishLocation: 'Container'

- stage: TestWindows
  displayName: Live Test on Windows 2019
  condition: succeededOrFailed()
  jobs:
  - job: LiveTest
    displayName: Live Test
    # Sometimes the live test will be stuck forever, so the timeout cannot be set to 0, it is recommended to set it to twice the maximum time
    timeoutInMinutes: 1200
    strategy:
      # If the maxParallel is too large, the resource group will reach the limit of 980, so it is recommended that the maxParallel should not exceed 30.
      maxParallel: ${{ parameters.maxParallel }}
      matrix:
        acr:
          Target: acr
        acs:
          Target: acs
        advisor:
          Target: advisor
        ams:
          Target: ams
        apim:
          Target: apim
        appconfig:
          Target: appconfig
        appservice:
          Target: appservice
        aro:
          Target: aro
        backup:
          Target: backup
        batch:
          Target: batch
        batchai:
          Target: batchai
        billing:
          Target: billing
        botservice:
          Target: botservice
        cdn:
          Target: cdn
        cloud:
          Target: cloud
        cognitiveservices:
          Target: cognitiveservices
        config:
          Target: config
        configure:
          Target: configure
        consumption:
          Target: consumption
        container:
          Target: container
        cosmosdb:
          Target: cosmosdb
        databoxedge:
          Target: databoxedge
        #deploymentmanager:
        #  Target: deploymentmanager
        dla:
          Target: dla
        dls:
          Target: dls
        dms:
          Target: dms
        eventgrid:
          Target: eventgrid
        eventhubs:
          Target: eventhubs
        extension:
          Target: extension
        feedback:
          Target: feedback
        find:
          Target: find
        hdinsight:
          Target: hdinsight
        identity:
          Target: identity
        iot:
          Target: iot
        keyvault:
          Target: keyvault
        kusto:
          Target: kusto
        lab:
          Target: lab
        managedservices:
          Target: managedservices
        maps:
          Target: maps
        marketplaceordering:
          Target: marketplaceordering
        monitor:
          Target: monitor
        mysql:
          Target: mysql
        #natgateway:
        #  Target: natgateway
        netappfiles:
          Target: netappfiles
        network:
          Target: network
        policyinsights:
          Target: policyinsights
        postgresql:
          Target: postgresql
        privatedns:
          Target: privatedns
        profile:
          Target: profile
        rdbms:
          Target: rdbms
        redis:
          Target: redis
        relay:
          Target: relay
        resource:
          Target: resource
        role:
          Target: role
        search:
          Target: search
        security:
          Target: security
        servicebus:
          Target: servicebus
        serviceconnector:
          Target: serviceconnector
        servicefabric:
          Target: servicefabric
        signalr:
          Target: signalr
        sql:
          Target: sql
        sqlvm:
          Target: sqlvm
        storage:
          Target: storage
        synapse:
          Target: synapse
        util:
          Target: util
        vm:
          Target: vm
        ext-account:
          Target: ext-account
        ext-acrquery:
          Target: ext-acrquery
        ext-acrtransfer:
          Target: ext-acrtransfer
        ext-ad:
          Target: ext-ad
        ext-adp:
          Target: ext-adp
        ext-aem:
          Target: ext-aem
        ext-ai-examples:
          Target: ext-ai-examples
        ext-aks-preview:
          Target: ext-aks-preview
        ext-alertsmanagement:
          Target: ext-alertsmanagement
        #ext-alias:
        #  Target: ext-alias
        ext-amg:
          Target: ext-amg
        ext-application-insights:
          Target: ext-application-insights
        ext-appservice-kube:
          Target: ext-appservice-kube
        #ext-attestation:
        #  Target: ext-attestation
        ext-authV2:
          Target: ext-authV2
        ext-automanage:
          Target: ext-automanage
        ext-automation:
          Target: ext-automation
        ext-azure-firewall:
          Target: ext-azure-firewall
        ext-bastion:
          Target: ext-bastion
        ext-billing-benefits:
          Target: ext-billing-benefits
        ext-blockchain:
          Target: ext-blockchain
        ext-blueprint:
          Target: ext-blueprint
        ext-change-analysis:
          Target: ext-change-analysis
        ext-cli-translator:
          Target: ext-cli-translator
        #ext-cloudservice:
        #  Target: ext-cloudservice
        ext-communication:
          Target: ext-communication
        ext-confcom:
          Target: ext-confcom
        ext-confidentialledger:
          Target: ext-confidentialledger
        ext-confluent:
          Target: ext-confluent
        ext-connectedk8s:
          Target: ext-connectedk8s
        ext-connectedmachine:
          Target: ext-connectedmachine
        ext-connectedvmware:
          Target: ext-connectedvmware
        ext-connection-monitor-preview:
          Target: ext-connection-monitor-preview
        ext-containerapp:
          Target: ext-containerapp
        #ext-containerapp-preview:
        #  Target: ext-containerapp-preview
        ext-cosmosdb-preview:
          Target: ext-cosmosdb-preview
        ext-costmanagement:
          Target: ext-costmanagement
        ext-custom-providers:
          Target: ext-custom-providers
        ext-databox:
          Target: ext-databox
        ext-databricks:
          Target: ext-databricks
        ext-datadog:
          Target: ext-datadog
        ext-datafactory:
          Target: ext-datafactory
        ext-datamigration:
          Target: ext-datamigration
        ext-dataprotection:
          Target: ext-dataprotection
        ext-datashare:
          Target: ext-datashare
        ext-db-up:
          Target: ext-db-up
        ext-desktopvirtualization:
          Target: ext-desktopvirtualization
        #ext-dev-spaces:
        #  Target: ext-dev-spaces
        ext-devcenter:
          Target: ext-devcenter
        ext-diskpool:
          Target: ext-diskpool
        ext-dms-preview:
          Target: ext-dms-preview
        ext-dnc:
          Target: ext-dnc
        ext-dns-resolver:
          Target: ext-dns-resolver
        ext-dynatrace:
          Target: ext-dynatrace
        ext-edgeorder:
          Target: ext-edgeorder
        ext-elastic:
          Target: ext-elastic
        ext-elastic-san:
          Target: ext-elastic-san
        #ext-eventgrid:
        #  Target: ext-eventgrid
        #ext-express-route-cross-connection:
        #  Target: ext-express-route-cross-connection
        ext-fleet:
          Target: ext-fleet
        ext-fluid-relay:
          Target: ext-fluid-relay
        ext-footprint:
          Target: ext-footprint
        ext-front-door:
          Target: ext-front-door
        #ext-functionapp:
        #  Target: ext-functionapp
        ext-guestconfig:
          Target: ext-guestconfig
        #ext-hack:
        #  Target: ext-hack
        ext-hardware-security-modules:
          Target: ext-hardware-security-modules
        ext-healthbot:
          Target: ext-healthbot
        ext-healthcareapis:
          Target: ext-healthcareapis
        ext-hpc-cache:
          Target: ext-hpc-cache
        #ext-image-copy-extension:
        #  Target: ext-image-copy-extension
        ext-image-gallery:
          Target: ext-image-gallery
        ext-import-export:
          Target: ext-import-export
        ext-init:
          Target: ext-init
        ext-interactive:
          Target: ext-interactive
        ext-internet-analyzer:
          Target: ext-internet-analyzer
        ext-ip-group:
          Target: ext-ip-group
        ext-k8s-configuration:
          Target: ext-k8s-configuration
        ext-k8s-extension:
          Target: ext-k8s-extension
        #ext-kusto:
        #  Target: ext-kusto
        ext-load:
          Target: ext-load
        ext-log-analytics:
          Target: ext-log-analytics
        ext-log-analytics-solution:
          Target: ext-log-analytics-solution
        ext-logic:
          Target: ext-logic
        ext-logz:
          Target: ext-logz
        ext-maintenance:
          Target: ext-maintenance
        ext-managedccfs:
          Target: ext-managedccfs
        ext-managementpartner:
          Target: ext-managementpartner
        ext-mesh:
          Target: ext-mesh
        ext-mixed-reality:
          Target: ext-mixed-reality
        ext-mobile-network:
          Target: ext-mobile-network
        ext-monitor-control-service:
          Target: ext-monitor-control-service
        ext-netappfiles-preview:
          Target: ext-netappfiles-preview
        ext-network-manager:
          Target: ext-network-manager
        ext-next:
          Target: ext-next
        ext-nginx:
          Target: ext-nginx
        ext-notification-hub:
          Target: ext-notification-hub
        ext-nsp:
          Target: ext-nsp
        ext-offazure:
          Target: ext-offazure
        ext-orbital:
          Target: ext-orbital
        ext-peering:
          Target: ext-peering
        ext-portal:
          Target: ext-portal
        ext-powerbidedicated:
          Target: ext-powerbidedicated
        ext-providerhub:
          Target: ext-providerhub
        ext-purview:
          Target: ext-purview
        ext-quantum:
          Target: ext-quantum
        ext-quota:
          Target: ext-quota
        #ext-rdbms-connect:
        #  Target: ext-rdbms-connect
        ext-redisenterprise:
          Target: ext-redisenterprise
        ext-reservation:
          Target: ext-reservation
        ext-resource-graph:
          Target: ext-resource-graph
        ext-resource-mover:
          Target: ext-resource-mover
        ext-scenario-guide:
          Target: ext-scenario-guide
        ext-scheduled-query:
          Target: ext-scheduled-query
        ext-scvmm:
          Target: ext-scvmm
        ext-securityinsight:
          Target: ext-securityinsight
        ext-self-help:
          Target: ext-self-help
        ext-serial-console:
          Target: ext-serial-console
        ext-serviceconnector-passwordless:
          Target: ext-serviceconnector-passwordless
        ext-spring:
          Target: ext-spring
        ext-spring-cloud:
          Target: ext-spring-cloud
        ext-ssh:
          Target: ext-ssh
        ext-stack-hci:
          Target: ext-stack-hci
        ext-staticwebapp:
          Target: ext-staticwebapp
        ext-storage-blob-preview:
          Target: ext-storage-blob-preview
        ext-storage-mover:
          Target: ext-storage-mover
        ext-storage-preview:
          Target: ext-storage-preview
        ext-storagesync:
          Target: ext-storagesync
        ext-stream-analytics:
          Target: ext-stream-analytics
        ext-subscription:
          Target: ext-subscription
        ext-support:
          Target: ext-support
        ext-swiftlet:
          Target: ext-swiftlet
        ext-timeseriesinsights:
          Target: ext-timeseriesinsights
        ext-traffic-collector:
          Target: ext-traffic-collector
        #ext-virtual-network-tap:
        #  Target: ext-virtual-network-tap
        ext-virtual-wan:
          Target: ext-virtual-wan
        ext-vm-repair:
          Target: ext-vm-repair
        ext-vmware:
          Target: ext-vmware
        ext-voice-service:
          Target: ext-voice-service
        #ext-webapp:
        #  Target: ext-webapp
        ext-webpubsub:
          Target: ext-webpubsub
    pool:
      name: ${{ variables.windows_pool }}
    steps:
      - bash: |
          if [[ "${{ parameters.userTarget }}" =~ "$(Target)" || ("${{ parameters.userTarget }}" == "main" && ! "$(Target)" =~ ^ext-.*) || ("${{ parameters.userTarget }}" == "extensions" && "$(Target)" =~ ^ext-.*) || ("${{ parameters.userTarget }}" == "ALL" || "${{ parameters.userTarget }}" == "all" || "${{ parameters.userTarget }}" == "") ]]; then
            echo "Match!"
            echo "##vso[task.setvariable variable=Match]1"
            echo "##vso[task.setvariable variable=platform]windows2019"
          else
            echo "Not match!"
            echo "##vso[task.setvariable variable=Match]0"
          fi
      - task: UsePythonVersion@0
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          versionSpec: '${{ parameters.pythonVersion }}'
          addToPath: true
          architecture: 'x64'
      - task: AzureCLI@2
        displayName: 'Run live test on Windows'
        inputs:
          connectedServiceNameARM: $(azure-cli-live-test-bami-connected-service)
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            cd ..
            pwd
            workDir=$(pwd)
            echo "##vso[task.setvariable variable=workDir]$(Agent.BuildDirectory)"
            ls
            # ssh-keygen -f ~/.ssh/id_rsa -t rsa -N ''
            git config --global user.email "AzurePipelines"
            git config --global user.name "AzurePipelines"
    
            # print variables
            echo "PLATFORM: $(PLATFORM)"
            echo "USER_TARGET: ${{ parameters.userTarget }}"
            echo "USER_REPO: ${{ parameters.userRepo }}"
            echo "USER_REPO_EXT: ${{ parameters.userRepoExt }}"
            echo "USER_BRANCH: ${{ parameters.userBranch }}"
            echo "USER_BRANCH_EXT: ${{ parameters.userBranchExt }}"
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " ]]; then
              REPO="${{ parameters.userRepo }}"
              EXT_REPO="${{ parameters.userRepoExt }}"
              # Pass username and token so that we can commit changes
              git clone -b ${{ parameters.userBranch }} ${REPO:0:8}${{ parameters.userUsername }}:${{ parameters.userToken }}@${REPO:8}
              git clone -b ${{ parameters.userBranchExt }} ${EXT_REPO:0:8}${{ parameters.userUsername }}:${{ parameters.userToken }}@${EXT_REPO:8}
            else
              git clone -b ${{ parameters.userBranch }} ${{ parameters.userRepo }}
              git clone -b ${{ parameters.userBranchExt }} ${{ parameters.userRepoExt }}
            fi
    
            python -m venv env
            source env/Scripts/activate
    
            # TODO: support pytest-html >= 4.0.0
            pip install azdev pytest-json-report pytest-html==3.2.0 pytest-rerunfailures
    
            azdev setup -c azure-cli -r azure-cli-extensions
            
            # Remove ext- prefix
            FinalTarget="$(Target)"
            if [[ "${FinalTarget}" =~ ^ext-.* ]]; then
              echo "It is an extention"
              echo $(Target)
              FinalTarget=${FinalTarget:4}
              azdev extension add ${FinalTarget}
            fi
            echo ${FinalTarget}
            
            serial_modules="appservice botservice cloud network azure-cli-core azure-cli-telemetry"
            
            # This helps detect issues in CI if a used SDK API version is deleted by the below script.
            python $workDir/s/scripts/trim_sdk.py
    
            # Compact aaz folders of modules
            python $workDir/s/scripts/compact_aaz.py
    
            az -v
            az account set -s $(azure-cli-live-test-bami-sub-id)
    
            # Clean policy
            python $workDir/s/scripts/live_test/clean_policy.py
            
            echo "Run tests"
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " ]]; then
              echo "Commit mode"
              azdev test ${FinalTarget} --no-exitfirst -a "-n ${{ parameters.userParallelism }}"
              azdev test ${FinalTarget} --live --lf --xml-path test_results.parallel.xml --no-exitfirst -a "-n ${{ parameters.userParallelism }} --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.parallel.json --html=$(Target).$(platform).report.parallel.html --self-contained-html --capture=sys"
            elif [[ "$serial_modules" =~ "$FinalTarget" ]]; then
              echo "Series mode"
              azdev test ${FinalTarget} ${{ parameters.userLive }} --xml-path test_results.sequential.xml --no-exitfirst -a "-n 1 --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.sequential.json --html=$(Target).$(platform).report.sequential.html --self-contained-html --capture=sys"
            else
              echo "Normal mode"
              # Sequential
              azdev test ${FinalTarget} ${{ parameters.userLive }} --mark serial --xml-path test_results.sequential.xml --no-exitfirst -a "-n 1 --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.sequential.json --html=$(Target).$(platform).report.sequential.html --self-contained-html --capture=sys"
              # Parallel
              azdev test ${FinalTarget} ${{ parameters.userLive }} --mark "not serial" --xml-path test_results.parallel.xml --no-exitfirst -a "-n ${{ parameters.userParallelism }} --json-report --json-report-summary --json-report-file=$(Target).$(platform).report.parallel.json --html=$(Target).$(platform).report.parallel.html --self-contained-html --capture=sys"
            fi
    
            pwd
            ls
            # Whether commit changes
            if [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " && ! "${{ parameters.userTarget }}" =~ ^ext-.* && "${{ parameters.userTarget }}" != "extensions" && "${{ parameters.userTarget }}" != ""  && "${{ parameters.userTarget }}" != "all" && "${{ parameters.userTarget }}" != "ALL" ]]; then
              cd azure-cli
              git status
              git add .
              git commit -m "Upload recording files"
              git push origin ${{ parameters.userBranch }}
            elif [[ "${{ parameters.userUsername }}" != " " && "${{ parameters.userToken }}" != " " && "${{ parameters.userTarget }}" =~ ^ext-.* ]]; then
              cd azure-cli-extensions
              git status
              git add .
              git commit -m "Upload recording files"
              git push origin ${{ parameters.userBranchExt }}
            fi
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
      - task: PublishTestResults@2
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          testResultsFiles: '$(workDir)/test_results.*.xml'
          testRunTitle: 'Azure CLI live test results of $(Target)'
          mergeTestResults: true
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.sequential.json'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.sequential.html'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.parallel.json'
          ArtifactName: 'livetest'
          publishLocation: 'Container'
      - task: PublishBuildArtifacts@1
        condition: and(succeededOrFailed(), eq(variables.Match, '1'))
        inputs:
          PathtoPublish: '$(workDir)/$(Target).$(platform).report.parallel.html'
          ArtifactName: 'livetest'
          publishLocation: 'Container'

- stage: SendEmail
  displayName: Send Email
  condition: succeededOrFailed()
  jobs:
  - job: SendEmail
    displayName: Send Email Job
    timeoutInMinutes: 120
    condition: succeededOrFailed()
    pool:
      name: ${{ variables.ubuntu_pool }}
    steps:
      - task: UsePythonVersion@0
        inputs:
          versionSpec: '${{ parameters.pythonVersion }}'
          addToPath: true
          architecture: 'x64'
      - task: DownloadBuildArtifacts@0
        inputs:
          buildType: 'current'
          downloadType: 'specific'
          downloadPath: '$(System.ArtifactsDirectory)'
      - task: AzureCLI@2
        displayName: 'Send live test email'
        inputs:
          connectedServiceNameARM: $(azure-cli-live-test-msft-connected-service)
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            set -o errexit
            echo "ArtifastsDirectory: $(System.ArtifactsDirectory)"
            ls $(System.ArtifactsDirectory)
            pwd
            workDir=$(pwd)
            commit_id=`git ls-remote https://github.com/Azure/azure-cli.git HEAD`
            pip install azure-communication-email
            pip install mysql-connector-python
            pip install requests
            pip install tqdm
            pip install azure-kusto-data azure-kusto-ingest bs4
            # Send notification
            az -v
            echo "login to user azureclilivetest"
            az account set -s $(azure-cli-msft-test-sub-id)
            echo "set account"
            echo "send email to grid"
            python $workDir/scripts/live_test/sendemail.py "$commit_id"
        env:
          ARTIFACTS_DIR: $(System.ArtifactsDirectory)
          BUILD_ID: $(Build.BuildId)
          EMAIL_KEY: $(email-key)
          EMAIL_ADDRESS: $(Build.RequestedForEmail)
          ACCOUNT_NAME: $(account-name)
          KUSTO_CLUSTER: $(kusto-cluster)
          KUSTO_DATABASE: $(kusto-database)
          KUSTO_TABLE: $(kusto-table)
          IDENTITY_CLIENT_ID: $(identity-client-id)
          PYTHON_VERSION: ${{ parameters.pythonVersion }}
          USER_BRANCH: ${{ parameters.userBranch }}
          USER_BRANCH_EXT: ${{ parameters.userBranchExt }}
          USER_LIVE: ${{ parameters.userLive }}
          USER_REPO: ${{ parameters.userRepo }}
          USER_REPO_EXT: ${{ parameters.userRepoExt }}
          USER_TARGET: ${{ parameters.userTarget }}
          STATIC_WEB_URL: ${{ parameters.staticWebUrl }}

  - job: CleanResource
    displayName: Clean Resource Job
    timeoutInMinutes: 1200
    condition: succeededOrFailed()
    continueOnError: true
    pool:
      name: ${{ variables.ubuntu_pool }}
    steps:
      - task: UsePythonVersion@0
        inputs:
          versionSpec: '${{ parameters.pythonVersion }}'
          addToPath: true
          architecture: 'x64'
      - task: AzureCLI@2
        displayName: 'Clean resource'
        inputs:
          connectedServiceNameARM: $(azure-cli-live-test-bami-connected-service)
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            set -o errexit
            workDir=$(pwd)
            pip install tqdm
            az -v
            echo "login to user azureclilivetest"
            az account set -s $(azure-cli-live-test-bami-sub-id)
            echo "set account"
            echo "clean resource"
            python $workDir/scripts/live_test/clean.py
