srv_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 discovery
  15. import (
  16. "errors"
  17. "net"
  18. "strings"
  19. "testing"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. )
  22. func TestSRVGetCluster(t *testing.T) {
  23. defer func() {
  24. lookupSRV = net.LookupSRV
  25. resolveTCPAddr = net.ResolveTCPAddr
  26. }()
  27. name := "dnsClusterTest"
  28. dns := map[string]string{
  29. "1.example.com.:2480": "10.0.0.1:2480",
  30. "2.example.com.:2480": "10.0.0.2:2480",
  31. "3.example.com.:2480": "10.0.0.3:2480",
  32. "4.example.com.:2380": "10.0.0.3:2380",
  33. }
  34. srvAll := []*net.SRV{
  35. {Target: "1.example.com.", Port: 2480},
  36. {Target: "2.example.com.", Port: 2480},
  37. {Target: "3.example.com.", Port: 2480},
  38. }
  39. tests := []struct {
  40. withSSL []*net.SRV
  41. withoutSSL []*net.SRV
  42. urls []string
  43. expected string
  44. }{
  45. {
  46. []*net.SRV{},
  47. []*net.SRV{},
  48. nil,
  49. "",
  50. },
  51. {
  52. srvAll,
  53. []*net.SRV{},
  54. nil,
  55. "0=https://1.example.com:2480,1=https://2.example.com:2480,2=https://3.example.com:2480",
  56. },
  57. {
  58. srvAll,
  59. []*net.SRV{{Target: "4.example.com.", Port: 2380}},
  60. nil,
  61. "0=https://1.example.com:2480,1=https://2.example.com:2480,2=https://3.example.com:2480,3=http://4.example.com:2380",
  62. },
  63. {
  64. srvAll,
  65. []*net.SRV{{Target: "4.example.com.", Port: 2380}},
  66. []string{"https://10.0.0.1:2480"},
  67. "dnsClusterTest=https://1.example.com:2480,0=https://2.example.com:2480,1=https://3.example.com:2480,2=http://4.example.com:2380",
  68. },
  69. // matching local member with resolved addr and return unresolved hostnames
  70. {
  71. srvAll,
  72. nil,
  73. []string{"https://10.0.0.1:2480"},
  74. "dnsClusterTest=https://1.example.com:2480,0=https://2.example.com:2480,1=https://3.example.com:2480",
  75. },
  76. // invalid
  77. }
  78. resolveTCPAddr = func(network, addr string) (*net.TCPAddr, error) {
  79. if strings.Contains(addr, "10.0.0.") {
  80. // accept IP addresses when resolving apurls
  81. return net.ResolveTCPAddr(network, addr)
  82. }
  83. if dns[addr] == "" {
  84. return nil, errors.New("missing dns record")
  85. }
  86. return net.ResolveTCPAddr(network, dns[addr])
  87. }
  88. for i, tt := range tests {
  89. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  90. if service == "etcd-server-ssl" {
  91. return "", tt.withSSL, nil
  92. }
  93. if service == "etcd-server" {
  94. return "", tt.withoutSSL, nil
  95. }
  96. return "", nil, errors.New("Unknown service in mock")
  97. }
  98. urls := testutil.MustNewURLs(t, tt.urls)
  99. str, token, err := SRVGetCluster(name, "example.com", "token", urls)
  100. if err != nil {
  101. t.Fatalf("%d: err: %#v", i, err)
  102. }
  103. if token != "token" {
  104. t.Errorf("%d: token: %s", i, token)
  105. }
  106. if str != tt.expected {
  107. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  108. }
  109. }
  110. }