config_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2016 The etcd Authors
  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 embed
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net/url"
  19. "os"
  20. "testing"
  21. "github.com/coreos/etcd/pkg/transport"
  22. "github.com/ghodss/yaml"
  23. )
  24. func TestConfigFileOtherFields(t *testing.T) {
  25. ctls := securityConfig{TrustedCAFile: "cca", CertFile: "ccert", KeyFile: "ckey"}
  26. ptls := securityConfig{TrustedCAFile: "pca", CertFile: "pcert", KeyFile: "pkey"}
  27. yc := struct {
  28. ClientSecurityCfgFile securityConfig `json:"client-transport-security"`
  29. PeerSecurityCfgFile securityConfig `json:"peer-transport-security"`
  30. ForceNewCluster bool `json:"force-new-cluster"`
  31. Logger string `json:"logger"`
  32. LogOutputs []string `json:"log-outputs"`
  33. Debug bool `json:"debug"`
  34. }{
  35. ctls,
  36. ptls,
  37. true,
  38. "zap",
  39. []string{"/dev/null"},
  40. false,
  41. }
  42. b, err := yaml.Marshal(&yc)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. tmpfile := mustCreateCfgFile(t, b)
  47. defer os.Remove(tmpfile.Name())
  48. cfg, err := ConfigFromFile(tmpfile.Name())
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if !cfg.ForceNewCluster {
  53. t.Errorf("ForceNewCluster = %v, want %v", cfg.ForceNewCluster, true)
  54. }
  55. if !ctls.equals(&cfg.ClientTLSInfo) {
  56. t.Errorf("ClientTLS = %v, want %v", cfg.ClientTLSInfo, ctls)
  57. }
  58. if !ptls.equals(&cfg.PeerTLSInfo) {
  59. t.Errorf("PeerTLS = %v, want %v", cfg.PeerTLSInfo, ptls)
  60. }
  61. }
  62. // TestUpdateDefaultClusterFromName ensures that etcd can start with 'etcd --name=abc'.
  63. func TestUpdateDefaultClusterFromName(t *testing.T) {
  64. cfg := NewConfig()
  65. defaultInitialCluster := cfg.InitialCluster
  66. oldscheme := cfg.APUrls[0].Scheme
  67. origpeer := cfg.APUrls[0].String()
  68. origadvc := cfg.ACUrls[0].String()
  69. cfg.Name = "abc"
  70. lpport := cfg.LPUrls[0].Port()
  71. // in case of 'etcd --name=abc'
  72. exp := fmt.Sprintf("%s=%s://localhost:%s", cfg.Name, oldscheme, lpport)
  73. cfg.UpdateDefaultClusterFromName(defaultInitialCluster)
  74. if exp != cfg.InitialCluster {
  75. t.Fatalf("initial-cluster expected %q, got %q", exp, cfg.InitialCluster)
  76. }
  77. // advertise peer URL should not be affected
  78. if origpeer != cfg.APUrls[0].String() {
  79. t.Fatalf("advertise peer url expected %q, got %q", origadvc, cfg.APUrls[0].String())
  80. }
  81. // advertise client URL should not be affected
  82. if origadvc != cfg.ACUrls[0].String() {
  83. t.Fatalf("advertise client url expected %q, got %q", origadvc, cfg.ACUrls[0].String())
  84. }
  85. }
  86. // TestUpdateDefaultClusterFromNameOverwrite ensures that machine's default host is only used
  87. // if advertise URLs are default values(localhost:2379,2380) AND if listen URL is 0.0.0.0.
  88. func TestUpdateDefaultClusterFromNameOverwrite(t *testing.T) {
  89. if defaultHostname == "" {
  90. t.Skip("machine's default host not found")
  91. }
  92. cfg := NewConfig()
  93. defaultInitialCluster := cfg.InitialCluster
  94. oldscheme := cfg.APUrls[0].Scheme
  95. origadvc := cfg.ACUrls[0].String()
  96. cfg.Name = "abc"
  97. lpport := cfg.LPUrls[0].Port()
  98. cfg.LPUrls[0] = url.URL{Scheme: cfg.LPUrls[0].Scheme, Host: fmt.Sprintf("0.0.0.0:%s", lpport)}
  99. dhost, _ := cfg.UpdateDefaultClusterFromName(defaultInitialCluster)
  100. if dhost != defaultHostname {
  101. t.Fatalf("expected default host %q, got %q", defaultHostname, dhost)
  102. }
  103. aphost, apport := cfg.APUrls[0].Hostname(), cfg.APUrls[0].Port()
  104. if apport != lpport {
  105. t.Fatalf("advertise peer url got different port %s, expected %s", apport, lpport)
  106. }
  107. if aphost != defaultHostname {
  108. t.Fatalf("advertise peer url expected machine default host %q, got %q", defaultHostname, aphost)
  109. }
  110. expected := fmt.Sprintf("%s=%s://%s:%s", cfg.Name, oldscheme, defaultHostname, lpport)
  111. if expected != cfg.InitialCluster {
  112. t.Fatalf("initial-cluster expected %q, got %q", expected, cfg.InitialCluster)
  113. }
  114. // advertise client URL should not be affected
  115. if origadvc != cfg.ACUrls[0].String() {
  116. t.Fatalf("advertise-client-url expected %q, got %q", origadvc, cfg.ACUrls[0].String())
  117. }
  118. }
  119. func (s *securityConfig) equals(t *transport.TLSInfo) bool {
  120. return s.CertFile == t.CertFile &&
  121. s.CertAuth == t.ClientCertAuth &&
  122. s.TrustedCAFile == t.TrustedCAFile
  123. }
  124. func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
  125. tmpfile, err := ioutil.TempFile("", "servercfg")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. if _, err = tmpfile.Write(b); err != nil {
  130. t.Fatal(err)
  131. }
  132. if err = tmpfile.Close(); err != nil {
  133. t.Fatal(err)
  134. }
  135. return tmpfile
  136. }
  137. func TestAutoCompactionModeInvalid(t *testing.T) {
  138. cfg := NewConfig()
  139. cfg.Logger = "zap"
  140. cfg.LogOutputs = []string{"/dev/null"}
  141. cfg.Debug = false
  142. cfg.AutoCompactionMode = "period"
  143. err := cfg.Validate()
  144. if err == nil {
  145. t.Errorf("expected non-nil error, got %v", err)
  146. }
  147. }
  148. func TestAutoCompactionModeParse(t *testing.T) {
  149. dur, err := parseCompactionRetention("revision", "1")
  150. if err != nil {
  151. t.Error(err)
  152. }
  153. if dur != 1 {
  154. t.Fatalf("AutoCompactionRetention expected 1, got %d", dur)
  155. }
  156. }