k8s.io/kubernetes@v1.29.3/pkg/kubelet/stats/host_stats_provider_test.go (about) 1 /* 2 Copyright 2021 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 stats 18 19 import ( 20 "reflect" 21 "testing" 22 23 cadvisorapiv2 "github.com/google/cadvisor/info/v2" 24 25 kubecontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing" 26 27 "k8s.io/apimachinery/pkg/types" 28 statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1" 29 ) 30 31 func Test_hostStatsProvider_getPodEtcHostsStats(t *testing.T) { 32 tests := []struct { 33 name string 34 podEtcHostsPathFunc PodEtcHostsPathFunc 35 podUID types.UID 36 rootFsInfo *cadvisorapiv2.FsInfo 37 want *statsapi.FsStats 38 wantErr bool 39 }{ 40 { 41 name: "Should return nil for runtimes that do not support etc host file", 42 podEtcHostsPathFunc: func(podUID types.UID) string { 43 return "" 44 }, 45 podUID: "fake0001", 46 rootFsInfo: nil, 47 want: nil, 48 wantErr: false, 49 }, 50 } 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 h := hostStatsProvider{ 54 osInterface: &kubecontainertest.FakeOS{}, 55 podEtcHostsPathFunc: tt.podEtcHostsPathFunc, 56 } 57 got, err := h.getPodEtcHostsStats(tt.podUID, tt.rootFsInfo) 58 if (err != nil) != tt.wantErr { 59 t.Errorf("getPodEtcHostsStats() error = %v, wantErr %v", err, tt.wantErr) 60 return 61 } 62 if !reflect.DeepEqual(got, tt.want) { 63 t.Errorf("getPodEtcHostsStats() got = %v, want %v", got, tt.want) 64 } 65 }) 66 } 67 }