En tant que développeur, qu'il est bon d'être capable de débugguer sur sa machine un problème survenant en production, dans une configuration spécifique ! C'est ce que permet le framework TestContainers. Il permet de piloter Docker directement depuis ses tests JUnit et donc d'avoir un mécanisme extrêmement efficace pour déployer ses tests fonctionnels dans un environnement donné.
Cette session présentera TestContainers, appliqué à un cas réel avec une démonstration de comment l'utiliser pour effectuer des tests impliquant une base de données, un moteur de Servlet et plus. Au programme: Intégration JUnit5, création d'images Docker custom, enregistrement automatique de vidéos des tests, intégration avec un job Jenkins pipeline pour itérer sur les différentes configurations à tester.
2. Agenda
• Context: the XWiki use case
• The TestContainer approach
• Return of experience
• Conclusion
3. What is XWiki?
• Open source wiki
• 16 years
• 10-15 active committers
• Very extensible, scripting in wiki pages
• Platform for developing ad-hoc web applications
• Strong build practices using Maven and lots of “Quality” plugins
• Using Jenkins & custom pipeline library for the CI
https://xwiki.org
4. Use Case: XWiki
• Application with functional tests, e.g. web
app & UI tests
Servlet Engine
Webapp
(XWiki)
Browser
JDBC
HTTP
5. DB Type DB Version JDBC Driver Servlet E.Type
Servlet E.
Version
Browser Type Browser Version Other
5.7.24 5.1.45 8.5.35 63.0 N/A
9.6.8 42.1.4 Latest 9.x 70.0.3538.77 Solr External
2.4.1 2.4.1 9.4.12 60.0.2 Clustering
Latest 10.3.x Latest 9.4.8.v20171121 67.0.3396.87
LibreOf
fi
ce
Server
Problem: Con
fi
gurations
• Validate that XWiki works on supported
con
fi
gurations
6. Docker
Container #3
Docker Container #2
Docker
Container #1
Solution: Dockerization
• Use Docker to represent the various
con
fi
gurations
Servlet Engine
Webapp
(XWiki)
Browser
7. TestContainers
@Testcontainers
public class MyTestcontainersTests {
@Container
private static final MySQLContainer MY_SQL_CONTAINER = new
MySQLContainer();
@Container
private PostgreSQLContainer pgsqlContainer = new PostgreSQLContainer()
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
• Reproduce and debug locally and in IDE
testcontainers.org
8. TestContainers Features
• Several built-in Containers: MySQL, PostgreSQL,
Selenium/WebDriver, ElasticSearch, Nginx, and a lot
more… even a DockerCompose one!
• Automatically record videos
• Automatic and powerful clean up of containers
• Full Docker API available (uses docker-java
underneath)
• Create Docker images on the
fl
y
9. XWiki & TestContainers
@UITest(
database = Database.MYSQL,
databaseTag = "5",
servletEngine = ServletEngine.TOMCAT,
servletEngineTag = "8",
browser = Browser.CHROME)
public class MenuIT
• Custom JUnit5 Extension, makes it easy to
use for XWiki devs
Selenium test here
11. The Good
• Easy to use/debug a given
con
fi
guration and in your IDE
• Including production problems
• Works on all OS
• … with some work (e.g. Mac M1)
• It
fi
nds problems!
• Hard to know how many since
most don’t end up in JIRA…
12. The Bad (1/2)
• Requires maintenance
• Docker image tags to update
• Changes to support all architectures (e.g. Mac M1
with ARM support)
• Regular cryptic failures/false positives not due to
tests
• Hard to know if the problem is Jenkins (CI),
Network and XWiki infra, or TC
13. The Bad (2/2)
• Slow tests (between 6 to 14 hours for
950+ tests)
• Docker tests running in parallel on
the CI
• Increasing
fl
ickering UI tests…
• 90 as of now
• Not all related to con
fi
guration
testing
14. Conclusion
• TestContainers is very nice and easy to use
• Worth it if
fi
nding an environment-related problem is
critical and more important than the cost of
maintenance required
• Ideally, you need a full person dedicated to the build (at
least for the level of XWiki)
• High cost when building the initial framework
• Don’t write only UI tests, make sure to move the max #
of tests as unit tests or integration tests (especially for
edge conditions)