consumer driven contracts (ddd perth 2016)

70
Consumer Driven Contracts Say what you mean and mean what you say @robdcrowley robdcrowley

Upload: rob-crowley

Post on 15-Apr-2017

1.061 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Consumer Driven Contracts (DDD Perth 2016)

Consumer Driven ContractsSay what you mean and mean what you say

@robdcrowley

robdcrowley

Page 2: Consumer Driven Contracts (DDD Perth 2016)

Session Goals

Learn how consumer driven contracts facilitate a low friction approach to service evolution

Explore how Pact enables service providers to know if a change would break consumers before deploying

Consumer driven contracts as a catalyst for cultural change

Empower you with innovative ideas to bring back to your organisation!

Page 3: Consumer Driven Contracts (DDD Perth 2016)

Digital disruption is driving change across established industries

Page 4: Consumer Driven Contracts (DDD Perth 2016)

The agility to respond to rapidly changing customer needs is vital for remaining competitive

Page 5: Consumer Driven Contracts (DDD Perth 2016)

Software architecture must also become more agile

Page 6: Consumer Driven Contracts (DDD Perth 2016)

Modern Architectures are Complex

Service

Service

Service

Service Service

Service

Service Service

Service

Service

Service

Client

Client

Client

Service

Service

Service

Service

Page 7: Consumer Driven Contracts (DDD Perth 2016)

Some people, when confronted with a problem, think “I know, I'll use system integration tests.” Now their

problems scale combinatorially.

Page 8: Consumer Driven Contracts (DDD Perth 2016)

Combinatorial Scaling2 code paths: 26 = 64

4 code paths: 46 = 4096

6 code paths: 66 = 46656

8 code paths: 86 = 262144

Service

Service

Service

Service Service

Service

Service

Service

Service

Service

Service

Service

Client

Client

Client

Page 9: Consumer Driven Contracts (DDD Perth 2016)

“Reliability problems largely occur at module boundaries, and most result from inconsistencies in both sides expectations”

- Bertrand Meyer, Object Success

Page 10: Consumer Driven Contracts (DDD Perth 2016)

Implicit InterfacesNo explicit interface between collaborators enforced by type system or compiler checks

Service

Service

Service

Service Service

Service

Service

Service

Service

Service

Service

Service

Client

Client

Client

Page 11: Consumer Driven Contracts (DDD Perth 2016)

Implicit InterfacesNo explicit interface between collaborators enforced by type system or compiler checks

Let’s change and deploy a service

Service

Service

Service

Service Service

Service

Service

Service

Service

Service

Service

Service

Client

Client

Client

Page 12: Consumer Driven Contracts (DDD Perth 2016)

Implicit InterfacesNo explicit interface between collaborators enforced by type system or compiler checks

Let’s change and deploy a service

Hope we don’t break existing collaborators

Service

Service

Service

Service Service

Service

Service

Service

Service

Service

Service

Service

Client

Client

Client

Page 13: Consumer Driven Contracts (DDD Perth 2016)

Integration Tests

Slow (longer feedback cycle)

Brittle

Difficult to manage

Finding the actual issue requires triage

Scale combinatorially

Written after the code rather than before

Page 14: Consumer Driven Contracts (DDD Perth 2016)

So if integration tests are not the solution then what is?

Page 15: Consumer Driven Contracts (DDD Perth 2016)

So if integration tests are not the solution then what is?

Test Symmetry

Page 16: Consumer Driven Contracts (DDD Perth 2016)

symmetrys m tri/ˈ ɪ ɪnoun

The correspondence of the form and arrangement of elements or parts on opposite

sides of a dividing line.

Page 17: Consumer Driven Contracts (DDD Perth 2016)

Test Symmetry

A B

Page 18: Consumer Driven Contracts (DDD Perth 2016)

A B

Mock

Test Symmetry

Page 19: Consumer Driven Contracts (DDD Perth 2016)

A B

MockTrivial

Test Symmetry

Page 20: Consumer Driven Contracts (DDD Perth 2016)

A B

MockTrivial Tricky

Test Symmetry

Page 21: Consumer Driven Contracts (DDD Perth 2016)

