ops.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package container
  2. import (
  3. containertypes "github.com/docker/docker/api/types/container"
  4. "github.com/docker/docker/api/types/strslice"
  5. "github.com/docker/go-connections/nat"
  6. )
  7. // WithName sets the name of the container
  8. func WithName(name string) func(*TestContainerConfig) {
  9. return func(c *TestContainerConfig) {
  10. c.Name = name
  11. }
  12. }
  13. // WithLinks sets the links of the container
  14. func WithLinks(links ...string) func(*TestContainerConfig) {
  15. return func(c *TestContainerConfig) {
  16. c.HostConfig.Links = links
  17. }
  18. }
  19. // WithCmd sets the comannds of the container
  20. func WithCmd(cmds ...string) func(*TestContainerConfig) {
  21. return func(c *TestContainerConfig) {
  22. c.Config.Cmd = strslice.StrSlice(cmds)
  23. }
  24. }
  25. // WithNetworkMode sets the network mode of the container
  26. func WithNetworkMode(mode string) func(*TestContainerConfig) {
  27. return func(c *TestContainerConfig) {
  28. c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
  29. }
  30. }
  31. // WithExposedPorts sets the exposed ports of the container
  32. func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
  33. return func(c *TestContainerConfig) {
  34. c.Config.ExposedPorts = map[nat.Port]struct{}{}
  35. for _, port := range ports {
  36. c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
  37. }
  38. }
  39. }
  40. // WithTty sets the TTY mode of the container
  41. func WithTty(tty bool) func(*TestContainerConfig) {
  42. return func(c *TestContainerConfig) {
  43. c.Config.Tty = tty
  44. }
  45. }
  46. // WithWorkingDir sets the working dir of the container
  47. func WithWorkingDir(dir string) func(*TestContainerConfig) {
  48. return func(c *TestContainerConfig) {
  49. c.Config.WorkingDir = dir
  50. }
  51. }