srv_test.go 3.4 KB

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