ops.go 7.9 KB

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