...
1
16
17 package uploadconfig
18
19 import (
20 "context"
21 "reflect"
22 "testing"
23
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/runtime"
26 clientsetfake "k8s.io/client-go/kubernetes/fake"
27
28 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
29 kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
30 kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
31 configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
32 )
33
34 func TestUploadConfiguration(t *testing.T) {
35 tests := []struct {
36 name string
37 updateExisting bool
38 verifyResult bool
39 }{
40 {
41 name: "basic validation with correct key",
42 verifyResult: true,
43 },
44 {
45 name: "update existing should report no error",
46 updateExisting: true,
47 verifyResult: true,
48 },
49 }
50 for _, tt := range tests {
51 t.Run(tt.name, func(t2 *testing.T) {
52 cfg, err := configutil.DefaultedStaticInitConfiguration()
53 if err != nil {
54 t2.Fatalf("UploadConfiguration() error = %v", err)
55 }
56 cfg.ComponentConfigs = kubeadmapi.ComponentConfigMap{}
57 cfg.ClusterConfiguration.KubernetesVersion = kubeadmconstants.MinimumControlPlaneVersion.WithPatch(10).String()
58 cfg.NodeRegistration.Name = "node-foo"
59 cfg.NodeRegistration.CRISocket = kubeadmconstants.DefaultCRISocket
60
61 client := clientsetfake.NewSimpleClientset()
62
63 if err := UploadConfiguration(cfg, client); err != nil {
64 t2.Fatalf("UploadConfiguration() error = %v", err)
65 }
66 if tt.updateExisting {
67 if err := UploadConfiguration(cfg, client); err != nil {
68 t2.Fatalf("UploadConfiguration() error = %v", err)
69 }
70 }
71 if tt.verifyResult {
72 controlPlaneCfg, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.TODO(), kubeadmconstants.KubeadmConfigConfigMap, metav1.GetOptions{})
73 if err != nil {
74 t2.Fatalf("Fail to query ConfigMap error = %v", err)
75 }
76 configData := controlPlaneCfg.Data[kubeadmconstants.ClusterConfigurationConfigMapKey]
77 if configData == "" {
78 t2.Fatal("Fail to find ClusterConfigurationConfigMapKey key")
79 }
80
81 decodedCfg := &kubeadmapi.ClusterConfiguration{}
82 if err := runtime.DecodeInto(kubeadmscheme.Codecs.UniversalDecoder(), []byte(configData), decodedCfg); err != nil {
83 t2.Fatalf("unable to decode config from bytes: %v", err)
84 }
85
86 if len(decodedCfg.ComponentConfigs) != 0 {
87 t2.Errorf("unexpected component configs in decodedCfg: %d", len(decodedCfg.ComponentConfigs))
88 }
89
90
91 decodedCfg.ComponentConfigs = kubeadmapi.ComponentConfigMap{}
92
93 if !reflect.DeepEqual(decodedCfg, &cfg.ClusterConfiguration) {
94 t2.Errorf("the initial and decoded ClusterConfiguration didn't match:\n%#v\n===\n%#v", decodedCfg, &cfg.ClusterConfiguration)
95 }
96 }
97 })
98 }
99 }
100
View as plain text