srv_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 client
  15. import (
  16. "errors"
  17. "net"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestSRVDiscover(t *testing.T) {
  22. defer func() { lookupSRV = net.LookupSRV }()
  23. tests := []struct {
  24. withSSL []*net.SRV
  25. withoutSSL []*net.SRV
  26. expected []string
  27. }{
  28. {
  29. []*net.SRV{},
  30. []*net.SRV{},
  31. []string{},
  32. },
  33. {
  34. []*net.SRV{
  35. &net.SRV{Target: "10.0.0.1", Port: 2480},
  36. &net.SRV{Target: "10.0.0.2", Port: 2480},
  37. &net.SRV{Target: "10.0.0.3", Port: 2480},
  38. },
  39. []*net.SRV{},
  40. []string{"https://10.0.0.1:2480", "https://10.0.0.2:2480", "https://10.0.0.3:2480"},
  41. },
  42. {
  43. []*net.SRV{
  44. &net.SRV{Target: "10.0.0.1", Port: 2480},
  45. &net.SRV{Target: "10.0.0.2", Port: 2480},
  46. &net.SRV{Target: "10.0.0.3", Port: 2480},
  47. },
  48. []*net.SRV{
  49. &net.SRV{Target: "10.0.0.1", Port: 7001},
  50. },
  51. []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"},
  52. },
  53. {
  54. []*net.SRV{
  55. &net.SRV{Target: "10.0.0.1", Port: 2480},
  56. &net.SRV{Target: "10.0.0.2", Port: 2480},
  57. &net.SRV{Target: "10.0.0.3", Port: 2480},
  58. },
  59. []*net.SRV{
  60. &net.SRV{Target: "10.0.0.1", Port: 7001},
  61. },
  62. []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"},
  63. },
  64. {
  65. []*net.SRV{
  66. &net.SRV{Target: "a.example.com", Port: 2480},
  67. &net.SRV{Target: "b.example.com", Port: 2480},
  68. &net.SRV{Target: "c.example.com", Port: 2480},
  69. },
  70. []*net.SRV{},
  71. []string{"https://a.example.com:2480", "https://b.example.com:2480", "https://c.example.com:2480"},
  72. },
  73. }
  74. for i, tt := range tests {
  75. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  76. if service == "etcd-server-ssl" {
  77. return "", tt.withSSL, nil
  78. }
  79. if service == "etcd-server" {
  80. return "", tt.withoutSSL, nil
  81. }
  82. return "", nil, errors.New("Unkown service in mock")
  83. }
  84. d := NewSRVDiscover()
  85. endpoints, err := d.Discover("example.com")
  86. if err != nil {
  87. t.Fatalf("%d: err: %#v", i, err)
  88. }
  89. if !reflect.DeepEqual(endpoints, tt.expected) {
  90. t.Errorf("#%d: endpoints = %v, want %v", i, endpoints, tt.expected)
  91. }
  92. }
  93. }