Symmetrical tests have the exact opposite properties

Page 22: Consumer Driven Contracts (DDD Perth 2016)

Pactwww.pact.io

Page 23: Consumer Driven Contracts (DDD Perth 2016)

Know when you break a consumer before deploying the change

Page 24: Consumer Driven Contracts (DDD Perth 2016)

Specification by Example

Page 25: Consumer Driven Contracts (DDD Perth 2016)

Pact – Define Expectations

Consumer Provider

PACT

HTTP Request

HTTP Response

Record

Page 26: Consumer Driven Contracts (DDD Perth 2016)

Pact – Verify Expectations

Consumer Provider

PACT

HTTP Request

HTTP Response

Replay

Page 27: Consumer Driven Contracts (DDD Perth 2016)

Fast (rapid feedback cycle)

Reliable

Low ramp up cost

Straightforward to debug

Scale linearly

Contract Tests

Page 28: Consumer Driven Contracts (DDD Perth 2016)

Given ”products exists”Upon receiving ”a GET request for product catalogue”

With “an Accept header of application/hal+json”Will respond with ”200 OK”

And “a Content-Type header of application/hal+json”And ”the product catalogue details in the entity body”

Page 29: Consumer Driven Contracts (DDD Perth 2016)

Given ”products exists”Upon receiving ”a GET request for product catalogue”

With “an Accept header of text/plain”Will respond with ”406 Not Acceptable”

Page 30: Consumer Driven Contracts (DDD Perth 2016)

Given ”Widget product does not exist”Upon receiving ”a GET request for Widget product”

Will respond with ”404 Not Found”

Page 31: Consumer Driven Contracts (DDD Perth 2016)

Examples

Page 32: Consumer Driven Contracts (DDD Perth 2016)

Joining the dots...

ProductSales

Service

ProductCatalogue

Service

ProductStock

Service

PactBroker

git clone https://github.com/robcrowley/microservices-pact-demo.git

JUnit for pact generation

Gradle pact plugin for provider verification

Pact Broker for pact repository

Docker integration

Page 33: Consumer Driven Contracts (DDD Perth 2016)

Start with the consumer

Page 34: Consumer Driven Contracts (DDD Perth 2016)
Page 35: Consumer Driven Contracts (DDD Perth 2016)
Page 36: Consumer Driven Contracts (DDD Perth 2016)
Page 37: Consumer Driven Contracts (DDD Perth 2016)
Page 38: Consumer Driven Contracts (DDD Perth 2016)

Then move onto the provider

Page 39: Consumer Driven Contracts (DDD Perth 2016)
Page 40: Consumer Driven Contracts (DDD Perth 2016)
Page 41: Consumer Driven Contracts (DDD Perth 2016)
Page 42: Consumer Driven Contracts (DDD Perth 2016)
Page 43: Consumer Driven Contracts (DDD Perth 2016)

“Be liberal in what you accept, and conservative in what you send”

- Jon Postel, The Robustness Principle

Page 44: Consumer Driven Contracts (DDD Perth 2016)

Implications of Postel’s Law

An expectation may not be made for a field to be absent from a response

Exception: Request headers that change the behaviour of the provider must be specified in expectations

Page 45: Consumer Driven Contracts (DDD Perth 2016)

Sharing Pacts

Page 46: Consumer Driven Contracts (DDD Perth 2016)

Pact Broker

Consumer Provider

Pact Broker

Page 47: Consumer Driven Contracts (DDD Perth 2016)

Pact Broker

Consumer Provider

Pact BrokerPublish

PUT /pacts/provider/{provider}/consumer/{consumer}/versions/{version} HTTP/1.1

Page 48: Consumer Driven Contracts (DDD Perth 2016)

Pact Broker

Consumer Provider

Pact BrokerPublish

PUT /pacts/provider/{provider}/consumer/{consumer}/versions/{version} HTTP/1.1

Pull

GET /pacts/provider/{provider}/consumer/{consumer}/latest HTTP/1.1

Page 49: Consumer Driven Contracts (DDD Perth 2016)

Facilitates the sharing of pacts between consumer and provider projects

