Documentation
¶
Overview ¶
Package dft (Docker For Testing) is a lightweight wrapper around docker based on the std lib.
Only requirement: A running docker daemon.
The package is intended to be used in various testing setups from local testing to CI/CD pipelines. It's main goals are to reduce the need for mocks (especially database ones), and to lower the amount of packages required for testing.
Containers can be spun up with options for ports, environment variables or [CMD] overwrites.
Index ¶
- type Container
- func (c *Container) ExposedPortAddresses(port uint) ([]string, bool)
- func (c *Container) ExposedPorts(port uint) ([]uint, bool)
- func (c *Container) Logs(ctx context.Context) (string, error)
- func (c Container) Stop(ctx context.Context) error
- func (c *Container) WaitCmd(ctx context.Context, cmd []string, ...) error
- type ContainerOption
- type WaitOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
func StartContainer ¶
func StartContainer( ctx context.Context, imageName string, opts ...ContainerOption, ) (*Container, error)
StartContainer tries to spin up a container for the given image. This may take a while if the given image is not present on the host since it will be pulled from the registry.
func (*Container) ExposedPortAddresses ¶
ExposedPortAddresses will return a list of host ports exposing the internal port in the format of "<IP>:<PORT>"
func (*Container) ExposedPorts ¶
ExposedPorts will return a list of host ports exposing the internal port
func (*Container) Logs ¶
Logs will retrieve the latest logs from the container This call errors once `Stop` was called.
func (Container) Stop ¶
Stop will stop the container and remove it (as well as related volumes) from the host system
func (*Container) WaitCmd ¶
func (c *Container) WaitCmd( ctx context.Context, cmd []string, metCondition func(stdOut string, stdErr string, code int) bool, opts ...WaitOption, ) error
WaitCmd takes a command in the form of a ["<cmd>", "(<arg> | <-flag> | <flagvalue>)"...] which will be executed periodically until either it returns true or the context expires
type ContainerOption ¶
type ContainerOption func(cfg *containerCfg)
func WithCmd ¶
func WithCmd(args []string) ContainerOption
WithCmd overwrites the [CMD] part of the dockerfile
func WithEnvVar ¶
func WithEnvVar(key string, value string) ContainerOption
WithEnvVar will add "<KEY>=<VALUE>" to the env of the container
func WithMount ¶ added in v0.0.3
func WithMount(dest string, trgt string) ContainerOption
WithExecuteInsideContainer defines if the wait cmd is executed inside the container or on the host machine
func WithPort ¶
func WithPort(port uint, target uint) ContainerOption
WithPort will expose the passed internal port via a given target port on the host.
func WithRandomPort ¶
func WithRandomPort(port uint) ContainerOption
WithRandomPort will expose the passed internal port via a random port on the host. Use
ExposedPort ExposedPortAddr
to retrieve the actual host port used
(shorthand for `WithPort(x,0)`)
type WaitOption ¶
type WaitOption func(cfg *waitCfg)
func WithExecuteInsideContainer ¶
func WithExecuteInsideContainer(b bool) WaitOption
WithExecuteInsideContainer defines if the wait cmd is executed inside the container or on the host machine