Documentation
¶
Overview ¶
Package gnomock contains a framework to set up temporary docker containers for integration and end-to-end testing of other applications. It handles pulling images, starting containers, waiting for them to become available, setting up their initial state and cleaning up in the end.
It can be used either directly, or via already existing implementations of various connectors built by the community.
Index ¶
- Constants
- Variables
- func Stop(c *Container) error
- type Container
- type HealthcheckFunc
- type InitFunc
- type NamedPorts
- type Option
- func WithContext(ctx context.Context) Option
- func WithEnv(env string) Option
- func WithHealthCheck(f HealthcheckFunc) Option
- func WithHealthCheckInterval(t time.Duration) Option
- func WithInit(f InitFunc) Option
- func WithStartTimeout(t time.Duration) Option
- func WithWaitTimeout(t time.Duration) Option
- type Port
- type Preset
Constants ¶
const DefaultPort = "default"
DefaultPort should be used with simple containers that expose only one TCP port. Use DefaultTCP function when creating a container, and use DefaultPort when calling Address()
Variables ¶
var ErrEnvClient = fmt.Errorf("can't connect to docker host")
ErrEnvClient means that Gnomock can't connect to docker daemon in the testing environment. See https://docs.docker.com/compose/reference/overview/ for information on required configuration
var ErrPortNotFound = errors.New("port not found")
ErrPortNotFound means that a port with the requested name was not found in the created container. Make sure that the name used in Find method matches the name used in in NamedPorts. For default values, use DefaultTCP and DefaultPort
Functions ¶
Types ¶
type Container ¶
type Container struct { ID string Host string Ports NamedPorts }
Container represents a docker container created for testing. Host and Ports fields should be used to configure the connection to this container. ID matches the original docker container ID
func Start ¶
func Start(image string, ports NamedPorts, opts ...Option) (c *Container, err error)
Start creates a new container using provided image and binds random ports on the host to the provided ports inside the container. Image may include tag, which is set to "latest" by default. Optional configuration is available through Option functions. The returned container must be stopped when no longer needed using its Stop() method
func StartPreset ¶
StartPreset creates a container using the provided Preset. The Preset provides its own Options to configure Gnomock container. Usually this is enough, but it is still possible to extend/override Preset options with new values. For example, wait timeout defined in the Preset, if at all, might be not enough for this particular usage, so it can't be changed during this call.
All provided Options are applied. First, Preset options are applied. Then, custom Options. If both Preset and custom Options change the same configuration, custom Options are used
type HealthcheckFunc ¶
HealthcheckFunc defines a function to be used to determine container health. It receives a host and a port, and returns an error if the container is not ready, or nil when the container can be used. One example of HealthcheckFunc would be an attempt to establish the same connection to the container that the application under test uses
type InitFunc ¶
InitFunc defines a function to be called on a ready to use container to set up its initial state before running the tests. For example, InitFunc can take care of creating a SQL table and inserting test data into it
type NamedPorts ¶ added in v0.1.0
NamedPorts is a collection of ports exposed by a container, where every exposed port is given a name. Some examples of names are "web" or "api" for a container that exposes two separate ports: one for web access and another for API calls
func DefaultTCP ¶ added in v0.1.0
func DefaultTCP(port int) NamedPorts
DefaultTCP is a utility function to use with simple containers exposing a single TCP port. Use it to create default named port for the provided port number. Pass DefaultPort to Address() method to get the address of the default port
func (NamedPorts) Find ¶ added in v0.1.0
func (p NamedPorts) Find(proto string, portNum int) (string, error)
Find returns the name of a port with the provided protocol and number. Use this method to find out the name of an exposed ports, when port number and protocol are known
func (NamedPorts) Get ¶ added in v0.1.0
func (p NamedPorts) Get(name string) Port
Get returns a port with the provided name. An empty value is returned if there are no ports with the given name
type Option ¶
type Option func(*options)
Option is an optional Gnomock configuration. Functions implementing this signature may be combined to configure Gnomock containers for different use cases
func WithContext ¶
WithContext sets the provided context to be used for setting up a Gnomock container. Canceling this context will cause Start() to abort
func WithEnv ¶ added in v0.1.1
WithEnv adds environment variable to the container. For example, AWS_ACCESS_KEY_ID=FOOBARBAZ
func WithHealthCheck ¶
func WithHealthCheck(f HealthcheckFunc) Option
WithHealthCheck allows to define a rule to consider a Gnomock container ready to use. For example, it can attempt to connect to this container, and return an error on any failure, or nil on success. This function is called repeatedly until the timeout is reached, or until a nil error is returned
func WithHealthCheckInterval ¶
WithHealthCheckInterval defines an interval between two consecutive health check calls. This is a constant interval
func WithInit ¶
WithInit lets the provided InitFunc to be called when a Gnomock container is created, but before Start() returns. Use this function to run arbitrary code on the new container before using it. It can be useful to bring the container to a certain state (e.g create SQL schema)
func WithStartTimeout ¶
WithStartTimeout sets the amount of time to wait for a new Gnomock container to start. This includes pulling an image and creating a new container from it. To set the amount of time to wait before a created container healthy and ready to use, use WithWaitTimeout
func WithWaitTimeout ¶
WithWaitTimeout sets the amount of time to wait for a created container to become ready to use. If health check function does not return nil error until this timeout is reached, Start() fails
type Port ¶ added in v0.1.0
type Port struct { // Protocol of the exposed port (TCP/UDP) Protocol string // Port number of the exposed port Port int }
Port is a combination of port number and protocol that are exposed in a container
type Preset ¶
type Preset interface { // Image returns a canonical docker image used to setup this Preset Image() string // Ports returns a group of ports exposed by this Preset, where every port // is given a unique name. For example, if a container exposes API endpoint // on port 8080, and web interface on port 80, there should be two named // ports: "web" and "api" Ports() NamedPorts // Options returns a list of Option functions that allow to setup this // Preset implementation Options() []Option }
Preset is a type that includes ready to use Gnomock configuration. Such configuration includes image and ports as well as options specific to this implementation. For example, well known services like Redis or Postgres may have Gnomock implementations, providing healthcheck functions and basic initialization options