@@ -66,10 +66,10 @@ func newFakeCsiDriverClientWithVolumeStats(t *testing.T, volumeStatsSet bool) *f
66
66
}
67
67
}
68
68
69
- func newFakeCsiDriverClientWithVolumeStatsAndCondition (t * testing.T , volumeStatsSet , volumeConditionSet bool ) * fakeCsiDriverClient {
69
+ func newFakeCsiDriverClientWithVolumeStatsAndCondition (t * testing.T , volumeStatsSet , volumeConditionSet , setVolumeStat , setVolumeCondition bool ) * fakeCsiDriverClient {
70
70
return & fakeCsiDriverClient {
71
71
t : t ,
72
- nodeClient : fake .NewNodeClientWithVolumeStatsAndCondition (volumeStatsSet , volumeConditionSet ),
72
+ nodeClient : fake .NewNodeClientWithVolumeStatsAndCondition (volumeStatsSet , volumeConditionSet , setVolumeStat , setVolumeCondition ),
73
73
}
74
74
}
75
75
@@ -100,18 +100,23 @@ func (c *fakeCsiDriverClient) NodeGetVolumeStats(ctx context.Context, volID stri
100
100
VolumeId : volID ,
101
101
VolumePath : targetPath ,
102
102
}
103
+ fakeResp := & csipbv1.NodeGetVolumeStatsResponse {}
104
+ if c .nodeClient .SetVolumeStats {
105
+ fakeResp = getRawVolumeInfo ()
106
+ }
107
+ if c .nodeClient .SetVolumecondition {
108
+ fakeResp .VolumeCondition = & csipbv1.VolumeCondition {
109
+ Abnormal : true ,
110
+ Message : "Volume is abnormal" ,
111
+ }
112
+ }
113
+ c .nodeClient .SetNodeVolumeStatsResp (fakeResp )
103
114
104
- c .nodeClient .SetNodeVolumeStatsResp (getRawVolumeInfo ())
105
115
resp , err := c .nodeClient .NodeGetVolumeStats (ctx , req )
106
116
if err != nil {
107
117
return nil , err
108
118
}
109
119
110
- usages := resp .GetUsage ()
111
- if usages == nil {
112
- return nil , nil
113
- }
114
-
115
120
metrics := & volume.Metrics {}
116
121
117
122
isSupportNodeVolumeCondition , err := c .nodeSupportsVolumeCondition (ctx )
@@ -124,6 +129,10 @@ func (c *fakeCsiDriverClient) NodeGetVolumeStats(ctx context.Context, volID stri
124
129
metrics .Abnormal , metrics .Message = & abnormal , & message
125
130
}
126
131
132
+ usages := resp .GetUsage ()
133
+ if ! isSupportNodeVolumeCondition && usages == nil {
134
+ return nil , errors .New ("volume usage is nil" )
135
+ }
127
136
for _ , usage := range usages {
128
137
if usage == nil {
129
138
continue
@@ -377,8 +386,8 @@ func setupClientWithExpansion(t *testing.T, stageUnstageSet bool, expansionSet b
377
386
return newFakeCsiDriverClientWithExpansion (t , stageUnstageSet , expansionSet )
378
387
}
379
388
380
- func setupClientWithVolumeStatsAndCondition (t * testing.T , volumeStatsSet , volumeConditionSet bool ) csiClient {
381
- return newFakeCsiDriverClientWithVolumeStatsAndCondition (t , volumeStatsSet , volumeConditionSet )
389
+ func setupClientWithVolumeStatsAndCondition (t * testing.T , volumeStatsSet , volumeConditionSet , setVolumeStat , setVolumecondition bool ) csiClient {
390
+ return newFakeCsiDriverClientWithVolumeStatsAndCondition (t , volumeStatsSet , volumeConditionSet , setVolumeStat , setVolumecondition )
382
391
}
383
392
384
393
func setupClientWithVolumeStats (t * testing.T , volumeStatsSet bool ) csiClient {
@@ -839,13 +848,17 @@ func TestVolumeHealthEnable(t *testing.T) {
839
848
tests := []struct {
840
849
name string
841
850
volumeStatsSet bool
851
+ setVolumeStat bool
852
+ setVolumecondition bool
842
853
volumeConditionSet bool
843
854
volumeData VolumeStatsOptions
844
855
success bool
845
856
}{
846
857
{
847
- name : "when nodeVolumeStats=on, VolumeID =on, DeviceMountPath =on, VolumeCondition =on" ,
858
+ name : "when nodeVolumeStats=on, volumeStatsSet =on, setVolumeStat =on, volumeCondition=on, setVolumecondition =on" ,
848
859
volumeStatsSet : true ,
860
+ setVolumeStat : true ,
861
+ setVolumecondition : true ,
849
862
volumeConditionSet : true ,
850
863
volumeData : VolumeStatsOptions {
851
864
VolumeSpec : spec ,
@@ -855,8 +868,10 @@ func TestVolumeHealthEnable(t *testing.T) {
855
868
success : true ,
856
869
},
857
870
{
858
- name : "when nodeVolumeStats=on, VolumeID =on, DeviceMountPath =on, VolumeCondition =off" ,
871
+ name : "when nodeVolumeStats=on, volumeStatsSet =on, setVolumeStat =on, volumeCondition=off, setVolumecondition =off" ,
859
872
volumeStatsSet : true ,
873
+ setVolumeStat : true ,
874
+ setVolumecondition : false ,
860
875
volumeConditionSet : false ,
861
876
volumeData : VolumeStatsOptions {
862
877
VolumeSpec : spec ,
@@ -865,31 +880,55 @@ func TestVolumeHealthEnable(t *testing.T) {
865
880
},
866
881
success : true ,
867
882
},
883
+ {
884
+ name : "when nodeVolumeStats=on, volumeStatsSet=off, setVolumeStat=off, volumeCondition=on, setVolumecondition=on" ,
885
+ volumeStatsSet : false ,
886
+ setVolumeStat : false ,
887
+ setVolumecondition : true ,
888
+ volumeConditionSet : true ,
889
+ volumeData : VolumeStatsOptions {
890
+ VolumeSpec : spec ,
891
+ VolumeID : "volume1" ,
892
+ DeviceMountPath : "/foo/bar" ,
893
+ },
894
+ success : true ,
895
+ },
896
+ {
897
+ name : "when nodeVolumeStats=on, volumeStatsSet=off, setVolumeStat=off, volumeCondition=off, setVolumecondition=off" ,
898
+ setVolumeStat : false ,
899
+ volumeConditionSet : false ,
900
+ volumeData : VolumeStatsOptions {
901
+ VolumeSpec : spec ,
902
+ VolumeID : "volume1" ,
903
+ DeviceMountPath : "/foo/bar" ,
904
+ },
905
+ success : false ,
906
+ },
868
907
}
869
908
870
909
for _ , tc := range tests {
871
910
t .Run (tc .name , func (t * testing.T ) {
872
911
ctx , cancel := context .WithTimeout (context .Background (), csiTimeout )
873
912
defer cancel ()
874
913
csiSource , _ := getCSISourceFromSpec (tc .volumeData .VolumeSpec )
875
- csClient := setupClientWithVolumeStatsAndCondition (t , tc .volumeStatsSet , tc .volumeConditionSet )
914
+ csClient := setupClientWithVolumeStatsAndCondition (t , tc .volumeStatsSet , tc .volumeConditionSet , tc . setVolumeStat , tc . setVolumecondition )
876
915
metrics , err := csClient .NodeGetVolumeStats (ctx , csiSource .VolumeHandle , tc .volumeData .DeviceMountPath )
877
- if tc .success {
878
- assert . Nil ( t , err )
916
+ if err != nil && tc .success {
917
+ t . Errorf ( "For %s : expected %v got %v" , tc . name , tc . success , err )
879
918
}
880
-
881
- if metrics == nil {
882
- t .Errorf ("csi.NodeGetVolumeStats returned nil metrics for volume %s" , tc .volumeData .VolumeID )
883
- } else {
884
- if tc .volumeConditionSet {
885
- assert .NotNil (t , metrics .Abnormal )
886
- assert .NotNil (t , metrics .Message )
919
+ if tc .success {
920
+ if metrics == nil {
921
+ t .Errorf ("csi.NodeGetVolumeStats returned nil metrics for volume %s" , tc .volumeData .VolumeID )
887
922
} else {
888
- assert .Nil (t , metrics .Abnormal )
889
- assert .Nil (t , metrics .Message )
923
+ if tc .volumeConditionSet {
924
+ assert .NotNil (t , metrics .Abnormal )
925
+ assert .NotNil (t , metrics .Message )
926
+ } else {
927
+ assert .Nil (t , metrics .Abnormal )
928
+ assert .Nil (t , metrics .Message )
929
+ }
890
930
}
891
931
}
892
-
893
932
})
894
933
}
895
934
}
@@ -919,7 +958,7 @@ func TestVolumeHealthDisable(t *testing.T) {
919
958
ctx , cancel := context .WithTimeout (context .Background (), csiTimeout )
920
959
defer cancel ()
921
960
csiSource , _ := getCSISourceFromSpec (tc .volumeData .VolumeSpec )
922
- csClient := setupClientWithVolumeStatsAndCondition (t , tc .volumeStatsSet , false )
961
+ csClient := setupClientWithVolumeStatsAndCondition (t , tc .volumeStatsSet , false , true , false )
923
962
metrics , err := csClient .NodeGetVolumeStats (ctx , csiSource .VolumeHandle , tc .volumeData .DeviceMountPath )
924
963
if tc .success {
925
964
assert .Nil (t , err )
0 commit comments