srv_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. "go.etcd.io/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. scheme string
  42. records []*net.SRV
  43. urls []string
  44. expected string
  45. }{
  46. {
  47. "https",
  48. []*net.SRV{},
  49. nil,
  50. "",
  51. },
  52. {
  53. "https",
  54. srvAll,
  55. nil,
  56. "0=https://1.example.com:2480,1=https://2.example.com:2480,2=https://3.example.com:2480",
  57. },
  58. {
  59. "http",
  60. srvAll,
  61. nil,
  62. "0=http://1.example.com:2480,1=http://2.example.com:2480,2=http://3.example.com:2480",
  63. },
  64. {
  65. "https",
  66. srvAll,
  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",
  69. },
  70. // matching local member with resolved addr and return unresolved hostnames
  71. {
  72. "https",
  73. srvAll,
  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. "http",
  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. return "", tt.records, nil
  98. }
  99. urls := testutil.MustNewURLs(t, tt.urls)
  100. str, err := GetCluster(tt.scheme, "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. }
  181. func TestGetSRVService(t *testing.T) {
  182. tests := []struct {
  183. scheme string
  184. serviceName string
  185. expected string
  186. }{
  187. {
  188. "https",
  189. "",
  190. "etcd-client-ssl",
  191. },
  192. {
  193. "http",
  194. "",
  195. "etcd-client",
  196. },
  197. {
  198. "https",
  199. "foo",
  200. "etcd-client-ssl-foo",
  201. },
  202. {
  203. "http",
  204. "bar",
  205. "etcd-client-bar",
  206. },
  207. }
  208. for i, tt := range tests {
  209. service := GetSRVService("etcd-client", tt.serviceName, tt.scheme)
  210. if strings.Compare(service, tt.expected) != 0 {
  211. t.Errorf("#%d: service = %s, want %s", i, service, tt.expected)
  212. }
  213. }
  214. }