@@ -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 {
@@ -1094,3 +1099,183 @@ func TestDeleteVPCLoadBalancerPoolMember(t *testing.T) {
10941099 })
10951100 })
10961101}
1102+
1103+ func TestGetVolumeAttachments (t * testing.T ) {
1104+ setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
1105+ t .Helper ()
1106+ return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
1107+ }
1108+
1109+ vpcMachine := infrav1.IBMVPCMachine {
1110+ Status : infrav1.IBMVPCMachineStatus {
1111+ InstanceID : "foo-instance-id" ,
1112+ },
1113+ }
1114+ volumeAttachmentName := "foo-volume-attachment"
1115+
1116+ testVolumeAttachments := vpcv1.VolumeAttachmentCollection {
1117+ VolumeAttachments : []vpcv1.VolumeAttachment {{
1118+ Name : & volumeAttachmentName ,
1119+ },
1120+ {
1121+ Name : & volumeName ,
1122+ }},
1123+ }
1124+
1125+ t .Run ("Return List of Volume Attachments for Machine" , func (t * testing.T ) {
1126+ g := NewWithT (t )
1127+ mockController , mockVPC := setup (t )
1128+ t .Cleanup (mockController .Finish )
1129+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1130+ scope .IBMVPCMachine .Status = vpcMachine .Status
1131+ mockVPC .EXPECT ().GetVolumeAttachments (gomock .AssignableToTypeOf (& vpcv1.ListInstanceVolumeAttachmentsOptions {})).Return (& testVolumeAttachments , nil , nil )
1132+ attachments , err := scope .GetVolumeAttachments ()
1133+ g .Expect (attachments ).To (Equal (testVolumeAttachments .VolumeAttachments ))
1134+ g .Expect (err ).Should (Succeed ())
1135+ })
1136+
1137+ t .Run ("Return Error when GetVolumeAttachments fails" , func (t * testing.T ) {
1138+ g := NewWithT (t )
1139+ mockController , mockVPC := setup (t )
1140+ t .Cleanup (mockController .Finish )
1141+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1142+ scope .IBMVPCMachine .Status = vpcMachine .Status
1143+ mockVPC .EXPECT ().GetVolumeAttachments (gomock .AssignableToTypeOf (& vpcv1.ListInstanceVolumeAttachmentsOptions {})).Return (nil , nil , errors .New ("Error when getting volume attachments" ))
1144+ attachments , err := scope .GetVolumeAttachments ()
1145+ g .Expect (attachments ).To (BeNil ())
1146+ g .Expect (err ).ShouldNot (Succeed ())
1147+ })
1148+ }
1149+
1150+ func TestGetVolumeState (t * testing.T ) {
1151+ setup := func (t * testing.T ) (* gomock.Controller , * mock.MockVpc ) {
1152+ t .Helper ()
1153+ return gomock .NewController (t ), mock .NewMockVpc (gomock .NewController (t ))
1154+ }
1155+
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+ }
1201+
1202+ vpcMachine := infrav1.IBMVPCMachine {
1203+ Status : infrav1.IBMVPCMachineStatus {
1204+ InstanceID : "foo-instance-id" ,
1205+ },
1206+ }
1207+
1208+ infraVolume := infrav1.VPCVolume {
1209+ Name : volumeName ,
1210+ Profile : "custom" ,
1211+ Iops : 100 ,
1212+ SizeGiB : 50 ,
1213+ }
1214+ pendingState := vpcv1 .VolumeStatusPendingConst
1215+
1216+ vpcVolume := vpcv1.Volume {
1217+ Name : & volumeName ,
1218+ ID : & volumeID ,
1219+ Status : & pendingState ,
1220+ }
1221+
1222+ volumeCreationError := errors .New ("error while creating volume" )
1223+ t .Run ("Volume creation is successful" , func (t * testing.T ) {
1224+ g := NewWithT (t )
1225+ mockController , mockVPC := setup (t )
1226+ t .Cleanup (mockController .Finish )
1227+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1228+ mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (& vpcVolume , nil , nil )
1229+ id , err := scope .CreateVolume (& infraVolume )
1230+ g .Expect (err ).Should (Succeed ())
1231+ g .Expect (id ).Should (Equal (volumeID ))
1232+ })
1233+ t .Run ("Volume creation fails" , func (t * testing.T ) {
1234+ g := NewWithT (t )
1235+ mockController , mockVPC := setup (t )
1236+ t .Cleanup (mockController .Finish )
1237+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1238+ scope .IBMVPCMachine .Status = vpcMachine .Status
1239+ mockVPC .EXPECT ().CreateVolume (gomock .AssignableToTypeOf (& vpcv1.CreateVolumeOptions {})).Return (nil , nil , volumeCreationError )
1240+ id , err := scope .CreateVolume (& infraVolume )
1241+ g .Expect (err ).ShouldNot (Succeed ())
1242+ g .Expect (errors .Is (err , volumeCreationError )).To (BeTrue ())
1243+ g .Expect (id ).To (BeZero ())
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+ }
1252+
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 ) {
1271+ g := NewWithT (t )
1272+ mockController , mockVPC := setup (t )
1273+ t .Cleanup (mockController .Finish )
1274+ scope := setupMachineScope (clusterName , machineName , mockVPC )
1275+ scope .IBMVPCMachine .Status = vpcMachine .Status
1276+ mockVPC .EXPECT ().AttachVolumeToInstance (gomock .AssignableToTypeOf (& vpcv1.CreateInstanceVolumeAttachmentOptions {})).Return (nil , nil , volumeAttachmentError )
1277+ err := scope .AttachVolume (deleteOnInstanceDelete , volumeID , volumeName )
1278+ g .Expect (err ).ShouldNot (Succeed ())
1279+ g .Expect (errors .Is (err , volumeAttachmentError )).To (BeTrue ())
1280+ })
1281+ }
0 commit comments