@@ -23,15 +23,19 @@ import (
23
23
"github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes"
24
24
"github.com/stretchr/testify/assert"
25
25
"github.com/stretchr/testify/mock"
26
+ "google.golang.org/grpc/codes"
27
+ "google.golang.org/grpc/status"
26
28
27
29
sharedcsi "k8s.io/cloud-provider-openstack/pkg/csi"
28
30
"k8s.io/cloud-provider-openstack/pkg/csi/cinder/openstack"
29
31
)
30
32
31
- var fakeCs * controllerServer
32
- var fakeCsMultipleClouds * controllerServer
33
- var osmock * openstack.OpenStackMock
34
- var osmockRegionX * openstack.OpenStackMock
33
+ var (
34
+ fakeCs * controllerServer
35
+ fakeCsMultipleClouds * controllerServer
36
+ osmock * openstack.OpenStackMock
37
+ osmockRegionX * openstack.OpenStackMock
38
+ )
35
39
36
40
// Init Controller Server
37
41
func init () {
@@ -94,7 +98,51 @@ func TestCreateVolume(t *testing.T) {
94
98
assert .NotEqual (0 , len (actualRes .Volume .VolumeId ), "Volume Id is nil" )
95
99
assert .NotNil (actualRes .Volume .AccessibleTopology )
96
100
assert .Equal (FakeAvailability , actualRes .Volume .AccessibleTopology [0 ].GetSegments ()[topologyKey ])
101
+ }
102
+
103
+ // Test CreateVolume fails with quota exceeded error
104
+ func TestCreateVolumeQuotaError (t * testing.T ) {
105
+ // mock OpenStack
106
+ properties := map [string ]string {cinderCSIClusterIDKey : FakeCluster }
107
+ // CreateVolume(name string, size int, vtype, availability string, snapshotID string, sourceVolID string, sourceBackupID string, tags map[string]string) (string, string, int, error)
108
+ osmock .On ("CreateVolume" , FakeVolName , mock .AnythingOfType ("int" ), FakeVolType , FakeAvailability , "" , "" , "" , properties ).Return (& volumes.Volume {}, openstack.ErrQuotaExceeded {})
97
109
110
+ osmock .On ("GetVolumesByName" , FakeVolName ).Return (FakeVolListEmpty , nil )
111
+ // Init assert
112
+ assert := assert .New (t )
113
+
114
+ // Fake request
115
+ fakeReq := & csi.CreateVolumeRequest {
116
+ Name : FakeVolName ,
117
+ VolumeCapabilities : []* csi.VolumeCapability {
118
+ {
119
+ AccessMode : & csi.VolumeCapability_AccessMode {
120
+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
121
+ },
122
+ },
123
+ },
124
+
125
+ AccessibilityRequirements : & csi.TopologyRequirement {
126
+ Requisite : []* csi.Topology {
127
+ {
128
+ Segments : map [string ]string {topologyKey : FakeAvailability },
129
+ },
130
+ },
131
+ },
132
+ }
133
+
134
+ // Invoke CreateVolume
135
+ _ , err := fakeCs .CreateVolume (FakeCtx , fakeReq )
136
+ if err == nil {
137
+ t .Errorf ("CreateVolume did not return an error" )
138
+ }
139
+ statusErr , ok := status .FromError (err )
140
+ if ! ok {
141
+ t .Errorf ("CreateVolume did not return a grpc status as error, got %v" , err )
142
+ }
143
+
144
+ // Assert
145
+ assert .Equal (statusErr .Code (), codes .ResourceExhausted )
98
146
}
99
147
100
148
// Test CreateVolume with additional param
@@ -146,7 +194,6 @@ func TestCreateVolumeWithParam(t *testing.T) {
146
194
assert .NotEqual (0 , len (actualRes .Volume .VolumeId ), "Volume Id is nil" )
147
195
assert .NotNil (actualRes .Volume .AccessibleTopology )
148
196
assert .Equal (FakeAvailability , actualRes .Volume .AccessibleTopology [0 ].GetSegments ()[topologyKey ])
149
-
150
197
}
151
198
152
199
func TestCreateVolumeWithExtraMetadata (t * testing.T ) {
@@ -192,7 +239,6 @@ func TestCreateVolumeWithExtraMetadata(t *testing.T) {
192
239
if err != nil {
193
240
t .Errorf ("failed to CreateVolume: %v" , err )
194
241
}
195
-
196
242
}
197
243
198
244
func TestCreateVolumeFromSnapshot (t * testing.T ) {
@@ -239,7 +285,6 @@ func TestCreateVolumeFromSnapshot(t *testing.T) {
239
285
assert .NotEqual (0 , len (actualRes .Volume .VolumeId ), "Volume Id is nil" )
240
286
241
287
assert .Equal (FakeSnapshotID , actualRes .Volume .ContentSource .GetSnapshot ().SnapshotId )
242
-
243
288
}
244
289
245
290
func TestCreateVolumeFromSourceVolume (t * testing.T ) {
@@ -286,7 +331,6 @@ func TestCreateVolumeFromSourceVolume(t *testing.T) {
286
331
assert .NotEqual (0 , len (actualRes .Volume .VolumeId ), "Volume Id is nil" )
287
332
288
333
assert .Equal (FakeVolID , actualRes .Volume .ContentSource .GetVolume ().VolumeId )
289
-
290
334
}
291
335
292
336
// Test CreateVolumeDuplicate
@@ -436,6 +480,7 @@ func genFakeVolumeEntry(fakeVol volumes.Volume) *csi.ListVolumesResponse_Entry {
436
480
},
437
481
}
438
482
}
483
+
439
484
func genFakeVolumeEntries (fakeVolumes []volumes.Volume ) []* csi.ListVolumesResponse_Entry {
440
485
entries := make ([]* csi.ListVolumesResponse_Entry , 0 , len (fakeVolumes ))
441
486
for _ , fakeVol := range fakeVolumes {
@@ -800,7 +845,6 @@ func TestGlobalListVolumesMultipleClouds(t *testing.T) {
800
845
801
846
// Test CreateSnapshot
802
847
func TestCreateSnapshot (t * testing.T ) {
803
-
804
848
osmock .On ("CreateSnapshot" , FakeSnapshotName , FakeVolID , map [string ]string {cinderCSIClusterIDKey : "cluster" }).Return (& FakeSnapshotRes , nil )
805
849
osmock .On ("ListSnapshots" , map [string ]string {"Name" : FakeSnapshotName }).Return (FakeSnapshotListEmpty , "" , nil )
806
850
osmock .On ("WaitSnapshotReady" , FakeSnapshotID ).Return (FakeSnapshotRes .Status , nil )
@@ -944,7 +988,6 @@ func TestControllerExpandVolume(t *testing.T) {
944
988
945
989
// Assert
946
990
assert .Equal (expectedRes , actualRes )
947
-
948
991
}
949
992
950
993
func TestValidateVolumeCapabilities (t * testing.T ) {
@@ -1000,13 +1043,11 @@ func TestValidateVolumeCapabilities(t *testing.T) {
1000
1043
}
1001
1044
1002
1045
actualRes2 , err := fakeCs .ValidateVolumeCapabilities (FakeCtx , fakereq2 )
1003
-
1004
1046
if err != nil {
1005
1047
t .Errorf ("failed to ValidateVolumeCapabilities: %v" , err )
1006
1048
}
1007
1049
1008
1050
// assert
1009
1051
assert .Equal (expectedRes , actualRes )
1010
1052
assert .Equal (expectedRes2 , actualRes2 )
1011
-
1012
1053
}
0 commit comments