ops.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package container
  2. import (
  3. "strings"
  4. "github.com/docker/docker/api/types/container"
  5. "github.com/docker/docker/api/types/mount"
  6. "github.com/docker/docker/api/types/network"
  7. "github.com/docker/docker/api/types/strslice"
  8. "github.com/docker/go-connections/nat"
  9. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  10. )
  11. // WithName sets the name of the container
  12. func WithName(name string) func(*TestContainerConfig) {
  13. return func(c *TestContainerConfig) {
  14. c.Name = name
  15. }
  16. }
  17. // WithLinks sets the links of the container
  18. func WithLinks(links ...string) func(*TestContainerConfig) {
  19. return func(c *TestContainerConfig) {
  20. c.HostConfig.Links = links
  21. }
  22. }
  23. // WithImage sets the image of the container
  24. func WithImage(image string) func(*TestContainerConfig) {
  25. return func(c *TestContainerConfig) {
  26. c.Config.Image = image
  27. }
  28. }
  29. // WithCmd sets the comannds of the container
  30. func WithCmd(cmds ...string) func(*TestContainerConfig) {
  31. return func(c *TestContainerConfig) {
  32. c.Config.Cmd = strslice.StrSlice(cmds)
  33. }
  34. }
  35. // WithNetworkMode sets the network mode of the container
  36. func WithNetworkMode(mode string) func(*TestContainerConfig) {
  37. return func(c *TestContainerConfig) {
  38. c.HostConfig.NetworkMode = container.NetworkMode(mode)
  39. }
  40. }
  41. // WithExposedPorts sets the exposed ports of the container
  42. func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
  43. return func(c *TestContainerConfig) {
  44. c.Config.ExposedPorts = map[nat.Port]struct{}{}
  45. for _, port := range ports {
  46. c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
  47. }
  48. }
  49. }
  50. // WithTty sets the TTY mode of the container
  51. func WithTty(tty bool) func(*TestContainerConfig) {
  52. return func(c *TestContainerConfig) {
  53. c.Config.Tty = tty
  54. }
  55. }
  56. // WithWorkingDir sets the working dir of the container
  57. func WithWorkingDir(dir string) func(*TestContainerConfig) {
  58. return func(c *TestContainerConfig) {
  59. c.Config.WorkingDir = dir
  60. }
  61. }
  62. // WithMount adds an mount
  63. func WithMount(m mount.Mount) func(*TestContainerConfig) {
  64. return func(c *TestContainerConfig) {
  65. c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
  66. }
  67. }
  68. // WithVolume sets the volume of the container
  69. func WithVolume(target string) func(*TestContainerConfig) {
  70. return func(c *TestContainerConfig) {
  71. if c.Config.Volumes == nil {
  72. c.Config.Volumes = map[string]struct{}{}
  73. }
  74. c.Config.Volumes[target] = struct{}{}
  75. }
  76. }
  77. // WithBind sets the bind mount of the container
  78. func WithBind(src, target string) func(*TestContainerConfig) {
  79. return func(c *TestContainerConfig) {
  80. c.HostConfig.Binds = append(c.HostConfig.Binds, src+":"+target)
  81. }
  82. }
  83. // WithBindRaw sets the bind mount of the container
  84. func WithBindRaw(s string) func(*TestContainerConfig) {
  85. return func(c *TestContainerConfig) {
  86. c.HostConfig.Binds = append(c.HostConfig.Binds, s)
  87. }
  88. }
  89. // WithTmpfs sets a target path in the container to a tmpfs, with optional options
  90. // (separated with a colon).
  91. func WithTmpfs(targetAndOpts string) func(config *TestContainerConfig) {
  92. return func(c *TestContainerConfig) {
  93. if c.HostConfig.Tmpfs == nil {
  94. c.HostConfig.Tmpfs = make(map[string]string)
  95. }
  96. target, opts, _ := strings.Cut(targetAndOpts, ":")
  97. c.HostConfig.Tmpfs[target] = opts
  98. }
  99. }
  100. func WithMacAddress(networkName, mac string) func(config *TestContainerConfig) {
  101. return func(c *TestContainerConfig) {
  102. if c.NetworkingConfig.EndpointsConfig == nil {
  103. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  104. }
  105. if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
  106. c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
  107. }
  108. c.NetworkingConfig.EndpointsConfig[networkName].MacAddress = mac
  109. }
  110. }
  111. // WithIPv4 sets the specified ip for the specified network of the container
  112. func WithIPv4(networkName, ip string) func(*TestContainerConfig) {
  113. return func(c *TestContainerConfig) {
  114. if c.NetworkingConfig.EndpointsConfig == nil {
  115. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  116. }
  117. if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
  118. c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
  119. }
  120. if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
  121. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
  122. }
  123. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv4Address = ip
  124. }
  125. }
  126. // WithIPv6 sets the specified ip6 for the specified network of the container
  127. func WithIPv6(networkName, ip string) func(*TestContainerConfig) {
  128. return func(c *TestContainerConfig) {
  129. if c.NetworkingConfig.EndpointsConfig == nil {
  130. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  131. }
  132. if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
  133. c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
  134. }
  135. if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
  136. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
  137. }
  138. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv6Address = ip
  139. }
  140. }
  141. func WithEndpointSettings(nw string, config *network.EndpointSettings) func(*TestContainerConfig) {
  142. return func(c *TestContainerConfig) {
  143. if c.NetworkingConfig.EndpointsConfig == nil {
  144. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  145. }
  146. if _, ok := c.NetworkingConfig.EndpointsConfig[nw]; !ok {
  147. c.NetworkingConfig.EndpointsConfig[nw] = config
  148. }
  149. }
  150. }
  151. // WithLogDriver sets the log driver to use for the container
  152. func WithLogDriver(driver string) func(*TestContainerConfig) {
  153. return func(c *TestContainerConfig) {
  154. c.HostConfig.LogConfig.Type = driver
  155. }
  156. }
  157. // WithAutoRemove sets the container to be removed on exit
  158. func WithAutoRemove(c *TestContainerConfig) {
  159. c.HostConfig.AutoRemove = true
  160. }
  161. // WithPidsLimit sets the container's "pids-limit
  162. func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
  163. return func(c *TestContainerConfig) {
  164. if c.HostConfig == nil {
  165. c.HostConfig = &container.HostConfig{}
  166. }
  167. c.HostConfig.PidsLimit = limit
  168. }
  169. }
  170. // WithRestartPolicy sets container's restart policy
  171. func WithRestartPolicy(policy container.RestartPolicyMode) func(c *TestContainerConfig) {
  172. return func(c *TestContainerConfig) {
  173. c.HostConfig.RestartPolicy.Name = policy
  174. }
  175. }
  176. // WithUser sets the user
  177. func WithUser(user string) func(c *TestContainerConfig) {
  178. return func(c *TestContainerConfig) {
  179. c.Config.User = user
  180. }
  181. }
  182. // WithAdditionalGroups sets the additional groups for the container
  183. func WithAdditionalGroups(groups ...string) func(c *TestContainerConfig) {
  184. return func(c *TestContainerConfig) {
  185. c.HostConfig.GroupAdd = groups
  186. }
  187. }
  188. // WithPrivileged sets privileged mode for the container
  189. func WithPrivileged(privileged bool) func(*TestContainerConfig) {
  190. return func(c *TestContainerConfig) {
  191. if c.HostConfig == nil {
  192. c.HostConfig = &container.HostConfig{}
  193. }
  194. c.HostConfig.Privileged = privileged
  195. }
  196. }
  197. // WithCgroupnsMode sets the cgroup namespace mode for the container
  198. func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
  199. return func(c *TestContainerConfig) {
  200. if c.HostConfig == nil {
  201. c.HostConfig = &container.HostConfig{}
  202. }
  203. c.HostConfig.CgroupnsMode = container.CgroupnsMode(mode)
  204. }
  205. }
  206. // WithExtraHost sets the user defined IP:Host mappings in the container's
  207. // /etc/hosts file
  208. func WithExtraHost(extraHost string) func(*TestContainerConfig) {
  209. return func(c *TestContainerConfig) {
  210. c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
  211. }
  212. }
  213. // WithPlatform specifies the desired platform the image should have.
  214. func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) {
  215. return func(c *TestContainerConfig) {
  216. c.Platform = p
  217. }
  218. }
  219. // WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
  220. func WithWindowsDevice(device string) func(*TestContainerConfig) {
  221. return func(c *TestContainerConfig) {
  222. c.HostConfig.Devices = append(c.HostConfig.Devices, container.DeviceMapping{PathOnHost: device})
  223. }
  224. }
  225. // WithIsolation specifies the isolation technology to apply to the container
  226. func WithIsolation(isolation container.Isolation) func(*TestContainerConfig) {
  227. return func(c *TestContainerConfig) {
  228. c.HostConfig.Isolation = isolation
  229. }
  230. }
  231. // WithConsoleSize sets the initial console size of the container
  232. func WithConsoleSize(width, height uint) func(*TestContainerConfig) {
  233. return func(c *TestContainerConfig) {
  234. c.HostConfig.ConsoleSize = [2]uint{height, width}
  235. }
  236. }
  237. // WithRuntime sets the runtime to use to start the container
  238. func WithRuntime(name string) func(*TestContainerConfig) {
  239. return func(c *TestContainerConfig) {
  240. c.HostConfig.Runtime = name
  241. }
  242. }
  243. // WithCDIDevices sets the CDI devices to use to start the container
  244. func WithCDIDevices(cdiDeviceNames ...string) func(*TestContainerConfig) {
  245. return func(c *TestContainerConfig) {
  246. request := container.DeviceRequest{
  247. Driver: "cdi",
  248. DeviceIDs: cdiDeviceNames,
  249. }
  250. c.HostConfig.DeviceRequests = append(c.HostConfig.DeviceRequests, request)
  251. }
  252. }
  253. func WithCapability(capabilities ...string) func(*TestContainerConfig) {
  254. return func(c *TestContainerConfig) {
  255. c.HostConfig.CapAdd = append(c.HostConfig.CapAdd, capabilities...)
  256. }
  257. }
  258. func WithDropCapability(capabilities ...string) func(*TestContainerConfig) {
  259. return func(c *TestContainerConfig) {
  260. c.HostConfig.CapDrop = append(c.HostConfig.CapDrop, capabilities...)
  261. }
  262. }
  263. func WithSecurityOpt(opt string) func(*TestContainerConfig) {
  264. return func(c *TestContainerConfig) {
  265. c.HostConfig.SecurityOpt = append(c.HostConfig.SecurityOpt, opt)
  266. }
  267. }
  268. // WithPIDMode sets the PID-mode for the container.
  269. func WithPIDMode(mode container.PidMode) func(c *TestContainerConfig) {
  270. return func(c *TestContainerConfig) {
  271. c.HostConfig.PidMode = mode
  272. }
  273. }
  274. func WithStopSignal(stopSignal string) func(c *TestContainerConfig) {
  275. return func(c *TestContainerConfig) {
  276. c.Config.StopSignal = stopSignal
  277. }
  278. }
  279. func WithContainerWideMacAddress(address string) func(c *TestContainerConfig) {
  280. return func(c *TestContainerConfig) {
  281. c.Config.MacAddress = address //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
  282. }
  283. }