Difficulty: Intermediate
Estimated Time: 30 minutes

Consumer-drive contracts (CDC) was first promoted as a testing technique in 2006 by Ian Robinson. CDC has been slow to adapt over the years, but remains a vital testing technique. Recently our community has adopted a keener interest in CDC as it's a helpful method for testing microservices.

CDC is a concept and testing approach that embraces the perspective of multiple consumers that communicate with providers. Typically, testing tends to define API contracts from the providers perspective. Through a registry of contracts, multiple consumers now have a voice to provide producers their expectations on how data should be exchanged between the consumers and producers.

Over the years the Pact Foundation has established itself as the primary framework that implements this testing approach.

This scenario leverages on Kubernetes as it's ideal for running microservices and containers.

You will explore a sample application composed of several microservices. The application aggregates population and COVID-19 datasources and presents data at an API gateway. We will use Pact to verify the API.

You will learn how to:

  1. set up a Pact Broker on Kubernetes
  2. write a consumer that defines and publishes Pact contracts
  3. deploy and run a few Spring Boot microservices on Kubernetes
  4. connect microservices to a database and public data source
  5. verify the consumer pacts against a producer
  6. find API defects and fix them

Hopefully, you can now start to see how changing the perspective of test by embracing the consumer perspectives can be a powerful testing technique, especially for Microservices.

With an established library of contacts, a producer writer can have much more confidence when making adjustments to the API that others consumer. With any change they can run pactVerify, to help understand the impact of changes to their consumers.

Consumers can work with customer requirements and evolving APIs by mocking up new communication scenarios. Those new contacts can be published and verified against their providers. The Pacts provide opportunities for conversations and understanding impacts of change while maintaining a clean separation between the producers and consumers.

Lessons Learned

With these steps you have learned how to:

  • ✔ set up a Pact Broker on Kubernetes
  • ✔ write a consumer that defines and publishes Pact contracts
  • ✔ deploy and run a few Spring Boot microservices on Kubernetes
  • ✔ connect microservices to a database and public data source
  • ✔ verify the consumer pacts against a producer
  • ✔ find API defects and fix them

References


For a deeper understanding of these topics and more join
Jonathan Johnson
at various conferences, symposiums, workshops, and meetups.

Software Architectures ★ Speaker ★ Workshop Hosting ★ Kubernetes & Java Specialist

Consumer-driven Contracts with Kubernetes

Step 1 of 13

Your Kubernetes Cluster

For this scenario, Katacoda has just started a fresh Kubernetes cluster for you. Verify it's ready for your use.

kubectl version --short && \ kubectl get componentstatus && \ kubectl get nodes && \ kubectl cluster-info

The Helm package manager used for installing applications on Kubernetes is also available.

helm version --short

Kubernetes Dashboard

You can administer your cluster with the kubectl CLI tool or use the visual Kubernetes Dashboard. Use this script to access the protected Dashboard.

token.sh

Terminal
Kubernetes Dashboard
Pact Broker
H2
Population
COVID-19
Aggregator