srv_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package discovery
  14. import (
  15. "errors"
  16. "net"
  17. "testing"
  18. "github.com/coreos/etcd/pkg/testutil"
  19. )
  20. func TestSRVGetCluster(t *testing.T) {
  21. defer func() { lookupSRV = net.LookupSRV }()
  22. name := "dnsClusterTest"
  23. tests := []struct {
  24. withSSL []*net.SRV
  25. withoutSSL []*net.SRV
  26. urls []string
  27. expected string
  28. }{
  29. {
  30. []*net.SRV{},
  31. []*net.SRV{},
  32. nil,
  33. "",
  34. },
  35. {
  36. []*net.SRV{
  37. &net.SRV{Target: "10.0.0.1", Port: 2480},
  38. &net.SRV{Target: "10.0.0.2", Port: 2480},
  39. &net.SRV{Target: "10.0.0.3", Port: 2480},
  40. },
  41. []*net.SRV{},
  42. nil,
  43. "0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480",
  44. },
  45. {
  46. []*net.SRV{
  47. &net.SRV{Target: "10.0.0.1", Port: 2480},
  48. &net.SRV{Target: "10.0.0.2", Port: 2480},
  49. &net.SRV{Target: "10.0.0.3", Port: 2480},
  50. },
  51. []*net.SRV{
  52. &net.SRV{Target: "10.0.0.1", Port: 7001},
  53. },
  54. nil,
  55. "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",
  56. },
  57. {
  58. []*net.SRV{
  59. &net.SRV{Target: "10.0.0.1", Port: 2480},
  60. &net.SRV{Target: "10.0.0.2", Port: 2480},
  61. &net.SRV{Target: "10.0.0.3", Port: 2480},
  62. },
  63. []*net.SRV{
  64. &net.SRV{Target: "10.0.0.1", Port: 7001},
  65. },
  66. []string{"https://10.0.0.1:2480"},
  67. "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",
  68. },
  69. }
  70. for i, tt := range tests {
  71. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  72. if service == "etcd-server-ssl" {
  73. return "", tt.withSSL, nil
  74. }
  75. if service == "etcd-server" {
  76. return "", tt.withoutSSL, nil
  77. }
  78. return "", nil, errors.New("Unkown service in mock")
  79. }
  80. urls := testutil.MustNewURLs(t, tt.urls)
  81. str, token, err := SRVGetCluster(name, "example.com", "token", urls)
  82. if err != nil {
  83. t.Fatalf("%d: err: %#v", i, err)
  84. }
  85. if token != "token" {
  86. t.Errorf("%d: token: %s", i, token)
  87. }
  88. if str != tt.expected {
  89. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  90. }
  91. }
  92. }