Would you like to learn contract test from 0 to advanced? I provide practical training, just contact me via Linkedin and I'll send you the proposal with all the grid and important details.
To get the most out of the repository, see the following materials, which you need to learn about contract testing concepts:
- Introduction - Pact documentation
- The steps for reaching Pact Nirvana - Pact documentation
- [Portuguese] AT Talks: Contract test with Pact - Paulo Gonçalves
- [Portuguese] Contract tests with PACT #1 Concepts - Vinícius Ribeiro
The purpose of this material is to demonstrate how the implementation of pipeline and contract testing of provider and consumer applications should be in a way that follows what is considered the Pact Nirvana.
The diagram below is the representation of the Pact Nirvana that was implemented in this repository.
The pact-cli informed in the diagram refers to Pact Broker Client commands.
The applications used are divided into 2 directories, each with its source files (/src), tests, dockerfile, package.json, etc., as well as each application has its pipeline implemented in .github/workflows.
The provider is the application that provides an API to be consumed by another service. It's in provider-clients-service.
The provider pipeline runs only when there is a change outside the
consumer-frontend
(consumer) directory.
The consumer is the application that consumes another API. It can also be a provider. It's in consumer-frontend.
The consumer pipeline runs only when there is a change outside the
provider-clients-service
(provider) directory.
- Docker and Docker-compose installed.
make consumer-test-contract
After execution, the contract is generated in consumer-frontend/pacts and log.
As shown in the diagram, publishing the generated pact in Pactflow is done using the publish command from pactfoundation/pact-cli and only in CI, as can be seen in the consumer-ci pipeline.
make provider-test-contract
To be able to run the provider test, change the docker-compose file as shown in the example below to use the Pactflow read token and allow you to download the contracts published by the consumer.
environment:
- - PACT_BROKER_TOKEN
+ - PACT_BROKER_TOKEN=7L0Ijmqyh7LOMk5DyqteLQ
- PACT_URL
Publishing the execution result is done only via CI and with a writing token. (publishVerificationResult: process.env.CI == 'true'
)
When the consumer publishes a new contract, it is necessary to ensure that the provider has performed on top of this contract.
For this to happen, a webhook is used in Pactflow which, when identifying that a contract with changed content or a new branch has been published, triggers the provider's pipeline by passing the URL of this contract.
The pipeline called via webhook is provider-ci-triggered-by-webhook.yml.
The configuration of can-i-deploy
in the consumer pipeline allows it to wait for the contract test to be executed by the provider and the publication of the result to know whether or not it is possible to proceed with the new contract.
It is important that teams have visibility into the integration between applications, and the way found for this in contract testing is to publish the result of the contract test performed on the provider side in the commit status in the consumer repository.
Every time the contract test is executed on the provider side, regardless of the status of this test, the result of the integration between the applications will be published in the consumer repository with a link to the pactflow.
You will need to create 1 webhook for each consumer
The status on commit shows which providers have verified the published consumer and which branch have been executed.
In the example in the image, the consumer is successfully integrating with the client-service provider that is deployed to production
and in the main
branch.
Clicking on details will open the Pactflow page containing the contract published in the commit and all the execution details.