0

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"
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
cpiock
  • 1,275
  • 2
  • 17
  • 44
  • Have you confirmed that the service is indeed running and healthy, e.g. by connecting from a different location? – Vince Bowdren May 07 '22 at 21:00
  • @VinceBowdren is running "inside" of azure devops and I don't have access from the outside. How can a connect from a different location – cpiock May 09 '22 at 12:00
  • Ah, maybe I have misunderstood. What kind of agent are you running? An MS-hosted one, or a self-hosted one on an environment you control? – Vince Bowdren May 09 '22 at 13:07
  • @VinceBowdren i use the MS-hosted one – cpiock May 09 '22 at 13:22
  • Ah; I think this will be very difficult, given the restrictions on accessing the agent. For example, it would be useful to see the docker container's logs to find out what if something has gone wrong. – Vince Bowdren May 09 '22 at 13:30
  • Honestly, I'd suggest a different way of doing this; perhaps run your service on a Azure Container Instance, or a VM? – Vince Bowdren May 09 '22 at 13:31
  • You were right Vince, i have now deleted that answer and i am posting the comments from that answer in here. – Gurpreet May 09 '22 at 21:24
  • Please try to use -SkipHttpErrorCheck parameter with the Invoke-WebRequest command. This should show us what status code do you get when it fails. This could help understand a bit more – Gurpreet May 09 '22 at 21:25
  • cpiock said => @gurpreet this doesn't change the error log Failed webrequest Connection refused (localhost:32777) Invoke-WebRequest -Uri localhost:32777/health -Method Get -TimeoutSec 10 -SkipHttpErrorCheck – Gurpreet May 09 '22 at 21:26
  • @VinceBowdren so it is better to run it outside of azure devops? – cpiock May 10 '22 at 14:13
  • @cpiock I'd recommend so. There are lots of options: you could use your own local environment (by installing an agent there) or you could deploy to a cloud environment. – Vince Bowdren May 10 '22 at 15:17

0 Answers0