gopkg.in/docker/docker.v20@v20.10.27/integration/internal/container/ops.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	containertypes "github.com/docker/docker/api/types/container"
     8  	mounttypes "github.com/docker/docker/api/types/mount"
     9  	networktypes "github.com/docker/docker/api/types/network"
    10  	"github.com/docker/docker/api/types/strslice"
    11  	"github.com/docker/go-connections/nat"
    12  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    13  )
    14  
    15  // WithName sets the name of the container
    16  func WithName(name string) func(*TestContainerConfig) {
    17  	return func(c *TestContainerConfig) {
    18  		c.Name = name
    19  	}
    20  }
    21  
    22  // WithLinks sets the links of the container
    23  func WithLinks(links ...string) func(*TestContainerConfig) {
    24  	return func(c *TestContainerConfig) {
    25  		c.HostConfig.Links = links
    26  	}
    27  }
    28  
    29  // WithImage sets the image of the container
    30  func WithImage(image string) func(*TestContainerConfig) {
    31  	return func(c *TestContainerConfig) {
    32  		c.Config.Image = image
    33  	}
    34  }
    35  
    36  // WithCmd sets the comannds of the container
    37  func WithCmd(cmds ...string) func(*TestContainerConfig) {
    38  	return func(c *TestContainerConfig) {
    39  		c.Config.Cmd = strslice.StrSlice(cmds)
    40  	}
    41  }
    42  
    43  // WithNetworkMode sets the network mode of the container
    44  func WithNetworkMode(mode string) func(*TestContainerConfig) {
    45  	return func(c *TestContainerConfig) {
    46  		c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
    47  	}
    48  }
    49  
    50  // WithExposedPorts sets the exposed ports of the container
    51  func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
    52  	return func(c *TestContainerConfig) {
    53  		c.Config.ExposedPorts = map[nat.Port]struct{}{}
    54  		for _, port := range ports {
    55  			c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
    56  		}
    57  	}
    58  }
    59  
    60  // WithTty sets the TTY mode of the container
    61  func WithTty(tty bool) func(*TestContainerConfig) {
    62  	return func(c *TestContainerConfig) {
    63  		c.Config.Tty = tty
    64  	}
    65  }
    66  
    67  // WithWorkingDir sets the working dir of the container
    68  func WithWorkingDir(dir string) func(*TestContainerConfig) {
    69  	return func(c *TestContainerConfig) {
    70  		c.Config.WorkingDir = dir
    71  	}
    72  }
    73  
    74  // WithMount adds an mount
    75  func WithMount(m mounttypes.Mount) func(*TestContainerConfig) {
    76  	return func(c *TestContainerConfig) {
    77  		c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
    78  	}
    79  }
    80  
    81  // WithVolume sets the volume of the container
    82  func WithVolume(target string) func(*TestContainerConfig) {
    83  	return func(c *TestContainerConfig) {
    84  		if c.Config.Volumes == nil {
    85  			c.Config.Volumes = map[string]struct{}{}
    86  		}
    87  		c.Config.Volumes[target] = struct{}{}
    88  	}
    89  }
    90  
    91  // WithBind sets the bind mount of the container
    92  func WithBind(src, target string) func(*TestContainerConfig) {
    93  	return func(c *TestContainerConfig) {
    94  		c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
    95  	}
    96  }
    97  
    98  // WithTmpfs sets a target path in the container to a tmpfs
    99  func WithTmpfs(target string) func(config *TestContainerConfig) {
   100  	return func(c *TestContainerConfig) {
   101  		if c.HostConfig.Tmpfs == nil {
   102  			c.HostConfig.Tmpfs = make(map[string]string)
   103  		}
   104  
   105  		spec := strings.SplitN(target, ":", 2)
   106  		var opts string
   107  		if len(spec) > 1 {
   108  			opts = spec[1]
   109  		}
   110  		c.HostConfig.Tmpfs[spec[0]] = opts
   111  	}
   112  }
   113  
   114  // WithIPv4 sets the specified ip for the specified network of the container
   115  func WithIPv4(network, ip string) func(*TestContainerConfig) {
   116  	return func(c *TestContainerConfig) {
   117  		if c.NetworkingConfig.EndpointsConfig == nil {
   118  			c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
   119  		}
   120  		if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
   121  			c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
   122  		}
   123  		if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
   124  			c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
   125  		}
   126  		c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
   127  	}
   128  }
   129  
   130  // WithIPv6 sets the specified ip6 for the specified network of the container
   131  func WithIPv6(network, ip string) func(*TestContainerConfig) {
   132  	return func(c *TestContainerConfig) {
   133  		if c.NetworkingConfig.EndpointsConfig == nil {
   134  			c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
   135  		}
   136  		if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
   137  			c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
   138  		}
   139  		if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
   140  			c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
   141  		}
   142  		c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
   143  	}
   144  }
   145  
   146  // WithLogDriver sets the log driver to use for the container
   147  func WithLogDriver(driver string) func(*TestContainerConfig) {
   148  	return func(c *TestContainerConfig) {
   149  		c.HostConfig.LogConfig.Type = driver
   150  	}
   151  }
   152  
   153  // WithAutoRemove sets the container to be removed on exit
   154  func WithAutoRemove(c *TestContainerConfig) {
   155  	c.HostConfig.AutoRemove = true
   156  }
   157  
   158  // WithPidsLimit sets the container's "pids-limit
   159  func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
   160  	return func(c *TestContainerConfig) {
   161  		if c.HostConfig == nil {
   162  			c.HostConfig = &containertypes.HostConfig{}
   163  		}
   164  		c.HostConfig.PidsLimit = limit
   165  	}
   166  }
   167  
   168  // WithRestartPolicy sets container's restart policy
   169  func WithRestartPolicy(policy string) func(c *TestContainerConfig) {
   170  	return func(c *TestContainerConfig) {
   171  		c.HostConfig.RestartPolicy.Name = policy
   172  	}
   173  }
   174  
   175  // WithUser sets the user
   176  func WithUser(user string) func(c *TestContainerConfig) {
   177  	return func(c *TestContainerConfig) {
   178  		c.Config.User = user
   179  	}
   180  }
   181  
   182  // WithPrivileged sets privileged mode for the container
   183  func WithPrivileged(privileged bool) func(*TestContainerConfig) {
   184  	return func(c *TestContainerConfig) {
   185  		if c.HostConfig == nil {
   186  			c.HostConfig = &containertypes.HostConfig{}
   187  		}
   188  		c.HostConfig.Privileged = privileged
   189  	}
   190  }
   191  
   192  // WithCgroupnsMode sets the cgroup namespace mode for the container
   193  func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
   194  	return func(c *TestContainerConfig) {
   195  		if c.HostConfig == nil {
   196  			c.HostConfig = &containertypes.HostConfig{}
   197  		}
   198  		c.HostConfig.CgroupnsMode = containertypes.CgroupnsMode(mode)
   199  	}
   200  }
   201  
   202  // WithExtraHost sets the user defined IP:Host mappings in the container's
   203  // /etc/hosts file
   204  func WithExtraHost(extraHost string) func(*TestContainerConfig) {
   205  	return func(c *TestContainerConfig) {
   206  		c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
   207  	}
   208  }
   209  
   210  // WithPlatform specifies the desired platform the image should have.
   211  func WithPlatform(p *specs.Platform) func(*TestContainerConfig) {
   212  	return func(c *TestContainerConfig) {
   213  		c.Platform = p
   214  	}
   215  }