A collection of pre-configured testcontainers for your golang integration tests.
Available containers (feel free to contribute):
- MongoDB (based on mongo)
- Zookeeper (based on bitnami/zookeeper)
- Kafka (based on wurstmeister/kafka)
- RabbitMQ (based on rabbitmq)
- Redis (based on redis)
- Minio (based on minio/minio)
import (
"context"
"log"
"time"
tc "github.com/romnn/testcontainers/mongo"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func testDatabaseFunction(mongoClient *mongo.Client) error {
_ = mongoClient.Database("testdatabase").Collection("my-collection")
log.Println("Implement a real function here")
return nil
}
func main() {
// Start mongo container
mongoC, mongoConn, err := tc.StartMongoContainer(context.Background(), tc.ContainerOptions{})
if err != nil {
log.Fatalf("Failed to start mongoDB container: %v", err)
}
defer mongoC.Terminate(context.Background())
// Connect to the container
mongoURI := mongoConn.ConnectionURI()
client, err := mongo.NewClient(options.Client().ApplyURI(mongoURI))
if err != nil {
log.Fatalf("Failed to create mongo client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
client.Connect(ctx)
testDatabaseFunction(client)
}
For full examples of all available containers, see examples/
.
Before you get started, make sure you have installed the following tools::
$ python3 -m pip install -U cookiecutter>=1.4.0
$ python3 -m pip install pre-commit bump2version invoke ruamel.yaml halo
$ go get -u golang.org/x/tools/cmd/goimports
$ go get -u golang.org/x/lint/golint
$ go get -u github.com/fzipp/gocyclo/cmd/gocyclo
$ go get -u github.com/mitchellh/gox # if you want to test building on different architectures
Remember: To be able to excecute the tools downloaded with go get
,
make sure to include $GOPATH/bin
in your $PATH
.
If echo $GOPATH
does not give you a path make sure to run
(export GOPATH="$HOME/go"
to set it). In order for your changes to persist,
do not forget to add these to your shells .bashrc
.
With the tools in place, it is strongly advised to install the git commit hooks to make sure checks are passing in CI:
invoke install-hooks
You can check if all checks pass at any time:
invoke pre-commit
Note for Maintainers: After merging changes, tag your commits with a new version and push to GitHub to create a release:
bump2version (major | minor | patch)
git push --follow-tags
This project is still in the alpha stage and should not be considered production ready.