netutil_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package netutil
  14. import (
  15. "errors"
  16. "net"
  17. "net/url"
  18. "reflect"
  19. "strconv"
  20. "testing"
  21. )
  22. func TestResolveTCPAddrs(t *testing.T) {
  23. defer func() { resolveTCPAddr = net.ResolveTCPAddr }()
  24. tests := []struct {
  25. urls [][]url.URL
  26. expected [][]url.URL
  27. hostMap map[string]string
  28. hasError bool
  29. }{
  30. {
  31. urls: [][]url.URL{
  32. []url.URL{
  33. url.URL{Scheme: "http", Host: "127.0.0.1:4001"},
  34. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  35. },
  36. []url.URL{
  37. url.URL{Scheme: "http", Host: "127.0.0.1:7001"},
  38. url.URL{Scheme: "http", Host: "127.0.0.1:2380"},
  39. },
  40. },
  41. expected: [][]url.URL{
  42. []url.URL{
  43. url.URL{Scheme: "http", Host: "127.0.0.1:4001"},
  44. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  45. },
  46. []url.URL{
  47. url.URL{Scheme: "http", Host: "127.0.0.1:7001"},
  48. url.URL{Scheme: "http", Host: "127.0.0.1:2380"},
  49. },
  50. },
  51. },
  52. {
  53. urls: [][]url.URL{
  54. []url.URL{
  55. url.URL{Scheme: "http", Host: "infra0.example.com:4001"},
  56. url.URL{Scheme: "http", Host: "infra0.example.com:2379"},
  57. },
  58. []url.URL{
  59. url.URL{Scheme: "http", Host: "infra0.example.com:7001"},
  60. url.URL{Scheme: "http", Host: "infra0.example.com:2380"},
  61. },
  62. },
  63. expected: [][]url.URL{
  64. []url.URL{
  65. url.URL{Scheme: "http", Host: "10.0.1.10:4001"},
  66. url.URL{Scheme: "http", Host: "10.0.1.10:2379"},
  67. },
  68. []url.URL{
  69. url.URL{Scheme: "http", Host: "10.0.1.10:7001"},
  70. url.URL{Scheme: "http", Host: "10.0.1.10:2380"},
  71. },
  72. },
  73. hostMap: map[string]string{
  74. "infra0.example.com": "10.0.1.10",
  75. },
  76. hasError: false,
  77. },
  78. {
  79. urls: [][]url.URL{
  80. []url.URL{
  81. url.URL{Scheme: "http", Host: "infra0.example.com:4001"},
  82. url.URL{Scheme: "http", Host: "infra0.example.com:2379"},
  83. },
  84. []url.URL{
  85. url.URL{Scheme: "http", Host: "infra0.example.com:7001"},
  86. url.URL{Scheme: "http", Host: "infra0.example.com:2380"},
  87. },
  88. },
  89. hostMap: map[string]string{
  90. "infra0.example.com": "",
  91. },
  92. hasError: true,
  93. },
  94. {
  95. urls: [][]url.URL{
  96. []url.URL{
  97. url.URL{Scheme: "http", Host: "ssh://infra0.example.com:4001"},
  98. url.URL{Scheme: "http", Host: "ssh://infra0.example.com:2379"},
  99. },
  100. []url.URL{
  101. url.URL{Scheme: "http", Host: "ssh://infra0.example.com:7001"},
  102. url.URL{Scheme: "http", Host: "ssh://infra0.example.com:2380"},
  103. },
  104. },
  105. hasError: true,
  106. },
  107. }
  108. for _, tt := range tests {
  109. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  110. host, port, err := net.SplitHostPort(addr)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if tt.hostMap[host] == "" {
  115. return nil, errors.New("cannot resolve host.")
  116. }
  117. i, err := strconv.Atoi(port)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
  122. }
  123. err := ResolveTCPAddrs(tt.urls...)
  124. if tt.hasError {
  125. if err == nil {
  126. t.Errorf("expected error")
  127. }
  128. continue
  129. }
  130. if !reflect.DeepEqual(tt.urls, tt.expected) {
  131. t.Errorf("expected: %v, got %v", tt.expected, tt.urls)
  132. }
  133. }
  134. }