@@ -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 ) * infrav1beta2.IBMVPCMachine {
4752 return & infrav1beta2.IBMVPCMachine {
4853 ObjectMeta : metav1.ObjectMeta {
@@ -1109,7 +1114,6 @@ func TestGetVolumeAttachments(t *testing.T) {
11091114 },
11101115 }
11111116 volumeAttachmentName := "foo-volume-attachment"
1112- volumeName := "foo-volume"
11131117
11141118 testVolumeAttachments := vpcv1.VolumeAttachmentCollection {
11151119 VolumeAttachments : []vpcv1.VolumeAttachment {{
@@ -1145,14 +1149,57 @@ 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
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+ }
11561203
11571204 vpcMachine := infrav1beta2.IBMVPCMachine {
11581205 Status : infrav1beta2.IBMVPCMachineStatus {
@@ -1161,54 +1208,79 @@ func TestCreateAndAttachVolume(t *testing.T) {
11611208 }
11621209
11631210 infraVolume := infrav1beta2.VPCVolume {
1164- Name : volumeName ,
1211+ Name : volumeName ,
1212+ Profile : "custom" ,
1213+ Iops : 100 ,
1214+ SizeGiB : 50 ,
11651215 }
1216+ pendingState := vpcv1 .VolumeStatusPendingConst
11661217
11671218 vpcVolume := vpcv1.Volume {
1168- Name : & volumeName ,
1169- ID : & volumeID ,
1219+ Name : & volumeName ,
1220+ ID : & volumeID ,
1221+ Status : & pendingState ,
11701222 }
11711223
11721224 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 ) {
11771227 g := NewWithT (t )
11781228 mockController , mockVPC := setup (t )
11791229 t .Cleanup (mockController .Finish )
11801230 scope := setupMachineScope (clusterName , machineName , mockVPC )
11811231 scope .IBMVPCMachine .Status = vpcMachine .Status
11821232 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 ))
11871237 })
1188-
1189- t .Run ("Volume Creation Fails" , func (t * testing.T ) {
1238+ t .Run ("Volume creation fails" , func (t * testing.T ) {
11901239 g := NewWithT (t )
11911240 mockController , mockVPC := setup (t )
11921241 t .Cleanup (mockController .Finish )
11931242 scope := setupMachineScope (clusterName , machineName , mockVPC )
11941243 scope .IBMVPCMachine .Status = vpcMachine .Status
11951244 mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (nil , nil , volumeCreationError )
11961245
1197- err := scope .CreateAndAttachVolume (& infraVolume )
1246+ id , err := scope .CreateVolume (& infraVolume )
11981247 g .Expect (err ).ShouldNot (Succeed ())
11991248 g .Expect (errors .Is (err , volumeCreationError )).To (BeTrue ())
1249+ g .Expect (id ).To (BeZero ())
12001250 })
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+ }
12011258
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 ) {
12031277 g := NewWithT (t )
12041278 mockController , mockVPC := setup (t )
12051279 t .Cleanup (mockController .Finish )
12061280 scope := setupMachineScope (clusterName , machineName , mockVPC )
12071281 scope .IBMVPCMachine .Status = vpcMachine .Status
1208- mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (& vpcVolume , nil , nil )
12091282 mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , volumeAttachmentError )
1210-
1211- err := scope .CreateAndAttachVolume (& infraVolume )
1283+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
12121284 g .Expect (err ).ShouldNot (Succeed ())
12131285 g .Expect (errors .Is (err , volumeAttachmentError )).To (BeTrue ())
12141286 })
0 commit comments