config_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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:12345",
  101. []string{"http://localhost:2380", "http://localhost:12345"},
  102. true,
  103. true,
  104. },
  105. {
  106. // Advertised peer URLs must match those in cluster-state
  107. "node1=http://localhost:2380",
  108. []string{},
  109. true,
  110. true,
  111. },
  112. {
  113. // do not care about the urls if strict is not set
  114. "node1=http://localhost:2380",
  115. []string{},
  116. false,
  117. false,
  118. },
  119. }
  120. for i, tt := range tests {
  121. cluster, err := types.NewURLsMap(tt.clusterSetting)
  122. if err != nil {
  123. t.Fatalf("#%d: Got unexpected error: %v", i, err)
  124. }
  125. cfg := ServerConfig{
  126. Name: "node1",
  127. InitialPeerURLsMap: cluster,
  128. }
  129. if tt.apurls != nil {
  130. cfg.PeerURLs = mustNewURLs(t, tt.apurls)
  131. }
  132. if err = cfg.hasLocalMember(); err == nil && tt.strict {
  133. err = cfg.advertiseMatchesCluster()
  134. }
  135. if (err == nil) && tt.shouldError {
  136. t.Errorf("#%d: Got no error where one was expected", i)
  137. }
  138. if (err != nil) && !tt.shouldError {
  139. t.Errorf("#%d: Got unexpected error: %v", i, err)
  140. }
  141. }
  142. }
  143. func TestSnapDir(t *testing.T) {
  144. tests := map[string]string{
  145. "/": "/member/snap",
  146. "/var/lib/etc": "/var/lib/etc/member/snap",
  147. }
  148. for dd, w := range tests {
  149. cfg := ServerConfig{
  150. DataDir: dd,
  151. }
  152. if g := cfg.SnapDir(); g != w {
  153. t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
  154. }
  155. }
  156. }
  157. func TestWALDir(t *testing.T) {
  158. tests := map[string]string{
  159. "/": "/member/wal",
  160. "/var/lib/etc": "/var/lib/etc/member/wal",
  161. }
  162. for dd, w := range tests {
  163. cfg := ServerConfig{
  164. DataDir: dd,
  165. }
  166. if g := cfg.WALDir(); g != w {
  167. t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
  168. }
  169. }
  170. }
  171. func TestShouldDiscover(t *testing.T) {
  172. tests := map[string]bool{
  173. "": false,
  174. "foo": true,
  175. "http://discovery.etcd.io/asdf": true,
  176. }
  177. for durl, w := range tests {
  178. cfg := ServerConfig{
  179. DiscoveryURL: durl,
  180. }
  181. if g := cfg.ShouldDiscover(); g != w {
  182. t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
  183. }
  184. }
  185. }