github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration/internal/container/ops.go (about) 1 package container 2 3 import ( 4 "fmt" 5 6 containertypes "github.com/docker/docker/api/types/container" 7 mounttypes "github.com/docker/docker/api/types/mount" 8 networktypes "github.com/docker/docker/api/types/network" 9 "github.com/docker/docker/api/types/strslice" 10 "github.com/docker/go-connections/nat" 11 ) 12 13 // WithName sets the name of the container 14 func WithName(name string) func(*TestContainerConfig) { 15 return func(c *TestContainerConfig) { 16 c.Name = name 17 } 18 } 19 20 // WithLinks sets the links of the container 21 func WithLinks(links ...string) func(*TestContainerConfig) { 22 return func(c *TestContainerConfig) { 23 c.HostConfig.Links = links 24 } 25 } 26 27 // WithImage sets the image of the container 28 func WithImage(image string) func(*TestContainerConfig) { 29 return func(c *TestContainerConfig) { 30 c.Config.Image = image 31 } 32 } 33 34 // WithCmd sets the comannds of the container 35 func WithCmd(cmds ...string) func(*TestContainerConfig) { 36 return func(c *TestContainerConfig) { 37 c.Config.Cmd = strslice.StrSlice(cmds) 38 } 39 } 40 41 // WithNetworkMode sets the network mode of the container 42 func WithNetworkMode(mode string) func(*TestContainerConfig) { 43 return func(c *TestContainerConfig) { 44 c.HostConfig.NetworkMode = containertypes.NetworkMode(mode) 45 } 46 } 47 48 // WithExposedPorts sets the exposed ports of the container 49 func WithExposedPorts(ports ...string) func(*TestContainerConfig) { 50 return func(c *TestContainerConfig) { 51 c.Config.ExposedPorts = map[nat.Port]struct{}{} 52 for _, port := range ports { 53 c.Config.ExposedPorts[nat.Port(port)] = struct{}{} 54 } 55 } 56 } 57 58 // WithTty sets the TTY mode of the container 59 func WithTty(tty bool) func(*TestContainerConfig) { 60 return func(c *TestContainerConfig) { 61 c.Config.Tty = tty 62 } 63 } 64 65 // WithWorkingDir sets the working dir of the container 66 func WithWorkingDir(dir string) func(*TestContainerConfig) { 67 return func(c *TestContainerConfig) { 68 c.Config.WorkingDir = dir 69 } 70 } 71 72 // WithMount adds an mount 73 func WithMount(m mounttypes.Mount) func(*TestContainerConfig) { 74 return func(c *TestContainerConfig) { 75 c.HostConfig.Mounts = append(c.HostConfig.Mounts, m) 76 } 77 } 78 79 // WithVolume sets the volume of the container 80 func WithVolume(name string) func(*TestContainerConfig) { 81 return func(c *TestContainerConfig) { 82 if c.Config.Volumes == nil { 83 c.Config.Volumes = map[string]struct{}{} 84 } 85 c.Config.Volumes[name] = struct{}{} 86 } 87 } 88 89 // WithBind sets the bind mount of the container 90 func WithBind(src, target string) func(*TestContainerConfig) { 91 return func(c *TestContainerConfig) { 92 c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target)) 93 } 94 } 95 96 // WithIPv4 sets the specified ip for the specified network of the container 97 func WithIPv4(network, ip string) func(*TestContainerConfig) { 98 return func(c *TestContainerConfig) { 99 if c.NetworkingConfig.EndpointsConfig == nil { 100 c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{} 101 } 102 if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil { 103 c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{} 104 } 105 if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil { 106 c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{} 107 } 108 c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip 109 } 110 } 111 112 // WithIPv6 sets the specified ip6 for the specified network of the container 113 func WithIPv6(network, ip string) func(*TestContainerConfig) { 114 return func(c *TestContainerConfig) { 115 if c.NetworkingConfig.EndpointsConfig == nil { 116 c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{} 117 } 118 if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil { 119 c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{} 120 } 121 if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil { 122 c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{} 123 } 124 c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip 125 } 126 } 127 128 // WithLogDriver sets the log driver to use for the container 129 func WithLogDriver(driver string) func(*TestContainerConfig) { 130 return func(c *TestContainerConfig) { 131 c.HostConfig.LogConfig.Type = driver 132 } 133 } 134 135 // WithAutoRemove sets the container to be removed on exit 136 func WithAutoRemove(c *TestContainerConfig) { 137 c.HostConfig.AutoRemove = true 138 } 139 140 // WithPidsLimit sets the container's "pids-limit 141 func WithPidsLimit(limit *int64) func(*TestContainerConfig) { 142 return func(c *TestContainerConfig) { 143 if c.HostConfig == nil { 144 c.HostConfig = &containertypes.HostConfig{} 145 } 146 c.HostConfig.PidsLimit = limit 147 } 148 } 149 150 // WithRestartPolicy sets container's restart policy 151 func WithRestartPolicy(policy string) func(c *TestContainerConfig) { 152 return func(c *TestContainerConfig) { 153 c.HostConfig.RestartPolicy.Name = policy 154 } 155 } 156 157 // WithUser sets the user 158 func WithUser(user string) func(c *TestContainerConfig) { 159 return func(c *TestContainerConfig) { 160 c.Config.User = user 161 } 162 }