@@ -401,11 +401,12 @@ func TestAllocENI(t *testing.T) {
401
401
mockEC2 .EXPECT ().ModifyNetworkInterfaceAttributeWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
402
402
403
403
cache := & EC2InstanceMetadataCache {
404
- ec2SVC : mockEC2 ,
405
- imds : TypedIMDS {mockMetadata },
404
+ ec2SVC : mockEC2 ,
405
+ imds : TypedIMDS {mockMetadata },
406
+ instanceType : "c5n.18xlarge" ,
406
407
}
407
408
408
- _ , err := cache .AllocENI (false , nil , "" )
409
+ _ , err := cache .AllocENI (false , nil , "" , 5 )
409
410
assert .NoError (t , err )
410
411
}
411
412
@@ -435,11 +436,12 @@ func TestAllocENINoFreeDevice(t *testing.T) {
435
436
mockEC2 .EXPECT ().DeleteNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
436
437
437
438
cache := & EC2InstanceMetadataCache {
438
- ec2SVC : mockEC2 ,
439
- imds : TypedIMDS {mockMetadata },
439
+ ec2SVC : mockEC2 ,
440
+ imds : TypedIMDS {mockMetadata },
441
+ instanceType : "c5n.18xlarge" ,
440
442
}
441
443
442
- _ , err := cache .AllocENI (false , nil , "" )
444
+ _ , err := cache .AllocENI (false , nil , "" , 5 )
443
445
assert .Error (t , err )
444
446
}
445
447
@@ -471,11 +473,128 @@ func TestAllocENIMaxReached(t *testing.T) {
471
473
mockEC2 .EXPECT ().DeleteNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
472
474
473
475
cache := & EC2InstanceMetadataCache {
474
- ec2SVC : mockEC2 ,
475
- imds : TypedIMDS {mockMetadata },
476
+ ec2SVC : mockEC2 ,
477
+ imds : TypedIMDS {mockMetadata },
478
+ instanceType : "c5n.18xlarge" ,
476
479
}
477
480
478
- _ , err := cache .AllocENI (false , nil , "" )
481
+ _ , err := cache .AllocENI (false , nil , "" , 5 )
482
+ assert .Error (t , err )
483
+ }
484
+
485
+ func TestAllocENIWithIPAddresses (t * testing.T ) {
486
+ ctrl , mockEC2 := setup (t )
487
+ defer ctrl .Finish ()
488
+
489
+ // when required IP numbers(5) is below ENI's limit(30)
490
+ currentEniID := eniID
491
+ eni := ec2.CreateNetworkInterfaceOutput {NetworkInterface : & ec2.NetworkInterface {NetworkInterfaceId : & currentEniID }}
492
+ mockEC2 .EXPECT ().CreateNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (& eni , nil )
493
+
494
+ ec2ENIs := make ([]* ec2.InstanceNetworkInterface , 0 )
495
+ deviceNum1 := int64 (0 )
496
+ ec2ENI := & ec2.InstanceNetworkInterface {Attachment : & ec2.InstanceNetworkInterfaceAttachment {DeviceIndex : & deviceNum1 }}
497
+ ec2ENIs = append (ec2ENIs , ec2ENI )
498
+
499
+ deviceNum2 := int64 (3 )
500
+ ec2ENI = & ec2.InstanceNetworkInterface {Attachment : & ec2.InstanceNetworkInterfaceAttachment {DeviceIndex : & deviceNum2 }}
501
+ ec2ENIs = append (ec2ENIs , ec2ENI )
502
+
503
+ result := & ec2.DescribeInstancesOutput {
504
+ Reservations : []* ec2.Reservation {{Instances : []* ec2.Instance {{NetworkInterfaces : ec2ENIs }}}}}
505
+ mockEC2 .EXPECT ().DescribeInstancesWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (result , nil )
506
+ attachmentID := "eni-attach-58ddda9d"
507
+ attachResult := & ec2.AttachNetworkInterfaceOutput {
508
+ AttachmentId : & attachmentID }
509
+ mockEC2 .EXPECT ().AttachNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (attachResult , nil )
510
+ mockEC2 .EXPECT ().ModifyNetworkInterfaceAttributeWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
511
+
512
+ cache := & EC2InstanceMetadataCache {ec2SVC : mockEC2 , instanceType : "c5n.18xlarge" }
513
+ _ , err := cache .AllocENI (false , nil , subnetID , 5 )
514
+ assert .NoError (t , err )
515
+
516
+ // when required IP numbers(50) is higher than ENI's limit(49)
517
+ mockEC2 .EXPECT ().CreateNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (& eni , nil )
518
+ mockEC2 .EXPECT ().DescribeInstancesWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (result , nil )
519
+ mockEC2 .EXPECT ().AttachNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (attachResult , nil )
520
+ mockEC2 .EXPECT ().ModifyNetworkInterfaceAttributeWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
521
+ cache = & EC2InstanceMetadataCache {ec2SVC : mockEC2 , instanceType : "c5n.18xlarge" }
522
+ _ , err = cache .AllocENI (false , nil , subnetID , 49 )
523
+ assert .NoError (t , err )
524
+ }
525
+
526
+ func TestAllocENIWithIPAddressesAlreadyFull (t * testing.T ) {
527
+ ctrl , mockEC2 := setup (t )
528
+ defer ctrl .Finish ()
529
+
530
+ mockMetadata := testMetadata (nil )
531
+
532
+ retErr := awserr .New ("PrivateIpAddressLimitExceeded" , "Too many IPs already allocated" , nil )
533
+ mockEC2 .EXPECT ().CreateNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , retErr )
534
+
535
+ cache := & EC2InstanceMetadataCache {
536
+ ec2SVC : mockEC2 ,
537
+ imds : TypedIMDS {mockMetadata },
538
+ instanceType : "t3.xlarge" ,
539
+ }
540
+ _ , err := cache .AllocENI (true , nil , "" , 14 )
541
+ assert .Error (t , err )
542
+ }
543
+
544
+ func TestAllocENIWithPrefixAddresses (t * testing.T ) {
545
+ ctrl , mockEC2 := setup (t )
546
+ defer ctrl .Finish ()
547
+
548
+ mockMetadata := testMetadata (nil )
549
+
550
+ currentEniID := eniID
551
+ eni := ec2.CreateNetworkInterfaceOutput {NetworkInterface : & ec2.NetworkInterface {NetworkInterfaceId : & currentEniID }}
552
+ mockEC2 .EXPECT ().CreateNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (& eni , nil )
553
+
554
+ ec2ENIs := make ([]* ec2.InstanceNetworkInterface , 0 )
555
+ deviceNum1 := int64 (0 )
556
+ ec2ENI := & ec2.InstanceNetworkInterface {Attachment : & ec2.InstanceNetworkInterfaceAttachment {DeviceIndex : & deviceNum1 }}
557
+ ec2ENIs = append (ec2ENIs , ec2ENI )
558
+
559
+ deviceNum2 := int64 (3 )
560
+ ec2ENI = & ec2.InstanceNetworkInterface {Attachment : & ec2.InstanceNetworkInterfaceAttachment {DeviceIndex : & deviceNum2 }}
561
+ ec2ENIs = append (ec2ENIs , ec2ENI )
562
+
563
+ result := & ec2.DescribeInstancesOutput {
564
+ Reservations : []* ec2.Reservation {{Instances : []* ec2.Instance {{NetworkInterfaces : ec2ENIs }}}}}
565
+ mockEC2 .EXPECT ().DescribeInstancesWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (result , nil )
566
+ attachmentID := "eni-attach-58ddda9d"
567
+ attachResult := & ec2.AttachNetworkInterfaceOutput {
568
+ AttachmentId : & attachmentID }
569
+ mockEC2 .EXPECT ().AttachNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (attachResult , nil )
570
+ mockEC2 .EXPECT ().ModifyNetworkInterfaceAttributeWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
571
+
572
+ cache := & EC2InstanceMetadataCache {
573
+ ec2SVC : mockEC2 ,
574
+ imds : TypedIMDS {mockMetadata },
575
+ instanceType : "c5n.18xlarge" ,
576
+ enablePrefixDelegation : true ,
577
+ }
578
+ _ , err := cache .AllocENI (false , nil , subnetID , 1 )
579
+ assert .NoError (t , err )
580
+ }
581
+
582
+ func TestAllocENIWithPrefixesAlreadyFull (t * testing.T ) {
583
+ ctrl , mockEC2 := setup (t )
584
+ defer ctrl .Finish ()
585
+
586
+ mockMetadata := testMetadata (nil )
587
+
588
+ retErr := awserr .New ("PrivateIpAddressLimitExceeded" , "Too many IPs already allocated" , nil )
589
+ mockEC2 .EXPECT ().CreateNetworkInterfaceWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , retErr )
590
+
591
+ cache := & EC2InstanceMetadataCache {
592
+ ec2SVC : mockEC2 ,
593
+ imds : TypedIMDS {mockMetadata },
594
+ instanceType : "c5n.18xlarge" ,
595
+ enablePrefixDelegation : true ,
596
+ }
597
+ _ , err := cache .AllocENI (true , nil , "" , 1 )
479
598
assert .Error (t , err )
480
599
}
481
600
0 commit comments