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

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright 2024 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  	"reflect"
    24  	"testing"
    25  
    26  	kubeletconfig "k8s.io/kubelet/config/v1beta1"
    27  	"k8s.io/utils/ptr"
    28  )
    29  
    30  func TestMutateResolverConfig(t *testing.T) {
    31  	var fooResolverConfig = "/foo/resolver"
    32  
    33  	tests := []struct {
    34  		name                string
    35  		cfg                 *kubeletconfig.KubeletConfiguration
    36  		isServiceActiveFunc func(string) (bool, error)
    37  		expected            *kubeletconfig.KubeletConfiguration
    38  	}{
    39  		{
    40  			name: "the resolver config should not be mutated when it was set already even if systemd-resolved is active",
    41  			cfg: &kubeletconfig.KubeletConfiguration{
    42  				ResolverConfig: ptr.To(fooResolverConfig),
    43  			},
    44  			isServiceActiveFunc: func(string) (bool, error) { return true, nil },
    45  			expected: &kubeletconfig.KubeletConfiguration{
    46  				ResolverConfig: ptr.To(fooResolverConfig),
    47  			},
    48  		},
    49  		{
    50  			name: "the resolver config should be set when systemd-resolved is active",
    51  			cfg: &kubeletconfig.KubeletConfiguration{
    52  				ResolverConfig: nil,
    53  			},
    54  			isServiceActiveFunc: func(string) (bool, error) { return true, nil },
    55  			expected: &kubeletconfig.KubeletConfiguration{
    56  				ResolverConfig: ptr.To(kubeletSystemdResolverConfig),
    57  			},
    58  		},
    59  		{
    60  			name: "the resolver config should not be set when systemd-resolved is not active",
    61  			cfg: &kubeletconfig.KubeletConfiguration{
    62  				ResolverConfig: nil,
    63  			},
    64  			isServiceActiveFunc: func(string) (bool, error) { return false, nil },
    65  			expected: &kubeletconfig.KubeletConfiguration{
    66  				ResolverConfig: nil,
    67  			},
    68  		},
    69  	}
    70  
    71  	for _, test := range tests {
    72  		t.Run(test.name, func(t *testing.T) {
    73  			err := mutateResolverConfig(test.cfg, test.isServiceActiveFunc)
    74  			if err != nil {
    75  				t.Fatalf("failed to mutate ResolverConfig for KubeletConfiguration, %v", err)
    76  			}
    77  			if !reflect.DeepEqual(test.cfg, test.expected) {
    78  				t.Errorf("Missmatch between expected and got:\nExpected:\n%+v\n---\nGot:\n%+v",
    79  					test.expected, test.cfg)
    80  			}
    81  		})
    82  	}
    83  }