config_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. u, err := types.NewURLs(urls)
  22. if err != nil {
  23. t.Fatalf("error creating new URLs from %q: %v", urls, err)
  24. }
  25. return u
  26. }
  27. func TestBootstrapConfigVerify(t *testing.T) {
  28. tests := []struct {
  29. clusterSetting string
  30. newclst bool
  31. apurls []string
  32. disc string
  33. shouldError bool
  34. }{
  35. {
  36. // Node must exist in cluster
  37. "",
  38. true,
  39. nil,
  40. "",
  41. true,
  42. },
  43. {
  44. // Cannot have duplicate URLs in cluster config
  45. "node1=http://localhost:7001,node2=http://localhost:7001,node2=http://localhost:7002",
  46. true,
  47. nil,
  48. "",
  49. true,
  50. },
  51. {
  52. // Node defined, ClusterState OK
  53. "node1=http://localhost:7001,node2=http://localhost:7002",
  54. true,
  55. []string{"http://localhost:7001"},
  56. "",
  57. false,
  58. },
  59. {
  60. // Node defined, discovery OK
  61. "node1=http://localhost:7001",
  62. false,
  63. []string{"http://localhost:7001"},
  64. "http://discovery",
  65. false,
  66. },
  67. {
  68. // Cannot have ClusterState!=new && !discovery
  69. "node1=http://localhost:7001",
  70. false,
  71. nil,
  72. "",
  73. true,
  74. },
  75. {
  76. // Advertised peer URLs must match those in cluster-state
  77. "node1=http://localhost:7001",
  78. true,
  79. []string{"http://localhost:12345"},
  80. "",
  81. true,
  82. },
  83. {
  84. // Advertised peer URLs must match those in cluster-state
  85. "node1=http://localhost:7001,node1=http://localhost:12345",
  86. true,
  87. []string{"http://localhost:12345"},
  88. "",
  89. true,
  90. },
  91. }
  92. for i, tt := range tests {
  93. cluster, err := NewClusterFromString("", tt.clusterSetting)
  94. if err != nil {
  95. t.Fatalf("#%d: Got unexpected error: %v", i, err)
  96. }
  97. cfg := ServerConfig{
  98. Name: "node1",
  99. DiscoveryURL: tt.disc,
  100. Cluster: cluster,
  101. NewCluster: tt.newclst,
  102. }
  103. if tt.apurls != nil {
  104. cfg.PeerURLs = mustNewURLs(t, tt.apurls)
  105. }
  106. err = cfg.VerifyBootstrapConfig()
  107. if (err == nil) && tt.shouldError {
  108. t.Errorf("%#v", *cluster)
  109. t.Errorf("#%d: Got no error where one was expected", i)
  110. }
  111. if (err != nil) && !tt.shouldError {
  112. t.Errorf("#%d: Got unexpected error: %v", i, err)
  113. }
  114. }
  115. }
  116. func TestSnapDir(t *testing.T) {
  117. tests := map[string]string{
  118. "/": "/member/snap",
  119. "/var/lib/etc": "/var/lib/etc/member/snap",
  120. }
  121. for dd, w := range tests {
  122. cfg := ServerConfig{
  123. DataDir: dd,
  124. }
  125. if g := cfg.SnapDir(); g != w {
  126. t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
  127. }
  128. }
  129. }
  130. func TestWALDir(t *testing.T) {
  131. tests := map[string]string{
  132. "/": "/member/wal",
  133. "/var/lib/etc": "/var/lib/etc/member/wal",
  134. }
  135. for dd, w := range tests {
  136. cfg := ServerConfig{
  137. DataDir: dd,
  138. }
  139. if g := cfg.WALDir(); g != w {
  140. t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
  141. }
  142. }
  143. }
  144. func TestShouldDiscover(t *testing.T) {
  145. tests := map[string]bool{
  146. "": false,
  147. "foo": true,
  148. "http://discovery.etcd.io/asdf": true,
  149. }
  150. for durl, w := range tests {
  151. cfg := ServerConfig{
  152. DiscoveryURL: durl,
  153. }
  154. if g := cfg.ShouldDiscover(); g != w {
  155. t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
  156. }
  157. }
  158. }