...

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

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

     1  /*
     2  Copyright 2023 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  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/lithammer/dedent"
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
    28  )
    29  
    30  func TestLoadResetConfigurationFromFile(t *testing.T) {
    31  	// Create temp folder for the test case
    32  	tmpdir, err := os.MkdirTemp("", "")
    33  	if err != nil {
    34  		t.Fatalf("Couldn't create tmpdir: %v", err)
    35  	}
    36  	defer os.RemoveAll(tmpdir)
    37  
    38  	var tests = []struct {
    39  		name         string
    40  		fileContents string
    41  		expectErr    bool
    42  	}{
    43  		{
    44  			name:      "empty file causes error",
    45  			expectErr: true,
    46  		},
    47  		{
    48  			name: "Invalid v1beta4 causes error",
    49  			fileContents: dedent.Dedent(`
    50  				apiVersion: kubeadm.k8s.io/unknownVersion
    51  				kind: ResetConfiguration
    52  				criSocket: unix:///var/run/containerd/containerd.sock
    53  			`),
    54  			expectErr: true,
    55  		},
    56  		{
    57  			name: "valid v1beta4 is loaded",
    58  			fileContents: dedent.Dedent(`
    59  				apiVersion: kubeadm.k8s.io/v1beta4
    60  				kind: ResetConfiguration
    61  				force: true
    62  				cleanupTmpDir: true
    63  				criSocket: unix:///var/run/containerd/containerd.sock
    64  				certificatesDir: /etc/kubernetes/pki
    65  				ignorePreflightErrors:
    66  				- a
    67  				- b
    68  			`),
    69  		},
    70  	}
    71  
    72  	for _, rt := range tests {
    73  		t.Run(rt.name, func(t2 *testing.T) {
    74  			cfgPath := filepath.Join(tmpdir, rt.name)
    75  			err := os.WriteFile(cfgPath, []byte(rt.fileContents), 0644)
    76  			if err != nil {
    77  				t.Errorf("Couldn't create file: %v", err)
    78  				return
    79  			}
    80  
    81  			opts := LoadOrDefaultConfigurationOptions{
    82  				AllowExperimental: true,
    83  				SkipCRIDetect:     true,
    84  			}
    85  
    86  			obj, err := LoadResetConfigurationFromFile(cfgPath, opts)
    87  			if rt.expectErr {
    88  				if err == nil {
    89  					t.Error("Unexpected success")
    90  				}
    91  			} else {
    92  				if err != nil {
    93  					t.Errorf("Error reading file: %v", err)
    94  					return
    95  				}
    96  
    97  				if obj == nil {
    98  					t.Error("Unexpected nil return value")
    99  				}
   100  			}
   101  		})
   102  	}
   103  }
   104  
   105  func TestSetResetDynamicDefaults(t *testing.T) {
   106  	type args struct {
   107  		cfg           *kubeadmapi.ResetConfiguration
   108  		skipCRIDetect bool
   109  	}
   110  	tests := []struct {
   111  		name string
   112  		args args
   113  	}{
   114  		{
   115  			name: "CRISocket is empty and skipCRIDetect is true",
   116  			args: args{
   117  				cfg:           &kubeadmapi.ResetConfiguration{},
   118  				skipCRIDetect: true,
   119  			},
   120  		},
   121  		{
   122  			name: "CRISocket is empty and skipCRIDetect is false",
   123  			args: args{
   124  				cfg:           &kubeadmapi.ResetConfiguration{},
   125  				skipCRIDetect: false,
   126  			},
   127  		},
   128  		{
   129  			name: "CRISocket is valid",
   130  			args: args{
   131  				cfg: &kubeadmapi.ResetConfiguration{
   132  					CRISocket: "unix:///var/run/containerd/containerd.sock",
   133  				},
   134  				skipCRIDetect: false,
   135  			},
   136  		},
   137  		{
   138  			name: "CRISocket is invalid",
   139  			args: args{
   140  				cfg: &kubeadmapi.ResetConfiguration{
   141  					CRISocket: "var/run/containerd/containerd.sock",
   142  				},
   143  				skipCRIDetect: false,
   144  			},
   145  		},
   146  	}
   147  	for _, rt := range tests {
   148  		t.Run(rt.name, func(t *testing.T) {
   149  			err := SetResetDynamicDefaults(rt.args.cfg, rt.args.skipCRIDetect)
   150  			assert.NoError(t, err)
   151  		})
   152  	}
   153  }
   154  

View as plain text