config_test.go 4.9 KB

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