config_test.go 4.1 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 TestConfigVerifyBootstrapWithoutClusterAndDiscoveryURLFail(t *testing.T) {
  28. cluster, err := NewClusterFromString("", "")
  29. if err != nil {
  30. t.Fatalf("NewClusterFromString error: %v", err)
  31. }
  32. c := &ServerConfig{
  33. Name: "node1",
  34. DiscoveryURL: "",
  35. Cluster: cluster,
  36. }
  37. if err := c.VerifyBootstrap(); err == nil {
  38. t.Errorf("err = nil, want not nil")
  39. }
  40. }
  41. func TestConfigVerifyExistingWithDiscoveryURLFail(t *testing.T) {
  42. cluster, err := NewClusterFromString("", "node1=http://127.0.0.1:2380")
  43. if err != nil {
  44. t.Fatalf("NewClusterFromString error: %v", err)
  45. }
  46. c := &ServerConfig{
  47. Name: "node1",
  48. DiscoveryURL: "http://127.0.0.1:4001/abcdefg",
  49. PeerURLs: mustNewURLs(t, []string{"http://127.0.0.1:2380"}),
  50. Cluster: cluster,
  51. NewCluster: false,
  52. }
  53. if err := c.VerifyJoinExisting(); err == nil {
  54. t.Errorf("err = nil, want not nil")
  55. }
  56. }
  57. func TestConfigVerifyLocalMember(t *testing.T) {
  58. tests := []struct {
  59. clusterSetting string
  60. apurls []string
  61. shouldError bool
  62. }{
  63. {
  64. // Node must exist in cluster
  65. "",
  66. nil,
  67. true,
  68. },
  69. {
  70. // Initial cluster set
  71. "node1=http://localhost:7001,node2=http://localhost:7002",
  72. []string{"http://localhost:7001"},
  73. false,
  74. },
  75. {
  76. // Default initial cluster
  77. "node1=http://localhost:2380,node1=http://localhost:7001",
  78. []string{"http://localhost:2380", "http://localhost:7001"},
  79. false,
  80. },
  81. {
  82. // Advertised peer URLs must match those in cluster-state
  83. "node1=http://localhost:7001",
  84. []string{"http://localhost:12345"},
  85. true,
  86. },
  87. {
  88. // Advertised peer URLs must match those in cluster-state
  89. "node1=http://localhost:7001,node1=http://localhost:12345",
  90. []string{"http://localhost:12345"},
  91. true,
  92. },
  93. }
  94. for i, tt := range tests {
  95. cluster, err := NewClusterFromString("", tt.clusterSetting)
  96. if err != nil {
  97. t.Fatalf("#%d: Got unexpected error: %v", i, err)
  98. }
  99. cfg := ServerConfig{
  100. Name: "node1",
  101. Cluster: cluster,
  102. }
  103. if tt.apurls != nil {
  104. cfg.PeerURLs = mustNewURLs(t, tt.apurls)
  105. }
  106. err = cfg.verifyLocalMember()
  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. }