netutil_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. "context"
  17. "errors"
  18. "net"
  19. "net/url"
  20. "reflect"
  21. "strconv"
  22. "testing"
  23. "time"
  24. )
  25. func TestResolveTCPAddrs(t *testing.T) {
  26. defer func() { resolveTCPAddr = resolveTCPAddrDefault }()
  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(ctx context.Context, 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 = resolveTCPAddrDefault }()
  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(ctx context.Context, addr string) (*net.TCPAddr, error) {
  148. host, port, herr := net.SplitHostPort(addr)
  149. if herr != nil {
  150. return nil, herr
  151. }
  152. if _, ok := hostm[host]; !ok {
  153. return nil, errors.New("cannot resolve host.")
  154. }
  155. i, err := strconv.Atoi(port)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil
  160. }
  161. tests := []struct {
  162. a []url.URL
  163. b []url.URL
  164. expect bool
  165. }{
  166. {
  167. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  168. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  169. expect: true,
  170. },
  171. {
  172. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  173. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  174. expect: true,
  175. },
  176. {
  177. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  178. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  179. expect: true,
  180. },
  181. {
  182. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  183. b: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  184. expect: true,
  185. },
  186. {
  187. a: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  188. b: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  189. expect: true,
  190. },
  191. {
  192. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  193. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}},
  194. expect: false,
  195. },
  196. {
  197. a: []url.URL{{Scheme: "http", Host: "example.com:2380"}},
  198. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  199. expect: false,
  200. },
  201. {
  202. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  203. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  204. expect: false,
  205. },
  206. {
  207. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  208. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  209. expect: false,
  210. },
  211. {
  212. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  213. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  214. expect: false,
  215. },
  216. {
  217. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  218. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  219. expect: false,
  220. },
  221. {
  222. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  223. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  224. expect: false,
  225. },
  226. {
  227. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  228. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  229. expect: false,
  230. },
  231. {
  232. a: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  233. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  234. expect: false,
  235. },
  236. {
  237. a: []url.URL{{Scheme: "http", Host: "first.com:2379"}, {Scheme: "http", Host: "second.com:2380"}},
  238. b: []url.URL{{Scheme: "http", Host: "10.0.11.1:2379"}, {Scheme: "http", Host: "10.0.11.2:2380"}},
  239. expect: true,
  240. },
  241. {
  242. a: []url.URL{{Scheme: "http", Host: "second.com:2380"}, {Scheme: "http", Host: "first.com:2379"}},
  243. b: []url.URL{{Scheme: "http", Host: "10.0.11.1:2379"}, {Scheme: "http", Host: "10.0.11.2:2380"}},
  244. expect: true,
  245. },
  246. }
  247. for _, test := range tests {
  248. result := urlsEqual(context.TODO(), test.a, test.b)
  249. if result != test.expect {
  250. t.Errorf("a:%v b:%v, expected %v but %v", test.a, test.b, test.expect, result)
  251. }
  252. }
  253. }
  254. func TestURLStringsEqual(t *testing.T) {
  255. result := URLStringsEqual(context.TODO(), []string{"http://127.0.0.1:8080"}, []string{"http://127.0.0.1:8080"})
  256. if !result {
  257. t.Errorf("unexpected result %v", result)
  258. }
  259. }