Documentation ¶
Overview ¶
Package containertest provides an ephemeral container (such as a database) for integration testing. It's designed to be used in code that needs to work inside and outside google.
Index ¶
- type ConnInfo
- type MySQL
- func (m *MySQL) Environment() []string
- func (m *MySQL) ImageRepository() string
- func (m *MySQL) ImageTag() string
- func (m *MySQL) Password() string
- func (m *MySQL) Port() string
- func (m *MySQL) StartupPorts() []string
- func (m *MySQL) TestConn(progressLogger TestLogger, connInfo *ConnInfo) error
- func (m *MySQL) Username() string
- type Option
- type Postgres
- func (p *Postgres) Environment() []string
- func (p *Postgres) ImageRepository() string
- func (p *Postgres) ImageTag() string
- func (p *Postgres) Password() string
- func (p *Postgres) Port() string
- func (p *Postgres) StartupPorts() []string
- func (p *Postgres) TestConn(progressLogger TestLogger, connInfo *ConnInfo) error
- func (p *Postgres) Username() string
- type Service
- type TestLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnInfo ¶
type ConnInfo struct { Host string // PortMapper maps from container port to host port. Do not use after container is closed. PortMapper func(containerPort string) (hostPort string) // contains filtered or unexported fields }
ConnInfo specifies how connect to the created container.
type MySQL ¶
type MySQL struct {
Version string
}
MySQL satisfies Service, defining a MySQL server container.
func (*MySQL) Environment ¶
Environment satisfies [Service.Environment].
func (*MySQL) ImageRepository ¶
ImageRepository satisfies [Service.ImageRepository].
func (*MySQL) StartupPorts ¶
StartupPorts satisfies [Service.StartupPorts].
type Option ¶
type Option func(*config) *config
Option sets a configuration option for this package. Users should not implement these functions, they should use one of the With* functions.
func WithKillAfterSeconds ¶
WithKillAfterSeconds is an option that overrides the default time period after which the docker container will kill itself.
Containers might bypass the normal clean shutdown logic if the test terminates abnormally, such as when ctrl-C is pressed during a test. Therefore we instruct the container to kill itself after a while. The duration must be longer than longest test that uses the container. There's no harm in leaving lots of extra time.
func WithLogger ¶
func WithLogger(l TestLogger) Option
WithLogger overrides the default logger. This logger will receive messages about service startup progress. The default is to use the go "log" package.
type Postgres ¶
type Postgres struct { // Version is the ImageTag that will be returned by the Service interface. Version string }
Postgres satisfies Service, defining a Postgres server container.
func (*Postgres) Environment ¶
Environment satisfies [Service.Environment].
func (*Postgres) ImageRepository ¶
ImageRepository satisfies [Service.ImageRepository].
func (*Postgres) StartupPorts ¶
StartupPorts satisfies [Service.StartupPorts].
type Service ¶
type Service interface { // ImageRepository returns the repository for docker image (ex: mysql). ImageRepository() string // ImageTag returns the tag for docker image (ex: 5.3). ImageTag() string // Environment returns variables to be set in container. Each element is in format of "KEY=VALUE". Environment() []string // StartupPorts is the list of ports that must be exposed by container before TestConn is run. StartupPorts() []string // TestConn takes a logger and a struct with connection info, and returns nil if app has started. TestConn(progressLogger TestLogger, info *ConnInfo) error }
Service provides information about what container image should be started and how to know when it has finished stating up.
type TestLogger ¶
TestLogger allows the caller to optionally provide a custom logger for printing status updates about service startup progress. The default is to use the go "log" package. testing.TB satisfies TestLogger, and is usually what you want to put here.