@@ -43,6 +43,11 @@ import (
4343 . "github.com/onsi/gomega"
4444)
4545
46+ var (
47+ volumeName = "foo-volume"
48+ volumeID = "foo-volume-id"
49+ )
50+
4651func newVPCMachine (clusterName , machineName string ) * infrav1.IBMVPCMachine {
4752 return & infrav1.IBMVPCMachine {
4853 ObjectMeta : metav1.ObjectMeta {
@@ -1103,13 +1108,12 @@ func TestGetVolumeAttachments(t *testing.T) {
11031108 return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
11041109 }
11051110
1106- vpcMachine := infrav1beta2 .IBMVPCMachine {
1107- Status : infrav1beta2 .IBMVPCMachineStatus {
1111+ vpcMachine := infrav1 .IBMVPCMachine {
1112+ Status : infrav1 .IBMVPCMachineStatus {
11081113 InstanceID : "foo-instance-id" ,
11091114 },
11101115 }
11111116 volumeAttachmentName := "foo-volume-attachment"
1112- volumeName := "foo-volume"
11131117
11141118 testVolumeAttachments := vpcv1.VolumeAttachmentCollection {
11151119 VolumeAttachments : []vpcv1.VolumeAttachment {{
@@ -1145,70 +1149,134 @@ func TestGetVolumeAttachments(t *testing.T) {
11451149 })
11461150}
11471151
1148- func TestCreateAndAttachVolume (t * testing.T ) {
1152+ func TestGetVolumeState (t * testing.T ) {
11491153 setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
11501154 t .Helper ()
11511155 return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
11521156 }
11531157
1154- volumeName := "foo-volume"
1155- volumeID := "foo-volume-id"
1158+ volumeStatus := vpcv1 .VolumeStatusPendingConst
11561159
1157- vpcMachine := infrav1beta2 .IBMVPCMachine {
1158- Status : infrav1beta2 .IBMVPCMachineStatus {
1160+ vpcMachine := infrav1 .IBMVPCMachine {
1161+ Status : infrav1 .IBMVPCMachineStatus {
11591162 InstanceID : "foo-instance-id" ,
11601163 },
11611164 }
11621165
1163- infraVolume := infrav1beta2.VPCVolume {
1164- Name : volumeName ,
1166+ vpcVolume := vpcv1.Volume {
1167+ Name : & volumeName ,
1168+ ID : & volumeID ,
1169+ Status : & volumeStatus ,
11651170 }
1171+ volumeFetchError := errors .New ("error while fetching volume" )
11661172
1167- vpcVolume := vpcv1.Volume {
1168- Name : & volumeName ,
1169- ID : & volumeID ,
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 ))
11701202 }
11711203
1172- volumeCreationError := errors .New ("error while creating volume" )
1204+ vpcMachine := infrav1.IBMVPCMachine {
1205+ Status : infrav1.IBMVPCMachineStatus {
1206+ InstanceID : "foo-instance-id" ,
1207+ },
1208+ }
11731209
1174- volumeAttachmentError := errors .New ("error while attaching volume" )
1210+ infraVolume := infrav1.VPCVolume {
1211+ Name : volumeName ,
1212+ Profile : "custom" ,
1213+ Iops : 100 ,
1214+ SizeGiB : 50 ,
1215+ }
1216+ pendingState := vpcv1 .VolumeStatusPendingConst
1217+
1218+ vpcVolume := vpcv1.Volume {
1219+ Name : & volumeName ,
1220+ ID : & volumeID ,
1221+ Status : & pendingState ,
1222+ }
11751223
1176- t .Run ("Volume creation and attachment is successful" , func (t * testing.T ) {
1224+ volumeCreationError := errors .New ("error while creating volume" )
1225+ t .Run ("Volume creation is successful" , func (t * testing.T ) {
11771226 g := NewWithT (t )
11781227 mockController , mockVPC := setup (t )
11791228 t .Cleanup (mockController .Finish )
11801229 scope := setupMachineScope (clusterName , machineName , mockVPC )
1181- scope .IBMVPCMachine .Status = vpcMachine .Status
11821230 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 )
1231+ id , err := scope .CreateVolume (& infraVolume )
11861232 g .Expect (err ).Should (Succeed ())
1233+ g .Expect (id ).Should (Equal (volumeID ))
11871234 })
1188-
1189- t .Run ("Volume Creation Fails" , func (t * testing.T ) {
1235+ t .Run ("Volume creation fails" , func (t * testing.T ) {
11901236 g := NewWithT (t )
11911237 mockController , mockVPC := setup (t )
11921238 t .Cleanup (mockController .Finish )
11931239 scope := setupMachineScope (clusterName , machineName , mockVPC )
11941240 scope .IBMVPCMachine .Status = vpcMachine .Status
11951241 mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (nil , nil , volumeCreationError )
1196-
1197- err := scope .CreateAndAttachVolume (& infraVolume )
1242+ id , err := scope .CreateVolume (& infraVolume )
11981243 g .Expect (err ).ShouldNot (Succeed ())
11991244 g .Expect (errors .Is (err , volumeCreationError )).To (BeTrue ())
1245+ g .Expect (id ).To (BeZero ())
12001246 })
1247+ }
1248+
1249+ func TestAttachVolume (t * testing.T ) {
1250+ setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
1251+ t .Helper ()
1252+ return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
1253+ }
12011254
1202- t .Run ("Volume Attachment Fails" , func (t * testing.T ) {
1255+ deleteOnInstanceDelete := true
1256+ vpcMachine := infrav1.IBMVPCMachine {
1257+ Status : infrav1.IBMVPCMachineStatus {
1258+ InstanceID : "foo-instance-id" ,
1259+ },
1260+ }
1261+ volumeAttachmentError := errors .New ("error while attaching volume" )
1262+ t .Run ("Volume attachment is successful" , func (t * testing.T ) {
1263+ g := NewWithT (t )
1264+ mockController , mockVPC := setup (t )
1265+ t .Cleanup (mockController .Finish )
1266+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1267+ scope .IBMVPCMachine .Status = vpcMachine .Status
1268+ mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , nil )
1269+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
1270+ g .Expect (err ).Should (Succeed ())
1271+ })
1272+ t .Run ("Volume attachment fails" , func (t * testing.T ) {
12031273 g := NewWithT (t )
12041274 mockController , mockVPC := setup (t )
12051275 t .Cleanup (mockController .Finish )
12061276 scope := setupMachineScope (clusterName , machineName , mockVPC )
12071277 scope .IBMVPCMachine .Status = vpcMachine .Status
1208- mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (& vpcVolume , nil , nil )
12091278 mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , volumeAttachmentError )
1210-
1211- err := scope .CreateAndAttachVolume (& infraVolume )
1279+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
12121280 g .Expect (err ).ShouldNot (Succeed ())
12131281 g .Expect (errors .Is (err , volumeAttachmentError )).To (BeTrue ())
12141282 })
0 commit comments