config_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "net/url"
  16. "testing"
  17. "github.com/coreos/etcd/pkg/types"
  18. )
  19. func mustNewURLs(t *testing.T, urls []string) []url.URL {
  20. u, err := types.NewURLs(urls)
  21. if err != nil {
  22. t.Fatalf("error creating new URLs from %q: %v", urls, err)
  23. }
  24. return u
  25. }
  26. func TestBootstrapConfigVerify(t *testing.T) {
  27. tests := []struct {
  28. clusterSetting string
  29. newclst bool
  30. apurls []string
  31. disc string
  32. shouldError bool
  33. }{
  34. {
  35. // Node must exist in cluster
  36. "",
  37. true,
  38. nil,
  39. "",
  40. true,
  41. },
  42. {
  43. // Cannot have duplicate URLs in cluster config
  44. "node1=http://localhost:7001,node2=http://localhost:7001,node2=http://localhost:7002",
  45. true,
  46. nil,
  47. "",
  48. true,
  49. },
  50. {
  51. // Node defined, ClusterState OK
  52. "node1=http://localhost:7001,node2=http://localhost:7002",
  53. true,
  54. []string{"http://localhost:7001"},
  55. "",
  56. false,
  57. },
  58. {
  59. // Node defined, discovery OK
  60. "node1=http://localhost:7001",
  61. false,
  62. []string{"http://localhost:7001"},
  63. "http://discovery",
  64. false,
  65. },
  66. {
  67. // Cannot have ClusterState!=new && !discovery
  68. "node1=http://localhost:7001",
  69. false,
  70. nil,
  71. "",
  72. true,
  73. },
  74. {
  75. // Advertised peer URLs must match those in cluster-state
  76. "node1=http://localhost:7001",
  77. true,
  78. []string{"http://localhost:12345"},
  79. "",
  80. true,
  81. },
  82. {
  83. // Advertised peer URLs must match those in cluster-state
  84. "node1=http://localhost:7001,node1=http://localhost:12345",
  85. true,
  86. []string{"http://localhost:12345"},
  87. "",
  88. true,
  89. },
  90. }
  91. for i, tt := range tests {
  92. cluster, err := NewClusterFromString("", tt.clusterSetting)
  93. if err != nil {
  94. t.Fatalf("#%d: Got unexpected error: %v", i, err)
  95. }
  96. cfg := ServerConfig{
  97. Name: "node1",
  98. DiscoveryURL: tt.disc,
  99. Cluster: cluster,
  100. NewCluster: tt.newclst,
  101. }
  102. if tt.apurls != nil {
  103. cfg.PeerURLs = mustNewURLs(t, tt.apurls)
  104. }
  105. err = cfg.VerifyBootstrapConfig()
  106. if (err == nil) && tt.shouldError {
  107. t.Errorf("%#v", *cluster)
  108. t.Errorf("#%d: Got no error where one was expected", i)
  109. }
  110. if (err != nil) && !tt.shouldError {
  111. t.Errorf("#%d: Got unexpected error: %v", i, err)
  112. }
  113. }
  114. }
  115. func TestSnapDir(t *testing.T) {
  116. tests := map[string]string{
  117. "/": "/snap",
  118. "/var/lib/etc": "/var/lib/etc/snap",
  119. }
  120. for dd, w := range tests {
  121. cfg := ServerConfig{
  122. DataDir: dd,
  123. }
  124. if g := cfg.SnapDir(); g != w {
  125. t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
  126. }
  127. }
  128. }
  129. func TestWALDir(t *testing.T) {
  130. tests := map[string]string{
  131. "/": "/wal",
  132. "/var/lib/etc": "/var/lib/etc/wal",
  133. }
  134. for dd, w := range tests {
  135. cfg := ServerConfig{
  136. DataDir: dd,
  137. }
  138. if g := cfg.WALDir(); g != w {
  139. t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
  140. }
  141. }
  142. }
  143. func TestShouldDiscover(t *testing.T) {
  144. tests := map[string]bool{
  145. "": false,
  146. "foo": true,
  147. "http://discovery.etcd.io/asdf": true,
  148. }
  149. for durl, w := range tests {
  150. cfg := ServerConfig{
  151. DiscoveryURL: durl,
  152. }
  153. if g := cfg.ShouldDiscover(); g != w {
  154. t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
  155. }
  156. }
  157. }