netutil_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. err error
  166. }{
  167. {
  168. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  169. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  170. expect: true,
  171. },
  172. {
  173. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  174. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  175. expect: true,
  176. },
  177. {
  178. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  179. b: []url.URL{{Scheme: "https", Host: "10.0.10.1:2379"}},
  180. expect: false,
  181. err: errors.New(`"http://10.0.10.1:2379"(resolved from "http://example.com:2379") != "https://10.0.10.1:2379"(resolved from "https://10.0.10.1:2379")`),
  182. },
  183. {
  184. a: []url.URL{{Scheme: "https", Host: "example.com:2379"}},
  185. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  186. expect: false,
  187. err: errors.New(`"https://10.0.10.1:2379"(resolved from "https://example.com:2379") != "http://10.0.10.1:2379"(resolved from "http://10.0.10.1:2379")`),
  188. },
  189. {
  190. a: []url.URL{{Scheme: "unix", Host: "abc:2379"}},
  191. b: []url.URL{{Scheme: "unix", Host: "abc:2379"}},
  192. expect: true,
  193. },
  194. {
  195. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  196. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  197. expect: true,
  198. },
  199. {
  200. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  201. b: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  202. expect: true,
  203. },
  204. {
  205. a: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  206. b: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  207. expect: true,
  208. },
  209. {
  210. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  211. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}},
  212. expect: false,
  213. err: errors.New(`"http://127.0.0.1:2379"(resolved from "http://127.0.0.1:2379") != "http://127.0.0.1:2380"(resolved from "http://127.0.0.1:2380")`),
  214. },
  215. {
  216. a: []url.URL{{Scheme: "http", Host: "example.com:2380"}},
  217. b: []url.URL{{Scheme: "http", Host: "10.0.10.1:2379"}},
  218. expect: false,
  219. err: errors.New(`"http://10.0.10.1:2380"(resolved from "http://example.com:2380") != "http://10.0.10.1:2379"(resolved from "http://10.0.10.1:2379")`),
  220. },
  221. {
  222. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}},
  223. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  224. expect: false,
  225. err: errors.New(`"http://127.0.0.1:2379"(resolved from "http://127.0.0.1:2379") != "http://10.0.0.1:2379"(resolved from "http://10.0.0.1:2379")`),
  226. },
  227. {
  228. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}},
  229. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  230. expect: false,
  231. err: errors.New(`"http://10.0.10.1:2379"(resolved from "http://example.com:2379") != "http://10.0.0.1:2379"(resolved from "http://10.0.0.1:2379")`),
  232. },
  233. {
  234. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  235. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  236. expect: false,
  237. err: errors.New(`"http://127.0.0.1:2379"(resolved from "http://127.0.0.1:2379") != "http://127.0.0.1:2380"(resolved from "http://127.0.0.1:2380")`),
  238. },
  239. {
  240. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  241. b: []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  242. expect: false,
  243. err: errors.New(`"http://10.0.10.1:2379"(resolved from "http://example.com:2379") != "http://127.0.0.1:2380"(resolved from "http://127.0.0.1:2380")`),
  244. },
  245. {
  246. a: []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  247. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  248. expect: false,
  249. err: errors.New(`"http://127.0.0.1:2379"(resolved from "http://127.0.0.1:2379") != "http://10.0.0.1:2379"(resolved from "http://10.0.0.1:2379")`),
  250. },
  251. {
  252. a: []url.URL{{Scheme: "http", Host: "example.com:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  253. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  254. expect: false,
  255. err: errors.New(`"http://10.0.10.1:2379"(resolved from "http://example.com:2379") != "http://10.0.0.1:2379"(resolved from "http://10.0.0.1:2379")`),
  256. },
  257. {
  258. a: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}},
  259. b: []url.URL{{Scheme: "http", Host: "10.0.0.1:2379"}, {Scheme: "http", Host: "127.0.0.1:2380"}},
  260. expect: false,
  261. err: errors.New(`len(["http://10.0.0.1:2379"]) != len(["http://10.0.0.1:2379" "http://127.0.0.1:2380"])`),
  262. },
  263. {
  264. a: []url.URL{{Scheme: "http", Host: "first.com:2379"}, {Scheme: "http", Host: "second.com:2380"}},
  265. b: []url.URL{{Scheme: "http", Host: "10.0.11.1:2379"}, {Scheme: "http", Host: "10.0.11.2:2380"}},
  266. expect: true,
  267. },
  268. {
  269. a: []url.URL{{Scheme: "http", Host: "second.com:2380"}, {Scheme: "http", Host: "first.com:2379"}},
  270. b: []url.URL{{Scheme: "http", Host: "10.0.11.1:2379"}, {Scheme: "http", Host: "10.0.11.2:2380"}},
  271. expect: true,
  272. },
  273. }
  274. for i, test := range tests {
  275. result, err := urlsEqual(context.TODO(), test.a, test.b)
  276. if result != test.expect {
  277. t.Errorf("#%d: a:%v b:%v, expected %v but %v", i, test.a, test.b, test.expect, result)
  278. }
  279. if test.err != nil {
  280. if err.Error() != test.err.Error() {
  281. t.Errorf("#%d: err expected %v but %v", i, test.err, err)
  282. }
  283. }
  284. }
  285. }
  286. func TestURLStringsEqual(t *testing.T) {
  287. result, err := URLStringsEqual(context.TODO(), []string{"http://127.0.0.1:8080"}, []string{"http://127.0.0.1:8080"})
  288. if !result {
  289. t.Errorf("unexpected result %v", result)
  290. }
  291. if err != nil {
  292. t.Errorf("unexpected error %v", err)
  293. }
  294. }