spectolabs xebia testworks conf october 2016

37
Automated Testing in a World of Interdependence: Service Virtualisation for the Modern Age TestWorksConf Workshop, 7th October Benji Hooper, Daniel Bryant, John Davenport, Tom Cunliffe

Upload: spectolabs

Post on 14-Apr-2017

312 views

Category:

Software


1 download

TRANSCRIPT

Automated Testing in a World of Interdependence:Service Virtualisation for the Modern Age

TestWorksConf Workshop, 7th October

Benji Hooper, Daniel Bryant, John Davenport, Tom Cunliffe

About Specto Workshops

• This slide deck is an excerpt from a workshop delivered as part of the test automation TestWorksConf

• http://testworksconf.com/talk/automated-testing-in-a-world-of-interdependence

• The Specto team run public and private workshops ([email protected])

• All aspects of test automation

• Microservice testing and service virtualisation with Hoverfly

• Incorporating FR / NFR testing into a build pipeline

Testing is kind of important.

Knight Capital, 2012

$440m loss

Neiman Marcus, Black Friday 2015

IPO cancelled

Stock trading disruption

12 hour outage

Royal Bank of Scotland, 2012 Transactions disrupted worldwide£500m in fines and

compensation

OK - very important.

In any complex distributed system…

… testing an individual component in isolation is difficult….

… and test automation is sometimes impossible

Agenda

• Group flip-chart sessions (30 minutes)

• Introduction to service virtualization (20 minutes)

• Hands-on session (50 minutes)

• Wrap up

Group flip-chart session

• Equal-sized groups:

• 1 facilitator - Edze, Tom, Daniel, Benji and John

• 1 scribe

• 1 presenter

• 30 minutes

• 10-15 minutes: read the scenario & discuss issues

• 4 presentations of 4 minutes - your main issues and solutions

The busy QA / developer’s guide to service virtualisation

credit to @mogronalol 10

Resources

shopfront

stockmanager

productcatalogue

Payments

Fraud

Cost

credit to @mogronalol 11

credit to @mogronalol 12

Licensing

Consumer

Producer

Consumer

Producer

Third party Cost

credit to @mogronalol 13

Dependency Constraints

Consumer

Producer

Consumer

Producer

credit to @mogronalol 14

Flakey Dependencies

Consumer

Producer

Consumer

Producer

credit to @mogronalol 15

Doesn't exist

Consumer

?

credit to @mogronalol 16

Non Deterministic

assertThat(booking.getDate()) .isEqualTo(LocalDate.of(2013,1,8));

Consumer

Booking Service

Consumer

Booking Service

Consumer

Booking Service {

“date” : “2013-01-08”, … }

{

“date” : “2016-02-07”, … }

credit to @mogronalol 17

Fault Injection

Consumer

Producer

Consumer

Producer

4xx, 5xx, timeouts

Operationally challenging to trigger

faults

credit to @mogronalol 18

Service Virtualisation

• Emulation of real service • Non-intrusive • Lightweight - or should be

credit to @mogronalol 19

Service Virtualisation

Consumer ProducerVirtual Producer

credit to @mogronalol 20

Creating Data

Consumer ProducerService

Virtualisation Tool

credit to @mogronalol 21

Creating Data

https://github.com/tomakehurst/wiremock

credit to @mogronalol 22

Proxy Mode

Consumer ProducerService

Virtualisation Tool

credit to @mogronalol 23

Webserver / Facade Mode

Consumer Producer

Service Virtualisation

Tool

credit to @mogronalol 24

Not Mocking

Service Under Test

Mocking

Dependant Service

Http

Virtual Service

Mocks bleed into the architectural boundary of

your application

credit to @mogronalol 25

Open Source Tooling

credit to @mogronalol 26

Record and replay

Consumer External Producer

Service Virtualisati

on Tool

credit to @mogronalol 27

JUnit Example

@Rule public HoverflyRule hoverflyRule = HoverflyRule.inCaptureMode(“src/test/resources/external.json").build();

@Testpublic void shouldBeAbleToRetrieveABooking() throws URISyntaxException { // Given final int bookingId = 1; // When final Booking booking = bookingServiceGateway.getBooking(bookingId); // Then assertThat(booking.getCustomer()).isEqualTo(“Andrew Morgan”); assertThat(booking.getDate()).isEqualTo(LocalDate.of(2016,3,1));}

credit to @mogronalol 28

JUnit Example{ "data": [ { "request": { "path": "/api/bookings/1", "method": "GET", "destination": "www.my-test.com", "query": null, "body": "" }, "response": { "status": 200, "body": “{\”customer\”:\”Andrew Morgan\", \”date\”: \”2016-03-01\”}}}”, "headers": { "Content-Type": [ "application/json" ] } } } ]}

credit to @mogronalol 29

JUnit Example

@Testpublic void shouldBeAbleToRetrieveABooking() throws URISyntaxException { // Given final int bookingId = 1; // When final Booking booking = bookingServiceGateway.getBooking(bookingId); // Then assertThat(booking.getCustomer()).isEqualTo(“Andrew Morgan”); assertThat(booking.getDate()).isEqualTo(LocalDate.of(2016,3,1));}

@Rule public HoverflyRule hoverflyRule = HoverflyRule.buildFromClassPathResource(“external.json").build();

credit to @mogronalol 30

Advantages

• Isolation • Flaky dependencies • Rate limiting • Deterministic • Licensing

credit to @mogronalol 31

Deployment and Infrastructure

Consumer External Producer

Virtual Service

Internal Producer

Internal Producer

Internal Producer

Virtual Service

Virtual Service

Virtual Service

credit to @mogronalol 32

CLI Example

$ hoverctl start —proxy-port 8080 $ hoverctl import accounts-service-simulation.json

$ hoverctl start —proxy-port 8081 $ hoverctl import payments-service-simulation.json

$ curl http://www.account-service.com/api/v1/accounts/1 —proxy http://localhost:8080

[{"name":"foo"}]

$ curl http://www.payment-service.com/api/v1/payments/1 -proxy http://localhost:8081

[{“amount":"£23.27"}]

Hands-on session

• Exercise 1: Explore an application that needs testing and create the simulations required to test it independently

• Exercise 2: Test the circuit breakers in an application using Hoverfly

• Code available:

• https://github.com/benjih/oreilly-docker-java-shopping

• Edze, Tom, Daniel, Benji and John will be around for help if needed (and feel free to work with others!)

Let’s wrap this up…

If you create simulations of components…

… then individual components can be tested in isolation.

A simulation of a component behaves like the real thing…

… it misbehaves like the real thing…

So you can create a realistic, repeatable test environment…

… and automate all the things.

github.com/SpectoLabs/hoverfly

specto.io/blog

We run public and private training workshops: [email protected]