srv_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // reject if apurls are TLS but SRV is only http
  78. {
  79. nil,
  80. srvAll,
  81. []string{"https://10.0.0.1:2480"},
  82. "0=http://2.example.com:2480,1=http://3.example.com:2480",
  83. },
  84. }
  85. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  86. if strings.Contains(addr, "10.0.0.") {
  87. // accept IP addresses when resolving apurls
  88. return net.ResolveTCPAddr(network, addr)
  89. }
  90. if dns[addr] == "" {
  91. return nil, errors.New("missing dns record")
  92. }
  93. return net.ResolveTCPAddr(network, dns[addr])
  94. }
  95. for i, tt := range tests {
  96. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  97. if service == "etcd-server-ssl" {
  98. return "", tt.withSSL, nil
  99. }
  100. if service == "etcd-server" {
  101. return "", tt.withoutSSL, nil
  102. }
  103. return "", nil, errors.New("Unknown service in mock")
  104. }
  105. urls := testutil.MustNewURLs(t, tt.urls)
  106. str, err := GetCluster("etcd-server", name, "example.com", urls)
  107. if err != nil {
  108. t.Fatalf("%d: err: %#v", i, err)
  109. }
  110. if strings.Join(str, ",") != tt.expected {
  111. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  112. }
  113. }
  114. }
  115. func TestSRVDiscover(t *testing.T) {
  116. defer func() { lookupSRV = net.LookupSRV }()
  117. tests := []struct {
  118. withSSL []*net.SRV
  119. withoutSSL []*net.SRV
  120. expected []string
  121. }{
  122. {
  123. []*net.SRV{},
  124. []*net.SRV{},
  125. []string{},
  126. },
  127. {
  128. []*net.SRV{
  129. {Target: "10.0.0.1", Port: 2480},
  130. {Target: "10.0.0.2", Port: 2480},
  131. {Target: "10.0.0.3", Port: 2480},
  132. },
  133. []*net.SRV{},
  134. []string{"https://10.0.0.1:2480", "https://10.0.0.2:2480", "https://10.0.0.3:2480"},
  135. },
  136. {
  137. []*net.SRV{
  138. {Target: "10.0.0.1", Port: 2480},
  139. {Target: "10.0.0.2", Port: 2480},
  140. {Target: "10.0.0.3", Port: 2480},
  141. },
  142. []*net.SRV{
  143. {Target: "10.0.0.1", Port: 7001},
  144. },
  145. []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"},
  146. },
  147. {
  148. []*net.SRV{
  149. {Target: "10.0.0.1", Port: 2480},
  150. {Target: "10.0.0.2", Port: 2480},
  151. {Target: "10.0.0.3", Port: 2480},
  152. },
  153. []*net.SRV{
  154. {Target: "10.0.0.1", Port: 7001},
  155. },
  156. []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"},
  157. },
  158. {
  159. []*net.SRV{
  160. {Target: "a.example.com", Port: 2480},
  161. {Target: "b.example.com", Port: 2480},
  162. {Target: "c.example.com", Port: 2480},
  163. },
  164. []*net.SRV{},
  165. []string{"https://a.example.com:2480", "https://b.example.com:2480", "https://c.example.com:2480"},
  166. },
  167. }
  168. for i, tt := range tests {
  169. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  170. if service == "etcd-client-ssl" {
  171. return "", tt.withSSL, nil
  172. }
  173. if service == "etcd-client" {
  174. return "", tt.withoutSSL, nil
  175. }
  176. return "", nil, errors.New("Unknown service in mock")
  177. }
  178. srvs, err := GetClient("etcd-client", "example.com")
  179. if err != nil {
  180. t.Fatalf("%d: err: %#v", i, err)
  181. }
  182. if !reflect.DeepEqual(srvs.Endpoints, tt.expected) {
  183. t.Errorf("#%d: endpoints = %v, want %v", i, srvs.Endpoints, tt.expected)
  184. }
  185. }
  186. }