github.com/rawahars/moby@v24.0.4+incompatible/integration/internal/container/ops.go (about)

     1  package container
     2  
     3  import (
     4  	"strings"
     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  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    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, src+":"+target)
    94  	}
    95  }
    96  
    97  // WithTmpfs sets a target path in the container to a tmpfs, with optional options
    98  // (separated with a colon).
    99  func WithTmpfs(targetAndOpts 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  		target, opts, _ := strings.Cut(targetAndOpts, ":")
   106  		c.HostConfig.Tmpfs[target] = opts
   107  	}
   108  }
   109  
   110  // WithIPv4 sets the specified ip for the specified network of the container
   111  func WithIPv4(network, ip string) func(*TestContainerConfig) {
   112  	return func(c *TestContainerConfig) {
   113  		if c.NetworkingConfig.EndpointsConfig == nil {
   114  			c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
   115  		}
   116  		if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
   117  			c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
   118  		}
   119  		if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
   120  			c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
   121  		}
   122  		c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
   123  	}
   124  }
   125  
   126  // WithIPv6 sets the specified ip6 for the specified network of the container
   127  func WithIPv6(network, ip string) func(*TestContainerConfig) {
   128  	return func(c *TestContainerConfig) {
   129  		if c.NetworkingConfig.EndpointsConfig == nil {
   130  			c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
   131  		}
   132  		if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
   133  			c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
   134  		}
   135  		if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
   136  			c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
   137  		}
   138  		c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
   139  	}
   140  }
   141  
   142  // WithLogDriver sets the log driver to use for the container
   143  func WithLogDriver(driver string) func(*TestContainerConfig) {
   144  	return func(c *TestContainerConfig) {
   145  		c.HostConfig.LogConfig.Type = driver
   146  	}
   147  }
   148  
   149  // WithAutoRemove sets the container to be removed on exit
   150  func WithAutoRemove(c *TestContainerConfig) {
   151  	c.HostConfig.AutoRemove = true
   152  }
   153  
   154  // WithPidsLimit sets the container's "pids-limit
   155  func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
   156  	return func(c *TestContainerConfig) {
   157  		if c.HostConfig == nil {
   158  			c.HostConfig = &containertypes.HostConfig{}
   159  		}
   160  		c.HostConfig.PidsLimit = limit
   161  	}
   162  }
   163  
   164  // WithRestartPolicy sets container's restart policy
   165  func WithRestartPolicy(policy string) func(c *TestContainerConfig) {
   166  	return func(c *TestContainerConfig) {
   167  		c.HostConfig.RestartPolicy.Name = policy
   168  	}
   169  }
   170  
   171  // WithUser sets the user
   172  func WithUser(user string) func(c *TestContainerConfig) {
   173  	return func(c *TestContainerConfig) {
   174  		c.Config.User = user
   175  	}
   176  }
   177  
   178  // WithPrivileged sets privileged mode for the container
   179  func WithPrivileged(privileged bool) func(*TestContainerConfig) {
   180  	return func(c *TestContainerConfig) {
   181  		if c.HostConfig == nil {
   182  			c.HostConfig = &containertypes.HostConfig{}
   183  		}
   184  		c.HostConfig.Privileged = privileged
   185  	}
   186  }
   187  
   188  // WithCgroupnsMode sets the cgroup namespace mode for the container
   189  func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
   190  	return func(c *TestContainerConfig) {
   191  		if c.HostConfig == nil {
   192  			c.HostConfig = &containertypes.HostConfig{}
   193  		}
   194  		c.HostConfig.CgroupnsMode = containertypes.CgroupnsMode(mode)
   195  	}
   196  }
   197  
   198  // WithExtraHost sets the user defined IP:Host mappings in the container's
   199  // /etc/hosts file
   200  func WithExtraHost(extraHost string) func(*TestContainerConfig) {
   201  	return func(c *TestContainerConfig) {
   202  		c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
   203  	}
   204  }
   205  
   206  // WithPlatform specifies the desired platform the image should have.
   207  func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) {
   208  	return func(c *TestContainerConfig) {
   209  		c.Platform = p
   210  	}
   211  }
   212  
   213  // WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
   214  func WithWindowsDevice(device string) func(*TestContainerConfig) {
   215  	return func(c *TestContainerConfig) {
   216  		c.HostConfig.Devices = append(c.HostConfig.Devices, containertypes.DeviceMapping{PathOnHost: device})
   217  	}
   218  }
   219  
   220  // WithIsolation specifies the isolation technology to apply to the container
   221  func WithIsolation(isolation containertypes.Isolation) func(*TestContainerConfig) {
   222  	return func(c *TestContainerConfig) {
   223  		c.HostConfig.Isolation = isolation
   224  	}
   225  }
   226  
   227  // WithConsoleSize sets the initial console size of the container
   228  func WithConsoleSize(width, height uint) func(*TestContainerConfig) {
   229  	return func(c *TestContainerConfig) {
   230  		c.HostConfig.ConsoleSize = [2]uint{height, width}
   231  	}
   232  }
   233  
   234  // WithRuntime sets the runtime to use to start the container
   235  func WithRuntime(name string) func(*TestContainerConfig) {
   236  	return func(c *TestContainerConfig) {
   237  		c.HostConfig.Runtime = name
   238  	}
   239  }