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 ¶
- Variables
- func AwaitReachable(addr string, maxWait time.Duration) error
- func ConnectToCustomContainer(url string, tries int, delay time.Duration, connector func(url string) bool) error
- func DefaultConsulLocalConfig() (string, error)
- func GenerateContainerID() string
- func HaveImage(name string) (bool, error)
- func IP(containerID string) (string, error)
- func KillContainer(container string) error
- func Pull(image string) error
- func RandomPort() int
- func SetUpMySQLDatabase(databaseName, connectURL string) (url string, err error)
- func SetUpPostgreDatabase(databaseName, connectURL string) (modifiedURL string, err error)
- func StartContainer(container string) error
- func StopContainer(container string) error
- type ContainerID
- func ConnectToActiveMQ(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToCassandra(versionTag string, tries int, delay time.Duration, ...) (c ContainerID, err error)
- func ConnectToConsul(tries int, delay time.Duration, connector func(address string) bool) (c ContainerID, err error)
- func ConnectToElasticSearch(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToEtcd(tries int, delay time.Duration, connector func(address string) bool) (c ContainerID, err error)
- func ConnectToMockserver(tries int, delay time.Duration, mockConnector 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 ConnectToRabbitMQ(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 ConnectToRethinkDB(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func ConnectToZooKeeper(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
- func SetupActiveMQContainer() (c ContainerID, ip string, port int, err error)
- func SetupCassandraContainer(versionTag string, optionalParams ...string) (c ContainerID, ip string, port int, err error)
- func SetupConsulContainer() (c ContainerID, ip string, port int, err error)
- func SetupContainer(image string, port int, timeout time.Duration, start func() (string, error)) (c ContainerID, ip string, err error)
- func SetupCustomContainer(imageName string, exposedPort int, timeOut time.Duration, ...) (c ContainerID, ip string, localPort int, err error)
- func SetupElasticSearchContainer() (c ContainerID, ip string, port int, err error)
- func SetupEtcdContainer() (c ContainerID, ip string, port int, err error)
- func SetupMockserverContainer() (c ContainerID, ip string, mockPort, proxyPort int, err error)
- func SetupMongoContainer() (c ContainerID, ip string, port int, err error)
- func SetupMultiportContainer(image string, ports []int, timeout time.Duration, start func() (string, error)) (c ContainerID, ip string, 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 SetupRabbitMQContainer() (c ContainerID, ip string, port int, err error)
- func SetupRedisContainer() (c ContainerID, ip string, port int, err error)
- func SetupRethinkDBContainer() (c ContainerID, ip string, port int, err error)
- func SetupZooKeeperContainer() (c ContainerID, ip string, port int, err error)
Constants ¶
This section is empty.
Variables ¶
var ( // ConsulDatacenter must be defined when starting a Consul datacenter; this // value will be used for both the datacenter and the ACL datacenter ConsulDatacenter = "test" // ConsulACLDefaultPolicy defines the default policy to use with Consul ACLs ConsulACLDefaultPolicy = "deny" // ConsulACLMasterToken defines the master ACL token ConsulACLMasterToken = "test" // A function with no arguments that outputs a valid JSON string to be used // as the value of the environment variable CONSUL_LOCAL_CONFIG. ConsulLocalConfigGen = DefaultConsulLocalConfig )
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", "1")) // UseDockerMachine if set, forces docker to use the legacy docker-machine on OSX/Windows. UseDockerMachine = env.Getenv("DOCKERTEST_LEGACY_DOCKER_MACHINE", "") // ContainerPrefix will be prepended to all containers started by dockertest to make identification of these "test images" hassle-free. ContainerPrefix = env.Getenv("DOCKERTEST_CONTAINER_PREFIX", "dockertest-") )
Dockertest configuration
var ( // MongoDBImageName is the MongoDB image name on dockerhub. MongoDBImageName = env.Getenv("DOCKERTEST_MONGODB_IMAGE_NAME", "mongo") // MySQLImageName is the MySQL image name on dockerhub. MySQLImageName = env.Getenv("DOCKERTEST_MYSQL_IMAGE_NAME", "mysql") // PostgresImageName is the PostgreSQL image name on dockerhub. PostgresImageName = env.Getenv("DOCKERTEST_POSTGRES_IMAGE_NAME", "postgres") // ElasticSearchImageName is the ElasticSearch image name on dockerhub. ElasticSearchImageName = env.Getenv("DOCKERTEST_ELASTICSEARCH_IMAGE_NAME", "elasticsearch") // RedisImageName is the Redis image name on dockerhub. RedisImageName = env.Getenv("DOCKERTEST_REDIS_IMAGE_NAME", "redis") // NSQImageName is the NSQ image name on dockerhub. NSQImageName = env.Getenv("DOCKERTEST_NSQ_IMAGE_NAME", "nsqio/nsq") // RethinkDBImageName is the RethinkDB image name on dockerhub. RethinkDBImageName = env.Getenv("DOCKERTEST_RETHINKDB_IMAGE_NAME", "rethinkdb") // RabbitMQImage name is the RabbitMQ image name on dockerhub. RabbitMQImageName = env.Getenv("DOCKERTEST_RABBITMQ_IMAGE_NAME", "rabbitmq") // ActiveMQImage name is the ActiveMQ image name on dockerhub. ActiveMQImageName = env.Getenv("DOCKERTEST_ACTIVEMQ_IMAGE_NAME", "webcenter/activemq") // MockserverImageName name is the Mockserver image name on dockerhub. MockserverImageName = env.Getenv("DOCKERTEST_MOCKSERVER_IMAGE_NAME", "jamesdbloom/mockserver") // ConsulImageName is the Consul image name on dockerhub. ConsulImageName = env.Getenv("DOCKERTEST_CONSUL_IMAGE_NAME", "consul") // ZooKeeperImageName is the ZooKeeper image name on dockerhub. ZooKeeperImageName = env.Getenv("DOCKERTEST_ZOOKEEPER_IMAGE_NAME", "jplock/zookeeper") // CassandraImageName is the Cassandra image name on dockerhub. CassandraImageName = env.Getenv("DOCKERTEST_CASSANDRA_IMAGE_NAME", "cassandra") // EtcdImageName is the etcd image name on dockerhub. EtcdImageName = env.Getenv("DOCKERTEST_ETCD_IMAGE_NAME", "quay.io/coreos/etcd") )
Image configuration
var ( // 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" )
Username and password configuration
Functions ¶
func AwaitReachable ¶ added in v3.0.1
AwaitReachable tries to make a TCP connection to addr regularly. It returns an error if it's unable to make a connection before maxWait.
func ConnectToCustomContainer ¶ added in v3.0.1
func ConnectToCustomContainer(url string, tries int, delay time.Duration, connector func(url string) bool) error
ConnectToCustomContainer attempts to connect to a custom container until successful or the maximum number of tries is reached.
func DefaultConsulLocalConfig ¶ added in v3.0.1
func GenerateContainerID ¶ added in v3.0.1
func GenerateContainerID() string
GenerateContainerID generated a random container id.
func KillContainer ¶ added in v3.0.1
KillContainer runs docker kill on a container.
func RandomPort ¶ added in v3.0.1
func RandomPort() int
RandomPort returns a random non-priviledged port.
func SetUpMySQLDatabase ¶ added in v3.0.1
SetUpMySQLDatabase connects mysql container with given $connectURL and also creates a new database named $databaseName A modified url used to connect the created database will be returned
func SetUpPostgreDatabase ¶ added in v3.0.1
SetUpPostgreDatabase connects postgre container with given $connectURL and also creates a new database named $databaseName A modified url used to connect the created database will be returned
func StartContainer ¶ added in v3.0.1
StartContainer runs 'docker start' on a container.
func StopContainer ¶ added in v3.0.1
StopContainer runs 'docker stop' on a container.
Types ¶
type ContainerID ¶ added in v3.0.1
type ContainerID string
ContainerID represents a container and offers methods like Kill or IP.
func ConnectToActiveMQ ¶ added in v3.0.1
func ConnectToActiveMQ(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToActiveMQ starts a ActiveMQ image and passes the amqp url to the connector callback. The url will match the ip:port pattern (e.g. 123.123.123.123:4241)
func ConnectToCassandra ¶ added in v3.0.1
func ConnectToCassandra(versionTag string, tries int, delay time.Duration, connector func(url string) bool, optionalParams ...string) (c ContainerID, err error)
ConnectToCassandra starts a Cassandra image and passes the nodes connection string to the connector callback function. The connection string will match the ip:port pattern, where port is the mapped CQL port.
func ConnectToConsul ¶ added in v3.0.1
func ConnectToConsul(tries int, delay time.Duration, connector func(address string) bool) (c ContainerID, err error)
ConnectToConsul starts a Consul image and passes the address to the connector callback function.
func ConnectToElasticSearch ¶ added in v3.0.1
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 ConnectToEtcd ¶ added in v3.0.1
func ConnectToEtcd(tries int, delay time.Duration, connector func(address string) bool) (c ContainerID, err error)
ConnectToEtcd starts a etcd image and passes the address to the connector callback function.
func ConnectToMockserver ¶ added in v3.0.1
func ConnectToMockserver(tries int, delay time.Duration, mockConnector func(url string) bool, proxyConnector func(url string) bool) (c ContainerID, err error)
ConnectToMockserver starts a Mockserver image and passes the mock and proxy urls to the connector callback functions. The urls will match the http://ip:port pattern (e.g. http://123.123.123.123:4241)
func ConnectToMongoDB ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 ConnectToRabbitMQ ¶ added in v3.0.1
func ConnectToRabbitMQ(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToRabbitMQ starts a RabbitMQ image and passes the amqp url to the connector callback. The url will match the ip:port pattern (e.g. 123.123.123.123:4241)
func ConnectToRedis ¶ added in v3.0.1
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 ConnectToRethinkDB ¶ added in v3.0.1
func ConnectToRethinkDB(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToRethinkDB starts a RethinkDB 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 ConnectToZooKeeper ¶ added in v3.0.1
func ConnectToZooKeeper(tries int, delay time.Duration, connector func(url string) bool) (c ContainerID, err error)
ConnectToZooKeeper starts a ZooKeeper image and passes the nodes connection string to the connector callback function. The connection string will match the ip:port pattern.
func SetupActiveMQContainer ¶ added in v3.0.1
func SetupActiveMQContainer() (c ContainerID, ip string, port int, err error)
SetupActiveMQContainer sets up a real ActiveMQ 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 SetupCassandraContainer ¶ added in v3.0.1
func SetupCassandraContainer(versionTag string, optionalParams ...string) (c ContainerID, ip string, port int, err error)
SetupCassandraContainer sets up a real Cassandra node for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func SetupConsulContainer ¶ added in v3.0.1
func SetupConsulContainer() (c ContainerID, ip string, port int, err error)
SetupConsulContainer sets up a real Consul 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 SetupContainer ¶ added in v3.0.1
func SetupContainer(image string, port int, timeout time.Duration, start func() (string, error)) (c ContainerID, ip string, err error)
SetupContainer sets up a container, using the start function to run the given image. It also looks up the IP address of the container, and tests this address with the given port and timeout. It returns the container ID and its IP address, or makes the test fail on error.
func SetupCustomContainer ¶ added in v3.0.1
func SetupCustomContainer(imageName string, exposedPort int, timeOut time.Duration, extraDockerArgs ...string) (c ContainerID, ip string, localPort int, err error)
SetupCustomContainer sets up a real an instance of the given image for testing purposes, using a Docker container. It returns the container ID and its IP address, or makes the test fail on error.
func SetupElasticSearchContainer ¶ added in v3.0.1
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 SetupEtcdContainer ¶ added in v3.0.1
func SetupEtcdContainer() (c ContainerID, ip string, port int, err error)
SetupEtcdContainer sets up a real etcd 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 SetupMockserverContainer ¶ added in v3.0.1
func SetupMockserverContainer() (c ContainerID, ip string, mockPort, proxyPort int, err error)
SetupMockserverContainer sets up a real Mockserver 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 ¶ added in v3.0.1
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 SetupMultiportContainer ¶ added in v3.0.1
func SetupMultiportContainer(image string, ports []int, timeout time.Duration, start func() (string, error)) (c ContainerID, ip string, err error)
SetupMultiportContainer sets up a container, using the start function to run the given image. It also looks up the IP address of the container, and tests this address with the given ports and timeout. It returns the container ID and its IP address, or makes the test fail on error.
func SetupMySQLContainer ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 ¶ added in v3.0.1
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 SetupRabbitMQContainer ¶ added in v3.0.1
func SetupRabbitMQContainer() (c ContainerID, ip string, port int, err error)
SetupRabbitMQContainer sets up a real RabbitMQ 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 ¶ added in v3.0.1
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 SetupRethinkDBContainer ¶ added in v3.0.1
func SetupRethinkDBContainer() (c ContainerID, ip string, port int, err error)
SetupRethinkDBContainer sets up a real RethinkDB 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 SetupZooKeeperContainer ¶ added in v3.0.1
func SetupZooKeeperContainer() (c ContainerID, ip string, port int, err error)
SetupZooKeeperContainer sets up a real ZooKeeper node 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 ¶ added in v3.0.1
func (c ContainerID) IP() (string, error)
IP retrieves the container's IP address.
func (ContainerID) Kill ¶ added in v3.0.1
func (c ContainerID) Kill() error
Kill runs "docker kill" on the container.
func (ContainerID) KillRemove ¶ added in v3.0.1
func (c ContainerID) KillRemove() error
KillRemove calls Kill on the container, and then Remove if there was no error.
func (ContainerID) Remove ¶ added in v3.0.1
func (c ContainerID) Remove() error
Remove runs "docker rm" on the container
func (ContainerID) Start ¶ added in v3.0.1
func (c ContainerID) Start() error
Start runs "docker start" on the container.
func (ContainerID) Stop ¶ added in v3.0.1
func (c ContainerID) Stop() error
Stop runs "docker stop" on the container.