etcd_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 etcdmain
  14. import (
  15. "errors"
  16. "net"
  17. "net/url"
  18. "testing"
  19. "github.com/coreos/etcd/pkg/types"
  20. )
  21. func mustNewURLs(t *testing.T, urls []string) []url.URL {
  22. u, err := types.NewURLs(urls)
  23. if err != nil {
  24. t.Fatalf("unexpected new urls error: %v", err)
  25. }
  26. return u
  27. }
  28. func TestGenClusterString(t *testing.T) {
  29. tests := []struct {
  30. token string
  31. urls []string
  32. wstr string
  33. }{
  34. {
  35. "default", []string{"http://127.0.0.1:4001"},
  36. "default=http://127.0.0.1:4001",
  37. },
  38. {
  39. "node1", []string{"http://0.0.0.0:2379", "http://1.1.1.1:2379"},
  40. "node1=http://0.0.0.0:2379,node1=http://1.1.1.1:2379",
  41. },
  42. }
  43. for i, tt := range tests {
  44. urls := mustNewURLs(t, tt.urls)
  45. str := genClusterString(tt.token, urls)
  46. if str != tt.wstr {
  47. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.wstr)
  48. }
  49. }
  50. }
  51. func TestGenDNSClusterString(t *testing.T) {
  52. tests := []struct {
  53. withSSL []*net.SRV
  54. withoutSSL []*net.SRV
  55. expected string
  56. }{
  57. {
  58. []*net.SRV{},
  59. []*net.SRV{},
  60. "",
  61. },
  62. {
  63. []*net.SRV{
  64. &net.SRV{Target: "10.0.0.1", Port: 2480},
  65. &net.SRV{Target: "10.0.0.2", Port: 2480},
  66. &net.SRV{Target: "10.0.0.3", Port: 2480},
  67. },
  68. []*net.SRV{},
  69. "0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480",
  70. },
  71. {
  72. []*net.SRV{
  73. &net.SRV{Target: "10.0.0.1", Port: 2480},
  74. &net.SRV{Target: "10.0.0.2", Port: 2480},
  75. &net.SRV{Target: "10.0.0.3", Port: 2480},
  76. },
  77. []*net.SRV{
  78. &net.SRV{Target: "10.0.0.1", Port: 7001},
  79. },
  80. "0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480,0=http://10.0.0.1:7001",
  81. },
  82. }
  83. for i, tt := range tests {
  84. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  85. if service == "etcd-server-ssl" {
  86. return "", tt.withSSL, nil
  87. }
  88. if service == "etcd-server" {
  89. return "", tt.withoutSSL, nil
  90. }
  91. return "", nil, errors.New("Unkown service in mock")
  92. }
  93. defer func() { lookupSRV = net.LookupSRV }()
  94. str, token, err := genDNSClusterString("token")
  95. if err != nil {
  96. t.Fatalf("%d: err: %#v", i, err)
  97. }
  98. if token != "token" {
  99. t.Errorf("%d: token: %s", i, token)
  100. }
  101. if str != tt.expected {
  102. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  103. }
  104. }
  105. }