Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Integration testing with TestContainers-Go
Search
Nikolay Kuznetsov
January 23, 2020
Programming
0
260
Integration testing with TestContainers-Go
Go Days 2020 - Berlin
Nikolay Kuznetsov
January 23, 2020
Tweet
Share
More Decks by Nikolay Kuznetsov
See All by Nikolay Kuznetsov
Integration testing with TestСontainers-Go
nikolayk812
0
130
Integration testing with TestСontainers and JUnit 5
nikolayk812
0
170
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
350
Integration and end-to-end testing with TestСontainers
nikolayk812
0
120
Integration and end-to-end testing with TestСontainers
nikolayk812
0
29
Other Decks in Programming
See All in Programming
Graviton と Nitro と私
maroon1st
0
170
Pythonではじめるオープンデータ分析〜書籍の紹介と書籍で紹介しきれなかった事例の紹介〜
welliving
3
810
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.6k
Claude Codeの「Compacting Conversation」を体感50%減! CLAUDE.md + 8 Skills で挑むコンテキスト管理術
kmurahama
1
780
CSC307 Lecture 01
javiergs
PRO
0
680
PostgreSQLで手軽にDuckDBを使う!DuckDB&pg_duckdb入門/osc25hi-duckdb
takahashiikki
0
260
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
380
dchart: charts from deck markup
ajstarks
3
970
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.6k
公共交通オープンデータ × モバイルUX 複雑な運行情報を 『直感』に変換する技術
tinykitten
PRO
0
200
Basic Architectures
denyspoltorak
0
460
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
150
Featured
See All Featured
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
49
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
59
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
260
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Designing for humans not robots
tammielis
254
26k
Utilizing Notion as your number one productivity tool
mfonobong
2
200
GraphQLの誤解/rethinking-graphql
sonatard
74
11k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
110
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
43
Transcript
Integration testing with TestСontainers-Go Erdem Toraman Nikolay Kuznetsov GoDays -
Berlin 23 Jan 2020
About us • Go developers at Zalando in Helsinki •
Project with ~20 microservices in Go • User perspective of TestContainers-Go library
2 unit tests passed, 0 integration tests
Basic integration test
Getting a database for testing • Local database • In-memory
mock • Docker!
Docker advantages • 100% compatible databases • Same version as
production • Empty DB state
Integration testing with Docker
Easy-peasy! docker run -d -p 5432:5432 postgres:12.1
• Host port conflicts • Not ready сontainer / service
• Resource leak (the container keeps running) • Stale data (if reusing the same container) • Starting mechanism both for CI and a local machine What could go wrong?
Solving some issues
None
Docker API docs.docker.com/engine/api/latest
Exec example
None
TestContainers flavors
TestContainers-Go github.com/testcontainers/testcontainers-go • Docker Go client under the hood •
Host port randomization • Containers clean up at the test shutdown • Readiness waiting strategies
As simple as pgContainer, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ ContainerRequest: tc.ContainerRequest{
Image: "postgres:12.1", ExposedPorts: []string{"5432/tcp"}, }, })
Host port randomization • API to get a host port:
• Prevents port conflicts • Enables parallel builds port, err := pgContainer.MappedPort(ctx, "5432/tcp")
Containers cleanup: Ryuk github.com/testcontainers/moby-ryuk • Ryuk kills containers (networks, volumes)
by labels • TC assigns labels to started containers • TC keeps a connection to Ryuk open
Waiting strategies • Host port • HTTP • Logs •
Custom • Multi
Host port waiting strategy • Default (customizable) timeout is 60
seconds • Impl checks both from outside and inside container tc.ContainerRequest{ Image: "postgres:12.1", ExposedPorts: []string{"5432/tcp"}, WaitingFor: wait.ForListeningPort("5432/tcp"), },
HTTP waiting strategy WaitingFor: wait.ForHTTP("/health"). WithPort("8080/tcp"). WithStatusCodeMatcher( func(status int) bool
{ return status == http.StatusOK }),
Demo
Demo I: integration testing User Repository github.com/erdemtoraman/godays-testcontainers-demo • Testing interaction
with a database • Create and Get users from Postgres
What happened? • Ran a Postgres container • Mapped a
random port to the host machine • Ran tests against it • Cleaned up the containers
Demo II: end-to-end testing User Service github.com/erdemtoraman/godays-testcontainers-demo Ticket Service Docker
Network Tests HTTP
Takeaways • testcontainers.org • Balance between flexibility, speed and features
• Great for integration tests • Possible to use for end-to-end tests
Thank you! @erdem_toraman, @nikolayk812 erdemtoraman