netutil_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2015 CoreOS, Inc.
  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. )
  23. func TestResolveTCPAddrs(t *testing.T) {
  24. defer func() { resolveTCPAddr = net.ResolveTCPAddr }()
  25. tests := []struct {
  26. urls [][]url.URL
  27. expected [][]url.URL
  28. hostMap map[string]string
  29. hasError bool
  30. }{
  31. {
  32. urls: [][]url.URL{
  33. {
  34. {Scheme: "http", Host: "127.0.0.1:4001"},
  35. {Scheme: "http", Host: "127.0.0.1:2379"},
  36. },
  37. {
  38. {Scheme: "http", Host: "127.0.0.1:7001"},
  39. {Scheme: "http", Host: "127.0.0.1:2380"},
  40. },
  41. },
  42. expected: [][]url.URL{
  43. {
  44. {Scheme: "http", Host: "127.0.0.1:4001"},
  45. {Scheme: "http", Host: "127.0.0.1:2379"},
  46. },
  47. {
  48. {Scheme: "http", Host: "127.0.0.1:7001"},
  49. {Scheme: "http", Host: "127.0.0.1:2380"},
  50. },
  51. },
  52. },
  53. {
  54. urls: [][]url.URL{
  55. {
  56. {Scheme: "http", Host: "infra0.example.com:4001"},
  57. {Scheme: "http", Host: "infra0.example.com:2379"},
  58. },
  59. {
  60. {Scheme: "http", Host: "infra0.example.com:7001"},
  61. {Scheme: "http", Host: "infra0.example.com:2380"},
  62. },
  63. },
  64. expected: [][]url.URL{
  65. {
  66. {Scheme: "http", Host: "10.0.1.10:4001"},
  67. {Scheme: "http", Host: "10.0.1.10:2379"},
  68. },
  69. {
  70. {Scheme: "http", Host: "10.0.1.10:7001"},
  71. {Scheme: "http", Host: "10.0.1.10:2380"},
  72. },
  73. },
  74. hostMap: map[string]string{
  75. "infra0.example.com": "10.0.1.10",
  76. },
  77. hasError: false,
  78. },
  79. {
  80. urls: [][]url.URL{
  81. {
  82. {Scheme: "http", Host: "infra0.example.com:4001"},
  83. {Scheme: "http", Host: "infra0.example.com:2379"},
  84. },
  85. {
  86. {Scheme: "http", Host: "infra0.example.com:7001"},
  87. {Scheme: "http", Host: "infra0.example.com:2380"},
  88. },
  89. },
  90. hostMap: map[string]string{
  91. "infra0.example.com": "",
  92. },
  93. hasError: true,
  94. },
  95. {
  96. urls: [][]url.URL{
  97. {
  98. {Scheme: "http", Host: "ssh://infra0.example.com:4001"},
  99. {Scheme: "http", Host: "ssh://infra0.example.com:2379"},
  100. },
  101. {
  102. {Scheme: "http", Host: "ssh://infra0.example.com:7001"},
  103. {Scheme: "http", Host: "ssh://infra0.example.com:2380"},
  104. },
  105. },
  106. hasError: true,
  107. },
  108. }
  109. for _, tt := range tests {
  110. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  111. host, port, err := net.SplitHostPort(addr)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if tt.hostMap[host] == "" {
  116. return nil, errors.New("cannot resolve host.")
  117. }
  118. i, err := strconv.Atoi(port)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
  123. }
  124. err := ResolveTCPAddrs(tt.urls...)
  125. if tt.hasError {
  126. if err == nil {
  127. t.Errorf("expected error")
  128. }
  129. continue
  130. }
  131. if !reflect.DeepEqual(tt.urls, tt.expected) {
  132. t.Errorf("expected: %v, got %v", tt.expected, tt.urls)
  133. }
  134. }
  135. }