etcd_test.go 3.4 KB

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