1
© 2018 Pivotal
Contract Tests in the Enterprise
Marcin Grzejszczak, @mgrzejszczak
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
About me
Spring Cloud developer at Pivotal working mostly on
● Spring Cloud Sleuth
● Spring Cloud Contract
● Spring Cloud Pipelines
Connect with me
● Twitter: @mgrzejszczak
● Blog: http://toomuchcoding.com
2
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
#frameworkless
#backtothebasics
3
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Just another day at work
4
http://4.bp.blogspot.com/-6OK2CgfysNQ/U86JhcS_OKI/AAAAAAAAurI/O5lNezASrwo/s1600/10373729_294545550713169_1314239177487458186_n.jpg
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How the code is currently organized
5
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How our CI system looks like
6
https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.findbestopensource.com%
2FAppImages%2FArticle%2Fjenkins-img1.jpg&f=1
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How the code is currently deployed
7
https://www.process.st/business-process-modeling/
15 HOURS OF E2E
TESTS PLACE HERE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
We’re agile & we care about quality
8
THE SPRINT (6 MONTHS)
5 MONTHSLAST MONTH
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Application Architecture
9
Legacy
service
Customer Rental
History service
Payment
processor
Mainframe
Audit service
Insurance
service
● Interservice calls are over http
● Legacy service that is hard to test due the dependencies it
has on other downstream services
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What’s our testing strategy?
10
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How the code is currently tested
11
https://www.ontestautomation.com/wp-content/uploads/2014/06/pyramid_inverted.png
MANUAL TESTING
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
The testing pyramid...
12
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Unit Test
In computer programming, unit testing is a software testing method by which
individual units of source code, sets of one or more computer program
modules (...) are tested to determine whether they are fit for use.
13
Kolawa, Adam; Huizinga, Dorota (2007). Automated Defect Prevention: Best Practices in Software Management. Wiley-IEEE Computer Society Press. p. 75. ISBN 0-470-04212-5.
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Great! I’ll write unit tests only!
14
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Unit tests FTW!
15
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Integration Test
Integration testing (...) is the phase in software testing in which individual
software modules are combined and tested as a group. (...) Integration testing
takes as its input modules that have been unit tested, groups them in larger
aggregates, applies tests (...) and delivers as its output the integrated system
ready for system testing
16
Martyn A Ould & Charles Unwin (ed), Testing in Software Development, BCS (1986), p71.
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Ok I don’t need unit tests, I’ll write only
integration tests
17
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Integration tests FTW!
18
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
19
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
End to end Test
End-to-end testing involves ensuring that the integrated components of an
application function as expected. The entire application is tested in a real-world
scenario such as communicating with the database, network, hardware and other
applications.
20
https://www.techopedia.com/definition/7035/end-to-end-test
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
End to end tests FTW!
21
https://www.process.st/business-process-modeling/
15 HOURS OF E2E
TESTS PLACE HERE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
End to end tests FTW!
22
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Speaking of End to End tests…
The Customer Rental History is unreliable
23
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Application Architecture
24
Legacy
service
Customer Rental
History service
Payment
processor
Mainframe
Audit service
Insurance
service
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Testing Strategy - Hard Coded Object Stubs
25
Legacy
service
Customer Rental
History service
Payment
processor
Audit service
Insurance
service
Hard coded
Customer
Rental History
Service Stub Mainframe
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Problems with hard coding stubs
26
● 1225 line JSON response
● Hard to keep hard coded
stub in sync with the api
being stubbed
● Stub does not test that
requests are made
correctly to the remote
service
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Problems with hard coding stubs
27
Stubs that were used in the build phase have nothing to do with the real API!!
/foo
LEGACY APP
CUSTOMER
RENTAL
HISTORY
TEST
OK
/foo
LEGACY APP
CUSTOMER
RENTAL
HISTORY
REALITY
WAT?!
If /foo
respond
with OK
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How to create reliable stubs for services
we don’t control?
28
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Let’s proxy the calls!
29
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Generating Stubs From Proxy
30
Test that calls
Customer
Rental History
Service
Customer Rental
History Service
Payment
processor
Mainframe
PROXY
Record traffic and
dump stubs (e.g.
once per day)
Upload stubs for
other teams to use
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Stubs From Proxy
31
Given this
request
Send back
this
response
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Now that we have stubs,
how can we test the system?
32
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Testing Strategy - Generated Remote Stub
33
Legacy
service
Customer Rental
History Service
Payment
processor
Customer Rental
History Service Stub
from the proxy
Legacy service
integration test
Mainframe
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Outcome?
Fast feedback from quite reliable stubs
Less money spent on e2e
34
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
OK, we’ve dealt with the app we don’t control.
How about those we do control?
35
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Hard Problems
● How can something be added to a service without breaking the clients?
● How can something be removed from a service without breaking the clients?
● How can a service developer find out how clients are using their service?
● How to enable short release cycles and continuous delivery of services?
36
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Service Evolution Patterns
● Tolerant Reader
○ Extract only what is needed, ignore unknown content
● Schema Versioning
○ Put Schema versions in the API url / headers (v1, v2)
● Extension Points
○ Additional, optional data structured in a predefined extension field
● Consumer-Driven Contracts
37
http://servicedesignpatterns.com/WebServiceEvolution
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Definitions
● Producer
○ service that exposes an API / sends a message
● Consumer
○ service that consumes the API of the producer /
listens to a message from the producer
● Contract
○ agreement between producer and consumer how the API /
message will look like
38
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Provider Contract
• The implementer of a service defines the interface of the
service everyone uses the same interface
39
Service
Client A
Client B
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Consumer Contract
● The consumer of a service defines their interface to the service within the
conventions / spirit of provider contract
40
Service
Client A
Client B
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Consumer Driven Contracts
41
Source: http://martinfowler.com/articles/consumerDrivenContracts.html
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Benefits of Consumer Driven Contracts
● Align service providers with the business goals of their consumer through the
consumer driven contracts
● Provide insights into how a service is being used by its consumers
● Make evolving services easier by reducing the amount of coordination
required to evolve interfaces
● Help in the configuration of continuous delivery pipelines
42
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How the code should be tested
43
UNIT TESTS
SERVICE, API,
CONTRACT
TESTS
E2E, UI
TESTS
MANUAL
TESTS
MANUAL TESTING
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Migrating to microservices...
44
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Problem?
Our monolith is the source of truth
We don’t know if we can change its API
45
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Monolith as source of truth
46
Legacy
service
Audit service
Insurance
service
Ledger
service
Payment
service
Invoice
service
Warehouse
service
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
GET /user/GET /user/
Consumers create stubs (current approach)
47
Legacy
service
GET /user/Audit service
Insurance
service
Ledger
service
Payment
service
Invoice
service
Warehouse
service
GET /user/
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Problem?
Stubs on the consumer side not reliable
Time wasted on writing the same stubs
48
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Let’s write a Segfault Contract contract
testing framework!
#frameworkless
49
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
We need to define a contract somehow
50
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Example of a contract
51
Given this
request
Send back
this
response
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
We need to assert that contract is valid
We want to build reliable stubs
52
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Stubs
Generating Stubs & Tests (PRODUCER)
53
Legacy
service
Customer Rental
History Service
Payment
processor
Mainframe
Audit service
Insurance
service
Contract
Tests
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Generating Stubs & Tests (PRODUCER)
54
Generate reusable
stubs
Read
Run tests to verify
if contract is met
Run build
$ build project
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Contract Tests (PRODUCER)
55
Given this
request
Is the
response
correct ?
Send the
request
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Generated Stubs (PRODUCER)
56
Given this
request
Send back
this
response
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Monolith as source of truth (better approach)
57
GET
/user/
Legacy
service
POST
/user/
Audit service
Insurance
service
Ledger
service
Payment
service
Invoice
service
Warehouse
service
STUB
STORAGE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Reuse Stubs (CONSUMER)
58
Fetch stubs & run
stub servers
Consumer tests
Test
runner
Fetch stubs
STUB
STORAGE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Outcome?
Fast feedback from reliable stubs (fail fast)
Less money spent on e2e
Less time wasted on duplicating stubs
59
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
OK, what about new, shiny microservices?
60
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Nice API creation
● It’s the consumer that uses the API
● Consumers should take part in the creation of the API of the producer
● The producer’s API change should be driven by consumers
61
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Consumer Driven Contracts
62
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
There is no producer code!
63
CONSUMER
REPO WITH
CONTRACTS
INTERACTION
I THINK I LIKE THIS API
EVEN THOUGH THE
PRODUCER DOESN’T
EVEN EXIST!
WHAT IF I
CALLED THE
API LIKE THIS...
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Store the stubs from contracts locally
64
CONSUMER
REPO WITH
CONTRACTS
INTERACTION
LOCAL STORAGE
WITH STUBS
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Play with the API offline
65
CONSUMER
REPO WITH
CONTRACTS
TESTS
LOCAL STORAGE
WITH STUBS
FROM
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
File a PR
66
CONSUMER
REPO WITH
CONTRACTS
YUP, I LIKE IT!
TIME FOR A PR
PR
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Producer sets up a project
67
PRODUCER
REPO WITH
CONTRACTS
INTRACTION
OH, SOMEONE HAS
REQUESTED THAT
I HAVE SUCH API...
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Producer sets up a project
68
PRODUCER
REPO WITH
CONTRACTS
INTRACTION
THE TESTS ARE
FAILING… NO
WONDER, I HAVE
NO CODE :|
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Producer makes the generated tests pass
69
PRODUCER
REPO WITH
CONTRACTS
PR
OK, THE TESTS
ARE PASSING, LET
ME MERGE THE PR
TO MASTER
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
CI server builds the producer code
70
PRODUCER
BUILD
UPLOAD
BINARY
UPLOAD
STUBS
STUB
STORAGE
BINARY
STORAGE
CI SYSTEM
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Consumer uses uploaded stubs
71
CONSUMER
LET ME USE THE
PRODUCER’S
STUBS FOR MY
TESTS
FETCHSTUBS
STUB
STORAGE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Did you say, that ALL contracts
are stored in a single repo?
72
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
73
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
74
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
75
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
76
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What about messaging?
77
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
78
CONSUMERPRODUCER
TEST
PRODUCER CONSUMER
REALITY
topic: bar topic: foo
topic: bartopic: foo
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Messaging
79
(PRODUCER) Do I
trigger the message?
(PRODUCER) Is it sent
to a proper destination?
(PRODUCER) Can I
serialize it properly?
(CONSUMER) Do I
listen at a proper
destination?
(CONSUMER) Can I
deserialize it properly?
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
What about polyglot support?
80
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
NodeJs app
81
https://github.com/marcingrzejszczak/sc-contract-car-rental
NodeJs
Some HTTP
service
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Doesn’t work
82
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Polyglot
83
Non JVM app
Send a request
Stub will respond
RUN
STUB
STORAGE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
NodeJs works
84
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Can I generate tests for an app
in any language?
85
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Polyglot
86
contract1.yml
GET
/user/
App
contract2.yml
POST
/user/
Contracts mounted
to generate tests
and stubs
Tests executed
from the container
against a running
app
RUN
STUB
STORAGE
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
87
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
● The implemented framework: Spring Cloud Contract
● The stub server: WireMock
● Available DSLs: Groovy, YAML
● Supports:
○ messaging (Spring Cloud Stream, Spring Integration, Spring AMQP, whatever you want)
○ dynamic pieces
○ resolution of request from response
○ stateful stubs (stubs have memory)
○ you can store stubs in Git, Nexus / Artifactory, Pact Broker
● And much, much more…
● Read the docs: http://cloud.spring.io/spring-cloud-contract/
What have we managed to build?
88
#frameworkless
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons
Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Disclaimer Statement
89
mgrzejszczak

