config_test.go 4.4 KB

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