srv_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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() { lookupSRV = net.LookupSRV }()
  23. name := "dnsClusterTest"
  24. tests := []struct {
  25. withSSL []*net.SRV
  26. withoutSSL []*net.SRV
  27. urls []string
  28. expected string
  29. }{
  30. {
  31. []*net.SRV{},
  32. []*net.SRV{},
  33. nil,
  34. "",
  35. },
  36. {
  37. []*net.SRV{
  38. &net.SRV{Target: "10.0.0.1", Port: 2480},
  39. &net.SRV{Target: "10.0.0.2", Port: 2480},
  40. &net.SRV{Target: "10.0.0.3", Port: 2480},
  41. },
  42. []*net.SRV{},
  43. nil,
  44. "0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480",
  45. },
  46. {
  47. []*net.SRV{
  48. &net.SRV{Target: "10.0.0.1", Port: 2480},
  49. &net.SRV{Target: "10.0.0.2", Port: 2480},
  50. &net.SRV{Target: "10.0.0.3", Port: 2480},
  51. },
  52. []*net.SRV{
  53. &net.SRV{Target: "10.0.0.1", Port: 2380},
  54. },
  55. nil,
  56. "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:2380",
  57. },
  58. {
  59. []*net.SRV{
  60. &net.SRV{Target: "10.0.0.1", Port: 2480},
  61. &net.SRV{Target: "10.0.0.2", Port: 2480},
  62. &net.SRV{Target: "10.0.0.3", Port: 2480},
  63. },
  64. []*net.SRV{
  65. &net.SRV{Target: "10.0.0.1", Port: 2380},
  66. },
  67. []string{"https://10.0.0.1:2480"},
  68. "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:2380",
  69. },
  70. }
  71. for i, tt := range tests {
  72. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  73. if service == "etcd-server-ssl" {
  74. return "", tt.withSSL, nil
  75. }
  76. if service == "etcd-server" {
  77. return "", tt.withoutSSL, nil
  78. }
  79. return "", nil, errors.New("Unkown service in mock")
  80. }
  81. urls := testutil.MustNewURLs(t, tt.urls)
  82. str, token, err := SRVGetCluster(name, "example.com", "token", urls)
  83. if err != nil {
  84. t.Fatalf("%d: err: %#v", i, err)
  85. }
  86. if token != "token" {
  87. t.Errorf("%d: token: %s", i, token)
  88. }
  89. if str != tt.expected {
  90. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  91. }
  92. }
  93. }