github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/internal/container/ops.go (about)

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