@@ -43,6 +43,11 @@ import (
43
43
. "github.com/onsi/gomega"
44
44
)
45
45
46
+ var (
47
+ volumeName = "foo-volume"
48
+ volumeID = "foo-volume-id"
49
+ )
50
+
46
51
func newVPCMachine (clusterName , machineName string ) * infrav1beta2.IBMVPCMachine {
47
52
return & infrav1beta2.IBMVPCMachine {
48
53
ObjectMeta : metav1.ObjectMeta {
@@ -1109,7 +1114,6 @@ func TestGetVolumeAttachments(t *testing.T) {
1109
1114
},
1110
1115
}
1111
1116
volumeAttachmentName := "foo-volume-attachment"
1112
- volumeName := "foo-volume"
1113
1117
1114
1118
testVolumeAttachments := vpcv1.VolumeAttachmentCollection {
1115
1119
VolumeAttachments : []vpcv1.VolumeAttachment {{
@@ -1145,14 +1149,57 @@ func TestGetVolumeAttachments(t *testing.T) {
1145
1149
})
1146
1150
}
1147
1151
1148
- func TestCreateAndAttachVolume (t * testing.T ) {
1152
+ func TestGetVolumeState (t * testing.T ) {
1149
1153
setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
1150
1154
t .Helper ()
1151
1155
return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
1152
1156
}
1153
1157
1154
- volumeName := "foo-volume"
1155
- volumeID := "foo-volume-id"
1158
+ volumeStatus := vpcv1 .VolumeStatusPendingConst
1159
+
1160
+ vpcMachine := infrav1beta2.IBMVPCMachine {
1161
+ Status : infrav1beta2.IBMVPCMachineStatus {
1162
+ InstanceID : "foo-instance-id" ,
1163
+ },
1164
+ }
1165
+
1166
+ vpcVolume := vpcv1.Volume {
1167
+ Name : & volumeName ,
1168
+ ID : & volumeID ,
1169
+ Status : & volumeStatus ,
1170
+ }
1171
+ volumeFetchError := errors .New ("error while fetching volume" )
1172
+
1173
+ t .Run ("Return correct volume state" , func (t * testing.T ) {
1174
+ g := NewWithT (t )
1175
+ mockController , mockVPC := setup (t )
1176
+ t .Cleanup (mockController .Finish )
1177
+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1178
+ scope .IBMVPCMachine .Status = vpcMachine .Status
1179
+ mockVPC .EXPECT ().GetVolume (gomock .AssignableToTypeOf (& vpcv1.GetVolumeOptions {})).Return (& vpcVolume , nil , nil )
1180
+ state , err := scope .GetVolumeState (volumeID )
1181
+ g .Expect (err ).To (BeNil ())
1182
+ g .Expect (state ).To (Equal (volumeStatus ))
1183
+ })
1184
+
1185
+ t .Run ("Return error when GetVolumeState returns error" , func (t * testing.T ) {
1186
+ g := NewWithT (t )
1187
+ mockController , mockVPC := setup (t )
1188
+ t .Cleanup (mockController .Finish )
1189
+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1190
+ scope .IBMVPCMachine .Status = vpcMachine .Status
1191
+ mockVPC .EXPECT ().GetVolume (gomock .AssignableToTypeOf (& vpcv1.GetVolumeOptions {})).Return (nil , nil , volumeFetchError )
1192
+ state , err := scope .GetVolumeState (volumeID )
1193
+ g .Expect (state ).To (BeZero ())
1194
+ g .Expect (errors .Is (err , volumeFetchError )).To (BeTrue ())
1195
+ })
1196
+ }
1197
+
1198
+ func TestCreateVolume (t * testing.T ) {
1199
+ setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
1200
+ t .Helper ()
1201
+ return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
1202
+ }
1156
1203
1157
1204
vpcMachine := infrav1beta2.IBMVPCMachine {
1158
1205
Status : infrav1beta2.IBMVPCMachineStatus {
@@ -1161,54 +1208,79 @@ func TestCreateAndAttachVolume(t *testing.T) {
1161
1208
}
1162
1209
1163
1210
infraVolume := infrav1beta2.VPCVolume {
1164
- Name : volumeName ,
1211
+ Name : volumeName ,
1212
+ Profile : "custom" ,
1213
+ Iops : 100 ,
1214
+ SizeGiB : 50 ,
1165
1215
}
1216
+ pendingState := vpcv1 .VolumeStatusPendingConst
1166
1217
1167
1218
vpcVolume := vpcv1.Volume {
1168
- Name : & volumeName ,
1169
- ID : & volumeID ,
1219
+ Name : & volumeName ,
1220
+ ID : & volumeID ,
1221
+ Status : & pendingState ,
1170
1222
}
1171
1223
1172
1224
volumeCreationError := errors .New ("error while creating volume" )
1173
-
1174
- volumeAttachmentError := errors .New ("error while attaching volume" )
1175
-
1176
- t .Run ("Volume creation and attachment is successful" , func (t * testing.T ) {
1225
+ volumeNotAvailableError := errors .New ("volume is not in available state" )
1226
+ t .Run ("Volume creation is successful, volume is not yet in available state" , func (t * testing.T ) {
1177
1227
g := NewWithT (t )
1178
1228
mockController , mockVPC := setup (t )
1179
1229
t .Cleanup (mockController .Finish )
1180
1230
scope := setupMachineScope (clusterName , machineName , mockVPC )
1181
1231
scope .IBMVPCMachine .Status = vpcMachine .Status
1182
1232
mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (& vpcVolume , nil , nil )
1183
- mockVPC . EXPECT (). AttachVolumeToInstance ( gomock . AssignableToTypeOf ( & vpcv1. CreateInstanceVolumeAttachmentOptions {})). Return ( nil , nil , nil )
1184
-
1185
- err := scope . CreateAndAttachVolume ( & infraVolume )
1186
- g .Expect (err ).Should (Succeed ( ))
1233
+ id , err := scope . CreateVolume ( & infraVolume )
1234
+ g . Expect ( err ). ShouldNot ( Succeed ())
1235
+ g . Expect ( err ). To ( Equal ( volumeNotAvailableError ) )
1236
+ g .Expect (id ).Should (Equal ( volumeID ))
1187
1237
})
1188
-
1189
- t .Run ("Volume Creation Fails" , func (t * testing.T ) {
1238
+ t .Run ("Volume creation fails" , func (t * testing.T ) {
1190
1239
g := NewWithT (t )
1191
1240
mockController , mockVPC := setup (t )
1192
1241
t .Cleanup (mockController .Finish )
1193
1242
scope := setupMachineScope (clusterName , machineName , mockVPC )
1194
1243
scope .IBMVPCMachine .Status = vpcMachine .Status
1195
1244
mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (nil , nil , volumeCreationError )
1196
1245
1197
- err := scope .CreateAndAttachVolume (& infraVolume )
1246
+ id , err := scope .CreateVolume (& infraVolume )
1198
1247
g .Expect (err ).ShouldNot (Succeed ())
1199
1248
g .Expect (errors .Is (err , volumeCreationError )).To (BeTrue ())
1249
+ g .Expect (id ).To (BeZero ())
1200
1250
})
1251
+ }
1252
+
1253
+ func TestAttachVolume (t * testing.T ) {
1254
+ setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
1255
+ t .Helper ()
1256
+ return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
1257
+ }
1201
1258
1202
- t .Run ("Volume Attachment Fails" , func (t * testing.T ) {
1259
+ deleteOnInstanceDelete := true
1260
+ vpcMachine := infrav1beta2.IBMVPCMachine {
1261
+ Status : infrav1beta2.IBMVPCMachineStatus {
1262
+ InstanceID : "foo-instance-id" ,
1263
+ },
1264
+ }
1265
+ volumeAttachmentError := errors .New ("error while attaching volume" )
1266
+ t .Run ("Volume attachment is successful" , func (t * testing.T ) {
1267
+ g := NewWithT (t )
1268
+ mockController , mockVPC := setup (t )
1269
+ t .Cleanup (mockController .Finish )
1270
+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1271
+ scope .IBMVPCMachine .Status = vpcMachine .Status
1272
+ mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , nil )
1273
+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
1274
+ g .Expect (err ).Should (Succeed ())
1275
+ })
1276
+ t .Run ("Volume attachment fails" , func (t * testing.T ) {
1203
1277
g := NewWithT (t )
1204
1278
mockController , mockVPC := setup (t )
1205
1279
t .Cleanup (mockController .Finish )
1206
1280
scope := setupMachineScope (clusterName , machineName , mockVPC )
1207
1281
scope .IBMVPCMachine .Status = vpcMachine .Status
1208
- mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (& vpcVolume , nil , nil )
1209
1282
mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , volumeAttachmentError )
1210
-
1211
- err := scope .CreateAndAttachVolume (& infraVolume )
1283
+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
1212
1284
g .Expect (err ).ShouldNot (Succeed ())
1213
1285
g .Expect (errors .Is (err , volumeAttachmentError )).To (BeTrue ())
1214
1286
})
0 commit comments