config_test.go 5.8 KB

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