configset_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2019 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 componentconfigs
  14. import (
  15. "testing"
  16. "github.com/lithammer/dedent"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/util/version"
  21. clientsetfake "k8s.io/client-go/kubernetes/fake"
  22. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  23. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  24. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  25. )
  26. func TestDefault(t *testing.T) {
  27. clusterCfg := &kubeadmapi.ClusterConfiguration{}
  28. localAPIEndpoint := &kubeadmapi.APIEndpoint{}
  29. Default(clusterCfg, localAPIEndpoint)
  30. if len(clusterCfg.ComponentConfigs) != len(known) {
  31. t.Errorf("missmatch between supported and defaulted type numbers:\n\tgot: %d\n\texpected: %d", len(clusterCfg.ComponentConfigs), len(known))
  32. }
  33. }
  34. func TestFromCluster(t *testing.T) {
  35. clusterCfg := &kubeadmapi.ClusterConfiguration{
  36. KubernetesVersion: constants.CurrentKubernetesVersion.String(),
  37. }
  38. k8sVersion := version.MustParseGeneric(clusterCfg.KubernetesVersion)
  39. objects := []runtime.Object{
  40. &v1.ConfigMap{
  41. ObjectMeta: metav1.ObjectMeta{
  42. Name: constants.KubeProxyConfigMap,
  43. Namespace: metav1.NamespaceSystem,
  44. },
  45. Data: map[string]string{
  46. constants.KubeProxyConfigMapKey: dedent.Dedent(`
  47. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  48. kind: KubeProxyConfiguration
  49. `),
  50. },
  51. },
  52. &v1.ConfigMap{
  53. ObjectMeta: metav1.ObjectMeta{
  54. Name: constants.GetKubeletConfigMapName(k8sVersion),
  55. Namespace: metav1.NamespaceSystem,
  56. },
  57. Data: map[string]string{
  58. constants.KubeletBaseConfigurationConfigMapKey: dedent.Dedent(`
  59. apiVersion: kubelet.config.k8s.io/v1beta1
  60. kind: KubeletConfiguration
  61. `),
  62. },
  63. },
  64. }
  65. client := clientsetfake.NewSimpleClientset(objects...)
  66. if err := FetchFromCluster(clusterCfg, client); err != nil {
  67. t.Fatalf("FetchFromCluster failed: %v", err)
  68. }
  69. if len(clusterCfg.ComponentConfigs) != len(objects) {
  70. t.Fatalf("missmatch between supplied and loaded type numbers:\n\tgot: %d\n\texpected: %d", len(clusterCfg.ComponentConfigs), len(objects))
  71. }
  72. }
  73. func TestFetchFromDocumentMap(t *testing.T) {
  74. test := dedent.Dedent(`
  75. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  76. kind: KubeProxyConfiguration
  77. ---
  78. apiVersion: kubelet.config.k8s.io/v1beta1
  79. kind: KubeletConfiguration
  80. `)
  81. gvkmap, err := kubeadmutil.SplitYAMLDocuments([]byte(test))
  82. if err != nil {
  83. t.Fatalf("unexpected failure of SplitYAMLDocuments: %v", err)
  84. }
  85. clusterCfg := &kubeadmapi.ClusterConfiguration{}
  86. if err = FetchFromDocumentMap(clusterCfg, gvkmap); err != nil {
  87. t.Fatalf("FetchFromDocumentMap failed: %v", err)
  88. }
  89. if len(clusterCfg.ComponentConfigs) != len(gvkmap) {
  90. t.Fatalf("missmatch between supplied and loaded type numbers:\n\tgot: %d\n\texpected: %d", len(clusterCfg.ComponentConfigs), len(gvkmap))
  91. }
  92. }