● One service, one client
● API changes are easy
● One client team to sync with for migrations
● API deprecation is easy
Testing, monolith style
The good ol’ days
Slide 7
Slide 7 text
● Different requirements per client
=> Code complexity increases
● Harder to deploy without breaking at least one client
Testing, monolith style
Mo clients, mo problems
Slide 8
Slide 8 text
● More manual QA
expensive, slow, prone to human error,
doesn’t scale
● More end-to-end tests
Maintenance nightmare, flaky, slow,
creates bottlenecks
Testing, monolith style
Mo clients, mo problems
Slide 9
Slide 9 text
My first steps @ SoundCloud
Slide 10
Slide 10 text
My first steps @ SoundCloud
Whoa, micro-services
Slide 11
Slide 11 text
My first steps @ SoundCloud
Testing strategy
E2E
Unit
Integration
Unit tests are not enough
Introducing contract tests
“#cake” >> {
result = call(“cake”)
assert(result == “lie”)
}
GET “/cake”:
return “lie”
Slide 18
Slide 18 text
Unit tests are not enough
Introducing contract tests
GET “/cake”:
return “truth”
“#cake” >> {
result = call(“cake”)
assert(result == “lie”)
}
Slide 19
Slide 19 text
Unit tests are not enough
Introducing contract tests
import JsonLibFoo
GET “/cake”:
return toJson(current_state)
// { “current_state” : “lie” }
import JsonLibFoo
“#cake” >> {
result = fromJson(call(“cake”))
assert(result == “lie”)
}
Slide 20
Slide 20 text
Unit tests are not enough
Introducing contract tests
import JsonLibBar
GET “/cake”:
return toJson(current_state)
// { “currentState” : “lie” }
import JsonLibBar
“#cake” >> {
result = fromJson(call(“cake”))
assert(result == “lie”)
}
Slide 21
Slide 21 text
Introducing contract tests
How this works?
Consumer Provider
End-to-End Test
Slide 22
Slide 22 text
Introducing contract tests
How this works?
Consumer Provider
Unit Test Unit Test
Slide 23
Slide 23 text
Introducing contract tests
How this works?
Consumer Provider
Unit Test Unit Test
Slide 24
Slide 24 text
Introducing contract tests
How this works?
➢ Requesting “/cake”.
➢ Expecting a JSON response
with a key “current_state”.
➢ Asserting deserialization
of the response succeeds.
import JsonLibFoo
GET “/cake”:
return toJson(current_state)
// { “current_state” : “lie” }
Consumer
Provider
Slide 25
Slide 25 text
Pactifying our services
http://pact.io
Slide 26
Slide 26 text
What’s Pact?
Pactifying our services
● A family of frameworks designed for contract testing.
● Supports JSON over HTTP.
● Impl. for Java, Scala, Go, Ruby, .Net, Swift, JS etc.
● Tests run locally, no external dependencies.
Pactifying our services
Provider side verification
● Collect pact files from consumers and verify them all.
● Keep your provider environment isolated.
○ Dockerized databases.
○ Test doubles for upstream services.
Slide 55
Slide 55 text
Pactifying our services
Provider states
Slide 56
Slide 56 text
Pactifying our services
Provider states
Likes
App
Likes
DB
Slide 57
Slide 57 text
Pactifying our services
Provider states
Set state:
“User 1000 has liked 2 tracks”
Likes
App
Likes
DB
Slide 58
Slide 58 text
Pactifying our services
Provider states
INSERT
likes
Set state:
“User 1000 has liked 2 tracks”
Likes
App
Likes
DB
Slide 59
Slide 59 text
Pactifying our services
Provider states
INSERT
likes
Set state:
“User 1000 has liked 2 tracks”
HTTP Request: GET /likes/1000
Likes
App
Likes
DB
Slide 60
Slide 60 text
Pactifying our services
Provider states
HTTP Request: GET /likes/1000
HTTP Response: 200 OK
INSERT
likes
Set state:
“User 1000 has liked 2 tracks”
Likes
App
Likes
DB
Slide 61
Slide 61 text
Pactifying our services
Provider states
Likes
App
Likes
DB
HTTP Response: 200 OK
INSERT
likes
Set state:
“User 1000 has liked 2 tracks”
HTTP Request: GET /likes/2000
HTTP Response: 404 Not Found
Set state:
“User 2000 has liked no tracks”
HTTP Request: GET /likes/1000
DELETE
likes
Slide 62
Slide 62 text
Pactifying our services
Pact broker
● Share pact files between projects.
● API for pact frameworks to fetch all pacts by provider.
● Support for versioning and tagging.
● Useful UI and visualization of the network.
Pactifying our services
Our CI pipeline
Push new
change
Generate
consumer
contracts
Deploy
Upload
pacts & tag
with ‘prod’
Consumer pipeline
Slide 67
Slide 67 text
Pactifying our services
Our CI pipeline
Push new
change
Deploy
Upload
pacts & tag
with ‘prod’
Consumer pipeline
Push new
change
Verify all
‘prod’-tagged
pacts
Deploy
Provider pipeline
Generate
consumer
contracts
Slide 68
Slide 68 text
● Communication is key.
Caveats
Slide 69
Slide 69 text
● Communication is key.
● New moving parts.
Caveats
Slide 70
Slide 70 text
● Communication is key.
● New moving parts.
● Learning curve per pact framework.
Caveats
Slide 71
Slide 71 text
● Communication is key.
● New moving parts.
● Learning curve per pact framework.
● Frameworks are WIP.
Caveats
Slide 72
Slide 72 text
● Communication is key.
● New moving parts.
● Learning curve per pact framework.
● Frameworks are WIP.
● Postel's law (the robustness principle).
Caveats
Slide 73
Slide 73 text
● Communication is key.
● New moving parts.
● Learning curve per pact framework.
● Frameworks are WIP.
● Postel's law (the robustness principle).
● Provider side setup is time consuming.
Caveats
Slide 74
Slide 74 text
● Communication is key.
● New moving parts.
● Learning curve per pact framework.
● Frameworks are WIP.
● Postel's law (the robustness principle).
● Provider side setup is time consuming.
● Automating consumer-triggered provider verification.
Caveats
Slide 75
Slide 75 text
● Communication is key.
● New moving parts.
● Learning curve per pact framework.
● Frameworks are WIP.
● Postel's law (the robustness principle).
● Provider side setup is time consuming.
● Automating consumer-triggered provider verification.
● Adoption is essential.
Caveats
Slide 76
Slide 76 text
Recap
Slide 77
Slide 77 text
Recap
Write contract tests
Consum
ers
Slide 78
Slide 78 text
Recap
Write contract tests Generate pact files
Consum
ers
Slide 79
Slide 79 text
Recap
Write contract tests Generate pact files
Publish to broker
Consum
ers
Slide 80
Slide 80 text
Recap
Write contract tests Generate pact files
Publish to broker
Verify pacts
Consum
ers
Providers
Slide 81
Slide 81 text
Recap
Write contract tests Generate pact files
Publish to broker
Verify pacts
Consum
ers
Providers