Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some test coverage to allocating ENIs #1234

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 78 additions & 22 deletions pkg/awsutils/awsutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const (
sg2 = "sg-2e080f51"
sgs = sg1 + " " + sg2
subnetID = "subnet-6b245523"
vpcCIDR = "10.0.0.0/16"
subnetCIDR = "10.0.1.0/24"
primaryeniID = "eni-00000000"
eniID = primaryeniID
Expand All @@ -68,7 +67,6 @@ const (
eni1PrivateIP = "10.0.0.1"
eni2Device = "1"
eni2PrivateIP = "10.0.0.2"
eni2AttachID = "eni-attach-fafdfafd"
eni2ID = "eni-12341234"
metadataVPCIPv4CIDRs = "192.168.0.0/16 100.66.0.0/1"
)
Expand Down Expand Up @@ -331,28 +329,64 @@ func TestTagEni(t *testing.T) {
assert.NoError(t, err)
}

func TestClusterNameTag(t *testing.T) {
ctrl, mockEC2 := setup(t)
defer ctrl.Finish()
_ = os.Setenv(clusterNameEnvVar, "cni-test")
tagKey1 := eniClusterTagKey
tagValue1 := "cni-test"
additionalEniTags := ec2.Tag{
Key: &tagKey1,
Value: &tagValue1,
}
tags := []*ec2.Tag{
{
Key: aws.String(eniNodeTagKey),
Value: aws.String(instanceID),
},
}
tags = append(tags, &additionalEniTags)
input := &ec2.CreateTagsInput{
Resources: []*string{
aws.String(eniID),
},
Tags: tags,
}

ins := &EC2InstanceMetadataCache{instanceID: instanceID, ec2SVC: mockEC2}
mockEC2.EXPECT().CreateTagsWithContext(gomock.Any(), input, gomock.Any()).Return(nil, nil)
ins.tagENI(eniID, time.Millisecond)
_ = os.Unsetenv(clusterNameEnvVar)
}

func TestAdditionalTagsEni(t *testing.T) {
ctrl, mockEC2 := setup(t)
defer ctrl.Finish()
_ = os.Setenv(additionalEniTagsEnvVar, `{"testKey": "testing"}`)
currentENIID := eniID
//result key
tagKey1 := "testKey"
//result value
tagValue1 := "testing"
tag := ec2.Tag{
additionalEniTags := ec2.Tag{
Key: &tagKey1,
Value: &tagValue1}
result := &ec2.DescribeNetworkInterfacesOutput{
NetworkInterfaces: []*ec2.NetworkInterface{{TagSet: []*ec2.Tag{&tag}}}}

ins := &EC2InstanceMetadataCache{ec2SVC: mockEC2}
mockEC2.EXPECT().CreateTagsWithContext(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
ins.tagENI(currentENIID, time.Millisecond)
Value: &tagValue1,
}
tags := []*ec2.Tag{
{
Key: aws.String(eniNodeTagKey),
Value: aws.String(instanceID),
},
}
tags = append(tags, &additionalEniTags)
input := &ec2.CreateTagsInput{
Resources: []*string{
aws.String(eniID),
},
Tags: tags,
}

// Verify the tags are registered.
assert.Equal(t, aws.StringValue(result.NetworkInterfaces[0].TagSet[0].Key), tagKey1)
assert.Equal(t, aws.StringValue(result.NetworkInterfaces[0].TagSet[0].Value), tagValue1)
ins := &EC2InstanceMetadataCache{instanceID: instanceID, ec2SVC: mockEC2}
mockEC2.EXPECT().CreateTagsWithContext(gomock.Any(), input, gomock.Any()).Return(nil, nil)
ins.tagENI(eniID, time.Millisecond)
os.Unsetenv(additionalEniTagsEnvVar)
}

func TestMapToTags(t *testing.T) {
Expand Down Expand Up @@ -606,28 +640,50 @@ func TestAllocIPAddresses(t *testing.T) {

// when required IP numbers(5) is below ENI's limit(30)
input := &ec2.AssignPrivateIpAddressesInput{
NetworkInterfaceId: aws.String("eni-id"),
NetworkInterfaceId: aws.String(eniID),
SecondaryPrivateIpAddressCount: aws.Int64(5),
}
mockEC2.EXPECT().AssignPrivateIpAddressesWithContext(gomock.Any(), input, gomock.Any()).Return(nil, nil)

ins := &EC2InstanceMetadataCache{ec2SVC: mockEC2, instanceType: "c5n.18xlarge"}
err := ins.AllocIPAddresses("eni-id", 5)
err := ins.AllocIPAddresses(eniID, 5)
assert.NoError(t, err)

// when required IP numbers(50) is higher than ENI's limit(49)
input = &ec2.AssignPrivateIpAddressesInput{
NetworkInterfaceId: aws.String("eni-id"),
NetworkInterfaceId: aws.String(eniID),
SecondaryPrivateIpAddressCount: aws.Int64(49),
}
mockEC2.EXPECT().AssignPrivateIpAddressesWithContext(gomock.Any(), input, gomock.Any()).Return(nil, nil)
addresses := make([]*ec2.AssignedPrivateIpAddress, 49)
output := ec2.AssignPrivateIpAddressesOutput{
AssignedPrivateIpAddresses: addresses,
NetworkInterfaceId: aws.String(eniID),
}
mockEC2.EXPECT().AssignPrivateIpAddressesWithContext(gomock.Any(), input, gomock.Any()).Return(&output, nil)

ins = &EC2InstanceMetadataCache{ec2SVC: mockEC2, instanceType: "c5n.18xlarge"}
err = ins.AllocIPAddresses("eni-id", 50)
err = ins.AllocIPAddresses(eniID, 50)
assert.NoError(t, err)

// Adding 0 should do nothing
err = ins.AllocIPAddresses("eni-id", 0)
err = ins.AllocIPAddresses(eniID, 0)
assert.NoError(t, err)
}

func TestAllocIPAddressesAlreadyFull(t *testing.T) {
ctrl, mockEC2 := setup(t)
defer ctrl.Finish()
// The required IP numbers(14) is the ENI's limit(14)
input := &ec2.AssignPrivateIpAddressesInput{
NetworkInterfaceId: aws.String(eniID),
SecondaryPrivateIpAddressCount: aws.Int64(14),
}
ins := &EC2InstanceMetadataCache{ec2SVC: mockEC2, instanceType: "t3.xlarge"}

retErr := awserr.New("PrivateIpAddressLimitExceeded", "Too many IPs already allocated", nil)
mockEC2.EXPECT().AssignPrivateIpAddressesWithContext(gomock.Any(), input, gomock.Any()).Return(nil, retErr)
// If EC2 says that all IPs are already attached, we do nothing
err := ins.AllocIPAddresses(eniID, 14)
assert.NoError(t, err)
}

Expand Down