config_test.go 4.6 KB

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