container_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package formatter
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "gotest.tools/v3/assert"
  6. "github.com/docker/api/cli/options/run"
  7. )
  8. func TestDisplayPorts(t *testing.T) {
  9. testCases := []struct {
  10. name string
  11. in []string
  12. expected string
  13. }{
  14. {
  15. name: "simple",
  16. in: []string{"80"},
  17. expected: "0.0.0.0:80->80/tcp",
  18. },
  19. {
  20. name: "different ports",
  21. in: []string{"80:90"},
  22. expected: "0.0.0.0:80->90/tcp",
  23. },
  24. {
  25. name: "host ip",
  26. in: []string{"192.168.0.1:80:90"},
  27. expected: "192.168.0.1:80->90/tcp",
  28. },
  29. {
  30. name: "port range",
  31. in: []string{"80-90:80-90"},
  32. expected: "0.0.0.0:80-90->80-90/tcp",
  33. },
  34. {
  35. name: "grouping",
  36. in: []string{"80:80", "81:81"},
  37. expected: "0.0.0.0:80-81->80-81/tcp",
  38. },
  39. {
  40. name: "groups",
  41. in: []string{"80:80", "82:82"},
  42. expected: "0.0.0.0:80->80/tcp, 0.0.0.0:82->82/tcp",
  43. },
  44. }
  45. for _, testCase := range testCases {
  46. t.Run(testCase.name, func(t *testing.T) {
  47. runOpts := run.Opts{
  48. Publish: testCase.in,
  49. }
  50. containerConfig, err := runOpts.ToContainerConfig("test")
  51. require.Nil(t, err)
  52. out := PortsString(containerConfig.Ports)
  53. assert.Equal(t, testCase.expected, out)
  54. })
  55. }
  56. }