ops.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package container
  2. import (
  3. "fmt"
  4. containertypes "github.com/docker/docker/api/types/container"
  5. "github.com/docker/docker/api/types/strslice"
  6. "github.com/docker/go-connections/nat"
  7. )
  8. // WithName sets the name of the container
  9. func WithName(name string) func(*TestContainerConfig) {
  10. return func(c *TestContainerConfig) {
  11. c.Name = name
  12. }
  13. }
  14. // WithLinks sets the links of the container
  15. func WithLinks(links ...string) func(*TestContainerConfig) {
  16. return func(c *TestContainerConfig) {
  17. c.HostConfig.Links = links
  18. }
  19. }
  20. // WithCmd sets the comannds of the container
  21. func WithCmd(cmds ...string) func(*TestContainerConfig) {
  22. return func(c *TestContainerConfig) {
  23. c.Config.Cmd = strslice.StrSlice(cmds)
  24. }
  25. }
  26. // WithNetworkMode sets the network mode of the container
  27. func WithNetworkMode(mode string) func(*TestContainerConfig) {
  28. return func(c *TestContainerConfig) {
  29. c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
  30. }
  31. }
  32. // WithExposedPorts sets the exposed ports of the container
  33. func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
  34. return func(c *TestContainerConfig) {
  35. c.Config.ExposedPorts = map[nat.Port]struct{}{}
  36. for _, port := range ports {
  37. c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
  38. }
  39. }
  40. }
  41. // WithTty sets the TTY mode of the container
  42. func WithTty(tty bool) func(*TestContainerConfig) {
  43. return func(c *TestContainerConfig) {
  44. c.Config.Tty = tty
  45. }
  46. }
  47. // WithWorkingDir sets the working dir of the container
  48. func WithWorkingDir(dir string) func(*TestContainerConfig) {
  49. return func(c *TestContainerConfig) {
  50. c.Config.WorkingDir = dir
  51. }
  52. }
  53. // WithVolume sets the volume of the container
  54. func WithVolume(name string) func(*TestContainerConfig) {
  55. return func(c *TestContainerConfig) {
  56. if c.Config.Volumes == nil {
  57. c.Config.Volumes = map[string]struct{}{}
  58. }
  59. c.Config.Volumes[name] = struct{}{}
  60. }
  61. }
  62. // WithBind sets the bind mount of the container
  63. func WithBind(src, target string) func(*TestContainerConfig) {
  64. return func(c *TestContainerConfig) {
  65. c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
  66. }
  67. }