35
35
)
36
36
37
37
func TestModify (t * testing.T ) {
38
- basePVC := createTestPVC (pvcName , testVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ )
38
+ basePVC := createTestPVC (pvcName , testVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ , "" /*modifyVolumeStatus*/ )
39
39
basePV := createTestPV (1 , pvcName , pvcNamespace , "foobaz" /*pvcUID*/ , & fsVolumeMode , testVac )
40
40
41
41
var tests = []struct {
@@ -61,7 +61,7 @@ func TestModify(t *testing.T) {
61
61
},
62
62
{
63
63
name : "vac does not exist, no modification and set ModifyVolumeStatus to pending" ,
64
- pvc : createTestPVC (pvcName , targetVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ ),
64
+ pvc : createTestPVC (pvcName , targetVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ , "" /*modifyVolumeStatus*/ ),
65
65
pv : basePV ,
66
66
expectModifyCall : false ,
67
67
expectedModifyVolumeStatus : & v1.ModifyVolumeStatus {
@@ -73,7 +73,7 @@ func TestModify(t *testing.T) {
73
73
},
74
74
{
75
75
name : "modify volume success" ,
76
- pvc : createTestPVC (pvcName , targetVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ ),
76
+ pvc : createTestPVC (pvcName , targetVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ , "" /*modifyVolumeStatus*/ ),
77
77
pv : basePV ,
78
78
vacExists : true ,
79
79
expectModifyCall : true ,
@@ -83,7 +83,7 @@ func TestModify(t *testing.T) {
83
83
},
84
84
{
85
85
name : "modify volume success with extra metadata" ,
86
- pvc : createTestPVC (pvcName , targetVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ ),
86
+ pvc : createTestPVC (pvcName , targetVac /*vacName*/ , testVac /*curVacName*/ , testVac /*targetVacName*/ , "" /*modifyVolumeStatus*/ ),
87
87
pv : basePV ,
88
88
vacExists : true ,
89
89
expectModifyCall : true ,
@@ -98,6 +98,26 @@ func TestModify(t *testing.T) {
98
98
"csi.storage.k8s.io/pv/name" : "testPV" ,
99
99
},
100
100
},
101
+ {
102
+ name : "modify volume rollback succeeds for infeasible errors" ,
103
+ pvc : createTestPVC (pvcName , testVac /*vacName*/ , testVac /*curVacName*/ , targetVac /*targetVacName*/ , v1 .PersistentVolumeClaimModifyVolumeInfeasible ),
104
+ pv : basePV ,
105
+ vacExists : true ,
106
+ expectModifyCall : false ,
107
+ expectedModifyVolumeStatus : nil ,
108
+ expectedCurrentVolumeAttributesClassName : & testVac ,
109
+ expectedPVVolumeAttributesClassName : & testVac ,
110
+ },
111
+ {
112
+ name : "modify volume rollback to nil succeeds for infeasible errors" ,
113
+ pvc : createTestPVC (pvcName , "" /*vacName*/ , "" /*curVacName*/ , targetVac /*targetVacName*/ , v1 .PersistentVolumeClaimModifyVolumeInfeasible ),
114
+ pv : createTestPV (1 , pvcName , pvcNamespace , "foobaz" /*pvcUID*/ , & fsVolumeMode , "" ),
115
+ vacExists : true ,
116
+ expectModifyCall : false ,
117
+ expectedModifyVolumeStatus : nil ,
118
+ expectedCurrentVolumeAttributesClassName : nil ,
119
+ expectedPVVolumeAttributesClassName : nil ,
120
+ },
101
121
}
102
122
103
123
for i := range tests {
@@ -130,16 +150,20 @@ func TestModify(t *testing.T) {
130
150
131
151
actualCurrentVolumeAttributesClassName := pvc .Status .CurrentVolumeAttributesClassName
132
152
133
- if diff := cmp .Diff (* test .expectedCurrentVolumeAttributesClassName , * actualCurrentVolumeAttributesClassName ); diff != "" {
134
- t .Errorf ("expected CurrentVolumeAttributesClassName to be %v, got %v" , * test .expectedCurrentVolumeAttributesClassName , * actualCurrentVolumeAttributesClassName )
153
+ if test .expectedCurrentVolumeAttributesClassName != nil && actualCurrentVolumeAttributesClassName != nil {
154
+ if diff := cmp .Diff (* test .expectedCurrentVolumeAttributesClassName , * actualCurrentVolumeAttributesClassName ); diff != "" {
155
+ t .Errorf ("expected CurrentVolumeAttributesClassName to be %v, got %v" , * test .expectedCurrentVolumeAttributesClassName , * actualCurrentVolumeAttributesClassName )
156
+ }
135
157
}
136
158
137
159
actualPVVolumeAttributesClassName := pv .Spec .VolumeAttributesClassName
138
- if diff := cmp .Diff (* test .expectedPVVolumeAttributesClassName , * actualPVVolumeAttributesClassName ); diff != "" {
139
- t .Errorf ("expected VolumeAttributesClassName of pv to be %v, got %v" , * test .expectedPVVolumeAttributesClassName , * actualPVVolumeAttributesClassName )
160
+ if test .expectedPVVolumeAttributesClassName != nil && actualPVVolumeAttributesClassName != nil {
161
+ if diff := cmp .Diff (* test .expectedPVVolumeAttributesClassName , * actualPVVolumeAttributesClassName ); diff != "" {
162
+ t .Errorf ("expected VolumeAttributesClassName of pv to be %v, got %v" , * test .expectedPVVolumeAttributesClassName , * actualPVVolumeAttributesClassName )
163
+ }
140
164
}
141
165
142
- if test .withExtraMetadata {
166
+ if test .withExtraMetadata && test . expectedPVVolumeAttributesClassName != nil {
143
167
vacObj , err := ctrlInstance .vacLister .Get (* test .expectedPVVolumeAttributesClassName )
144
168
if err != nil {
145
169
t .Errorf ("failed to get VAC: %v" , err )
@@ -154,7 +178,7 @@ func TestModify(t *testing.T) {
154
178
}
155
179
}
156
180
157
- func createTestPVC (pvcName string , vacName string , curVacName string , targetVacName string ) * v1.PersistentVolumeClaim {
181
+ func createTestPVC (pvcName string , vacName string , curVacName string , targetVacName string , modifyVolumeStatus v1. PersistentVolumeClaimModifyVolumeStatus ) * v1.PersistentVolumeClaim {
158
182
pvc := & v1.PersistentVolumeClaim {
159
183
ObjectMeta : metav1.ObjectMeta {Name : pvcName , Namespace : pvcNamespace },
160
184
Spec : v1.PersistentVolumeClaimSpec {
@@ -167,21 +191,25 @@ func createTestPVC(pvcName string, vacName string, curVacName string, targetVacN
167
191
v1 .ResourceName (v1 .ResourceStorage ): resource .MustParse ("2Gi" ),
168
192
},
169
193
},
170
- VolumeAttributesClassName : & vacName ,
171
- VolumeName : pvName ,
194
+ VolumeName : pvName ,
172
195
},
173
196
Status : v1.PersistentVolumeClaimStatus {
174
197
Phase : v1 .ClaimBound ,
175
198
Capacity : v1.ResourceList {
176
199
v1 .ResourceStorage : resource .MustParse ("2Gi" ),
177
200
},
178
- CurrentVolumeAttributesClassName : & curVacName ,
179
201
ModifyVolumeStatus : & v1.ModifyVolumeStatus {
180
202
TargetVolumeAttributesClassName : targetVacName ,
181
- Status : "" ,
203
+ Status : modifyVolumeStatus ,
182
204
},
183
205
},
184
206
}
207
+ if vacName != "" {
208
+ pvc .Spec .VolumeAttributesClassName = & vacName
209
+ }
210
+ if curVacName != "" {
211
+ pvc .Status .CurrentVolumeAttributesClassName = & curVacName
212
+ }
185
213
return pvc
186
214
}
187
215
0 commit comments