config_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "testing"
  15. func TestBootstrapConfigVerify(t *testing.T) {
  16. tests := []struct {
  17. clusterSetting string
  18. clst ClusterState
  19. disc string
  20. shouldError bool
  21. }{
  22. {
  23. // Node must exist in cluster
  24. "",
  25. ClusterStateValueNew,
  26. "",
  27. true,
  28. },
  29. {
  30. // Cannot have duplicate URLs in cluster config
  31. "node1=http://localhost:7001,node2=http://localhost:7001,node2=http://localhost:7002",
  32. ClusterStateValueNew,
  33. "",
  34. true,
  35. },
  36. {
  37. // Node defined, ClusterState OK
  38. "node1=http://localhost:7001,node2=http://localhost:7002",
  39. ClusterStateValueNew,
  40. "",
  41. false,
  42. },
  43. {
  44. // Node defined, discovery OK
  45. "node1=http://localhost:7001",
  46. // TODO(jonboulle): replace with ClusterStateExisting once it exists
  47. "",
  48. "http://discovery",
  49. false,
  50. },
  51. {
  52. // Cannot have ClusterState!=new && !discovery
  53. "node1=http://localhost:7001",
  54. // TODO(jonboulle): replace with ClusterStateExisting once it exists
  55. ClusterState("foo"),
  56. "",
  57. true,
  58. },
  59. }
  60. for i, tt := range tests {
  61. cluster, err := NewClusterFromString("", tt.clusterSetting)
  62. if err != nil {
  63. t.Fatalf("#%d: Got unexpected error: %v", i, err)
  64. }
  65. cfg := ServerConfig{
  66. Name: "node1",
  67. DiscoveryURL: tt.disc,
  68. Cluster: cluster,
  69. ClusterState: tt.clst,
  70. }
  71. err = cfg.VerifyBootstrapConfig()
  72. if (err == nil) && tt.shouldError {
  73. t.Errorf("%#v", *cluster)
  74. t.Errorf("#%d: Got no error where one was expected", i)
  75. }
  76. if (err != nil) && !tt.shouldError {
  77. t.Errorf("#%d: Got unexpected error: %v", i, err)
  78. }
  79. }
  80. }
  81. func TestSnapDir(t *testing.T) {
  82. tests := map[string]string{
  83. "/": "/snap",
  84. "/var/lib/etc": "/var/lib/etc/snap",
  85. }
  86. for dd, w := range tests {
  87. cfg := ServerConfig{
  88. DataDir: dd,
  89. }
  90. if g := cfg.SnapDir(); g != w {
  91. t.Errorf("DataDir=%q: SnapDir()=%q, want=%q", dd, g, w)
  92. }
  93. }
  94. }
  95. func TestWALDir(t *testing.T) {
  96. tests := map[string]string{
  97. "/": "/wal",
  98. "/var/lib/etc": "/var/lib/etc/wal",
  99. }
  100. for dd, w := range tests {
  101. cfg := ServerConfig{
  102. DataDir: dd,
  103. }
  104. if g := cfg.WALDir(); g != w {
  105. t.Errorf("DataDir=%q: WALDir()=%q, want=%q", dd, g, w)
  106. }
  107. }
  108. }
  109. func TestShouldDiscover(t *testing.T) {
  110. tests := map[string]bool{
  111. "": false,
  112. "foo": true,
  113. "http://discovery.etcd.io/asdf": true,
  114. }
  115. for durl, w := range tests {
  116. cfg := ServerConfig{
  117. DiscoveryURL: durl,
  118. }
  119. if g := cfg.ShouldDiscover(); g != w {
  120. t.Errorf("durl=%q: ShouldDiscover()=%t, want=%t", durl, g, w)
  121. }
  122. }
  123. }