port_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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 util
  14. import "testing"
  15. type fakeClosable struct {
  16. closed bool
  17. }
  18. func (c *fakeClosable) Close() error {
  19. c.closed = true
  20. return nil
  21. }
  22. func TestLocalPortString(t *testing.T) {
  23. testCases := []struct {
  24. description string
  25. ip string
  26. port int
  27. protocol string
  28. expectedStr string
  29. }{
  30. {"IPv4 UDP", "1.2.3.4", 9999, "udp", "\"IPv4 UDP\" (1.2.3.4:9999/udp)"},
  31. {"IPv4 TCP", "5.6.7.8", 1053, "tcp", "\"IPv4 TCP\" (5.6.7.8:1053/tcp)"},
  32. {"IPv6 TCP", "2001:db8::1", 80, "tcp", "\"IPv6 TCP\" ([2001:db8::1]:80/tcp)"},
  33. {"IPv4 SCTP", "9.10.11.12", 7777, "sctp", "\"IPv4 SCTP\" (9.10.11.12:7777/sctp)"},
  34. {"IPv6 SCTP", "2001:db8::2", 80, "sctp", "\"IPv6 SCTP\" ([2001:db8::2]:80/sctp)"},
  35. }
  36. for _, tc := range testCases {
  37. lp := &LocalPort{
  38. Description: tc.description,
  39. IP: tc.ip,
  40. Port: tc.port,
  41. Protocol: tc.protocol,
  42. }
  43. str := lp.String()
  44. if str != tc.expectedStr {
  45. t.Errorf("Unexpected output for %s, expected: %s, got: %s", tc.description, tc.expectedStr, str)
  46. }
  47. }
  48. }
  49. func TestRevertPorts(t *testing.T) {
  50. testCases := []struct {
  51. replacementPorts []LocalPort
  52. existingPorts []LocalPort
  53. expectToBeClose []bool
  54. }{
  55. {
  56. replacementPorts: []LocalPort{
  57. {Port: 5001},
  58. {Port: 5002},
  59. {Port: 5003},
  60. },
  61. existingPorts: []LocalPort{},
  62. expectToBeClose: []bool{true, true, true},
  63. },
  64. {
  65. replacementPorts: []LocalPort{},
  66. existingPorts: []LocalPort{
  67. {Port: 5001},
  68. {Port: 5002},
  69. {Port: 5003},
  70. },
  71. expectToBeClose: []bool{},
  72. },
  73. {
  74. replacementPorts: []LocalPort{
  75. {Port: 5001},
  76. {Port: 5002},
  77. {Port: 5003},
  78. },
  79. existingPorts: []LocalPort{
  80. {Port: 5001},
  81. {Port: 5002},
  82. {Port: 5003},
  83. },
  84. expectToBeClose: []bool{false, false, false},
  85. },
  86. {
  87. replacementPorts: []LocalPort{
  88. {Port: 5001},
  89. {Port: 5002},
  90. {Port: 5003},
  91. },
  92. existingPorts: []LocalPort{
  93. {Port: 5001},
  94. {Port: 5003},
  95. },
  96. expectToBeClose: []bool{false, true, false},
  97. },
  98. {
  99. replacementPorts: []LocalPort{
  100. {Port: 5001},
  101. {Port: 5002},
  102. {Port: 5003},
  103. },
  104. existingPorts: []LocalPort{
  105. {Port: 5001},
  106. {Port: 5002},
  107. {Port: 5003},
  108. {Port: 5004},
  109. },
  110. expectToBeClose: []bool{false, false, false},
  111. },
  112. }
  113. for i, tc := range testCases {
  114. replacementPortsMap := make(map[LocalPort]Closeable)
  115. for _, lp := range tc.replacementPorts {
  116. replacementPortsMap[lp] = &fakeClosable{}
  117. }
  118. existingPortsMap := make(map[LocalPort]Closeable)
  119. for _, lp := range tc.existingPorts {
  120. existingPortsMap[lp] = &fakeClosable{}
  121. }
  122. RevertPorts(replacementPortsMap, existingPortsMap)
  123. for j, expectation := range tc.expectToBeClose {
  124. if replacementPortsMap[tc.replacementPorts[j]].(*fakeClosable).closed != expectation {
  125. t.Errorf("Expect replacement localport %v to be %v in test case %v", tc.replacementPorts[j], expectation, i)
  126. }
  127. }
  128. for _, lp := range tc.existingPorts {
  129. if existingPortsMap[lp].(*fakeClosable).closed == true {
  130. t.Errorf("Expect existing localport %v to be false in test case %v", lp, i)
  131. }
  132. }
  133. }
  134. }