netutil_test.go 7.7 KB

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