pact-foundation / pact-reference

Reference implementations for the pact specifications
https://pact.io
MIT License
91 stars 46 forks source link

Option to wait for provider to start in rust/pact_verifier_cli #197

Closed jbecker44 closed 1 year ago

jbecker44 commented 2 years ago

I'm trying to use docker-compose to run both the pact_verifier_cli (via the docker image) and my provider service, like so:

  pact_verifier:
    image: pactfoundation/pact-ref-verifier
    depends_on:
      - provider_service
    command: >
      --broker-url http://my-broker-url.com/
      --provider-name test-service
      --hostname localhost
      --port 8080
      --state-change-url http://localhost:8080/_pact/provider_states
      --request-timeout 60000

My provider service is a python FastAPI service though which briefly returns an empty reply to all requests while it is starting up. This unfortunately happens to coincide with when docker-compose treats it as "up" and proceeds to run the pact_verifier_cli command, so when pact_verifier_cli attempts to POST state change requests to my provider, it gets an empty response and fails. This is still the case even when using the --request-timeout option, since the request is not timing out.

As a work-around, I had to remove the pact_verifier_cli from my docker compose file and instead call it as part of a script so I could wait for the service to return a valid response instead of an empty response (here I'm just GETting the state change endpoint, so I expect it to return a 405 since it only accepts POSTs, but that's still enough to know that the service is done starting up):

set -o pipefail

docker-compose up -d

function teardown {
    docker-compose down
}
trap teardown EXIT

while [[ $(curl -s -o /dev/null -w %{http_code} 0.0.0.0:8080/_pact/provider_states) != 405 ]]
do sleep 1
done

docker run --network host pactfoundation/pact-ref-verifier --broker-url "http://my-broker-url.com/" \
    --provider-name "test-service" \
    --hostname "localhost" \
    --port "8080" \
    --state-change-url "http://localhost:8080/_pact/provider_states"

As you can see, it would be nice if an option which did something similar to this were built into the rust/pact_verifier_cli so I could rely solely on docker-compose to orchestrate the start-up of my verification.

rholshausen commented 2 years ago

I can add the retry option, but it will retry a number of times until it gets a < 500 response. Will this work for you?

YOU54F commented 1 year ago

Oh this is nice! saves having a wait for script before running a test