container_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package formatter
  14. import (
  15. "testing"
  16. "gotest.tools/v3/assert"
  17. "github.com/docker/compose-cli/cli/options/run"
  18. )
  19. func TestDisplayPorts(t *testing.T) {
  20. testCases := []struct {
  21. name string
  22. in []string
  23. expected []string
  24. }{
  25. {
  26. name: "simple",
  27. in: []string{"80"},
  28. expected: []string{"0.0.0.0:80->80/tcp"},
  29. },
  30. {
  31. name: "different ports",
  32. in: []string{"80:90"},
  33. expected: []string{"0.0.0.0:80->90/tcp"},
  34. },
  35. {
  36. name: "host ip",
  37. in: []string{"192.168.0.1:80:90"},
  38. expected: []string{"192.168.0.1:80->90/tcp"},
  39. },
  40. {
  41. name: "port range",
  42. in: []string{"80-90:80-90"},
  43. expected: []string{"0.0.0.0:80-90->80-90/tcp"},
  44. },
  45. {
  46. name: "grouping",
  47. in: []string{"80:80", "81:81"},
  48. expected: []string{"0.0.0.0:80-81->80-81/tcp"},
  49. },
  50. {
  51. name: "groups",
  52. in: []string{"80:80", "82:82"},
  53. expected: []string{"0.0.0.0:80->80/tcp", "0.0.0.0:82->82/tcp"},
  54. },
  55. }
  56. for _, testCase := range testCases {
  57. t.Run(testCase.name, func(t *testing.T) {
  58. runOpts := run.Opts{
  59. Publish: testCase.in,
  60. }
  61. containerConfig, err := runOpts.ToContainerConfig("test")
  62. assert.NilError(t, err)
  63. out := PortsToStrings(containerConfig.Ports)
  64. assert.DeepEqual(t, testCase.expected, out)
  65. })
  66. }
  67. }