github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/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  	networktypes "github.com/docker/docker/api/types/network"
     8  	"github.com/docker/docker/api/types/strslice"
     9  	"github.com/docker/go-connections/nat"
    10  )
    11  
    12  // WithName sets the name of the container
    13  func WithName(name string) func(*TestContainerConfig) {
    14  	return func(c *TestContainerConfig) {
    15  		c.Name = name
    16  	}
    17  }
    18  
    19  // WithLinks sets the links of the container
    20  func WithLinks(links ...string) func(*TestContainerConfig) {
    21  	return func(c *TestContainerConfig) {
    22  		c.HostConfig.Links = links
    23  	}
    24  }
    25  
    26  // WithImage sets the image of the container
    27  func WithImage(image string) func(*TestContainerConfig) {
    28  	return func(c *TestContainerConfig) {
    29  		c.Config.Image = image
    30  	}
    31  }
    32  
    33  // WithCmd sets the comannds of the container
    34  func WithCmd(cmds ...string) func(*TestContainerConfig) {
    35  	return func(c *TestContainerConfig) {
    36  		c.Config.Cmd = strslice.StrSlice(cmds)
    37  	}
    38  }
    39  
    40  // WithNetworkMode sets the network mode of the container
    41  func WithNetworkMode(mode string) func(*TestContainerConfig) {
    42  	return func(c *TestContainerConfig) {
    43  		c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
    44  	}
    45  }
    46  
    47  // WithExposedPorts sets the exposed ports of the container
    48  func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
    49  	return func(c *TestContainerConfig) {
    50  		c.Config.ExposedPorts = map[nat.Port]struct{}{}
    51  		for _, port := range ports {
    52  			c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
    53  		}
    54  	}
    55  }
    56  
    57  // WithTty sets the TTY mode of the container
    58  func WithTty(tty bool) func(*TestContainerConfig) {
    59  	return func(c *TestContainerConfig) {
    60  		c.Config.Tty = tty
    61  	}
    62  }
    63  
    64  // WithWorkingDir sets the working dir of the container
    65  func WithWorkingDir(dir string) func(*TestContainerConfig) {
    66  	return func(c *TestContainerConfig) {
    67  		c.Config.WorkingDir = dir
    68  	}
    69  }
    70  
    71  // WithVolume sets the volume of the container
    72  func WithVolume(name string) func(*TestContainerConfig) {
    73  	return func(c *TestContainerConfig) {
    74  		if c.Config.Volumes == nil {
    75  			c.Config.Volumes = map[string]struct{}{}
    76  		}
    77  		c.Config.Volumes[name] = struct{}{}
    78  	}
    79  }
    80  
    81  // WithBind sets the bind mount of the container
    82  func WithBind(src, target string) func(*TestContainerConfig) {
    83  	return func(c *TestContainerConfig) {
    84  		c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
    85  	}
    86  }
    87  
    88  // WithIPv4 sets the specified ip for the specified network of the container
    89  func WithIPv4(network, ip string) func(*TestContainerConfig) {
    90  	return func(c *TestContainerConfig) {
    91  		if c.NetworkingConfig.EndpointsConfig == nil {
    92  			c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
    93  		}
    94  		if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
    95  			c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
    96  		}
    97  		if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
    98  			c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
    99  		}
   100  		c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
   101  	}
   102  }
   103  
   104  // WithIPv6 sets the specified ip6 for the specified network of the container
   105  func WithIPv6(network, ip string) func(*TestContainerConfig) {
   106  	return func(c *TestContainerConfig) {
   107  		if c.NetworkingConfig.EndpointsConfig == nil {
   108  			c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
   109  		}
   110  		if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
   111  			c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
   112  		}
   113  		if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
   114  			c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
   115  		}
   116  		c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
   117  	}
   118  }