config_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2015 The etcd Authors
  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 etcdserver
  15. import (
  16. "net/url"
  17. "testing"
  18. "github.com/coreos/etcd/pkg/types"
  19. )
  20. func mustNewURLs(t *testing.T, urls []string) []url.URL {
  21. if len(urls) == 0 {
  22. return nil
  23. }
  24. u, err := types.NewURLs(urls)
  25. if err != nil {
  26. t.Fatalf("error creating new URLs from %q: %v", urls, err)
  27. }
  28. return u
  29. }
  30. func TestConfigVerifyBootstrapWithoutClusterAndDiscoveryURLFail(t *testing.T) {
  31. c := &ServerConfig{
  32. Name: "node1",
  33. DiscoveryURL: "",
  34. InitialPeerURLsMap: types.URLsMap{},
  35. }
  36. if err := c.VerifyBootstrap(); err == nil {
  37. t.Errorf("err = nil, want not nil")
  38. }
  39. }
  40. func TestConfigVerifyExistingWithDiscoveryURLFail(t *testing.T) {
  41. cluster, err := types.NewURLsMap("node1=http://127.0.0.1:2380")
  42. if err != nil {
  43. t.Fatalf("NewCluster error: %v", err)
  44. }
  45. c := &ServerConfig{
  46. Name: "node1",
  47. DiscoveryURL: "http://127.0.0.1:2379/abcdefg",
  48. PeerURLs: mustNewURLs(t, []string{"http://127.0.0.1:2380"}),
  49. InitialPeerURLsMap: cluster,
  50. NewCluster: false,
  51. }
  52. if err := c.VerifyJoinExisting(); err == nil {
  53. t.Errorf("err = nil, want not nil")
  54. }
  55. }
  56. func TestConfigVerifyLocalMember(t *testing.T) {
  57. tests := []struct {
  58. clusterSetting string
  59. apurls []string
  60. strict bool
  61. shouldError bool
  62. }{
  63. {
  64. // Node must exist in cluster
  65. "",
  66. nil,
  67. true,
  68. true,
  69. },
  70. {
  71. // Initial cluster set
  72. "node1=http://localhost:7001,node2=http://localhost:7002",
  73. []string{"http://localhost:7001"},
  74. true,
  75. false,
  76. },
  77. {
  78. // Default initial cluster
  79. "node1=http://localhost:2380,node1=http://localhost:7001",
  80. []string{"http://localhost:2380", "http://localhost:7001"},
  81. true,
  82. false,
  83. },
  84. {
  85. // Advertised peer URLs must match those in cluster-state
  86. "node1=http://localhost:7001",
  87. []string{"http://localhost:12345"},
  88. true,
  89. true,
  90. },
  91. {
  92. // Advertised peer URLs must match those in cluster-state
  93. "node1=http://localhost:2380,node1=http://localhost:12345",
  94. []string{"http://localhost:12345"},
  95. true,
  96. true,
  97. },
  98. {
  99. // Advertised peer URLs must match those in cluster-state
  100. "node1=http://localhost:2380",
  101. []string{},
  102. true,
  103. true,
  104. },
  105. {
  106. // do not care about the urls if strict is not set
  107. "node1=http://localhost:2380",
  108. []string{},
  109. false,
  110. false,
  111. },
  112. }
  113. for i, tt := range tests {
  114. cluster, err := types.NewURLsMap(tt.clusterSetting)
  115. if err != nil {
  116. t.Fatalf("#%d: Got unexpected error: %v", i, err)
  117. }
  118. cfg := ServerConfig{
  119. Name: "node1",
  120. InitialPeerURLsMap: cluster,
  121. }
  122. if tt.apurls != nil {
  123. cfg.PeerURLs = mustNewURLs(t, tt.apurls)
  124. }
  125. err = cfg.verifyLocalMember(tt.strict)
  126. if (err == nil) && tt.shouldError {
  127. t.Errorf("#%d: Got no error where one was expected", i)
  128. }
  129. if (err != nil) && !tt.shouldError {
  130. t.Errorf("#%d: Got unexpected error: %v", i, err)
  131. }
  132. }
  133. }
  134. func TestSnapDir(t *testing.T) {
  135. tests := map[string]string{
  136. "/": "/member/snap",
  137. "/var/lib/etc": "/var/lib/etc/member/snap",
  138. }
  139. for dd, w := range tests {
  140. cfg := ServerConfig{
  141. DataDir: dd,
  142. }
  143. if g := cfg.SnapDir(); g != w {
  144. t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
  145. }
  146. }
  147. }
  148. func TestWALDir(t *testing.T) {
  149. tests := map[string]string{
  150. "/": "/member/wal",
  151. "/var/lib/etc": "/var/lib/etc/member/wal",
  152. }
  153. for dd, w := range tests {
  154. cfg := ServerConfig{
  155. DataDir: dd,
  156. }
  157. if g := cfg.WALDir(); g != w {
  158. t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
  159. }
  160. }
  161. }
  162. func TestShouldDiscover(t *testing.T) {
  163. tests := map[string]bool{
  164. "": false,
  165. "foo": true,
  166. "http://discovery.etcd.io/asdf": true,
  167. }
  168. for durl, w := range tests {
  169. cfg := ServerConfig{
  170. DiscoveryURL: durl,
  171. }
  172. if g := cfg.ShouldDiscover(); g != w {
  173. t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
  174. }
  175. }
  176. }