opts_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package run
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/stretchr/testify/suite"
  7. "github.com/docker/api/containers"
  8. )
  9. type RunOptsSuite struct {
  10. suite.Suite
  11. }
  12. func (s *RunOptsSuite) TestPortParse() {
  13. testCases := []struct {
  14. in string
  15. expected []containers.Port
  16. }{
  17. {
  18. in: "80",
  19. expected: []containers.Port{
  20. {
  21. HostPort: 80,
  22. ContainerPort: 80,
  23. Protocol: "tcp",
  24. },
  25. },
  26. },
  27. {
  28. in: "80:80",
  29. expected: []containers.Port{
  30. {
  31. HostPort: 80,
  32. ContainerPort: 80,
  33. Protocol: "tcp",
  34. },
  35. },
  36. },
  37. {
  38. in: "80:80/udp",
  39. expected: []containers.Port{
  40. {
  41. ContainerPort: 80,
  42. HostPort: 80,
  43. Protocol: "udp",
  44. },
  45. },
  46. },
  47. {
  48. in: "8080:80",
  49. expected: []containers.Port{
  50. {
  51. HostPort: 8080,
  52. ContainerPort: 80,
  53. Protocol: "tcp",
  54. },
  55. },
  56. },
  57. {
  58. in: "192.168.0.2:8080:80",
  59. expected: []containers.Port{
  60. {
  61. HostPort: 8080,
  62. ContainerPort: 80,
  63. Protocol: "tcp",
  64. HostIP: "192.168.0.2",
  65. },
  66. },
  67. },
  68. {
  69. in: "80-81:80-81",
  70. expected: []containers.Port{
  71. {
  72. HostPort: 80,
  73. ContainerPort: 80,
  74. Protocol: "tcp",
  75. },
  76. {
  77. HostPort: 81,
  78. ContainerPort: 81,
  79. Protocol: "tcp",
  80. },
  81. },
  82. },
  83. }
  84. for _, testCase := range testCases {
  85. opts := Opts{
  86. Publish: []string{testCase.in},
  87. }
  88. result, err := opts.toPorts()
  89. require.Nil(s.T(), err)
  90. assert.ElementsMatch(s.T(), testCase.expected, result)
  91. }
  92. }
  93. func TestExampleTestSuite(t *testing.T) {
  94. suite.Run(t, new(RunOptsSuite))
  95. }