k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/volumemanager/metrics/metrics_test.go (about) 1 /* 2 Copyright 2018 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 metrics 18 19 import ( 20 "k8s.io/klog/v2/ktesting" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 k8stypes "k8s.io/apimachinery/pkg/types" 26 "k8s.io/kubernetes/pkg/kubelet/volumemanager/cache" 27 "k8s.io/kubernetes/pkg/volume" 28 29 volumetesting "k8s.io/kubernetes/pkg/volume/testing" 30 "k8s.io/kubernetes/pkg/volume/util" 31 "k8s.io/kubernetes/pkg/volume/util/operationexecutor" 32 ) 33 34 func TestMetricCollection(t *testing.T) { 35 volumePluginMgr, fakePlugin := volumetesting.GetTestKubeletVolumePluginMgr(t) 36 seLinuxTranslator := util.NewFakeSELinuxLabelTranslator() 37 dsw := cache.NewDesiredStateOfWorld(volumePluginMgr, seLinuxTranslator) 38 asw := cache.NewActualStateOfWorld(k8stypes.NodeName("node-name"), volumePluginMgr) 39 pod := &v1.Pod{ 40 ObjectMeta: metav1.ObjectMeta{ 41 Name: "pod1", 42 UID: "pod1uid", 43 }, 44 Spec: v1.PodSpec{ 45 Volumes: []v1.Volume{ 46 { 47 Name: "volume-name", 48 VolumeSource: v1.VolumeSource{ 49 GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{ 50 PDName: "fake-device1", 51 }, 52 }, 53 }, 54 }, 55 }, 56 } 57 volumeSpec := &volume.Spec{Volume: &pod.Spec.Volumes[0]} 58 podName := util.GetUniquePodName(pod) 59 60 // Add one volume to DesiredStateOfWorld 61 generatedVolumeName, err := dsw.AddPodToVolume(podName, pod, volumeSpec, volumeSpec.Name(), "", nil /* seLinuxOptions */) 62 if err != nil { 63 t.Fatalf("AddPodToVolume failed. Expected: <no error> Actual: <%v>", err) 64 } 65 66 mounter, err := fakePlugin.NewMounter(volumeSpec, pod, volume.VolumeOptions{}) 67 if err != nil { 68 t.Fatalf("NewMounter failed. Expected: <no error> Actual: <%v>", err) 69 } 70 71 mapper, err := fakePlugin.NewBlockVolumeMapper(volumeSpec, pod, volume.VolumeOptions{}) 72 if err != nil { 73 t.Fatalf("NewBlockVolumeMapper failed. Expected: <no error> Actual: <%v>", err) 74 } 75 76 // Add one volume to ActualStateOfWorld 77 devicePath := "fake/device/path" 78 logger, _ := ktesting.NewTestContext(t) 79 err = asw.MarkVolumeAsAttached(logger, "", volumeSpec, "", devicePath) 80 if err != nil { 81 t.Fatalf("MarkVolumeAsAttached failed. Expected: <no error> Actual: <%v>", err) 82 } 83 84 markVolumeOpts := operationexecutor.MarkVolumeOpts{ 85 PodName: podName, 86 PodUID: pod.UID, 87 VolumeName: generatedVolumeName, 88 Mounter: mounter, 89 BlockVolumeMapper: mapper, 90 OuterVolumeSpecName: volumeSpec.Name(), 91 VolumeSpec: volumeSpec, 92 VolumeMountState: operationexecutor.VolumeMounted, 93 } 94 err = asw.AddPodToVolume(markVolumeOpts) 95 if err != nil { 96 t.Fatalf("AddPodToVolume failed. Expected: <no error> Actual: <%v>", err) 97 } 98 99 metricCollector := &totalVolumesCollector{asw: asw, dsw: dsw, pluginMgr: volumePluginMgr} 100 101 // Check if getVolumeCount returns correct data 102 count := metricCollector.getVolumeCount() 103 if len(count) != 2 { 104 t.Errorf("getVolumeCount failed. Expected <2> states, got <%d>", len(count)) 105 } 106 107 dswCount, ok := count["desired_state_of_world"] 108 if !ok { 109 t.Errorf("getVolumeCount failed. Expected <desired_state_of_world>, got nothing") 110 } 111 112 fakePluginCount := dswCount["fake-plugin"] 113 if fakePluginCount != 1 { 114 t.Errorf("getVolumeCount failed. Expected <1> fake-plugin volume in DesiredStateOfWorld, got <%d>", 115 fakePluginCount) 116 } 117 118 aswCount, ok := count["actual_state_of_world"] 119 if !ok { 120 t.Errorf("getVolumeCount failed. Expected <actual_state_of_world>, got nothing") 121 } 122 123 fakePluginCount = aswCount["fake-plugin"] 124 if fakePluginCount != 1 { 125 t.Errorf("getVolumeCount failed. Expected <1> fake-plugin volume in ActualStateOfWorld, got <%d>", 126 fakePluginCount) 127 } 128 }