Schedule a FREE onboarding and start making pipelines fast.

How to Test Microservice Integration with Pact

Docker Tutorial | October 9, 2017

The Challenge : Microservice Integration Testing

The move to microservices brings new challenges with regards to testing our systems. In theory each microservice should be able to operate in isolation. But in practice a service usually isn’t worth much without its counterparts. On the other hand – bringing up the full system topology for testing just one service cancels out the benefits of modularity and encapsulation that microservices are expected to bring.

The challenge here is verifying that integration with other services isn’t broken. We want to do this as early as possible. And we don’t want to to replicate the complex production environment for this. Traditionally this verification was the function of integration or so-called end-to-end tests.  But the fact is that the more complex our systems grow –  the less value end-to-end tests bring. The sheer amount of interdependencies leads to multiple false negatives and excessively long execution times. Effectively making such tests too expensive to manage and debug.

There’s even an accepted concept of a test pyramid (first described by Mike Cohn in his book ‘Succeeding with Agile’) which basically says that for optimal effect you should have much more low-level unit tests than high-level end-to-end tests.

Psst, read this article and make sure to check out Codefresh, it’s the best CI for Docker out there.

The Test Pyramid

Unit tests are great! But with all the benefits they bring – they provide no value for testing integration with other services.

So how do we allow each service team to iterate independently but still preserve the overall health of our system? How do we enable Continuous Delivery, small batch sizes and short feedback loops without breaking interdependent services on every other change?

One of the possible answers is Consumer-Driven Contract (CDC) testing. This testing strategy is based on a service evolution pattern first defined more than a decade ago. And it’s getting wider adoption now that distributed systems are becoming more and more commonplace.

 

Consumer-Driven Contracts:

I’ll try to explain this in very basic terms. Consumer-Driven Contracts are all about service-oriented relationship between… well, services. What this means is that instead of a provider dictating what the interface and service level will be (while consumers try their best to adapt) – now the consumers lead the dance. Each consumer defines what it expects the provider to deliver and the provider has to do the checking. It’s about shifting the responsibility for the integration to the providing side.

Which leads to the following flow:

In business terms this is usually described as ‘putting the customer first’ or ‘listening to your customers’. Because in order to provide great service we need to do what our customers expect and need. Not what we may assume the right thing to do is.

When talking about evolving microservices – this becomes especially important in a large enterprise setting where each service may be developed by a separate team. Sometimes these teams may also reside in different geographical and temporal regions. This prevents the immediacy of communication and makes collaborative evolution of business functionality ever more challenging.

Contract Testing Frameworks

Consumer-driven contracts can of course be managed by investing in communication and collaboration between teams. And also by using structured serialization formats such as protobuf, thrift or messagepack. But to manage a defined flow – it’s great to have a framework. Especially if it’s an open-source one.

And such frameworks have indeed emerged. The most prominent and actively developed ones are Pact and Spring Cloud Contract. The latter is only targeting the JVM-based projects. Pact, on the other hand was initially written for Ruby but has since been ported to quite a number of languages, including Java, Go, Python and Javascript. Which makes it a perfect choice for a complex, polyglot microservice system.

Today we will see how to use Pact in order to define and verify the contract between 2 services. A consumer service written in Python. And a provider service written in Go. The testing will occur as a part of our CI/CD process – right inside the Codefresh pipeline.

Pact

So, how does Pact work? Well, it all starts with the consumer.

The developer of the consumer service writes a test. The test defines an interaction with a provider. This includes a state the provider should be in, the body of the request and the expected response. Based on this definition Pact creates and spins up a provider stub against which the test executes. The output of this test is one or more json files that look something like this:

These are the contracts, the pacts. They are now passed on to the provider service. This can be done by committing them to a shared git repo, by uploading to a shared file storage or by using a special Pact Broker application.

Once the contract gets updated – the provider has to test if it still obeys it. It now runs its own verification tests against a real-life version of its service, using the shared pact file. If all the interactions go as expected and all the tests succeed – we’re good to go.  If not – the developers of the provider should notify the developers of the consumer. Then, together they can analyze what led to contract breach.

Our example:

We will be testing the integration of 2 small services.

The provider is the same service we’ve used in our Jenkins plugin example. It’s called ‘bringon’, is written in Go and is a mongoDB-backed registry of software build information.

Our consumer will be a tiny python-based client that currently knows only how to fetch build information by build number from the bringon service.

As consumer comes first in CDC – we’ll start with it.

The whole code of the consumer currently consists of one client.py file that has 2 functions in it. We’re only concerned with the function called ‘build’ – as this is the functionality we will be testing.

 

 

In order to generate a pact for it – we created a test file build_test.py which looks like this:

It’s pretty straightforward – we’re spinning up a mock service, defining an expected http response code and body, and then calling the client.build() routine to make sure the interaction occurs as expected.

If all goes well – a pact file named buildreader-bringon.json gets written in our working directory.

Now we can share this file with the bringon developers so they can test the pact against their service.

This can be done with the help of pact-go – the Golang port of the framework. The test would look something like this:

 

Note this requires quite a bit more work. We need to implement the startInstrumentedBringon() function which starts our service with an additional ‘/setup’ endpoint for defining the service state. In our case this will be used for creating a build entry which fits our consumer’s expectations. And we’ll also need to create a Pact client object that takes care of verifying all the interactions. Like this:

One downside of using pact-go in its current form is it requires you to run a daemon in the background. The daemon takes care of service initiation, shutdown and pact verification.

Not really a good fit for running one-off ephemeral processes inside containers.

So if all we want to do is test the pacts against our service – we can instead use the lightweight pact-provider-verifier utility that comes packed with pact-go.

Like this:

 

Please note that in this case we’ll have to implement and build the ‘/setup’ endpoint as a part of our service. Which can be a good idea if we want to make our service ultimately testable.

The code for both services can be found on our Github:

bringon (or The Provider) :

https://github.com/codefreshdemo/bringon

buildreader (or The Consumer):

https://github.com/antweiss/cdc-pact-demo

Pact sources and examples are here:

https://github.com/pact-foundation

In the next installment of this blog we will show how to run the contract test as a part of your Codefresh pipeline.

And what’s your favorite strategy for testing microservice integration?

New to Codefresh? Schedule a FREE onboarding and start making pipelines fast. and start building, testing and deploying Docker images faster than ever.

 

Anton Weiss

About Anton Weiss

Ant(on) Weiss is a DevOps Evangelist and Enabler. He's been architecting software delivery processes at large and small companies for the last 15 years. Today he's the CEO and Principal Consultant at Otomato. When not evangelising for Codefresh, he's deeply involved with technical training and public speaking.

Reader Interactions

Enjoy this article? Don't forget to share.

Comments

  1. Hi Anton, what about pure event driven services integrated via Kafka/AMQP, are these frameworks ready to be adopted? In my current case even UI is considered as fully asynchronous microservice, so we don’t plan to use much of HTTP endpoints at all.

    • Hi Ladislav. I agree – asynchronous systems are a very real challenge. For JVM world there’s awaitility :https://github.com/awaitility/awaitility. But I’m not aware of any existing framework for non-jvm technologies. Will research this deeper and come back if I have any findings.

Comments

Your email address will not be published. Required fields are marked *

Follow me on Twitter