github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/internal/container/ops.go (about)

     1  package container
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/Prakhar-Agarwal-byte/moby/api/types/container"
     7  	"github.com/Prakhar-Agarwal-byte/moby/api/types/mount"
     8  	"github.com/Prakhar-Agarwal-byte/moby/api/types/network"
     9  	"github.com/Prakhar-Agarwal-byte/moby/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 = container.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 mount.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  // WithBindRaw sets the bind mount of the container
    98  func WithBindRaw(s string) func(*TestContainerConfig) {
    99  	return func(c *TestContainerConfig) {
   100  		c.HostConfig.Binds = append(c.HostConfig.Binds, s)
   101  	}
   102  }
   103  
   104  // WithTmpfs sets a target path in the container to a tmpfs, with optional options
   105  // (separated with a colon).
   106  func WithTmpfs(targetAndOpts string) func(config *TestContainerConfig) {
   107  	return func(c *TestContainerConfig) {
   108  		if c.HostConfig.Tmpfs == nil {
   109  			c.HostConfig.Tmpfs = make(map[string]string)
   110  		}
   111  
   112  		target, opts, _ := strings.Cut(targetAndOpts, ":")
   113  		c.HostConfig.Tmpfs[target] = opts
   114  	}
   115  }
   116  
   117  // WithIPv4 sets the specified ip for the specified network of the container
   118  func WithIPv4(networkName, ip string) func(*TestContainerConfig) {
   119  	return func(c *TestContainerConfig) {
   120  		if c.NetworkingConfig.EndpointsConfig == nil {
   121  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   122  		}
   123  		if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
   124  			c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
   125  		}
   126  		if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
   127  			c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
   128  		}
   129  		c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv4Address = ip
   130  	}
   131  }
   132  
   133  // WithIPv6 sets the specified ip6 for the specified network of the container
   134  func WithIPv6(networkName, ip string) func(*TestContainerConfig) {
   135  	return func(c *TestContainerConfig) {
   136  		if c.NetworkingConfig.EndpointsConfig == nil {
   137  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   138  		}
   139  		if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
   140  			c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
   141  		}
   142  		if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
   143  			c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
   144  		}
   145  		c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv6Address = ip
   146  	}
   147  }
   148  
   149  func WithEndpointSettings(nw string, config *network.EndpointSettings) func(*TestContainerConfig) {
   150  	return func(c *TestContainerConfig) {
   151  		if c.NetworkingConfig.EndpointsConfig == nil {
   152  			c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
   153  		}
   154  		if _, ok := c.NetworkingConfig.EndpointsConfig[nw]; !ok {
   155  			c.NetworkingConfig.EndpointsConfig[nw] = config
   156  		}
   157  	}
   158  }
   159  
   160  // WithLogDriver sets the log driver to use for the container
   161  func WithLogDriver(driver string) func(*TestContainerConfig) {
   162  	return func(c *TestContainerConfig) {
   163  		c.HostConfig.LogConfig.Type = driver
   164  	}
   165  }
   166  
   167  // WithAutoRemove sets the container to be removed on exit
   168  func WithAutoRemove(c *TestContainerConfig) {
   169  	c.HostConfig.AutoRemove = true
   170  }
   171  
   172  // WithPidsLimit sets the container's "pids-limit
   173  func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
   174  	return func(c *TestContainerConfig) {
   175  		if c.HostConfig == nil {
   176  			c.HostConfig = &container.HostConfig{}
   177  		}
   178  		c.HostConfig.PidsLimit = limit
   179  	}
   180  }
   181  
   182  // WithRestartPolicy sets container's restart policy
   183  func WithRestartPolicy(policy container.RestartPolicyMode) func(c *TestContainerConfig) {
   184  	return func(c *TestContainerConfig) {
   185  		c.HostConfig.RestartPolicy.Name = policy
   186  	}
   187  }
   188  
   189  // WithUser sets the user
   190  func WithUser(user string) func(c *TestContainerConfig) {
   191  	return func(c *TestContainerConfig) {
   192  		c.Config.User = user
   193  	}
   194  }
   195  
   196  // WithPrivileged sets privileged mode for the container
   197  func WithPrivileged(privileged bool) func(*TestContainerConfig) {
   198  	return func(c *TestContainerConfig) {
   199  		if c.HostConfig == nil {
   200  			c.HostConfig = &container.HostConfig{}
   201  		}
   202  		c.HostConfig.Privileged = privileged
   203  	}
   204  }
   205  
   206  // WithCgroupnsMode sets the cgroup namespace mode for the container
   207  func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
   208  	return func(c *TestContainerConfig) {
   209  		if c.HostConfig == nil {
   210  			c.HostConfig = &container.HostConfig{}
   211  		}
   212  		c.HostConfig.CgroupnsMode = container.CgroupnsMode(mode)
   213  	}
   214  }
   215  
   216  // WithExtraHost sets the user defined IP:Host mappings in the container's
   217  // /etc/hosts file
   218  func WithExtraHost(extraHost string) func(*TestContainerConfig) {
   219  	return func(c *TestContainerConfig) {
   220  		c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
   221  	}
   222  }
   223  
   224  // WithPlatform specifies the desired platform the image should have.
   225  func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) {
   226  	return func(c *TestContainerConfig) {
   227  		c.Platform = p
   228  	}
   229  }
   230  
   231  // WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
   232  func WithWindowsDevice(device string) func(*TestContainerConfig) {
   233  	return func(c *TestContainerConfig) {
   234  		c.HostConfig.Devices = append(c.HostConfig.Devices, container.DeviceMapping{PathOnHost: device})
   235  	}
   236  }
   237  
   238  // WithIsolation specifies the isolation technology to apply to the container
   239  func WithIsolation(isolation container.Isolation) func(*TestContainerConfig) {
   240  	return func(c *TestContainerConfig) {
   241  		c.HostConfig.Isolation = isolation
   242  	}
   243  }
   244  
   245  // WithConsoleSize sets the initial console size of the container
   246  func WithConsoleSize(width, height uint) func(*TestContainerConfig) {
   247  	return func(c *TestContainerConfig) {
   248  		c.HostConfig.ConsoleSize = [2]uint{height, width}
   249  	}
   250  }
   251  
   252  // WithRuntime sets the runtime to use to start the container
   253  func WithRuntime(name string) func(*TestContainerConfig) {
   254  	return func(c *TestContainerConfig) {
   255  		c.HostConfig.Runtime = name
   256  	}
   257  }
   258  
   259  // WithCDIDevices sets the CDI devices to use to start the container
   260  func WithCDIDevices(cdiDeviceNames ...string) func(*TestContainerConfig) {
   261  	return func(c *TestContainerConfig) {
   262  		request := container.DeviceRequest{
   263  			Driver:    "cdi",
   264  			DeviceIDs: cdiDeviceNames,
   265  		}
   266  		c.HostConfig.DeviceRequests = append(c.HostConfig.DeviceRequests, request)
   267  	}
   268  }
   269  
   270  func WithCapability(capabilities ...string) func(*TestContainerConfig) {
   271  	return func(c *TestContainerConfig) {
   272  		c.HostConfig.CapAdd = append(c.HostConfig.CapAdd, capabilities...)
   273  	}
   274  }
   275  
   276  func WithDropCapability(capabilities ...string) func(*TestContainerConfig) {
   277  	return func(c *TestContainerConfig) {
   278  		c.HostConfig.CapDrop = append(c.HostConfig.CapDrop, capabilities...)
   279  	}
   280  }
   281  
   282  func WithSecurityOpt(opt string) func(*TestContainerConfig) {
   283  	return func(c *TestContainerConfig) {
   284  		c.HostConfig.SecurityOpt = append(c.HostConfig.SecurityOpt, opt)
   285  	}
   286  }
   287  
   288  // WithPIDMode sets the PID-mode for the container.
   289  func WithPIDMode(mode container.PidMode) func(c *TestContainerConfig) {
   290  	return func(c *TestContainerConfig) {
   291  		c.HostConfig.PidMode = mode
   292  	}
   293  }
   294  
   295  func WithStopSignal(stopSignal string) func(c *TestContainerConfig) {
   296  	return func(c *TestContainerConfig) {
   297  		c.Config.StopSignal = stopSignal
   298  	}
   299  }
   300  
   301  func WithMacAddress(address string) func(c *TestContainerConfig) {
   302  	return func(c *TestContainerConfig) {
   303  		c.Config.MacAddress = address
   304  	}
   305  }