Testcontainers for Go
repository·main·Indexed 26 days ago
https://github.com/testcontainers/testcontainers-goA Go library for programmatically managing Docker containers as dependencies for integration and smoke testing, providing automated lifecycle management for setup and cleanup. Includes specialized modules such as the Dex module for managing OIDC containers with support for OAuth2 clients, users, and gRPC runtime management.
What's inside testcontainers-go
- Testcontainers for Go is a Go package designed to simplify the creation and cleanup of container-based dependencies for automated integration and smoke tests. It provides a programmatic API to define containers that run during your tests and ensures those resources are cleaned up once the tests are complete.
Overview of Azure modules
mainThe Azure module provides Testcontainers implementations for several Azure services. The available packages are:
github.com/testcontainers/testcontainers-go/modules/azure/azurite: Azurite (Azure Storage emulator)github.com/testcontainers/testcontainers-go/modules/azure/eventhubs: EventHubs (Note: Requires explicit EULA acceptance viaWithAcceptEULA)github.com/testcontainers/testcontainers-go/modules/azure/servicebus: ServiceBusgithub.com/testcontainers/testcontainers-go/modules/azure/cosmosdb: CosmosDBgithub.com/testcontainers/testcontainers-go/modules/azure/lowkeyvault: Lowkey Vaultgithub.com/testcontainers/testcontainers-go/modules/azure/sqledge: Azure SQL Edge
Use the Socat module to provide TCP port forwarding
mainThe Socat module provides a utility container for TCP port forwarding and network tunneling. This allows for transparent communication between containers and networks, which is useful for simulating network connections or accessing services in different containers.Use Ryuk for automatic resource cleanup
mainRyuk (also known as the
Reaper) is a sidecar container that automatically removes containers, networks, and volumes created by Testcontainers for Go. It identifies resources via container labels and ensures the environment stays clean even ifTerminateis not explicitly called.Note: If a container is running for more than 10 seconds, Ryuk will kill it.
Available GCloud modules
mainThe GCloud module provides emulators for several Google Cloud services via dedicated Go packages:
- BigQuery:
github.com/testcontainers/testcontainers-go/modules/gcloud/bigquery - BigTable:
github.com/testcontainers/testcontainers-go/modules/gcloud/bigtable - Datastore:
github.com/testcontainers/testcontainers-go/modules/gcloud/datastore - Firestore:
github.com/testcontainers/testcontainers-go/modules/gcloud/firestore - Pubsub:
github.com/testcontainers/testcontainers-go/modules/gcloud/pubsub - Spanner:
github.com/testcontainers/testcontainers-go/modules/gcloud/spanner
Default Docker Images:
- Most emulators use
gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators. - BigQuery uses
ghcr.io/goccy/bigquery-emulator:0.6.1. - Spanner uses
gcr.io/cloud-spanner-emulator/emulator:1.4.0.
- BigQuery:
Configure Wait Strategies in Testcontainers for Go
mainWait strategies allow your tests to pause execution until a container reaches a specific state (e.g., a network port is open, a log message appears, or a file exists). Wait strategies are implemented in the
waitpackage.Available strategies include:
Exec: Wait for a command to execute successfully.Exit: Wait for the container to exit.File: Wait for a file to appear.Health: Wait for a container health check to pass.HostPort: Wait for a port to become available on the host.HTTP: Wait for an HTTP response.Log: Wait for a specific log message.SQL: Wait for a database to be ready.TLS: Wait for a TLS connection.ForAll: Wait for all provided strategies to satisfy.ForAny: Wait for any one of the provided strategies to satisfy.
Understand Test Session Semantics in Testcontainers for Go
mainTestcontainers for Go uses 'Test Session Semantics' to identify and group container creations within a single test execution. This allows the library to aggregate tests across multiple Go packages that are part of the same execution context.
A 'test session' is defined as:
- A single
go testinvocation (including flags). - A single
go test ./...invocation for all subpackages. - The execution of a single test or a set of tests via an IDE.
To ensure uniqueness across different processes, Testcontainers for Go generates a unique
sessionIDby hashing the stringtestcontainers-go:combined with the parent process ID (PID) and the creation date of that parent process. ThissessionIDis used to:- Aggregate test executions across multiple packages in the same session.
- Pass the
sessionIDto the container runtime via an HTTP header to the daemon. - Tag created containers with a label containing the
sessionID.
- A single
Install the DinD module
mainTo add the Docker in Docker (DinD) module to your Go project, run the following command:
go get github.com/testcontainers/testcontainers-go/modules/dindInstall the Qdrant module
mainTo add the Qdrant module to your Go project, run the following command:
go get github.com/testcontainers/testcontainers-go/modules/qdrantAdd the Inbucket module to your project
mainTo use the Inbucket module with Testcontainers for Go, add it to your Go dependencies using the following command:
go get github.com/testcontainers/testcontainers-go/modules/inbucketAdd the TiDB module to your project
mainTo use the TiDB module in your Go project, add it to your dependencies using the following command:
go get github.com/testcontainers/testcontainers-go/modules/tidbAdd the CrateDB module to your project
mainTo use the CrateDB module in your Go project, add it to your dependencies using the following command:
go get github.com/testcontainers/testcontainers-go/modules/cratedb