ops.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package container
  2. import (
  3. "fmt"
  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. )
  10. // WithName sets the name of the container
  11. func WithName(name string) func(*TestContainerConfig) {
  12. return func(c *TestContainerConfig) {
  13. c.Name = name
  14. }
  15. }
  16. // WithLinks sets the links of the container
  17. func WithLinks(links ...string) func(*TestContainerConfig) {
  18. return func(c *TestContainerConfig) {
  19. c.HostConfig.Links = links
  20. }
  21. }
  22. // WithImage sets the image of the container
  23. func WithImage(image string) func(*TestContainerConfig) {
  24. return func(c *TestContainerConfig) {
  25. c.Config.Image = image
  26. }
  27. }
  28. // WithCmd sets the comannds of the container
  29. func WithCmd(cmds ...string) func(*TestContainerConfig) {
  30. return func(c *TestContainerConfig) {
  31. c.Config.Cmd = strslice.StrSlice(cmds)
  32. }
  33. }
  34. // WithNetworkMode sets the network mode of the container
  35. func WithNetworkMode(mode string) func(*TestContainerConfig) {
  36. return func(c *TestContainerConfig) {
  37. c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
  38. }
  39. }
  40. // WithExposedPorts sets the exposed ports of the container
  41. func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
  42. return func(c *TestContainerConfig) {
  43. c.Config.ExposedPorts = map[nat.Port]struct{}{}
  44. for _, port := range ports {
  45. c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
  46. }
  47. }
  48. }
  49. // WithTty sets the TTY mode of the container
  50. func WithTty(tty bool) func(*TestContainerConfig) {
  51. return func(c *TestContainerConfig) {
  52. c.Config.Tty = tty
  53. }
  54. }
  55. // WithWorkingDir sets the working dir of the container
  56. func WithWorkingDir(dir string) func(*TestContainerConfig) {
  57. return func(c *TestContainerConfig) {
  58. c.Config.WorkingDir = dir
  59. }
  60. }
  61. // WithMount adds an mount
  62. func WithMount(m mounttypes.Mount) func(*TestContainerConfig) {
  63. return func(c *TestContainerConfig) {
  64. c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
  65. }
  66. }
  67. // WithVolume sets the volume of the container
  68. func WithVolume(name string) func(*TestContainerConfig) {
  69. return func(c *TestContainerConfig) {
  70. if c.Config.Volumes == nil {
  71. c.Config.Volumes = map[string]struct{}{}
  72. }
  73. c.Config.Volumes[name] = struct{}{}
  74. }
  75. }
  76. // WithBind sets the bind mount of the container
  77. func WithBind(src, target string) func(*TestContainerConfig) {
  78. return func(c *TestContainerConfig) {
  79. c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
  80. }
  81. }
  82. // WithIPv4 sets the specified ip for the specified network of the container
  83. func WithIPv4(network, ip string) func(*TestContainerConfig) {
  84. return func(c *TestContainerConfig) {
  85. if c.NetworkingConfig.EndpointsConfig == nil {
  86. c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
  87. }
  88. if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
  89. c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
  90. }
  91. if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
  92. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
  93. }
  94. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
  95. }
  96. }
  97. // WithIPv6 sets the specified ip6 for the specified network of the container
  98. func WithIPv6(network, ip string) func(*TestContainerConfig) {
  99. return func(c *TestContainerConfig) {
  100. if c.NetworkingConfig.EndpointsConfig == nil {
  101. c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
  102. }
  103. if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
  104. c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
  105. }
  106. if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
  107. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
  108. }
  109. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
  110. }
  111. }
  112. // WithLogDriver sets the log driver to use for the container
  113. func WithLogDriver(driver string) func(*TestContainerConfig) {
  114. return func(c *TestContainerConfig) {
  115. c.HostConfig.LogConfig.Type = driver
  116. }
  117. }
  118. // WithAutoRemove sets the container to be removed on exit
  119. func WithAutoRemove(c *TestContainerConfig) {
  120. c.HostConfig.AutoRemove = true
  121. }
  122. // WithPidsLimit sets the container's "pids-limit
  123. func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
  124. return func(c *TestContainerConfig) {
  125. if c.HostConfig == nil {
  126. c.HostConfig = &containertypes.HostConfig{}
  127. }
  128. c.HostConfig.PidsLimit = limit
  129. }
  130. }
  131. // WithRestartPolicy sets container's restart policy
  132. func WithRestartPolicy(policy string) func(c *TestContainerConfig) {
  133. return func(c *TestContainerConfig) {
  134. c.HostConfig.RestartPolicy.Name = policy
  135. }
  136. }
  137. // WithUser sets the user
  138. func WithUser(user string) func(c *TestContainerConfig) {
  139. return func(c *TestContainerConfig) {
  140. c.Config.User = user
  141. }
  142. }
  143. // WithPrivileged sets privileged mode for the container
  144. func WithPrivileged(privileged bool) func(*TestContainerConfig) {
  145. return func(c *TestContainerConfig) {
  146. if c.HostConfig == nil {
  147. c.HostConfig = &containertypes.HostConfig{}
  148. }
  149. c.HostConfig.Privileged = privileged
  150. }
  151. }
  152. // WithCgroupnsMode sets the cgroup namespace mode for the container
  153. func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
  154. return func(c *TestContainerConfig) {
  155. if c.HostConfig == nil {
  156. c.HostConfig = &containertypes.HostConfig{}
  157. }
  158. c.HostConfig.CgroupnsMode = containertypes.CgroupnsMode(mode)
  159. }
  160. }