Documentation ¶
Overview ¶
Package dockertest contains helper functions for setting up and tearing down docker containers to aid in testing. dockertest supports spinning up MySQL, PostgreSQL and MongoDB out of the box.
Dockertest provides two environment variables
Index ¶
- Constants
- Variables
- func IP(containerID string) (string, error)
- func KillContainer(container string) error
- func Pull(image string) error
- type ContainerID
- func ConnectToElasticSearch(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToMongoDB(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToMySQL(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToNSQLookupd(tries int, delay time.Duration, ...) (c ContainerID, err error)
- func ConnectToNSQd(tries int, delay time.Duration, ...) (c ContainerID, err error)
- func ConnectToPostgreSQL(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToRedis(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func OpenElasticSearchContainerConnection(tries int, delay time.Duration) (c ContainerID, con *elastigo.Conn, err error)
- func OpenMongoDBContainerConnection(tries int, delay time.Duration) (c ContainerID, db *mgo.Session, err error)
- func OpenMySQLContainerConnection(tries int, delay time.Duration) (c ContainerID, db *sql.DB, err error)
- func OpenNSQLookupdContainerConnection(tries int, delay time.Duration) (c ContainerID, ip string, tcpPort int, httpPort int, err error)
- func OpenNSQdContainerConnection(tries int, delay time.Duration) (c ContainerID, ip string, tcpPort int, httpPort int, err error)
- func OpenPostgreSQLContainerConnection(tries int, delay time.Duration) (c ContainerID, db *sql.DB, err error)
- func OpenRedisContainerConnection(tries int, delay time.Duration) (c ContainerID, client *redis.Client, err error)
- func SetupElasticSearchContainer() (c ContainerID, ip string, port int, err error)
- func SetupMongoContainer() (c ContainerID, ip string, port int, err error)
- func SetupMySQLContainer() (c ContainerID, ip string, port int, err error)
- func SetupNSQLookupdContainer() (c ContainerID, ip string, tcpPort int, httpPort int, err error)
- func SetupNSQdContainer() (c ContainerID, ip string, tcpPort int, httpPort int, err error)
- func SetupPostgreSQLContainer() (c ContainerID, ip string, port int, err error)
- func SetupRedisContainer() (c ContainerID, ip string, port int, err error)
Constants ¶
const ( // MySQLUsername must be passed as username when connecting to mysql MySQLUsername = "root" // MySQLPassword must be passed as password when connecting to mysql MySQLPassword = "root" // PostgresUsername must be passed as username when connecting to postgres PostgresUsername = "postgres" // PostgresPassword must be passed as password when connecting to postgres PostgresPassword = "docker" )
Variables ¶
var ( // Debug if set, prevents any container from being removed. Debug bool // DockerMachineAvailable if true, uses docker-machine to run docker commands (for running tests on Windows and Mac OS) DockerMachineAvailable bool // DockerMachineName is the machine's name. You might want to use a dedicated machine for running your tests. // You can set this variable either directly or by defining a DOCKERTEST_IMAGE_NAME env variable. DockerMachineName = env.Getenv("DOCKERTEST_IMAGE_NAME", "default") // BindDockerToLocalhost if set, forces docker to bind the image to localhost. This for example is required when running tests on travis-ci. // You can set this variable either directly or by defining a DOCKERTEST_BIND_LOCALHOST env variable. // FIXME DOCKER_BIND_LOCALHOST remove legacy support BindDockerToLocalhost = env.Getenv("DOCKERTEST_BIND_LOCALHOST", env.Getenv("DOCKER_BIND_LOCALHOST", "")) )
Functions ¶
func KillContainer ¶
KillContainer runs docker kill on a container.
Types ¶
type ContainerID ¶
type ContainerID string
ContainerID represents a container and offers methods like Kill or IP.
func ConnectToElasticSearch ¶
func ConnectToElasticSearch(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToElasticSearch starts an ElasticSearch image and passes the database url to the connector callback function. The url will match the ip:port pattern (e.g. 123.123.123.123:4241)
func ConnectToMongoDB ¶
func ConnectToMongoDB(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToMongoDB starts a MongoDB image and passes the database url to the connector callback. The url will match the ip:port pattern (e.g. 123.123.123.123:4241)
func ConnectToMySQL ¶
func ConnectToMySQL(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToMySQL starts a MySQL image and passes the database url to the connector callback function. The url will match the username:password@tcp(ip:port) pattern (e.g. `root:root@tcp(123.123.123.123:3131)`)
func ConnectToNSQLookupd ¶
func ConnectToNSQLookupd(tries int, delay time.Duration, connector func(ip string, httpPort int, tcpPort int) bool) (c ContainerID, err error)
ConnectToNSQLookupd starts a NSQ image with `/nsqlookupd` running and passes the IP, HTTP port, and TCP port to the connector callback function. The url will match the ip pattern (e.g. 123.123.123.123).
func ConnectToNSQd ¶
func ConnectToNSQd(tries int, delay time.Duration, connector func(ip string, httpPort int, tcpPort int) bool) (c ContainerID, err error)
ConnectToNSQd starts a NSQ image with `/nsqd` running and passes the IP, HTTP port, and TCP port to the connector callback function. The url will match the ip pattern (e.g. 123.123.123.123).
func ConnectToPostgreSQL ¶
func ConnectToPostgreSQL(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToPostgreSQL starts a PostgreSQL image and passes the database url to the connector callback.
func ConnectToRedis ¶
func ConnectToRedis(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToRedis starts a Redis image and passes the database url to the connector callback function. The url will match the ip:port pattern (e.g. 123.123.123.123:6379)
func OpenElasticSearchContainerConnection ¶
func OpenElasticSearchContainerConnection(tries int, delay time.Duration) (c ContainerID, con *elastigo.Conn, err error)
OpenElasticSearchContainerConnection is supported for legacy reasons. Don't use it.
func OpenMongoDBContainerConnection ¶
func OpenMongoDBContainerConnection(tries int, delay time.Duration) (c ContainerID, db *mgo.Session, err error)
OpenMongoDBContainerConnection is supported for legacy reasons. Don't use it.
func OpenMySQLContainerConnection ¶
func OpenMySQLContainerConnection(tries int, delay time.Duration) (c ContainerID, db *sql.DB, err error)
OpenMySQLContainerConnection is supported for legacy reasons. Don't use it.
func OpenNSQLookupdContainerConnection ¶
func OpenNSQLookupdContainerConnection(tries int, delay time.Duration) (c ContainerID, ip string, tcpPort int, httpPort int, err error)
OpenNSQLookupdContainerConnection is supported for legacy reasons. Don't use it.
func OpenNSQdContainerConnection ¶
func OpenNSQdContainerConnection(tries int, delay time.Duration) (c ContainerID, ip string, tcpPort int, httpPort int, err error)
OpenNSQdContainerConnection is supported for legacy reasons. Don't use it.
func OpenPostgreSQLContainerConnection ¶
func OpenPostgreSQLContainerConnection(tries int, delay time.Duration) (c ContainerID, db *sql.DB, err error)
OpenPostgreSQLContainerConnection is supported for legacy reasons. Don't use it.
func OpenRedisContainerConnection ¶
func OpenRedisContainerConnection(tries int, delay time.Duration) (c ContainerID, client *redis.Client, err error)
OpenRedisContainerConnection is supported for legacy reasons. Don't use it.
func SetupElasticSearchContainer ¶
func SetupElasticSearchContainer() (c ContainerID, ip string, port int, err error)
SetupElasticSearchContainer sets up a real ElasticSearch instance for testing purposes using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func SetupMongoContainer ¶
func SetupMongoContainer() (c ContainerID, ip string, port int, err error)
SetupMongoContainer sets up a real MongoDB instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func SetupMySQLContainer ¶
func SetupMySQLContainer() (c ContainerID, ip string, port int, err error)
SetupMySQLContainer sets up a real MySQL instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func SetupNSQLookupdContainer ¶
func SetupNSQLookupdContainer() (c ContainerID, ip string, tcpPort int, httpPort int, err error)
SetupNSQLookupdContainer sets up a real NSQ instance for testing purposes using a Docker container and executing `/nsqlookupd`. It returns the container ID and its IP address, or makes the test fail on error.
func SetupNSQdContainer ¶
func SetupNSQdContainer() (c ContainerID, ip string, tcpPort int, httpPort int, err error)
SetupNSQdContainer sets up a real NSQ instance for testing purposes using a Docker container and executing `/nsqd`. It returns the container ID and its IP address, or makes the test fail on error.
func SetupPostgreSQLContainer ¶
func SetupPostgreSQLContainer() (c ContainerID, ip string, port int, err error)
SetupPostgreSQLContainer sets up a real PostgreSQL instance for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func SetupRedisContainer ¶
func SetupRedisContainer() (c ContainerID, ip string, port int, err error)
SetupRedisContainer sets up a real Redis instance for testing purposes using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func (ContainerID) IP ¶
func (c ContainerID) IP() (string, error)
IP retrieves the container's IP address.
func (ContainerID) Kill ¶
func (c ContainerID) Kill() error
Kill runs "docker kill" on the container.
func (ContainerID) KillRemove ¶
func (c ContainerID) KillRemove() error
KillRemove calls Kill on the container, and then Remove if there was no error.
func (ContainerID) Remove ¶
func (c ContainerID) Remove() error
Remove runs "docker rm" on the container