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