config_test.go 5.0 KB

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