Consumer-Driven Contracts with Pact (Sydney API Days 2015)

40
Pacts to the rescue! Making your microservices play nicely together with Consumer Driven Contracts Beth Skurrie @bethesque / @pact_up [email protected]

Transcript of Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Page 1: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Pacts to the rescue!Making your microservices play nicely together

with Consumer Driven Contracts

Beth Skurrie@bethesque / @[email protected]

Page 2: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

“Integrated tests are a scam. A self replicating virus that threatens the very health of your codebase, your sanity, and I'm not exaggerating when I say, your life.”

- JB Rainsberger

Page 3: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Integrated Tests: The Problems

! Slow! Easy to break! Hard to fix! Scales combinatorially

3 classes, 4 code paths each4 * 4 * 4 = 64 tests

Page 4: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

A B

mock

Test Symmetry… how it works

Page 5: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Test 1

Test 2

Test 3

Integrated tests

Page 6: Consumer-Driven Contracts with Pact (Sydney API Days 2015)
Page 7: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

! Lack of automated tools! Relies on developer eternal vigilance! Does not scale

Test Symmetry: The Problem

Page 8: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Do we have the technology to achieve test symmetry?

Posed to DiUS

Page 9: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Slower Easier to break

Harder to fix Scales BADLY

Lots of infrastructure

False negativesLots of set up

System Integration Tests

Page 10: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Pact to the rescue!!!

Page 11: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

A B

mock

C P

pact

Pact… how it works

Page 12: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Slower Easier to break

Harder to fix Scales BADLY

Lots of infrastructure

False negativesLots of set up

System Integration Tests

Page 13: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Fast Stable

Easy to debug Reliable

No extra infrastructure

Low setupScales linearly

Pact

Page 14: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Better API design!

Page 15: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Arrange - set up mock server

Page 16: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Arrange - set up mock server

Page 17: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Arrange - set up mock server

Page 18: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Arrange - set up expectations

Page 19: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Arrange - set up expectations

Page 20: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Arrange - set up expectations

Page 21: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Act and Assert

Page 22: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Act and Assert

Page 23: Consumer-Driven Contracts with Pact (Sydney API Days 2015)
Page 24: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Act and Assert

Page 25: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Act and Assert

Page 26: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Act and Assert

Page 27: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Set up test data

Consumer assumed:

Provider complies:

Page 28: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Set up test data

Consumer assumed:

Provider complies:

Page 29: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Verify: Fail

Page 30: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Verify: Success!

Page 31: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Postel’s law

Page 32: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Sharing pacts

Page 33: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

P

publish pact latest pact

C

Pact Broker

Pact Broker - Share pacts

Page 34: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Pact Broker - Autogenerated documentation

Page 35: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Pact Broker - Network graph

Page 36: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Support

! JSON is language independent! Ruby! JVM (Java and Scala)! .net! Javascript (consumer side, provider side WIP)! Pact specification

Page 37: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

“How is it working for you?”

Page 38: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Fast feedback in isolation +

Early detection +

Rapid recovery

Page 39: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

Pact allows you to:

! Combine the advantages of integrated tests and test symmetry! Mitigates the shortcomings of each method! Allows you to modify components with agility - quick feedback

on potential breakages! Have confidence that all the services in your system will work

together! Throw away your integration tests!!!

Page 40: Consumer-Driven Contracts with Pact (Sydney API Days 2015)

PactMakes your microservices play nicely together with Consumer Driven Contracts

github.com/realestate-com-au/pact@pact_up

Beth [email protected] @bethesque

• Regular expressions & type based matching

• Why doesn’t Pact use JSON Schema?

• How to avoid versioning APIs • How to use Pact between teams • Should I use Pact for testing external APIs?

• How not to do it • And moar!