@@ -42,6 +42,11 @@ import (
4242 . "github.com/onsi/gomega"
4343)
4444
45+ var (
46+ volumeName = "foo-volume"
47+ volumeID = "foo-volume-id"
48+ )
49+
4550func newVPCMachine (clusterName , machineName string ) * infrav1.IBMVPCMachine {
4651 return & infrav1.IBMVPCMachine {
4752 ObjectMeta : metav1.ObjectMeta {
@@ -1107,7 +1112,6 @@ func TestGetVolumeAttachments(t *testing.T) {
11071112 },
11081113 }
11091114 volumeAttachmentName := "foo-volume-attachment"
1110- volumeName := "foo-volume"
11111115
11121116 testVolumeAttachments := vpcv1.VolumeAttachmentCollection {
11131117 VolumeAttachments : []vpcv1.VolumeAttachment {{
@@ -1143,14 +1147,57 @@ func TestGetVolumeAttachments(t *testing.T) {
11431147 })
11441148}
11451149
1146- func TestCreateAndAttachVolume (t * testing.T ) {
1150+ func TestGetVolumeState (t * testing.T ) {
11471151 setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
11481152 t .Helper ()
11491153 return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
11501154 }
11511155
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+ }
11541201
11551202 vpcMachine := infrav1.IBMVPCMachine {
11561203 Status : infrav1.IBMVPCMachineStatus {
@@ -1159,54 +1206,75 @@ func TestCreateAndAttachVolume(t *testing.T) {
11591206 }
11601207
11611208 infraVolume := infrav1.VPCVolume {
1162- Name : volumeName ,
1209+ Name : volumeName ,
1210+ Profile : "custom" ,
1211+ Iops : 100 ,
1212+ SizeGiB : 50 ,
11631213 }
1214+ pendingState := vpcv1 .VolumeStatusPendingConst
11641215
11651216 vpcVolume := vpcv1.Volume {
1166- Name : & volumeName ,
1167- ID : & volumeID ,
1217+ Name : & volumeName ,
1218+ ID : & volumeID ,
1219+ Status : & pendingState ,
11681220 }
11691221
11701222 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 ) {
11751224 g := NewWithT (t )
11761225 mockController , mockVPC := setup (t )
11771226 t .Cleanup (mockController .Finish )
11781227 scope := setupMachineScope (clusterName , machineName , mockVPC )
1179- scope .IBMVPCMachine .Status = vpcMachine .Status
11801228 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 )
11841230 g .Expect (err ).Should (Succeed ())
1231+ g .Expect (id ).Should (Equal (volumeID ))
11851232 })
1186-
1187- t .Run ("Volume Creation Fails" , func (t * testing.T ) {
1233+ t .Run ("Volume creation fails" , func (t * testing.T ) {
11881234 g := NewWithT (t )
11891235 mockController , mockVPC := setup (t )
11901236 t .Cleanup (mockController .Finish )
11911237 scope := setupMachineScope (clusterName , machineName , mockVPC )
11921238 scope .IBMVPCMachine .Status = vpcMachine .Status
11931239 mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (nil , nil , volumeCreationError )
1194-
1195- err := scope .CreateAndAttachVolume (& infraVolume )
1240+ id , err := scope .CreateVolume (& infraVolume )
11961241 g .Expect (err ).ShouldNot (Succeed ())
11971242 g .Expect (errors .Is (err , volumeCreationError )).To (BeTrue ())
1243+ g .Expect (id ).To (BeZero ())
11981244 })
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+ }
11991252
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 ) {
12011271 g := NewWithT (t )
12021272 mockController , mockVPC := setup (t )
12031273 t .Cleanup (mockController .Finish )
12041274 scope := setupMachineScope (clusterName , machineName , mockVPC )
12051275 scope .IBMVPCMachine .Status = vpcMachine .Status
1206- mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (& vpcVolume , nil , nil )
12071276 mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , volumeAttachmentError )
1208-
1209- err := scope .CreateAndAttachVolume (& infraVolume )
1277+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
12101278 g .Expect (err ).ShouldNot (Succeed ())
12111279 g .Expect (errors .Is (err , volumeAttachmentError )).To (BeTrue ())
12121280 })
0 commit comments