config_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "io/ioutil"
  17. "os"
  18. "testing"
  19. "github.com/coreos/etcd/pkg/transport"
  20. "github.com/ghodss/yaml"
  21. )
  22. func TestConfigFileOtherFields(t *testing.T) {
  23. ctls := securityConfig{CAFile: "cca", CertFile: "ccert", KeyFile: "ckey"}
  24. ptls := securityConfig{CAFile: "pca", CertFile: "pcert", KeyFile: "pkey"}
  25. yc := struct {
  26. ClientSecurityCfgFile securityConfig `json:"client-transport-security"`
  27. PeerSecurityCfgFile securityConfig `json:"peer-transport-security"`
  28. ForceNewCluster bool `json:"force-new-cluster"`
  29. }{
  30. ctls,
  31. ptls,
  32. true,
  33. }
  34. b, err := yaml.Marshal(&yc)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. tmpfile := mustCreateCfgFile(t, b)
  39. defer os.Remove(tmpfile.Name())
  40. cfg, err := ConfigFromFile(tmpfile.Name())
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if !cfg.ForceNewCluster {
  45. t.Errorf("ForceNewCluster = %v, want %v", cfg.ForceNewCluster, true)
  46. }
  47. if !ctls.equals(&cfg.ClientTLSInfo) {
  48. t.Errorf("ClientTLS = %v, want %v", cfg.ClientTLSInfo, ctls)
  49. }
  50. if !ptls.equals(&cfg.PeerTLSInfo) {
  51. t.Errorf("PeerTLS = %v, want %v", cfg.PeerTLSInfo, ptls)
  52. }
  53. }
  54. func (s *securityConfig) equals(t *transport.TLSInfo) bool {
  55. return s.CAFile == t.CAFile &&
  56. s.CertFile == t.CertFile &&
  57. s.CertAuth == t.ClientCertAuth &&
  58. s.TrustedCAFile == t.TrustedCAFile
  59. }
  60. func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
  61. tmpfile, err := ioutil.TempFile("", "servercfg")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if _, err = tmpfile.Write(b); err != nil {
  66. t.Fatal(err)
  67. }
  68. if err = tmpfile.Close(); err != nil {
  69. t.Fatal(err)
  70. }
  71. return tmpfile
  72. }