From 45c52d1314e05201996e0111edb14c27352e8399 Mon Sep 17 00:00:00 2001 From: caiweidong Date: Tue, 23 Jul 2019 13:47:35 +0800 Subject: [PATCH] Fix potential panic in nodeGetVolumeStatsV1 --- pkg/volume/csi/csi_client.go | 3 +++ pkg/volume/csi/csi_client_test.go | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/volume/csi/csi_client.go b/pkg/volume/csi/csi_client.go index 566b8093031..73c778456c4 100644 --- a/pkg/volume/csi/csi_client.go +++ b/pkg/volume/csi/csi_client.go @@ -927,6 +927,9 @@ func (c *csiDriverClient) nodeGetVolumeStatsV1( InodesFree: resource.NewQuantity(int64(0), resource.BinarySI), } for _, usage := range usages { + if usage == nil { + continue + } unit := usage.GetUnit() switch unit { case csipbv1.VolumeUsage_BYTES: diff --git a/pkg/volume/csi/csi_client_test.go b/pkg/volume/csi/csi_client_test.go index 8bac6b71e18..cef0e814fc6 100644 --- a/pkg/volume/csi/csi_client_test.go +++ b/pkg/volume/csi/csi_client_test.go @@ -83,6 +83,9 @@ func (c *fakeCsiDriverClient) NodeGetVolumeStats(ctx context.Context, volID stri return nil, nil } for _, usage := range usages { + if usage == nil { + continue + } unit := usage.GetUnit() switch unit { case csipbv1.VolumeUsage_BYTES: @@ -661,14 +664,16 @@ func TestVolumeStats(t *testing.T) { }, } for _, tc := range tests { - ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) - defer cancel() - csiSource, _ := getCSISourceFromSpec(tc.volumeData.VolumeSpec) - csClient := setupClientWithVolumeStats(t, tc.volumeStatsSet) - _, err := csClient.NodeGetVolumeStats(ctx, csiSource.VolumeHandle, tc.volumeData.DeviceMountPath) - if err != nil && tc.success { - t.Errorf("For %s : expected %v got %v", tc.name, tc.success, err) - } + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), csiTimeout) + defer cancel() + csiSource, _ := getCSISourceFromSpec(tc.volumeData.VolumeSpec) + csClient := setupClientWithVolumeStats(t, tc.volumeStatsSet) + _, err := csClient.NodeGetVolumeStats(ctx, csiSource.VolumeHandle, tc.volumeData.DeviceMountPath) + if err != nil && tc.success { + t.Errorf("For %s : expected %v got %v", tc.name, tc.success, err) + } + }) } }