...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/config/upgradeconfiguration_test.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/config

     1  /*
     2  Copyright 2024 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/go-cmp/cmp/cmpopts"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/utils/ptr"
    27  	"sigs.k8s.io/yaml"
    28  
    29  	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
    30  	kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4"
    31  	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
    32  	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
    33  )
    34  
    35  func TestDocMapToUpgradeConfiguration(t *testing.T) {
    36  	tests := []struct {
    37  		name        string
    38  		cfg         kubeadmapiv1.UpgradeConfiguration
    39  		expectedCfg kubeadmapi.UpgradeConfiguration
    40  	}{
    41  		{
    42  			name: "default config is set correctly",
    43  			cfg: kubeadmapiv1.UpgradeConfiguration{
    44  				TypeMeta: metav1.TypeMeta{
    45  					APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
    46  					Kind:       constants.UpgradeConfigurationKind,
    47  				},
    48  			},
    49  			expectedCfg: kubeadmapi.UpgradeConfiguration{
    50  				Apply: kubeadmapi.UpgradeApplyConfiguration{
    51  					CertificateRenewal: ptr.To(true),
    52  					EtcdUpgrade:        ptr.To(true),
    53  				},
    54  				Node: kubeadmapi.UpgradeNodeConfiguration{
    55  					CertificateRenewal: ptr.To(true),
    56  					EtcdUpgrade:        ptr.To(true),
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "cfg has part of fields configured",
    62  			cfg: kubeadmapiv1.UpgradeConfiguration{
    63  				Apply: kubeadmapiv1.UpgradeApplyConfiguration{
    64  					CertificateRenewal: ptr.To(false),
    65  				},
    66  				Node: kubeadmapiv1.UpgradeNodeConfiguration{
    67  					EtcdUpgrade: ptr.To(false),
    68  				},
    69  				TypeMeta: metav1.TypeMeta{
    70  					APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
    71  					Kind:       constants.UpgradeConfigurationKind,
    72  				},
    73  			},
    74  			expectedCfg: kubeadmapi.UpgradeConfiguration{
    75  				Apply: kubeadmapi.UpgradeApplyConfiguration{
    76  					CertificateRenewal: ptr.To(false),
    77  					EtcdUpgrade:        ptr.To(true),
    78  				},
    79  				Node: kubeadmapi.UpgradeNodeConfiguration{
    80  					CertificateRenewal: ptr.To(true),
    81  					EtcdUpgrade:        ptr.To(false),
    82  				},
    83  			},
    84  		},
    85  	}
    86  
    87  	for _, tc := range tests {
    88  		t.Run(tc.name, func(t *testing.T) {
    89  			b, err := yaml.Marshal(tc.cfg)
    90  			if err != nil {
    91  				t.Fatalf("unexpected error while marshalling to YAML: %v", err)
    92  			}
    93  			docmap, err := kubeadmutil.SplitYAMLDocuments(b)
    94  			if err != nil {
    95  				t.Fatalf("Unexpect error of SplitYAMLDocuments: %v", err)
    96  			}
    97  			cfg, err := DocMapToUpgradeConfiguration(docmap)
    98  			if err != nil {
    99  				t.Fatalf("unexpected error of DocMapToUpgradeConfiguration: %v\nconfig: %s", err, string(b))
   100  			}
   101  			if diff := cmp.Diff(*cfg, tc.expectedCfg, cmpopts.IgnoreFields(kubeadmapi.UpgradeConfiguration{}, "Timeouts")); diff != "" {
   102  				t.Fatalf("DocMapToUpgradeConfiguration returned unexpected diff (-want,+got):\n%s", diff)
   103  			}
   104  		})
   105  	}
   106  }
   107  

View as plain text