# Live Test For Python SDK

name: $(BASE_BRANCH)

trigger:
  branches:
    exclude:
    - '*'

# avoid being triggered as part of CI check
pr:
  branches:
    exclude:
    - '*'


variables:
  - group: Release Secrets for GitHub
  - group: Azure SDK Auto Release Pipeline Secrets

jobs:
- job: LiveTestPython38
  displayName: Live Test Python 3.8
  timeoutInMinutes: 1500
  strategy:
    maxParallel: 5
  pool:
    vmImage: 'ubuntu-20.04'
  steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.8'
        addToPath: true
        architecture: 'x64'
    - bash: |
        script_path=$(pwd)/scripts/auto_release
        cd ..
        git config --global user.email "PythonSdkPipelines"
        git config --global user.name "PythonSdkPipelines"

        # clone(REPO: https://github.com/Azure/azure-sdk-for-python.git, USR_NAME: Azure, USR_TOKEN: xxxxxxxxxxxxx)
        mkdir azure-sdk-for-python
        git clone ${REPO:0:8}$(USR_NAME):$(azuresdk-github-pat)@${REPO:8} $(pwd)/azure-sdk-for-python

        # prepare env if need to generate SDK code
        if [ "$(SPEC_README)" ]; then
          mkdir azure-rest-api-specs
          git clone https://github.com/Azure/azure-rest-api-specs.git $(pwd)/azure-rest-api-specs

          if [ "$(REST_REPO_HASH)" ]; then
            cd azure-rest-api-specs
            git reset --hard $(REST_REPO_HASH)
            cd ../
          fi

          # install autorest
          sudo npm install -g n
          sudo n 16.15.0
          export PATH=/usr/local/n/versions/node/16.15.0/bin:$PATH
          sudo npm init -f
          sudo npm install -g autorest --save

          mkdir temp_folder

          export SPEC_README=$(SPEC_README)
          export SPEC_REPO=$(pwd)/azure-rest-api-specs
          export TEMP_FOLDER=$(pwd)/temp_folder
          export DEBUG_REST_BRANCH=$(DEBUG_REST_BRANCH)
        fi

        # create virtual env
        python -m venv venv-sdk
        source venv-sdk/bin/activate
        pip install -r $script_path/requirement.txt

        # import env variable
        export AZURE_TEST_RUN_LIVE=$(AZURE_TEST_RUN_LIVE)
        export TENANT_ID=$(ENV-TENANT-ID)
        export CLIENT_ID=$(ENV-CLIENT-ID)
        export CLIENT_SECRET=$(ENV-CLIENT-SECRET)
        export SUBSCRIPTION_ID=$(ENV-SUBSCRIPTION-ID)
        export AZURE_TENANT_ID=$(ENV-TENANT-ID)
        export AZURE_CLIENT_ID=$(ENV-CLIENT-ID)
        export AZURE_CLIENT_SECRET=$(ENV-CLIENT-SECRET)
        export AZURE_SUBSCRIPTION_ID=$(ENV-SUBSCRIPTION-ID)
        export ISSUE_LINK=$(ISSUE_LINK)
        export BASE_BRANCH=$(BASE_BRANCH)
        export SCRIPT_PATH=$script_path
        export AZURESDK_BOT_TOKEN=$(azuresdk-github-pat)
        export STORAGE_CONN_STR=$(storage-conn-str)
        export STORAGE_ENDPOINT=$(storage-endpoint)
        export PYTHON_TAG=$(PYTHON_TAG)
        export DEBUG_SDK_BRANCH=$(DEBUG_SDK_BRANCH)

        # run
        cd azure-sdk-for-python
        python $script_path/main.py
