parameters:
  forced_image: $(Horton.ForcedImage)
  suite: ''
  testcase: ''
  repeat_count: ''

steps:
- powershell: |
    function IsEmpty($s) { ( ($s -eq $null) -or ( $s -eq "" ) -or ( $s.StartsWith("$") ) ) } 

    function GetArchitecture() {
      $plat = (uname -m) | Out-String     
      if ($plat.StartsWith("armv7")) {
        "arm64"
      } else {
        "x64"
      }
    }

    $SkipTest="no"
    $UsesDocker = "yes"

    Write-Host "Configuring test suite=$env:Suite"

    $Variant = ""
    if ($env:Suite.StartsWith("c_")) {
        $Language = "c"
    } elseif ($env:Suite.StartsWith("java_")) {
        $Language = "java"
    } elseif ($env:Suite.StartsWith("csharp_")) {
        $Language = "csharp"
    } elseif ($env:Suite.StartsWith("node_")) {
        $Language = "node"
    } elseif ($env:Suite.StartsWith("node6_")) {
        $Language = "node"
        $Variant = "node6"
    } elseif ($env:Suite.StartsWith("node16_")) {
        $Language = "node"
        $Variant = "node16"
    } elseif ($env:Suite.StartsWith("node18_")) {
        $Language = "node"
        $Variant = "node18"
    } elseif ($env:Suite.StartsWith("py37_")) {
        $Language = "pythonv2"
        $Variant = "py37"
    } elseif ($env:Suite.StartsWith("py38_")) {
        $Language = "pythonv2"
        $Variant = "py38"
    } elseif ($env:Suite.StartsWith("py39_")) {
        $Language = "pythonv2"
        $Variant = "py39"
    } elseif ($env:Suite.StartsWith("py310")) {
        $Language = "pythonv2"
        $Variant = "py310"
    } else {
        throw("Unknown/missing language in suite: " + $env:Suite)
    }

    if ($env:Suite.Contains("iothub_module_quick_drop")) {
        $Scenario="iothub_module_quick_drop"
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("iothub_module_full_drop")) {
        $Scenario="iothub_module_full_drop"
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("iothub_device_quick_drop")) {
        $Scenario="iothub_device_quick_drop"
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("iothub_device_full_drop")) {
        $Scenario="iothub_device_full_drop"
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("edgehub_module_quick_drop")) {
        $Scenario="edgehub_module_quick_drop"
        $DeploymentType="iotedge"
    } elseif ($env:Suite.Contains("edgehub_module_full_drop")) {
        $Scenario="edgehub_module_full_drop"
        $DeploymentType="iotedge"
    } elseif ($env:Suite.Contains("iothub_module_and_device")) {
        $Scenario="iothub_module_and_device"
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("iothub_module")) {
        $Scenario="iothub_module"
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("iothub_device")) {
        $DeploymentType="iothub"
    } elseif ($env:Suite.Contains("edgehub_module")) {
        $Scenario="edgehub_module"
        $DeploymentType="iotedge"
    } else {
        throw("Unknown/missing scenario in suite: " + $env:Suite)
    }

    if ($env:Suite.Contains("mqttws")) {
        $Transport="mqttws"
    } elseif ($env:Suite.Contains("mqtt")) {
        $Transport="mqtt"
    } elseif ($env:Suite.Contains("amqpws")) {
        $Transport="amqpws"
    } elseif ($env:Suite.Contains("amqp")) {
        $Transport="amqp"
    } else {
        throw("Unknown/missing transport in suite: " + $env:Suite)
    }

    $Async = ""
    if ($env:Suite.Contains("async")) {
        $Async="yes"
    }

    # Skip tests based on SDK transport support
    if ($Language -eq "c") {
      if ($Scenario -eq "edgehub_module") {
        if (($Transport -eq "amqp") -or ($Transport -eq "amqpws")) {
          $SkipTest="yes"
        }
      }
    } elseif ($Language -eq "pythonv2") {
      if (($Transport -ne "mqtt") -and ($Transport -ne "mqttws")) {
        $SkipTest="yes"
      }
    }

    # add parameters if we're runnin a single case
    $TestArgs=""
    if ( -not (IsEmpty($env:TestCase)) ) {
      $TestArgs=$TestArgs + " " + $env:TestCase
    }
    if ( -not (IsEmpty($env:RepeatCount)) ) {
      $TestArgs=$TestArgs + " --count=" + $env:RepeatCount
    }

    # parameters for async
    if ($Async -eq "yes") {
      $TestArgs=$TestArgs + " --async"
    }

    if ($env:AGENT_JOBNAME -ne "Job") {
      $LogFolderName=$env:AGENT_JOBNAME.replace(" + ","").replace(" ","_")
    } else {
        $LogFolderName="${Language}_${Scenario}_${Transport}_${Variant}"
        if ($Async -eq "yes") {
          $LogFolderName="${LogFolderName}_Async"
        }
        $LogFolderName=$LogFolderName + "_" + $env:SYSTEM_JOBPOSITIONINPHASE
    }

    $TestImageTag=$env:DefaultImageTag
    if ( -not (IsEmpty($Variant)) ) {
      $TestImageTag=$TestImageTag + "-" + $Variant
    }
    if ( -not (IsEmpty($env:ForcedImage)) ) {
      $TestImageTag=$env:ForcedImageTag
    }

    $ImageTestMod = $env:IOTHUB_E2E_REPO_ADDRESS + "/" + ${Language} + "-e2e-v3:" + ${TestImageTag}

    $Architecture=GetArchitecture

    Write-Host "##vso[task.setvariable variable=architecture]${Architecture}"
    Write-Host "##vso[task.setvariable variable=language]${Language}"
    Write-Host "##vso[task.setvariable variable=variant]${Variant}"
    Write-Host "##vso[task.setvariable variable=scenario]${Scenario}"
    Write-Host "##vso[task.setvariable variable=transport]${Transport}"
    Write-Host "##vso[task.setvariable variable=async]${Async}"

    Write-Host "##vso[task.setvariable variable=logFolderName]${LogFolderName}"
    Write-Host "##vso[task.setvariable variable=skipTest]${SkipTest}"
    Write-Host "##vso[task.setvariable variable=testArgs]${TestArgs}"
    Write-Host "##vso[task.setvariable variable=testImageTag]${TestImageTag}"
    Write-Host "##vso[task.setvariable variable=imageTestMod]${ImageTestMod}"
    Write-Host "##vso[task.setvariable variable=deploymentType]${DeploymentType}"
    Write-Host "##vso[task.setvariable variable=usesDocker]${UsesDocker}"

    Write-Host "Architecture=${Architecture}"
    Write-Host "Language=${Language}"
    Write-Host "Variant=${Variant}"
    Write-Host "Scenario=${Scenario}"
    Write-Host "Transport=${Transport}"
    Write-Host "Async=${Async}"

    Write-Host "AgentJobName=${env:AGENT_JOBNAME}"
    Write-Host "LogFolderName=${LogFolderName}"
    Write-Host "SkipTest=${SkipTest}"
    Write-Host "TestArgs=${TestArgs}"
    Write-Host "TestImageTag=${TestImageTag}"
    Write-Host "ImageTestMod=${ImageTestMod}"
    Write-Host "DeploymentType=${DeploymentType}"
    Write-Host "UsesDocker=${UsesDocker}"


  displayName: Custom task execution logic
  env:
    Suite: ${{ parameters.suite }}
    DefaultImageTag: "vsts-$(Build.BuildId)"
    ForcedImageTag: ${{ parameters.forced_image }}
    TestCase: ${{ parameters.testcase }}
    RepeatCount: ${{ parameters.repeat_count }}
    IOTHUB_E2E_REPO_ADDRESS: $(IOTHUB-E2E-REPO-ADDRESS)
  ignoreLASTEXITCODE: false
  errorActionPreference: Stop
  failOnStderr: true

- template: steps-ensure-e2e-fx-repo.yaml

- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.7'
    architecture: $(architecture)
  condition: and(succeeded(), ne(variables['skipTest'],'yes'))

- bash: 
    source ${HORTON_FRAMEWORKROOT}/bin/activate_horton && pip list
  displayName: 'Create Horton environment'
  condition: and(succeeded(), ne(variables['skipTest'],'yes'))

- template: steps-pre-test.yaml
  parameters:
    language: $(language)
    test_image_tag: $(testImageTag)
    image_testMod: $(imageTestMod)
    deployment_type: $(deploymentType)

- template: steps-run-pytest.yaml
  parameters:
    language: $(language)
    transport: $(transport)
    scenario: $(scenario)
    log_folder_name: $(logFolderName)
    extra_args: $(testArgs)

- template: steps-post-test.yaml
  parameters:
    language: $(language)
    log_folder_name: $(logFolderName)
    deployment_type: $(deploymentType)
