netutil_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutil
  15. import (
  16. "errors"
  17. "net"
  18. "net/url"
  19. "reflect"
  20. "strconv"
  21. "testing"
  22. )
  23. func TestResolveTCPAddrs(t *testing.T) {
  24. defer func() { resolveTCPAddr = net.ResolveTCPAddr }()
  25. tests := []struct {
  26. urls [][]url.URL
  27. expected [][]url.URL
  28. hostMap map[string]string
  29. hasError bool
  30. }{
  31. {
  32. urls: [][]url.URL{
  33. {
  34. {Scheme: "http", Host: "127.0.0.1:4001"},
  35. {Scheme: "http", Host: "127.0.0.1:2379"},
  36. },
  37. {
  38. {Scheme: "http", Host: "127.0.0.1:7001"},
  39. {Scheme: "http", Host: "127.0.0.1:2380"},
  40. },
  41. },
  42. expected: [][]url.URL{
  43. {
  44. {Scheme: "http", Host: "127.0.0.1:4001"},
  45. {Scheme: "http", Host: "127.0.0.1:2379"},
  46. },
  47. {
  48. {Scheme: "http", Host: "127.0.0.1:7001"},
  49. {Scheme: "http", Host: "127.0.0.1:2380"},
  50. },
  51. },
  52. },
  53. {
  54. urls: [][]url.URL{
  55. {
  56. {Scheme: "http", Host: "infra0.example.com:4001"},
  57. {Scheme: "http", Host: "infra0.example.com:2379"},
  58. },
  59. {
  60. {Scheme: "http", Host: "infra0.example.com:7001"},
  61. {Scheme: "http", Host: "infra0.example.com:2380"},
  62. },
  63. },
  64. expected: [][]url.URL{
  65. {
  66. {Scheme: "http", Host: "10.0.1.10:4001"},
  67. {Scheme: "http", Host: "10.0.1.10:2379"},
  68. },
  69. {
  70. {Scheme: "http", Host: "10.0.1.10:7001"},
  71. {Scheme: "http", Host: "10.0.1.10:2380"},
  72. },
  73. },
  74. hostMap: map[string]string{
  75. "infra0.example.com": "10.0.1.10",
  76. },
  77. hasError: false,
  78. },
  79. {
  80. urls: [][]url.URL{
  81. {
  82. {Scheme: "http", Host: "infra0.example.com:4001"},
  83. {Scheme: "http", Host: "infra0.example.com:2379"},
  84. },
  85. {
  86. {Scheme: "http", Host: "infra0.example.com:7001"},
  87. {Scheme: "http", Host: "infra0.example.com:2380"},
  88. },
  89. },
  90. hostMap: map[string]string{
  91. "infra0.example.com": "",
  92. },
  93. hasError: true,
  94. },
  95. {
  96. urls: [][]url.URL{
  97. {
  98. {Scheme: "http", Host: "ssh://infra0.example.com:4001"},
  99. {Scheme: "http", Host: "ssh://infra0.example.com:2379"},
  100. },
  101. {
  102. {Scheme: "http", Host: "ssh://infra0.example.com:7001"},
  103. {Scheme: "http", Host: "ssh://infra0.example.com:2380"},
  104. },
  105. },
  106. hasError: true,
  107. },
  108. }
  109. for _, tt := range tests {
  110. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  111. host, port, err := net.SplitHostPort(addr)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if tt.hostMap[host] == "" {
  116. return nil, errors.New("cannot resolve host.")
  117. }
  118. i, err := strconv.Atoi(port)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
  123. }
  124. urls, err := resolveTCPAddrs(tt.urls)
  125. if tt.hasError {
  126. if err == nil {
  127. t.Errorf("expected error")
  128. }
  129. continue
  130. }
  131. if !reflect.DeepEqual(urls, tt.expected) {
  132. t.Errorf("expected: %v, got %v", tt.expected, urls)
  133. }
  134. }
  135. }
  136. func TestURLsEqual(t *testing.T) {
  137. defer func() { resolveTCPAddr = net.ResolveTCPAddr }()
  138. hostm := map[string]string{
  139. "example.com": "10.0.10.1",
  140. "first.com": "10.0.11.1",
  141. "second.com": "10.0.11.2",
  142. }
  143. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  144. host, port, err := net.SplitHostPort(addr)
  145. if _, ok := hostm[host]; !ok {
  146. return nil, errors.New("cannot resolve host.")
  147. }
  148. i, err := strconv.Atoi(port)
  149. if err != nil {
  150. return nil, err
  151. }
  152. return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil
  153. }
  154. tests := []struct {
  155. a []url.URL
  156. b []url.URL
  157. expect bool
  158. }{
  159. {
  160. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  161. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  162. expect: true,
  163. },
  164. {
  165. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  166. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  167. expect: true,
  168. },
  169. {
  170. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  171. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  172. expect: true,
  173. },
  174. {
  175. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  176. b: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  177. expect: true,
  178. },
  179. {
  180. a: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  181. b: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  182. expect: true,
  183. },
  184. {
  185. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  186. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}},
  187. expect: false,
  188. },
  189. {
  190. a: []url.URL{{Scheme: "http", Host: "example.com:2380"}},
  191. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  192. expect: false,
  193. },
  194. {
  195. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  196. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  197. expect: false,
  198. },
  199. {
  200. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  201. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  202. expect: false,
  203. },
  204. {
  205. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  206. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  207. expect: false,
  208. },
  209. {
  210. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  211. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  212. expect: false,
  213. },
  214. {
  215. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  216. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  217. expect: false,
  218. },
  219. {
  220. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  221. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  222. expect: false,
  223. },
  224. {
  225. a: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  226. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  227. expect: false,
  228. },
  229. {
  230. a: []url.URL{{Scheme: "http", Host: "first.com:2379"}, {Scheme: "http", Host: "second.com:2380"}},
  231. b: []url.URL{{Scheme: "http", Host: "10.0.11.1:2379"}, {Scheme: "http", Host: "10.0.11.2:2380"}},
  232. expect: true,
  233. },
  234. {
  235. a: []url.URL{{Scheme: "http", Host: "second.com:2380"}, {Scheme: "http", Host: "first.com:2379"}},
  236. b: []url.URL{{Scheme: "http", Host: "10.0.11.1:2379"}, {Scheme: "http", Host: "10.0.11.2:2380"}},
  237. expect: true,
  238. },
  239. }
  240. for _, test := range tests {
  241. result := urlsEqual(test.a, test.b)
  242. if result != test.expect {
  243. t.Errorf("a:%v b:%v, expected %v but %v", test.a, test.b, test.expect, result)
  244. }
  245. }
  246. }
  247. func TestURLStringsEqual(t *testing.T) {
  248. result := URLStringsEqual([]string{"http://127.0.0.1:8080"}, []string{"http://127.0.0.1:8080"})
  249. if !result {
  250. t.Errorf("unexpected result %v", result)
  251. }
  252. }