2017-02-26 21:53:19 +00:00
|
|
|
package dockertest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-26 23:51:00 +00:00
|
|
|
"io/ioutil"
|
2018-07-06 16:09:34 +00:00
|
|
|
"net"
|
2017-02-26 21:53:19 +00:00
|
|
|
"os"
|
2018-01-26 23:51:00 +00:00
|
|
|
"path/filepath"
|
2017-02-26 21:53:19 +00:00
|
|
|
"runtime"
|
2017-04-17 15:17:06 +00:00
|
|
|
"strings"
|
2018-01-26 23:51:00 +00:00
|
|
|
"time"
|
2017-04-17 15:17:06 +00:00
|
|
|
|
2018-02-11 00:29:52 +00:00
|
|
|
"github.com/cenkalti/backoff"
|
2018-07-06 16:09:34 +00:00
|
|
|
dc "github.com/ory/dockertest/docker"
|
2017-02-26 21:53:19 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pool represents a connection to the docker API and is used to create and remove docker images.
|
|
|
|
type Pool struct {
|
|
|
|
Client *dc.Client
|
|
|
|
MaxWait time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource represents a docker container.
|
|
|
|
type Resource struct {
|
2018-07-06 16:09:34 +00:00
|
|
|
pool *Pool
|
2017-02-26 21:53:19 +00:00
|
|
|
Container *dc.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPort returns a resource's published port. You can use it to connect to the service via localhost, e.g. tcp://localhost:1231/
|
|
|
|
func (r *Resource) GetPort(id string) string {
|
|
|
|
if r.Container == nil {
|
|
|
|
return ""
|
|
|
|
} else if r.Container.NetworkSettings == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
m, ok := r.Container.NetworkSettings.Ports[dc.Port(id)]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
} else if len(m) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return m[0].HostPort
|
|
|
|
}
|
|
|
|
|
2017-09-05 22:06:47 +00:00
|
|
|
func (r *Resource) GetBoundIP(id string) string {
|
|
|
|
if r.Container == nil {
|
|
|
|
return ""
|
|
|
|
} else if r.Container.NetworkSettings == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
m, ok := r.Container.NetworkSettings.Ports[dc.Port(id)]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
} else if len(m) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return m[0].HostIP
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// GetHostPort returns a resource's published port with an address.
|
|
|
|
func (r *Resource) GetHostPort(portID string) string {
|
|
|
|
if r.Container == nil {
|
|
|
|
return ""
|
|
|
|
} else if r.Container.NetworkSettings == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
m, ok := r.Container.NetworkSettings.Ports[dc.Port(portID)]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
} else if len(m) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ip := m[0].HostIP
|
|
|
|
if ip == "0.0.0.0" {
|
|
|
|
ip = "localhost"
|
|
|
|
}
|
|
|
|
return net.JoinHostPort(ip, m[0].HostPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close removes a container and linked volumes from docker by calling pool.Purge.
|
|
|
|
func (r *Resource) Close() error {
|
|
|
|
return r.pool.Purge(r)
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// Expire sets a resource's associated container to terminate after a period has passed
|
|
|
|
func (r *Resource) Expire(seconds uint) error {
|
|
|
|
go func() {
|
|
|
|
if err := r.pool.Client.StopContainer(r.Container.ID, seconds); err != nil {
|
|
|
|
// Error handling?
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:17:06 +00:00
|
|
|
// NewTLSPool creates a new pool given an endpoint and the certificate path. This is required for endpoints that
|
2017-02-26 21:53:19 +00:00
|
|
|
// require TLS communication.
|
|
|
|
func NewTLSPool(endpoint, certpath string) (*Pool, error) {
|
|
|
|
ca := fmt.Sprintf("%s/ca.pem", certpath)
|
|
|
|
cert := fmt.Sprintf("%s/cert.pem", certpath)
|
|
|
|
key := fmt.Sprintf("%s/key.pem", certpath)
|
2017-04-17 15:17:06 +00:00
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
client, err := dc.NewTLSClient(endpoint, cert, key, ca)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
2017-04-17 15:17:06 +00:00
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
return &Pool{
|
|
|
|
Client: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPool creates a new pool. You can pass an empty string to use the default, which is taken from the environment
|
2018-01-26 23:51:00 +00:00
|
|
|
// variable DOCKER_HOST and DOCKER_URL, or from docker-machine if the environment variable DOCKER_MACHINE_NAME is set,
|
2017-02-26 21:53:19 +00:00
|
|
|
// or if neither is defined a sensible default for the operating system you are on.
|
2018-01-26 23:51:00 +00:00
|
|
|
// TLS pools are automatically configured if the DOCKER_CERT_PATH environment variable exists.
|
2017-02-26 21:53:19 +00:00
|
|
|
func NewPool(endpoint string) (*Pool, error) {
|
|
|
|
if endpoint == "" {
|
2018-02-11 00:29:52 +00:00
|
|
|
if os.Getenv("DOCKER_MACHINE_NAME") != "" {
|
2017-02-26 21:53:19 +00:00
|
|
|
client, err := dc.NewClientFromEnv()
|
|
|
|
if err != nil {
|
2018-02-11 00:29:52 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create client from environment")
|
2017-02-26 21:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Pool{Client: client}, nil
|
2018-02-11 00:29:52 +00:00
|
|
|
} else if os.Getenv("DOCKER_HOST") != "" {
|
|
|
|
endpoint = os.Getenv("DOCKER_HOST")
|
|
|
|
} else if os.Getenv("DOCKER_URL") != "" {
|
|
|
|
endpoint = os.Getenv("DOCKER_URL")
|
2017-02-26 21:53:19 +00:00
|
|
|
} else if runtime.GOOS == "windows" {
|
|
|
|
endpoint = "http://localhost:2375"
|
|
|
|
} else {
|
|
|
|
endpoint = "unix:///var/run/docker.sock"
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:17:06 +00:00
|
|
|
|
2018-02-11 00:29:52 +00:00
|
|
|
if os.Getenv("DOCKER_CERT_PATH") != "" && shouldPreferTls(endpoint) {
|
2018-01-26 23:51:00 +00:00
|
|
|
return NewTLSPool(endpoint, os.Getenv("DOCKER_CERT_PATH"))
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
client, err := dc.NewClient(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Pool{
|
|
|
|
Client: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
func shouldPreferTls(endpoint string) bool {
|
|
|
|
return !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "unix://")
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:17:06 +00:00
|
|
|
// RunOptions is used to pass in optional parameters when running a container.
|
|
|
|
type RunOptions struct {
|
2018-01-26 23:51:00 +00:00
|
|
|
Hostname string
|
|
|
|
Name string
|
2017-07-18 14:15:54 +00:00
|
|
|
Repository string
|
|
|
|
Tag string
|
|
|
|
Env []string
|
|
|
|
Entrypoint []string
|
|
|
|
Cmd []string
|
|
|
|
Mounts []string
|
|
|
|
Links []string
|
|
|
|
ExposedPorts []string
|
2018-02-11 00:29:52 +00:00
|
|
|
ExtraHosts []string
|
2018-10-03 16:55:26 +00:00
|
|
|
CapAdd []string
|
|
|
|
SecurityOpt []string
|
2019-04-12 15:51:37 +00:00
|
|
|
DNS []string
|
2018-07-06 16:09:34 +00:00
|
|
|
WorkingDir string
|
2018-10-03 16:55:26 +00:00
|
|
|
NetworkID string
|
2018-07-06 16:09:34 +00:00
|
|
|
Labels map[string]string
|
2017-09-05 22:06:47 +00:00
|
|
|
Auth dc.AuthConfiguration
|
2018-01-26 23:51:00 +00:00
|
|
|
PortBindings map[dc.Port][]dc.PortBinding
|
2018-10-03 16:55:26 +00:00
|
|
|
Privileged bool
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// BuildAndRunWithOptions builds and starts a docker container.
|
|
|
|
// Optional modifier functions can be passed in order to change the hostconfig values not covered in RunOptions
|
|
|
|
func (d *Pool) BuildAndRunWithOptions(dockerfilePath string, opts *RunOptions, hcOpts ...func(*dc.HostConfig)) (*Resource, error) {
|
2018-01-26 23:51:00 +00:00
|
|
|
// Set the Dockerfile folder as build context
|
|
|
|
dir, file := filepath.Split(dockerfilePath)
|
|
|
|
|
|
|
|
err := d.Client.BuildImage(dc.BuildImageOptions{
|
|
|
|
Name: opts.Name,
|
|
|
|
Dockerfile: file,
|
|
|
|
OutputStream: ioutil.Discard,
|
|
|
|
ContextDir: dir,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Repository = opts.Name
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
return d.RunWithOptions(opts, hcOpts...)
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildAndRun builds and starts a docker container
|
|
|
|
func (d *Pool) BuildAndRun(name, dockerfilePath string, env []string) (*Resource, error) {
|
|
|
|
return d.BuildAndRunWithOptions(dockerfilePath, &RunOptions{Name: name, Env: env})
|
2017-04-17 15:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunWithOptions starts a docker container.
|
2019-04-12 15:51:37 +00:00
|
|
|
// Optional modifier functions can be passed in order to change the hostconfig values not covered in RunOptions
|
2017-02-26 21:53:19 +00:00
|
|
|
//
|
2017-04-17 15:17:06 +00:00
|
|
|
// pool.Run(&RunOptions{Repository: "mongo", Cmd: []string{"mongod", "--smallfiles"}})
|
2019-04-12 15:51:37 +00:00
|
|
|
// pool.Run(&RunOptions{Repository: "mongo", Cmd: []string{"mongod", "--smallfiles"}}, func(hostConfig *dc.HostConfig) {
|
|
|
|
// hostConfig.ShmSize = shmemsize
|
|
|
|
// })
|
|
|
|
func (d *Pool) RunWithOptions(opts *RunOptions, hcOpts ...func(*dc.HostConfig)) (*Resource, error) {
|
2017-04-17 15:17:06 +00:00
|
|
|
repository := opts.Repository
|
|
|
|
tag := opts.Tag
|
|
|
|
env := opts.Env
|
|
|
|
cmd := opts.Cmd
|
2017-07-18 14:15:54 +00:00
|
|
|
ep := opts.Entrypoint
|
2018-07-06 16:09:34 +00:00
|
|
|
wd := opts.WorkingDir
|
2017-07-18 14:15:54 +00:00
|
|
|
var exp map[dc.Port]struct{}
|
|
|
|
|
|
|
|
if len(opts.ExposedPorts) > 0 {
|
|
|
|
exp = map[dc.Port]struct{}{}
|
|
|
|
for _, p := range opts.ExposedPorts {
|
|
|
|
exp[dc.Port(p)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 15:17:06 +00:00
|
|
|
|
|
|
|
mounts := []dc.Mount{}
|
|
|
|
|
|
|
|
for _, m := range opts.Mounts {
|
|
|
|
sd := strings.Split(m, ":")
|
|
|
|
if len(sd) == 2 {
|
|
|
|
mounts = append(mounts, dc.Mount{
|
|
|
|
Source: sd[0],
|
|
|
|
Destination: sd[1],
|
|
|
|
RW: true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return nil, errors.Wrap(fmt.Errorf("invalid mount format: got %s, expected <src>:<dst>", m), "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
if tag == "" {
|
|
|
|
tag = "latest"
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
networkingConfig := dc.NetworkingConfig{
|
|
|
|
EndpointsConfig: map[string]*dc.EndpointConfig{},
|
|
|
|
}
|
|
|
|
if opts.NetworkID != "" {
|
|
|
|
networkingConfig.EndpointsConfig[opts.NetworkID] = &dc.EndpointConfig{}
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
_, err := d.Client.InspectImage(fmt.Sprintf("%s:%s", repository, tag))
|
|
|
|
if err != nil {
|
|
|
|
if err := d.Client.PullImage(dc.PullImageOptions{
|
|
|
|
Repository: repository,
|
|
|
|
Tag: tag,
|
2017-09-05 22:06:47 +00:00
|
|
|
}, opts.Auth); err != nil {
|
2017-02-26 21:53:19 +00:00
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
hostConfig := dc.HostConfig{
|
|
|
|
PublishAllPorts: true,
|
|
|
|
Binds: opts.Mounts,
|
|
|
|
Links: opts.Links,
|
|
|
|
PortBindings: opts.PortBindings,
|
|
|
|
ExtraHosts: opts.ExtraHosts,
|
|
|
|
CapAdd: opts.CapAdd,
|
|
|
|
SecurityOpt: opts.SecurityOpt,
|
|
|
|
Privileged: opts.Privileged,
|
|
|
|
DNS: opts.DNS,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostConfigOption := range hcOpts {
|
|
|
|
hostConfigOption(&hostConfig)
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
c, err := d.Client.CreateContainer(dc.CreateContainerOptions{
|
2018-01-26 23:51:00 +00:00
|
|
|
Name: opts.Name,
|
2017-02-26 21:53:19 +00:00
|
|
|
Config: &dc.Config{
|
2018-01-26 23:51:00 +00:00
|
|
|
Hostname: opts.Hostname,
|
2017-07-18 14:15:54 +00:00
|
|
|
Image: fmt.Sprintf("%s:%s", repository, tag),
|
|
|
|
Env: env,
|
|
|
|
Entrypoint: ep,
|
|
|
|
Cmd: cmd,
|
|
|
|
Mounts: mounts,
|
|
|
|
ExposedPorts: exp,
|
2018-07-06 16:09:34 +00:00
|
|
|
WorkingDir: wd,
|
|
|
|
Labels: opts.Labels,
|
2018-10-03 16:55:26 +00:00
|
|
|
StopSignal: "SIGWINCH", // to support timeouts
|
2017-02-26 21:53:19 +00:00
|
|
|
},
|
2019-04-12 15:51:37 +00:00
|
|
|
HostConfig: &hostConfig,
|
2018-10-03 16:55:26 +00:00
|
|
|
NetworkingConfig: &networkingConfig,
|
2017-02-26 21:53:19 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Client.StartContainer(c.ID, nil); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = d.Client.InspectContainer(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Resource{
|
2018-07-06 16:09:34 +00:00
|
|
|
pool: d,
|
2017-02-26 21:53:19 +00:00
|
|
|
Container: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:17:06 +00:00
|
|
|
// Run starts a docker container.
|
|
|
|
//
|
|
|
|
// pool.Run("mysql", "5.3", []string{"FOO=BAR", "BAR=BAZ"})
|
|
|
|
func (d *Pool) Run(repository, tag string, env []string) (*Resource, error) {
|
|
|
|
return d.RunWithOptions(&RunOptions{Repository: repository, Tag: tag, Env: env})
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// RemoveContainerByName find a container with the given name and removes it if present
|
|
|
|
func (d *Pool) RemoveContainerByName(containerName string) error {
|
|
|
|
containers, err := d.Client.ListContainers(dc.ListContainersOptions{
|
|
|
|
All: true,
|
|
|
|
Filters: map[string][]string{
|
|
|
|
"name": []string{containerName},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Error while listing containers with name %s", containerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(containers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Client.RemoveContainer(dc.RemoveContainerOptions{
|
|
|
|
ID: containers[0].ID,
|
|
|
|
Force: true,
|
|
|
|
RemoveVolumes: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Error while removing container with name %s", containerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-26 21:53:19 +00:00
|
|
|
// Purge removes a container and linked volumes from docker.
|
|
|
|
func (d *Pool) Purge(r *Resource) error {
|
|
|
|
if err := d.Client.RemoveContainer(dc.RemoveContainerOptions{ID: r.Container.ID, Force: true, RemoveVolumes: true}); err != nil {
|
|
|
|
return errors.Wrap(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry is an exponential backoff retry helper. You can use it to wait for e.g. mysql to boot up.
|
|
|
|
func (d *Pool) Retry(op func() error) error {
|
|
|
|
if d.MaxWait == 0 {
|
|
|
|
d.MaxWait = time.Minute
|
|
|
|
}
|
|
|
|
bo := backoff.NewExponentialBackOff()
|
|
|
|
bo.MaxInterval = time.Second * 5
|
|
|
|
bo.MaxElapsedTime = d.MaxWait
|
|
|
|
return backoff.Retry(op, bo)
|
|
|
|
}
|