From 38d8aae408cba997b6c78673ed7481bee45ad680 Mon Sep 17 00:00:00 2001 From: fengzixu Date: Sun, 27 Mar 2022 08:38:20 +0000 Subject: [PATCH] fix: add nil check --- .../stats/volume_stat_calculator_test.go | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/kubelet/server/stats/volume_stat_calculator_test.go b/pkg/kubelet/server/stats/volume_stat_calculator_test.go index a1d3ce4898b..7af179659db 100644 --- a/pkg/kubelet/server/stats/volume_stat_calculator_test.go +++ b/pkg/kubelet/server/stats/volume_stat_calculator_test.go @@ -206,8 +206,10 @@ func TestAbnormalVolumeEvent(t *testing.T) { } // Calculate stats for pod - volumeCondition.Message = "The target path of the volume doesn't exist" - volumeCondition.Abnormal = true + if volumeCondition != nil { + volumeCondition.Message = "The target path of the volume doesn't exist" + volumeCondition.Abnormal = true + } statsCalculator := newVolumeStatCalculator(mockStats, time.Minute, fakePod, &fakeEventRecorder) statsCalculator.calcAndStoreStats() @@ -237,16 +239,21 @@ func (v *fakeVolume) GetMetrics() (*volume.Metrics, error) { } func expectedMetrics() *volume.Metrics { - return &volume.Metrics{ + vMetrics := &volume.Metrics{ Available: resource.NewQuantity(available, resource.BinarySI), Capacity: resource.NewQuantity(capacity, resource.BinarySI), Used: resource.NewQuantity(available-capacity, resource.BinarySI), Inodes: resource.NewQuantity(inodesTotal, resource.BinarySI), InodesFree: resource.NewQuantity(inodesFree, resource.BinarySI), InodesUsed: resource.NewQuantity(inodesTotal-inodesFree, resource.BinarySI), - Message: &volumeCondition.Message, - Abnormal: &volumeCondition.Abnormal, } + + if volumeCondition != nil { + vMetrics.Message = &volumeCondition.Message + vMetrics.Abnormal = &volumeCondition.Abnormal + } + + return vMetrics } func expectedFSStats() kubestats.FsStats { @@ -269,9 +276,13 @@ func expectedFSStats() kubestats.FsStats { func expectedVolumeHealthStats() *kubestats.VolumeHealthStats { metric := expectedMetrics() - return &kubestats.VolumeHealthStats{ - Abnormal: *metric.Abnormal, + hs := &kubestats.VolumeHealthStats{} + + if metric != nil && metric.Abnormal != nil { + hs.Abnormal = *metric.Abnormal } + + return hs } // Fake block-volume/metrics provider, block-devices have no inodes @@ -290,12 +301,17 @@ func (v *fakeBlockVolume) GetMetrics() (*volume.Metrics, error) { } func expectedBlockMetrics() *volume.Metrics { - return &volume.Metrics{ + vMetrics := &volume.Metrics{ Available: resource.NewQuantity(available, resource.BinarySI), Capacity: resource.NewQuantity(capacity, resource.BinarySI), Used: resource.NewQuantity(available-capacity, resource.BinarySI), - Abnormal: &volumeCondition.Abnormal, } + + if volumeCondition != nil { + vMetrics.Abnormal = &volumeCondition.Abnormal + } + + return vMetrics } func expectedBlockStats() kubestats.FsStats {