endpoints_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 (
  15. "net"
  16. "testing"
  17. )
  18. func TestIPPart(t *testing.T) {
  19. const noError = ""
  20. testCases := []struct {
  21. endpoint string
  22. expectedIP string
  23. expectedError string
  24. }{
  25. {"1.2.3.4", "1.2.3.4", noError},
  26. {"1.2.3.4:9999", "1.2.3.4", noError},
  27. {"2001:db8::1:1", "2001:db8::1:1", noError},
  28. {"[2001:db8::2:2]:9999", "2001:db8::2:2", noError},
  29. {"1.2.3.4::9999", "", "too many colons"},
  30. {"1.2.3.4:[0]", "", "unexpected '[' in address"},
  31. {"1.2.3:8080", "", "invalid ip part"},
  32. }
  33. for _, tc := range testCases {
  34. ip := IPPart(tc.endpoint)
  35. if tc.expectedError == noError {
  36. if ip != tc.expectedIP {
  37. t.Errorf("Unexpected IP for %s: Expected: %s, Got %s", tc.endpoint, tc.expectedIP, ip)
  38. }
  39. } else if ip != "" {
  40. t.Errorf("Error did not occur for %s, expected: '%s' error", tc.endpoint, tc.expectedError)
  41. }
  42. }
  43. }
  44. func TestPortPart(t *testing.T) {
  45. tests := []struct {
  46. name string
  47. endpoint string
  48. want int
  49. wantErr bool
  50. }{
  51. {
  52. "no error parsing from ipv4-ip:port",
  53. "1.2.3.4:1024",
  54. 1024,
  55. false,
  56. },
  57. {
  58. "no error parsing from ipv6-ip:port",
  59. "[2001:db8::2:2]:9999",
  60. 9999,
  61. false,
  62. },
  63. {
  64. "error: missing port",
  65. "1.2.3.4",
  66. -1,
  67. true,
  68. },
  69. {
  70. "error: invalid port '1-2'",
  71. "1.2.3.4:1-2",
  72. -1,
  73. true,
  74. },
  75. {
  76. "error: invalid port 'port'",
  77. "100.200.3.4:port",
  78. -1,
  79. true,
  80. },
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.name, func(t *testing.T) {
  84. got, err := PortPart(tt.endpoint)
  85. if (err != nil) != tt.wantErr {
  86. t.Errorf("PortPart() error = %v, wantErr %v", err, tt.wantErr)
  87. return
  88. }
  89. if got != tt.want {
  90. t.Errorf("PortPart() = %v, want %v", got, tt.want)
  91. }
  92. })
  93. }
  94. }
  95. func TestToCIDR(t *testing.T) {
  96. testCases := []struct {
  97. ip string
  98. expectedAddr string
  99. }{
  100. {"1.2.3.4", "1.2.3.4/32"},
  101. {"2001:db8::1:1", "2001:db8::1:1/128"},
  102. }
  103. for _, tc := range testCases {
  104. ip := net.ParseIP(tc.ip)
  105. addr := ToCIDR(ip)
  106. if addr != tc.expectedAddr {
  107. t.Errorf("Unexpected host address for %s: Expected: %s, Got %s", tc.ip, tc.expectedAddr, addr)
  108. }
  109. }
  110. }