trigger: none

pool:
  vmImage: 'ubuntu-18.04'

parameters:
- name: neoloadApiUrl
  type: string
  default: 'https://neoload-api.saas.neotys.com/'
- name: neoloadToken
  type: string
  default: 'specify_token_or_get_it_from_credentials'
- name: zoneId
  type: string
  default: 'defaultzone'

# create sidecar containers, attach them to a zone; provide supply this pipeline demands
resources:
  containers:
  - container: neoload_ctrl
    image: neotys/neoload-controller:latest
    env: { MODE: Managed, NEOLOADWEB_TOKEN: ${{parameters.neoloadToken}}, ZONE: ${{parameters.zoneId}} }
  - container: lg1
    image: neotys/neoload-loadgenerator:latest
    env: { NEOLOADWEB_TOKEN: ${{parameters.neoloadToken}}, ZONE: ${{parameters.zoneId}}, LG_HOST=lg1, LG_PORT=7101, AGENT_SERVER_PORT=7101 }
  - container: lg2
    image: neotys/neoload-loadgenerator:latest
    env: { NEOLOADWEB_TOKEN: ${{parameters.neoloadToken}}, ZONE: ${{parameters.zoneId}}, LG_HOST=lg2, LG_PORT=7102, AGENT_SERVER_PORT=7102 }

jobs:
- job: RunLoadTest
  displayName: Start infrastructure and run NeoLoad Test from Azure Devops
  services:
    neoload_ctrl: neoload_ctrl
    lg1: lg1
    lg2: lg2
  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.8'

  - task: Bash@3
    displayName: Install NeoLoad
    continueOnError: false
    inputs:
      targetType: 'inline'
      script: |
        pip install neoload
        neoload --version

  - task: Bash@3
    displayName: Configure the NeoLoad test
    continueOnError: true
    inputs:
      targetType: 'inline'
      script: |
        neoload login --url ${{parameters.neoloadApiUrl}} ${{parameters.neoloadToken}} \
                test-settings --zone ${{parameters.zoneId}} --lgs 2 --scenario smokeScenario patch "My Azure Test With CLI" \
                project --path tests/neoload_projects/example_1/ upload "My Azure Test With CLI"

  - task: Bash@3
    displayName: Run the NeoLoad test
    continueOnError: true
    inputs:
      targetType: 'inline'
      script: |
        neoload run \
                       --web-vu 5 \
                       --as-code default.yaml,slas/uat.yaml \
                       --name "Azure pipeline performance regression task $(Build.BuildNumber)" \
                       --external-url "$(Build.BuildUri)" \
                       --external-url-label "Azure CI build $(Build.BuildNumber)" \
                       --description "Azure result description" \
                       "My Azure Test With CLI"

  - task: Bash@3
    displayName: Get the junit SLA report
    continueOnError: true
    inputs:
      targetType: 'inline'
      script: |
       neoload test-results --junit-file $(Common.TestResultsDirectory)/neoload-slas.xml junitsla

  - task: PublishTestResults@2
    inputs:
      testResultsFormat: 'JUnit'
      testResultsFiles: $(Common.TestResultsDirectory)/neoload-slas.xml
      failTaskOnFailedTests: true