Enables service release cycles to be decoupled

Example driven API documentation

Visualisations of relationships between services

Web Hooks to provide integration with CI Builds

Pact Broker

Page 50: Consumer Driven Contracts (DDD Perth 2016)
Page 51: Consumer Driven Contracts (DDD Perth 2016)
Page 52: Consumer Driven Contracts (DDD Perth 2016)

Backwards Compatibility TestingUse Tags to ensure provider is compatible with latest and released versions of consumers

PUT /participants/{consumer}/versions/{version}/tags/{tag} HTTP/1.1

GET /pacts/provider/{provider}/consumer/{consumer}/latest/{tag} HTTP/1.1

Consumer Head (Pact Broker)

Consumer Production(Pact Broker)

Provider Head(Source Control)

Contract TestsContract Tests

Producer backwards compatibility

Provider Production(Source Control)

Contract TestsConsumer backwards

compatibilityAlready Tested

Page 53: Consumer Driven Contracts (DDD Perth 2016)

So once contract tests are in place I can throw all of my integration tests away?

Page 54: Consumer Driven Contracts (DDD Perth 2016)

It depends on the criticality of the business capability the service supports and your risk appetite

Page 55: Consumer Driven Contracts (DDD Perth 2016)
Page 56: Consumer Driven Contracts (DDD Perth 2016)

Make production monitoring and alerting a priority

Page 57: Consumer Driven Contracts (DDD Perth 2016)

Other Benefits

Page 58: Consumer Driven Contracts (DDD Perth 2016)

Higher quality services

Page 59: Consumer Driven Contracts (DDD Perth 2016)

“Quality comes from deliberate thought”

- Michael Feathers, Testing Patience

Page 60: Consumer Driven Contracts (DDD Perth 2016)

Reduced waste in delivery process

Page 61: Consumer Driven Contracts (DDD Perth 2016)

The business value realized by a service is defined by the parts of it’s provider contract being leveraged

by consumers

Page 62: Consumer Driven Contracts (DDD Perth 2016)

“The biggest cause of failure in software-intensive systems is not technical failure; it’s

building the wrong thing.”

- Mary Poppendieck

Page 63: Consumer Driven Contracts (DDD Perth 2016)

Focusing on delivering the capabilities as defined in consumer contracts drives building the right thing

Page 64: Consumer Driven Contracts (DDD Perth 2016)

TakeawaysConsumer Driven Contracts enable services to evolve gracefully

Pact combines isolated unit testing with contract testing

Pact enables delivery at speed by providing rapid feedback on potential breakages

Pact allows test driven development principles to be applied to services

Consumer Driven Contracts can be a catalyst for cultural change by fostering collaboration between delivery teams and reducing waste

Page 65: Consumer Driven Contracts (DDD Perth 2016)

“Deploying to production quickly is easy; doing so quickly and responsibly

however is non trivial”

- Rob Crowley

Page 66: Consumer Driven Contracts (DDD Perth 2016)

Adopting a consumer driven contract testing approach can help

Page 67: Consumer Driven Contracts (DDD Perth 2016)

Questions

Page 68: Consumer Driven Contracts (DDD Perth 2016)

More Info

Gitbook: docs.pact.io

Github: realestate-com-au/pact and DiUS/pact-jvm

User group: https://groups.google.com/forum/#!forum/pact-support

Twitter: pact_up

Pact to the Rescue!: http://www.slideshare.net/bethesque/pact-44565612

Deploy with Confidence using Pact Go!: https://www.youtube.com/watch?v=SMadH_ALLII

Consumer Driven Contracts – A Service Evolution Pattern: http://martinfowler.com/articles/consumerDrivenContracts.html

Page 69: Consumer Driven Contracts (DDD Perth 2016)

Appendices

Page 70: Consumer Driven Contracts (DDD Perth 2016)

Contracts 101

Provider Consumer DrivenConsumer

Closed & Complete

Singular & Authoritive

Bounded Stability &Immutability

Open & Incomplete

Mutable & Non-Authoritive

Bounded Stability &Immutability

Closed & Complete

Singular & Non-Authoritive

Bounded Stability &Immutability