srv_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 srv
  15. import (
  16. "errors"
  17. "net"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestSRVGetCluster(t *testing.T) {
  24. defer func() {
  25. lookupSRV = net.LookupSRV
  26. resolveTCPAddr = net.ResolveTCPAddr
  27. }()
  28. name := "dnsClusterTest"
  29. dns := map[string]string{
  30. "1.example.com.:2480": "10.0.0.1:2480",
  31. "2.example.com.:2480": "10.0.0.2:2480",
  32. "3.example.com.:2480": "10.0.0.3:2480",
  33. "4.example.com.:2380": "10.0.0.3:2380",
  34. }
  35. srvAll := []*net.SRV{
  36. {Target: "1.example.com.", Port: 2480},
  37. {Target: "2.example.com.", Port: 2480},
  38. {Target: "3.example.com.", Port: 2480},
  39. }
  40. tests := []struct {
  41. withSSL []*net.SRV
  42. withoutSSL []*net.SRV
  43. urls []string
  44. expected string
  45. }{
  46. {
  47. []*net.SRV{},
  48. []*net.SRV{},
  49. nil,
  50. "",
  51. },
  52. {
  53. srvAll,
  54. []*net.SRV{},
  55. nil,
  56. "0=https://1.example.com:2480,1=https://2.example.com:2480,2=https://3.example.com:2480",
  57. },
  58. {
  59. srvAll,
  60. []*net.SRV{{Target: "4.example.com.", Port: 2380}},
  61. nil,
  62. "0=https://1.example.com:2480,1=https://2.example.com:2480,2=https://3.example.com:2480,3=http://4.example.com:2380",
  63. },
  64. {
  65. srvAll,
  66. []*net.SRV{{Target: "4.example.com.", Port: 2380}},
  67. []string{"https://10.0.0.1:2480"},
  68. "dnsClusterTest=https://1.example.com:2480,0=https://2.example.com:2480,1=https://3.example.com:2480,2=http://4.example.com:2380",
  69. },
  70. // matching local member with resolved addr and return unresolved hostnames
  71. {
  72. srvAll,
  73. nil,
  74. []string{"https://10.0.0.1:2480"},
  75. "dnsClusterTest=https://1.example.com:2480,0=https://2.example.com:2480,1=https://3.example.com:2480",
  76. },
  77. // invalid
  78. }
  79. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  80. if strings.Contains(addr, "10.0.0.") {
  81. // accept IP addresses when resolving apurls
  82. return net.ResolveTCPAddr(network, addr)
  83. }
  84. if dns[addr] == "" {
  85. return nil, errors.New("missing dns record")
  86. }
  87. return net.ResolveTCPAddr(network, dns[addr])
  88. }
  89. for i, tt := range tests {
  90. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  91. if service == "etcd-server-ssl" {
  92. return "", tt.withSSL, nil
  93. }
  94. if service == "etcd-server" {
  95. return "", tt.withoutSSL, nil
  96. }
  97. return "", nil, errors.New("Unknown service in mock")
  98. }
  99. urls := testutil.MustNewURLs(t, tt.urls)
  100. str, err := GetCluster("etcd-server", name, "example.com", urls)
  101. if err != nil {
  102. t.Fatalf("%d: err: %#v", i, err)
  103. }
  104. if strings.Join(str, ",") != tt.expected {
  105. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  106. }
  107. }
  108. }
  109. func TestSRVDiscover(t *testing.T) {
  110. defer func() { lookupSRV = net.LookupSRV }()
  111. tests := []struct {
  112. withSSL []*net.SRV
  113. withoutSSL []*net.SRV
  114. expected []string
  115. }{
  116. {
  117. []*net.SRV{},
  118. []*net.SRV{},
  119. []string{},
  120. },
  121. {
  122. []*net.SRV{
  123. {Target: "10.0.0.1", Port: 2480},
  124. {Target: "10.0.0.2", Port: 2480},
  125. {Target: "10.0.0.3", Port: 2480},
  126. },
  127. []*net.SRV{},
  128. []string{"https://10.0.0.1:2480", "https://10.0.0.2:2480", "https://10.0.0.3:2480"},
  129. },
  130. {
  131. []*net.SRV{
  132. {Target: "10.0.0.1", Port: 2480},
  133. {Target: "10.0.0.2", Port: 2480},
  134. {Target: "10.0.0.3", Port: 2480},
  135. },
  136. []*net.SRV{
  137. {Target: "10.0.0.1", Port: 7001},
  138. },
  139. []string{"https://10.0.0.1:2480", "https://10.0.0.2:2480", "https://10.0.0.3:2480", "http://10.0.0.1:7001"},
  140. },
  141. {
  142. []*net.SRV{
  143. {Target: "10.0.0.1", Port: 2480},
  144. {Target: "10.0.0.2", Port: 2480},
  145. {Target: "10.0.0.3", Port: 2480},
  146. },
  147. []*net.SRV{
  148. {Target: "10.0.0.1", Port: 7001},
  149. },
  150. []string{"https://10.0.0.1:2480", "https://10.0.0.2:2480", "https://10.0.0.3:2480", "http://10.0.0.1:7001"},
  151. },
  152. {
  153. []*net.SRV{
  154. {Target: "a.example.com", Port: 2480},
  155. {Target: "b.example.com", Port: 2480},
  156. {Target: "c.example.com", Port: 2480},
  157. },
  158. []*net.SRV{},
  159. []string{"https://a.example.com:2480", "https://b.example.com:2480", "https://c.example.com:2480"},
  160. },
  161. }
  162. for i, tt := range tests {
  163. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  164. if service == "etcd-client-ssl" {
  165. return "", tt.withSSL, nil
  166. }
  167. if service == "etcd-client" {
  168. return "", tt.withoutSSL, nil
  169. }
  170. return "", nil, errors.New("Unknown service in mock")
  171. }
  172. srvs, err := GetClient("etcd-client", "example.com")
  173. if err != nil {
  174. t.Fatalf("%d: err: %#v", i, err)
  175. }
  176. if !reflect.DeepEqual(srvs.Endpoints, tt.expected) {
  177. t.Errorf("#%d: endpoints = %v, want %v", i, srvs.Endpoints, tt.expected)
  178. }
  179. }
  180. }