Utility for starting docker containers from Go code. Useful for testing.
This library currently supports the following services out of the box:
Install the package with:
go get github.com/Storytel/go-docker-initiator
Once installed import it to your code:
import dockerinitiator github.com/Storytel/go-docker-initiator
This package is especially useful for testing. With go-docker-initiator
the configuration for your external integrations lives in the code and not in 3rd party configuration files.
Below is a typical and simple example using go-docker-initiator
in a test.
package example_test
import (
"log"
"testing"
dockerinitiator "github.com/Storytel/go-docker-initiator"
mysqlinitiator "github.com/Storytel/go-docker-initiator/mysql"
"github.com/stretchr/testify/assert"
)
// WithMySQL will clear obsolete containers an spin up a mysql container for use
func WithMySQL() *mysqlinitiator.MysqlInstance {
if err := dockerinitiator.ClearObsolete(); err != nil {
log.Panic(err)
}
instance, err := mysqlinitiator.Mysql(mysqlinitiator.MysqlConfig{
Password: "",
DbName: "testdb",
})
if err != nil {
log.Panic(err)
}
// Set the needed environment variables
// MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE
if err = instance.Setenv(); err != nil {
log.Panic(err)
}
return instance
}
func TestDatabaseIntegration(t *testing.T) {
mysqlInstance := WithMySQL()
defer mysqlInstance.Stop()
// Establish a database connection to the exposed environment variables
db, err := InitAndCreateDatabase()
assert.NoError(t, err)
defer db.Close()
// Run any DB seeds here
// Setup your service and inject the database
exampleService := ExampleService{
Db: db,
}
// Test your integration
_, err = exmapleService.Create()
assert.NoError(t, err)
}
A single image can be easily shared between tests using techniques such as TableDrivenTests.
As is often the case, if you have tests set to automatically run on each file-save you might do best to tag the tests running with go-docker-initiator
to avoid running them on-save.
Tag a file like so:
// +build <TAG>
Since the tests with the tag wont run automatically you have to manually invoke it with:
go test -tags=<TAG> ./...
https://github.com/Storytel/go-mysql-seed - Simple MySQL seeding package