netutil_test.go 10 KB

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