2018-02-09 13:37:55 -05:00
|
|
|
package container
|
|
|
|
|
2018-02-13 10:45:40 -05:00
|
|
|
import (
|
2018-02-12 16:54:12 -05:00
|
|
|
"fmt"
|
|
|
|
|
2018-02-13 10:45:40 -05:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
)
|
2018-02-09 13:37:55 -05:00
|
|
|
|
|
|
|
// WithName sets the name of the container
|
|
|
|
func WithName(name string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.Name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLinks sets the links of the container
|
|
|
|
func WithLinks(links ...string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.HostConfig.Links = links
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 14:02:50 -05:00
|
|
|
// WithImage sets the image of the container
|
|
|
|
func WithImage(image string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.Config.Image = image
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:55 -05:00
|
|
|
// WithCmd sets the comannds of the container
|
|
|
|
func WithCmd(cmds ...string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.Config.Cmd = strslice.StrSlice(cmds)
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 10:45:40 -05:00
|
|
|
|
|
|
|
// WithNetworkMode sets the network mode of the container
|
|
|
|
func WithNetworkMode(mode string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithExposedPorts sets the exposed ports of the container
|
|
|
|
func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.Config.ExposedPorts = map[nat.Port]struct{}{}
|
|
|
|
for _, port := range ports {
|
|
|
|
c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTty sets the TTY mode of the container
|
|
|
|
func WithTty(tty bool) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.Config.Tty = tty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithWorkingDir sets the working dir of the container
|
|
|
|
func WithWorkingDir(dir string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.Config.WorkingDir = dir
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 16:54:12 -05:00
|
|
|
|
|
|
|
// WithVolume sets the volume of the container
|
|
|
|
func WithVolume(name string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
if c.Config.Volumes == nil {
|
|
|
|
c.Config.Volumes = map[string]struct{}{}
|
|
|
|
}
|
|
|
|
c.Config.Volumes[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBind sets the bind mount of the container
|
|
|
|
func WithBind(src, target string) func(*TestContainerConfig) {
|
|
|
|
return func(c *TestContainerConfig) {
|
|
|
|
c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
|
|
|
|
}
|
|
|
|
}
|