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