config_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 yaml
  15. import (
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "reflect"
  20. "testing"
  21. "sigs.k8s.io/yaml"
  22. )
  23. var (
  24. certPath = "../../integration/fixtures/server.crt"
  25. privateKeyPath = "../../integration/fixtures/server.key.insecure"
  26. caPath = "../../integration/fixtures/ca.crt"
  27. )
  28. func TestConfigFromFile(t *testing.T) {
  29. tests := []struct {
  30. ym *yamlConfig
  31. werr bool
  32. }{
  33. {
  34. &yamlConfig{},
  35. false,
  36. },
  37. {
  38. &yamlConfig{
  39. InsecureTransport: true,
  40. },
  41. false,
  42. },
  43. {
  44. &yamlConfig{
  45. Keyfile: privateKeyPath,
  46. Certfile: certPath,
  47. TrustedCAfile: caPath,
  48. InsecureSkipTLSVerify: true,
  49. },
  50. false,
  51. },
  52. {
  53. &yamlConfig{
  54. Keyfile: "bad",
  55. Certfile: "bad",
  56. },
  57. true,
  58. },
  59. {
  60. &yamlConfig{
  61. Keyfile: privateKeyPath,
  62. Certfile: certPath,
  63. TrustedCAfile: "bad",
  64. },
  65. true,
  66. },
  67. }
  68. for i, tt := range tests {
  69. tmpfile, err := ioutil.TempFile("", "clientcfg")
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. b, err := yaml.Marshal(tt.ym)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. _, err = tmpfile.Write(b)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. err = tmpfile.Close()
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. cfg, cerr := NewConfig(tmpfile.Name())
  86. if cerr != nil && !tt.werr {
  87. t.Errorf("#%d: err = %v, want %v", i, cerr, tt.werr)
  88. continue
  89. }
  90. if cerr != nil {
  91. continue
  92. }
  93. if !reflect.DeepEqual(cfg.Endpoints, tt.ym.Endpoints) {
  94. t.Errorf("#%d: endpoint = %v, want %v", i, cfg.Endpoints, tt.ym.Endpoints)
  95. }
  96. if tt.ym.InsecureTransport != (cfg.TLS == nil) {
  97. t.Errorf("#%d: insecureTransport = %v, want %v", i, cfg.TLS == nil, tt.ym.InsecureTransport)
  98. }
  99. if !tt.ym.InsecureTransport {
  100. if tt.ym.Certfile != "" && len(cfg.TLS.Certificates) == 0 {
  101. t.Errorf("#%d: failed to load in cert", i)
  102. }
  103. if tt.ym.TrustedCAfile != "" && cfg.TLS.RootCAs == nil {
  104. t.Errorf("#%d: failed to load in ca cert", i)
  105. }
  106. if cfg.TLS.InsecureSkipVerify != tt.ym.InsecureSkipTLSVerify {
  107. t.Errorf("#%d: skipTLSVeify = %v, want %v", i, cfg.TLS.InsecureSkipVerify, tt.ym.InsecureSkipTLSVerify)
  108. }
  109. }
  110. os.Remove(tmpfile.Name())
  111. }
  112. }