Connection refused testing health page in azure pipleine

402 Views Asked by At

My target is to run our postman integration tests inside our azure pipeline. For this I am trying to access the health page of my service inside the Azure Pipeline. Sometime it works but sometimes I get a Connection Refused. I also tried a loop to retry the request for 5 minutes but or it worked immediately or I get a "Connection Refused" for 5 minutes.

Any ideas or hints welcome :-)

Here my yaml file:

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - src/MyService/*    
    
name: 1.0$(rev:.r)

resources:
  repositories:
  - repository: templates
    type: git
    name: Base/AzurePipelines

variables:
  - group: nuget-settings
  - name: projectServiceName
    value: 'MyServiceName'

stages:
- stage: 'Integration'
  displayName: 'Deploy to integration environment'    
  jobs:     
  - job: IntegrationTest    
    steps:
    - task: DockerCompose@0
      displayName: Run services
      continueOnError: false 
      inputs:
        action: Run services
        dockerComposeFile: src/MyService/docker-compose.yml
        additionalDockerComposeFiles: docker-compose-ci.override.yml 
        qualifyImageNames: true
        buildImages: true
        abortOnContainerExit: true
        detached: true

    - task: Npm@1
      displayName: "Install Newman CLI"
      continueOnError: false
      inputs:
        command: "custom"
        customCommand: "install newman -g"

    - task: Npm@1
      displayName: "Install Newman Reporter"
      continueOnError: false
      inputs:
        command: "custom"
        customCommand: "install newman-reporter-junitfull -g"
      
    - task: PowerShell@2
      displayName: 'Check health page is running'
      continueOnError: false
      timeoutInMinutes: 5
      inputs:
        targetType: 'inline'
        script: |        
            $statusCode = 000
            while (($statusCode -ne 200) -and ($statusCode -ne 201))
            {
                Write-Output "Start loop"
                try {
                    Write-Output "Start webrequest"
                    $response = Invoke-WebRequest -Uri http://localhost:32777/health -Method Get -TimeoutSec 10
                    $statusCode = $response.StatusCode                
                    Write-Output $statusCode
                    if(($statusCode -eq 200) -or ($statusCode -eq 201))
                    {
                        Write-Host "##vso[task.setvariable variable=status]ok"
                    }
                    else
                    {
                        Write-Host "##vso[task.setvaraible variable=status]notOk"
                    }
                }
                catch {
                    Write-Output "Failed webrequest" $_.Exception.Message
                    $statusCode = $response.StatusCode                     
                    Start-Sleep -Seconds 10
                }
            }
  
    - task: PowerShell@2
      displayName: "Execute Postman Collections"
      continueOnError: false
      inputs:
        filePath: "src/MyService/ado/newman-collection-runner.ps1"
        arguments: >          
          -PostmanFilePath "tests/postman"
          -CollectionName "myCollection"
          -EnvironmentFilePath "src/MyService/ado/localhost.environment.json"         

    - task: PublishTestResults@2
      displayName: "Publish Postman Test Results"
      condition: succeededOrFailed()
      continueOnError: false
      inputs:
        testResultsFormat: "JUnit"
        testResultsFiles: "tests/postman/Reports/*_JUnitReport.xml"
        testRunTitle: "Postman Tests"
0

There are 0 best solutions below