k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/componentconfigs/kubelet_windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2021 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package componentconfigs
    21  
    22  import (
    23  	"path/filepath"
    24  	"reflect"
    25  	"testing"
    26  
    27  	kubeletconfig "k8s.io/kubelet/config/v1beta1"
    28  	"k8s.io/utils/ptr"
    29  )
    30  
    31  func TestMutatePaths(t *testing.T) {
    32  	const drive = "C:"
    33  	var fooResolverConfig string = "/foo/resolver"
    34  
    35  	tests := []struct {
    36  		name     string
    37  		cfg      *kubeletconfig.KubeletConfiguration
    38  		expected *kubeletconfig.KubeletConfiguration
    39  	}{
    40  		{
    41  			name: "valid: all fields are absolute paths",
    42  			cfg: &kubeletconfig.KubeletConfiguration{
    43  				ResolverConfig: &fooResolverConfig,
    44  				StaticPodPath:  "/foo/staticpods",
    45  				Authentication: kubeletconfig.KubeletAuthentication{
    46  					X509: kubeletconfig.KubeletX509Authentication{
    47  						ClientCAFile: "/foo/ca.crt",
    48  					},
    49  				},
    50  			},
    51  			expected: &kubeletconfig.KubeletConfiguration{
    52  				ResolverConfig: ptr.To(""),
    53  				StaticPodPath:  filepath.Join(drive, "/foo/staticpods"),
    54  				Authentication: kubeletconfig.KubeletAuthentication{
    55  					X509: kubeletconfig.KubeletX509Authentication{
    56  						ClientCAFile: filepath.Join(drive, "/foo/ca.crt"),
    57  					},
    58  				},
    59  			},
    60  		},
    61  		{
    62  			name: "valid: some fields are not absolute paths",
    63  			cfg: &kubeletconfig.KubeletConfiguration{
    64  				ResolverConfig: &fooResolverConfig,
    65  				StaticPodPath:  "./foo/staticpods", // not an absolute Unix path
    66  				Authentication: kubeletconfig.KubeletAuthentication{
    67  					X509: kubeletconfig.KubeletX509Authentication{
    68  						ClientCAFile: "/foo/ca.crt",
    69  					},
    70  				},
    71  			},
    72  			expected: &kubeletconfig.KubeletConfiguration{
    73  				ResolverConfig: ptr.To(""),
    74  				StaticPodPath:  "./foo/staticpods",
    75  				Authentication: kubeletconfig.KubeletAuthentication{
    76  					X509: kubeletconfig.KubeletX509Authentication{
    77  						ClientCAFile: filepath.Join(drive, "/foo/ca.crt"),
    78  					},
    79  				},
    80  			},
    81  		},
    82  	}
    83  
    84  	for _, test := range tests {
    85  		t.Run(test.name, func(t *testing.T) {
    86  			mutatePaths(test.cfg, drive)
    87  			if !reflect.DeepEqual(test.cfg, test.expected) {
    88  				t.Errorf("Missmatch between expected and got:\nExpected:\n%+v\n---\nGot:\n%+v",
    89  					test.expected, test.cfg)
    90  			}
    91  		})
    92  	}
    93  }