Richtige Integrationstests mit Containern

A presentation at Developer Week in June 2019 in Nuremberg, Germany by Philipp Krenn

Slide 1

Slide 1

Richtige Integrationstests mit Containern Philipp Krenn @xeraa

Slide 2

Slide 2

Developer

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

Tests

Slide 6

Slide 6

Slide 7

Slide 7

But the unit tests passed, so… https://twitter.com/Aaronius/status/ 933497253347463168

Slide 8

Slide 8

Slide 9

Slide 9

Slide 10

Slide 10

Slide 11

Slide 11

Slide 12

Slide 12

Slide 13

Slide 13

Slide 14

Slide 14

Integration Tests

Slide 15

Slide 15

Mocks

Slide 16

Slide 16

Mockito, EasyMock, JMock,…

Slide 17

Slide 17

Slide 18

Slide 18

Ich mock mir die Welt widdewidde wie sie mir gefällt

Slide 19

Slide 19

https://www.monkeyuser.com/2018/ happy-flow/

Slide 20

Slide 20

! Unit tests, systems not under your control ! Test real datastore

Slide 21

Slide 21

In-Memory

Slide 22

Slide 22

H2, HSQLDB, Apache Derby,…

Slide 23

Slide 23

Slide 24

Slide 24

Embedded Elasticsearch unsupported in 5.0+ https://www.elastic.co/blog/elasticsearch-the-server

Slide 25

Slide 25

! Often good enough ! Test real datastore, supported systems

Slide 26

Slide 26

Actual Datastore

Slide 27

Slide 27

Local installation Docker container Cloud

Slide 28

Slide 28

Demo

Slide 29

Slide 29

! “Good old approach” ! External dependency, parallelization

Slide 30

Slide 30

Embedded

Slide 31

Slide 31

embedded-elasticsearch https://github.com/allegro/embedded-elasticsearch

Slide 32

Slide 32

Demo

Slide 33

Slide 33

Customization & Mappings .withPlugin(“analysis-stempel”) .withIndex(“cars”, IndexSettings.builder() .withType(“_doc”, getSystemResourceAsStream(“car-mapping.json”))

Slide 34

Slide 34

! IDE support, customization, custom lifecycle ! Custom integration

Slide 35

Slide 35

More embedded datastores https://github.com/flapdoodle-oss/ de.flapdoodle.embed.process

Slide 36

Slide 36

Build Tool

Slide 37

Slide 37

docker-maven-plugin http://dmp.fabric8.io

Slide 38

Slide 38

Dockerfile or Docker assembly <build> <from>java:8</from> <assembly> <descriptor>docker-assembly.xml</descriptor> </assembly> <cmd> <shell>java -jar /maven/service.jar</shell> </cmd> </build>

Slide 39

Slide 39

Build a custom image: docker:build Run container: docker:start docker:stop

Slide 40

Slide 40

Demo

Slide 41

Slide 41

maven-failsafe-plugin https://maven.apache.org/surefire/maven-failsafe-plugin/

Slide 42

Slide 42

! Standard or custom Docker image ! One instance for all tests, no IDE support

Slide 43

Slide 43

Slide 44

Slide 44

Testcontainers Generic

Slide 45

Slide 45

Dependency @ClassRule public static GenericContainer redis = new GenericContainer(“redis:5.0.5”) .withExposedPorts(6379);

Slide 46

Slide 46

Docker Compose integration @ClassRule public static DockerComposeContainer environment = new DockerComposeContainer(new File(“src/test/resources/compose.yml”)) .withExposedService(“elasticsearch_1”, ELASTICSEARCH_PORT, Wait.forHttp(“/”).forStatusCode(200));

Slide 47

Slide 47

Demo

Slide 48

Slide 48

What’s in docker ps -a

Slide 49

Slide 49

Docker for Mac API: socat Linux: Multipurpose relay (SOcket CAT) https://github.com/alpine-docker/socat

Slide 50

Slide 50

Cleanup: Moby Ryuk https://github.com/testcontainers/moby-ryuk

Slide 51

Slide 51

[Ryuk] drops a Death Note, a notebook that allows the user to kill anyone simply by knowing their name and face https://en.wikipedia.org/wiki/Ryuk_(Death_Note)

Slide 52

Slide 52

! IDE support, customization, custom lifecycle ! Hacky configuration

Slide 53

Slide 53

Testcontainers Custom

Slide 54

Slide 54

Slide 55

Slide 55

ElasticsearchContainer https://www.testcontainers.org/modules/elasticsearch/

Slide 56

Slide 56

Demo

Slide 57

Slide 57

More languages Scala, Go, .net , Python , JavaScript , Rust * * * Early development * *

Slide 58

Slide 58

Selenium 2 / Webdriver VNC screen recording

Slide 59

Slide 59

! IDE support, customization, custom lifecycle ! Custom integration

Slide 60

Slide 60

Inside Containers

Slide 61

Slide 61

Slide 62

Slide 62

Wormhole Pattern

Slide 63

Slide 63

$ docker run -it —rm -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock maven:3 mvn —projects parent,4_testcontainers-custom test $ docker ps -a

Slide 64

Slide 64

Conclusion

Slide 65

Slide 65

Why Integration Tests

Slide 66

Slide 66

Why not Mocking In-Memory Actual Datastore

Slide 67

Slide 67

How Embedded Build Tool Testcontainers

Slide 68

Slide 68

Code https://github.com/xeraa/integration-test-demo

Slide 69

Slide 69

Questions? Philipp Krenn @xeraa