Slide 1

Slide 1 text

Testing as you were in PROD , with Testcontainers

Slide 2

Slide 2 text

TABLE OF CONTENTS 1. whoami 2. the project 3. what do we want to test 4. what is testcontainers 5. code 6. references

Slide 3

Slide 3 text

$whoami JUG organiser at BarcelonaJUG Co-Founder of JBCNConf ( Barcelona ) Software Engineer at Red Hat App. Modernisation and Migration team @vilojona jvilalop@redhat.com aytartana.wordpress.com github.com/jonathanvila

Slide 4

Slide 4 text

the project

Slide 5

Slide 5 text

Ingress xavier KIE Server - jBPM - RBAC App - Security - S3 bucket A DB Postgres DB Postgres Kafka S3 bucket B The project 2 1 3 4 5 7 8 9 6 10

Slide 6

Slide 6 text

what do we want to test

Slide 7

Slide 7 text

What do we want to test End to End tests + clients network connections AWS S3 Kafka PostgreSQL

Slide 8

Slide 8 text

What do we want to test End to End tests + clients network connections + external services version PostgreSQL 9.6 KIE 7.3

Slide 9

Slide 9 text

What do we want to test End to End tests + clients network connections + external services version + timeouts,params, on clients and listeners Apache Camel Kafka AWS S3

Slide 10

Slide 10 text

What do we want to test End to End tests + network steps + external services version + timeouts,params, on clients and listeners + background async processes Insights Apache Camel KIE Server

Slide 11

Slide 11 text

What do we want to test End to End tests + network steps + external services version + timeouts,params, on clients and listeners + background async processes + external services specific configuration Apache Camel KIE Server

Slide 12

Slide 12 text

What do we want to test End to End tests + network steps + external services version + timeouts,params, on clients and listeners + background async processes + external services specific configuration Apache Camel KIE Server And everything executed in our CI process ( Github + TravisCI )

Slide 13

Slide 13 text

Have the whole test in CI

Slide 14

Slide 14 text

Testcontainers to the rescue Testcontainers ( not TestContainers ) www.testcontainers.org @testcontainers

Slide 15

Slide 15 text

what is Testcontainers

Slide 16

Slide 16 text

Testcontai...what ? Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container. Your APP JUnit test Testcontainer module Docker for Java Docker Container

Slide 17

Slide 17 text

12 Tools Modules ● Docker Compose Module ● Elasticsearch container ● Kafka Containers ● Localstack Module ● Mockserver Module ● Nginx Module ● Apache Pulsar Module ● RabbitMQ Module ● Solr Container ● Toxiproxy Module ● Hashicorp Vault Module ● Webdriver Containers OOTB Modules 18 DB Modules ● JDBC support ● R2DBC support ● Cassandra Module ● CockroachDB Module ● Couchbase Module ● Clickhouse Module ● DB2 Module ● Dynalite Module ● InfluxDB Module ● MariaDB Module ● MongoDB Module ● MS SQL Server Module ● MySQL Module ● Neo4j Module ● Oracle-XE Module ● OrientDB Module ● Postgres Module ● Presto Module

Slide 18

Slide 18 text

Ingress xavier KIE Server - jBPM - RBAC App - Security - Minio bucket A DB Postgres DB Postgres Kafka Localstack bucket B REAL SUBSTITUTE Components to have similar Prod env 2 1 3 4 5 7 8 9 6 10

Slide 19

Slide 19 text

Tasks with containers Definition OOTB Module, Docker image, Dockerfile Start JUnit rule, Manual, Manual parallel Logging Manually, SLF4J consumer Connection Shared network, IP & port Finish Manual, Automatic

Slide 20

Slide 20 text

Defining containers OOTB Module Dockerhub image Dockerfile file

Slide 21

Slide 21 text

Ingress xavier KIE Server - jBPM - RBAC App - Security - Minio bucket A DB Postgres DB Postgres Kafka Localstack bucket B REAL SUBSTITUTE Testcontainers modules 2 1 3 4 5 7 8 9 6 10 GenericContainer<>("jboss/kie-server-showcase:7.18.0.Final") PostgreSQLContainer () PostgreSQLContainer () LocalStackContainer() GenericContainer<>("minio/minio") GenericContainer<>("minio/mc") KafkaContainer () GenericContainer(new ImageFromDockerfile() GenericContainer<>(new ImageFromDockerfile()

Slide 22

Slide 22 text

Starting containers Manual Manual Parallel Automatically

Slide 23

Slide 23 text

Logging SLF4J Logger Manual LOG output sample

Slide 24

Slide 24 text

Connecting to a container Connecting containers to a same local network Getting container host and random exposed port

Slide 25

Slide 25 text

Waiting to be ready Wait strategy : specify the condition to consider a container as useful for testing vs Startup strategy : always always means wait until container is running

Slide 26

Slide 26 text

Passing properties to Spring Boot Overriding properties from config file with values from containers Setting the context initializer for Spring

Slide 27

Slide 27 text

Finishing the containers Manually Automatically

Slide 28

Slide 28 text

Ok, Ok, but …

Slide 29

Slide 29 text

The code

Slide 30

Slide 30 text

References

Slide 31

Slide 31 text

References Testcontainers : www.testcontainers.org @testcontainers Docker for Java : https://github.com/docker-java/docker-java Repository used in this presentation : https://github.com/project-xavier/xavier-integration.git ( class EndToEndTest.java )

Slide 32

Slide 32 text

Books ( devs not only consume tech ) Sci Fy Mind Novel

Slide 33

Slide 33 text

@vilojona jvilalop@redhat.com aytartana.wordpress.com github.com/jonathanvila Thank you :) THE END