joinconfiguration_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "bytes"
  16. "io/ioutil"
  17. "testing"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  20. "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
  21. kubeadmapiv1beta1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1"
  22. kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
  23. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  24. )
  25. const (
  26. nodeV1beta1YAML = "testdata/conversion/node/v1beta1.yaml"
  27. nodeV1beta2YAML = "testdata/conversion/node/v1beta2.yaml"
  28. nodeInternalYAML = "testdata/conversion/node/internal.yaml"
  29. nodeIncompleteYAML = "testdata/defaulting/node/incomplete.yaml"
  30. nodeDefaultedYAML = "testdata/defaulting/node/defaulted.yaml"
  31. nodeInvalidYAML = "testdata/validation/invalid_nodecfg.yaml"
  32. )
  33. func TestLoadJoinConfigurationFromFile(t *testing.T) {
  34. var tests = []struct {
  35. name, in, out string
  36. groupVersion schema.GroupVersion
  37. expectedErr bool
  38. }{
  39. // These tests are reading one file, loading it using LoadJoinConfigurationFromFile that all of kubeadm is using for unmarshal of our API types,
  40. // and then marshals the internal object to the expected groupVersion
  41. { // v1beta1 -> internal
  42. name: "v1beta1ToInternal",
  43. in: nodeV1beta1YAML,
  44. out: nodeInternalYAML,
  45. groupVersion: kubeadm.SchemeGroupVersion,
  46. },
  47. { // v1beta1 -> internal -> v1beta1
  48. name: "v1beta1Tov1beta1",
  49. in: nodeV1beta1YAML,
  50. out: nodeV1beta1YAML,
  51. groupVersion: kubeadmapiv1beta1.SchemeGroupVersion,
  52. },
  53. { // v1beta2 -> internal
  54. name: "v1beta2ToInternal",
  55. in: nodeV1beta2YAML,
  56. out: nodeInternalYAML,
  57. groupVersion: kubeadm.SchemeGroupVersion,
  58. },
  59. { // v1beta2 -> internal -> v1beta2
  60. name: "v1beta2Tov1beta2",
  61. in: nodeV1beta2YAML,
  62. out: nodeV1beta2YAML,
  63. groupVersion: kubeadmapiv1beta2.SchemeGroupVersion,
  64. },
  65. // These tests are reading one file that has only a subset of the fields populated, loading it using LoadJoinConfigurationFromFile,
  66. // and then marshals the internal object to the expected groupVersion
  67. { // v1beta2 -> default -> validate -> internal -> v1beta2
  68. name: "incompleteYAMLToDefaultedv1beta2",
  69. in: nodeIncompleteYAML,
  70. out: nodeDefaultedYAML,
  71. groupVersion: kubeadmapiv1beta2.SchemeGroupVersion,
  72. },
  73. { // v1beta2 -> validation should fail
  74. name: "invalidYAMLShouldFail",
  75. in: nodeInvalidYAML,
  76. expectedErr: true,
  77. },
  78. }
  79. for _, rt := range tests {
  80. t.Run(rt.name, func(t2 *testing.T) {
  81. internalcfg, err := LoadJoinConfigurationFromFile(rt.in)
  82. if err != nil {
  83. if rt.expectedErr {
  84. return
  85. }
  86. t2.Fatalf("couldn't unmarshal test data: %v", err)
  87. }
  88. actual, err := kubeadmutil.MarshalToYamlForCodecs(internalcfg, rt.groupVersion, scheme.Codecs)
  89. if err != nil {
  90. t2.Fatalf("couldn't marshal internal object: %v", err)
  91. }
  92. expected, err := ioutil.ReadFile(rt.out)
  93. if err != nil {
  94. t2.Fatalf("couldn't read test data: %v", err)
  95. }
  96. if !bytes.Equal(expected, actual) {
  97. t2.Errorf("the expected and actual output differs.\n\tin: %s\n\tout: %s\n\tgroupversion: %s\n\tdiff: \n%s\n",
  98. rt.in, rt.out, rt.groupVersion.String(), diff(expected, actual))
  99. }
  100. })
  101. }
  102. }