etcd_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. name := "dnsClusterTest"
  56. tests := []struct {
  57. withSSL []*net.SRV
  58. withoutSSL []*net.SRV
  59. urls []string
  60. expected string
  61. }{
  62. {
  63. []*net.SRV{},
  64. []*net.SRV{},
  65. nil,
  66. "",
  67. },
  68. {
  69. []*net.SRV{
  70. &net.SRV{Target: "10.0.0.1", Port: 2480},
  71. &net.SRV{Target: "10.0.0.2", Port: 2480},
  72. &net.SRV{Target: "10.0.0.3", Port: 2480},
  73. },
  74. []*net.SRV{},
  75. nil,
  76. "0=https://10.0.0.1:2480,1=https://10.0.0.2:2480,2=https://10.0.0.3:2480",
  77. },
  78. {
  79. []*net.SRV{
  80. &net.SRV{Target: "10.0.0.1", Port: 2480},
  81. &net.SRV{Target: "10.0.0.2", Port: 2480},
  82. &net.SRV{Target: "10.0.0.3", Port: 2480},
  83. },
  84. []*net.SRV{
  85. &net.SRV{Target: "10.0.0.1", Port: 7001},
  86. },
  87. nil,
  88. "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",
  89. },
  90. {
  91. []*net.SRV{
  92. &net.SRV{Target: "10.0.0.1", Port: 2480},
  93. &net.SRV{Target: "10.0.0.2", Port: 2480},
  94. &net.SRV{Target: "10.0.0.3", Port: 2480},
  95. },
  96. []*net.SRV{
  97. &net.SRV{Target: "10.0.0.1", Port: 7001},
  98. },
  99. []string{"https://10.0.0.1:2480"},
  100. "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",
  101. },
  102. }
  103. for i, tt := range tests {
  104. lookupSRV = func(service string, proto string, domain string) (string, []*net.SRV, error) {
  105. if service == "etcd-server-ssl" {
  106. return "", tt.withSSL, nil
  107. }
  108. if service == "etcd-server" {
  109. return "", tt.withoutSSL, nil
  110. }
  111. return "", nil, errors.New("Unkown service in mock")
  112. }
  113. defer func() { lookupSRV = net.LookupSRV }()
  114. urls := mustNewURLs(t, tt.urls)
  115. str, token, err := genDNSClusterString(name, "example.com", "token", urls)
  116. if err != nil {
  117. t.Fatalf("%d: err: %#v", i, err)
  118. }
  119. if token != "token" {
  120. t.Errorf("%d: token: %s", i, token)
  121. }
  122. if str != tt.expected {
  123. t.Errorf("#%d: cluster = %s, want %s", i, str, tt.expected)
  124. }
  125. }
  126. }