config_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. if err = cfg.hasLocalMember(); err == nil && tt.strict {
  126. err = cfg.advertiseMatchesCluster()
  127. }
  128. if (err == nil) && tt.shouldError {
  129. t.Errorf("#%d: Got no error where one was expected", i)
  130. }
  131. if (err != nil) && !tt.shouldError {
  132. t.Errorf("#%d: Got unexpected error: %v", i, err)
  133. }
  134. }
  135. }
  136. func TestSnapDir(t *testing.T) {
  137. tests := map[string]string{
  138. "/": "/member/snap",
  139. "/var/lib/etc": "/var/lib/etc/member/snap",
  140. }
  141. for dd, w := range tests {
  142. cfg := ServerConfig{
  143. DataDir: dd,
  144. }
  145. if g := cfg.SnapDir(); g != w {
  146. t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
  147. }
  148. }
  149. }
  150. func TestWALDir(t *testing.T) {
  151. tests := map[string]string{
  152. "/": "/member/wal",
  153. "/var/lib/etc": "/var/lib/etc/member/wal",
  154. }
  155. for dd, w := range tests {
  156. cfg := ServerConfig{
  157. DataDir: dd,
  158. }
  159. if g := cfg.WALDir(); g != w {
  160. t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
  161. }
  162. }
  163. }
  164. func TestShouldDiscover(t *testing.T) {
  165. tests := map[string]bool{
  166. "": false,
  167. "foo": true,
  168. "http://discovery.etcd.io/asdf": true,
  169. }
  170. for durl, w := range tests {
  171. cfg := ServerConfig{
  172. DiscoveryURL: durl,
  173. }
  174. if g := cfg.ShouldDiscover(); g != w {
  175. t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
  176. }
  177. }
  178. }