Marcin Grzejszczak - Contract Tests in the Enterprise

  • 1.
    1 © 2018 Pivotal ContractTests in the Enterprise Marcin Grzejszczak, @mgrzejszczak
  • 2.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ About me Spring Cloud developer at Pivotal working mostly on ● Spring Cloud Sleuth ● Spring Cloud Contract ● Spring Cloud Pipelines Connect with me ● Twitter: @mgrzejszczak ● Blog: http://toomuchcoding.com 2
  • 3.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ #frameworkless #backtothebasics 3
  • 4.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Just another day at work 4 http://4.bp.blogspot.com/-6OK2CgfysNQ/U86JhcS_OKI/AAAAAAAAurI/O5lNezASrwo/s1600/10373729_294545550713169_1314239177487458186_n.jpg
  • 5.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How the code is currently organized 5
  • 6.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How our CI system looks like 6 https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.findbestopensource.com% 2FAppImages%2FArticle%2Fjenkins-img1.jpg&f=1
  • 7.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How the code is currently deployed 7 https://www.process.st/business-process-modeling/ 15 HOURS OF E2E TESTS PLACE HERE
  • 8.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ We’re agile & we care about quality 8 THE SPRINT (6 MONTHS) 5 MONTHSLAST MONTH
  • 9.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Application Architecture 9 Legacy service Customer Rental History service Payment processor Mainframe Audit service Insurance service ● Interservice calls are over http ● Legacy service that is hard to test due the dependencies it has on other downstream services
  • 10.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What’s our testing strategy? 10
  • 11.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How the code is currently tested 11 https://www.ontestautomation.com/wp-content/uploads/2014/06/pyramid_inverted.png MANUAL TESTING
  • 12.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The testing pyramid... 12
  • 13.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Unit Test In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules (...) are tested to determine whether they are fit for use. 13 Kolawa, Adam; Huizinga, Dorota (2007). Automated Defect Prevention: Best Practices in Software Management. Wiley-IEEE Computer Society Press. p. 75. ISBN 0-470-04212-5.
  • 14.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Great! I’ll write unit tests only! 14
  • 15.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Unit tests FTW! 15
  • 16.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Integration Test Integration testing (...) is the phase in software testing in which individual software modules are combined and tested as a group. (...) Integration testing takes as its input modules that have been unit tested, groups them in larger aggregates, applies tests (...) and delivers as its output the integrated system ready for system testing 16 Martyn A Ould & Charles Unwin (ed), Testing in Software Development, BCS (1986), p71.
  • 17.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Ok I don’t need unit tests, I’ll write only integration tests 17
  • 18.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Integration tests FTW! 18
  • 19.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 19
  • 20.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ End to end Test End-to-end testing involves ensuring that the integrated components of an application function as expected. The entire application is tested in a real-world scenario such as communicating with the database, network, hardware and other applications. 20 https://www.techopedia.com/definition/7035/end-to-end-test
  • 21.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ End to end tests FTW! 21 https://www.process.st/business-process-modeling/ 15 HOURS OF E2E TESTS PLACE HERE
  • 22.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ End to end tests FTW! 22
  • 23.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Speaking of End to End tests… The Customer Rental History is unreliable 23
  • 24.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Application Architecture 24 Legacy service Customer Rental History service Payment processor Mainframe Audit service Insurance service
  • 25.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Testing Strategy - Hard Coded Object Stubs 25 Legacy service Customer Rental History service Payment processor Audit service Insurance service Hard coded Customer Rental History Service Stub Mainframe
  • 26.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Problems with hard coding stubs 26 ● 1225 line JSON response ● Hard to keep hard coded stub in sync with the api being stubbed ● Stub does not test that requests are made correctly to the remote service
  • 27.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Problems with hard coding stubs 27 Stubs that were used in the build phase have nothing to do with the real API!! /foo LEGACY APP CUSTOMER RENTAL HISTORY TEST OK /foo LEGACY APP CUSTOMER RENTAL HISTORY REALITY WAT?! If /foo respond with OK
  • 28.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How to create reliable stubs for services we don’t control? 28
  • 29.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Let’s proxy the calls! 29
  • 30.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Generating Stubs From Proxy 30 Test that calls Customer Rental History Service Customer Rental History Service Payment processor Mainframe PROXY Record traffic and dump stubs (e.g. once per day) Upload stubs for other teams to use
  • 31.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stubs From Proxy 31 Given this request Send back this response
  • 32.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Now that we have stubs, how can we test the system? 32
  • 33.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Testing Strategy - Generated Remote Stub 33 Legacy service Customer Rental History Service Payment processor Customer Rental History Service Stub from the proxy Legacy service integration test Mainframe
  • 34.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Outcome? Fast feedback from quite reliable stubs Less money spent on e2e 34
  • 35.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ OK, we’ve dealt with the app we don’t control. How about those we do control? 35
  • 36.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Hard Problems ● How can something be added to a service without breaking the clients? ● How can something be removed from a service without breaking the clients? ● How can a service developer find out how clients are using their service? ● How to enable short release cycles and continuous delivery of services? 36
  • 37.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Service Evolution Patterns ● Tolerant Reader ○ Extract only what is needed, ignore unknown content ● Schema Versioning ○ Put Schema versions in the API url / headers (v1, v2) ● Extension Points ○ Additional, optional data structured in a predefined extension field ● Consumer-Driven Contracts 37 http://servicedesignpatterns.com/WebServiceEvolution
  • 38.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Definitions ● Producer ○ service that exposes an API / sends a message ● Consumer ○ service that consumes the API of the producer / listens to a message from the producer ● Contract ○ agreement between producer and consumer how the API / message will look like 38
  • 39.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Provider Contract • The implementer of a service defines the interface of the service everyone uses the same interface 39 Service Client A Client B
  • 40.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consumer Contract ● The consumer of a service defines their interface to the service within the conventions / spirit of provider contract 40 Service Client A Client B
  • 41.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consumer Driven Contracts 41 Source: http://martinfowler.com/articles/consumerDrivenContracts.html
  • 42.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Benefits of Consumer Driven Contracts ● Align service providers with the business goals of their consumer through the consumer driven contracts ● Provide insights into how a service is being used by its consumers ● Make evolving services easier by reducing the amount of coordination required to evolve interfaces ● Help in the configuration of continuous delivery pipelines 42
  • 43.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ How the code should be tested 43 UNIT TESTS SERVICE, API, CONTRACT TESTS E2E, UI TESTS MANUAL TESTS MANUAL TESTING
  • 44.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Migrating to microservices... 44
  • 45.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Problem? Our monolith is the source of truth We don’t know if we can change its API 45
  • 46.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Monolith as source of truth 46 Legacy service Audit service Insurance service Ledger service Payment service Invoice service Warehouse service
  • 47.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ GET /user/GET /user/ Consumers create stubs (current approach) 47 Legacy service GET /user/Audit service Insurance service Ledger service Payment service Invoice service Warehouse service GET /user/
  • 48.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Problem? Stubs on the consumer side not reliable Time wasted on writing the same stubs 48
  • 49.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Let’s write a Segfault Contract contract testing framework! #frameworkless 49
  • 50.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ We need to define a contract somehow 50
  • 51.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Example of a contract 51 Given this request Send back this response
  • 52.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ We need to assert that contract is valid We want to build reliable stubs 52
  • 53.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stubs Generating Stubs & Tests (PRODUCER) 53 Legacy service Customer Rental History Service Payment processor Mainframe Audit service Insurance service Contract Tests
  • 54.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Generating Stubs & Tests (PRODUCER) 54 Generate reusable stubs Read Run tests to verify if contract is met Run build $ build project
  • 55.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Contract Tests (PRODUCER) 55 Given this request Is the response correct ? Send the request
  • 56.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Generated Stubs (PRODUCER) 56 Given this request Send back this response
  • 57.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Monolith as source of truth (better approach) 57 GET /user/ Legacy service POST /user/ Audit service Insurance service Ledger service Payment service Invoice service Warehouse service STUB STORAGE
  • 58.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reuse Stubs (CONSUMER) 58 Fetch stubs & run stub servers Consumer tests Test runner Fetch stubs STUB STORAGE
  • 59.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Outcome? Fast feedback from reliable stubs (fail fast) Less money spent on e2e Less time wasted on duplicating stubs 59
  • 60.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ OK, what about new, shiny microservices? 60
  • 61.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Nice API creation ● It’s the consumer that uses the API ● Consumers should take part in the creation of the API of the producer ● The producer’s API change should be driven by consumers 61
  • 62.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consumer Driven Contracts 62
  • 63.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ There is no producer code! 63 CONSUMER REPO WITH CONTRACTS INTERACTION I THINK I LIKE THIS API EVEN THOUGH THE PRODUCER DOESN’T EVEN EXIST! WHAT IF I CALLED THE API LIKE THIS...
  • 64.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Store the stubs from contracts locally 64 CONSUMER REPO WITH CONTRACTS INTERACTION LOCAL STORAGE WITH STUBS
  • 65.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Play with the API offline 65 CONSUMER REPO WITH CONTRACTS TESTS LOCAL STORAGE WITH STUBS FROM
  • 66.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ File a PR 66 CONSUMER REPO WITH CONTRACTS YUP, I LIKE IT! TIME FOR A PR PR
  • 67.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Producer sets up a project 67 PRODUCER REPO WITH CONTRACTS INTRACTION OH, SOMEONE HAS REQUESTED THAT I HAVE SUCH API...
  • 68.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Producer sets up a project 68 PRODUCER REPO WITH CONTRACTS INTRACTION THE TESTS ARE FAILING… NO WONDER, I HAVE NO CODE :|
  • 69.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Producer makes the generated tests pass 69 PRODUCER REPO WITH CONTRACTS PR OK, THE TESTS ARE PASSING, LET ME MERGE THE PR TO MASTER
  • 70.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ CI server builds the producer code 70 PRODUCER BUILD UPLOAD BINARY UPLOAD STUBS STUB STORAGE BINARY STORAGE CI SYSTEM
  • 71.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consumer uses uploaded stubs 71 CONSUMER LET ME USE THE PRODUCER’S STUBS FOR MY TESTS FETCHSTUBS STUB STORAGE
  • 72.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Did you say, that ALL contracts are stored in a single repo? 72
  • 73.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 73
  • 74.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 74
  • 75.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 75
  • 76.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 76
  • 77.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What about messaging? 77
  • 78.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 78 CONSUMERPRODUCER TEST PRODUCER CONSUMER REALITY topic: bar topic: foo topic: bartopic: foo
  • 79.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Messaging 79 (PRODUCER) Do I trigger the message? (PRODUCER) Is it sent to a proper destination? (PRODUCER) Can I serialize it properly? (CONSUMER) Do I listen at a proper destination? (CONSUMER) Can I deserialize it properly?
  • 80.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What about polyglot support? 80
  • 81.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NodeJs app 81 https://github.com/marcingrzejszczak/sc-contract-car-rental NodeJs Some HTTP service
  • 82.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Doesn’t work 82
  • 83.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Polyglot 83 Non JVM app Send a request Stub will respond RUN STUB STORAGE
  • 84.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ NodeJs works 84
  • 85.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Can I generate tests for an app in any language? 85
  • 86.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Polyglot 86 contract1.yml GET /user/ App contract2.yml POST /user/ Contracts mounted to generate tests and stubs Tests executed from the container against a running app RUN STUB STORAGE
  • 87.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 87
  • 88.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ● The implemented framework: Spring Cloud Contract ● The stub server: WireMock ● Available DSLs: Groovy, YAML ● Supports: ○ messaging (Spring Cloud Stream, Spring Integration, Spring AMQP, whatever you want) ○ dynamic pieces ○ resolution of request from response ○ stateful stubs (stubs have memory) ○ you can store stubs in Git, Nexus / Artifactory, Pact Broker ● And much, much more… ● Read the docs: http://cloud.spring.io/spring-cloud-contract/ What have we managed to build? 88 #frameworkless
  • 89.
    Unless otherwise indicated,these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Disclaimer Statement 89
  • 90.