ops.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package container
  2. import (
  3. "fmt"
  4. containertypes "github.com/docker/docker/api/types/container"
  5. networktypes "github.com/docker/docker/api/types/network"
  6. "github.com/docker/docker/api/types/strslice"
  7. "github.com/docker/go-connections/nat"
  8. )
  9. // WithName sets the name of the container
  10. func WithName(name string) func(*TestContainerConfig) {
  11. return func(c *TestContainerConfig) {
  12. c.Name = name
  13. }
  14. }
  15. // WithLinks sets the links of the container
  16. func WithLinks(links ...string) func(*TestContainerConfig) {
  17. return func(c *TestContainerConfig) {
  18. c.HostConfig.Links = links
  19. }
  20. }
  21. // WithImage sets the image of the container
  22. func WithImage(image string) func(*TestContainerConfig) {
  23. return func(c *TestContainerConfig) {
  24. c.Config.Image = image
  25. }
  26. }
  27. // WithCmd sets the comannds of the container
  28. func WithCmd(cmds ...string) func(*TestContainerConfig) {
  29. return func(c *TestContainerConfig) {
  30. c.Config.Cmd = strslice.StrSlice(cmds)
  31. }
  32. }
  33. // WithNetworkMode sets the network mode of the container
  34. func WithNetworkMode(mode string) func(*TestContainerConfig) {
  35. return func(c *TestContainerConfig) {
  36. c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
  37. }
  38. }
  39. // WithExposedPorts sets the exposed ports of the container
  40. func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
  41. return func(c *TestContainerConfig) {
  42. c.Config.ExposedPorts = map[nat.Port]struct{}{}
  43. for _, port := range ports {
  44. c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
  45. }
  46. }
  47. }
  48. // WithTty sets the TTY mode of the container
  49. func WithTty(tty bool) func(*TestContainerConfig) {
  50. return func(c *TestContainerConfig) {
  51. c.Config.Tty = tty
  52. }
  53. }
  54. // WithWorkingDir sets the working dir of the container
  55. func WithWorkingDir(dir string) func(*TestContainerConfig) {
  56. return func(c *TestContainerConfig) {
  57. c.Config.WorkingDir = dir
  58. }
  59. }
  60. // WithVolume sets the volume of the container
  61. func WithVolume(name string) func(*TestContainerConfig) {
  62. return func(c *TestContainerConfig) {
  63. if c.Config.Volumes == nil {
  64. c.Config.Volumes = map[string]struct{}{}
  65. }
  66. c.Config.Volumes[name] = struct{}{}
  67. }
  68. }
  69. // WithBind sets the bind mount of the container
  70. func WithBind(src, target string) func(*TestContainerConfig) {
  71. return func(c *TestContainerConfig) {
  72. c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
  73. }
  74. }
  75. // WithIPv4 sets the specified ip for the specified network of the container
  76. func WithIPv4(network, ip string) func(*TestContainerConfig) {
  77. return func(c *TestContainerConfig) {
  78. if c.NetworkingConfig.EndpointsConfig == nil {
  79. c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
  80. }
  81. if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
  82. c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
  83. }
  84. if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
  85. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
  86. }
  87. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
  88. }
  89. }
  90. // WithIPv6 sets the specified ip6 for the specified network of the container
  91. func WithIPv6(network, ip string) func(*TestContainerConfig) {
  92. return func(c *TestContainerConfig) {
  93. if c.NetworkingConfig.EndpointsConfig == nil {
  94. c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
  95. }
  96. if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
  97. c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
  98. }
  99. if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
  100. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
  101. }
  102. c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
  103. }
  104. }
  105. // WithLogDriver sets the log driver to use for the container
  106. func WithLogDriver(driver string) func(*TestContainerConfig) {
  107. return func(c *TestContainerConfig) {
  108. c.HostConfig.LogConfig.Type = driver
  109. }
  110. }
  111. // WithAutoRemove sets the container to be removed on exit
  112. func WithAutoRemove(c *TestContainerConfig) {
  113. c.HostConfig.AutoRemove = true
  114. }
  115. // WithUser sets the user
  116. func WithUser(user string) func(c *TestContainerConfig) {
  117. return func(c *TestContainerConfig) {
  118. c.Config.User = user
  119. }
  120. }