Skip to content

Commit

Permalink
fix subnet revert in e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
bwagner5 committed Aug 30, 2023
1 parent 30a8f91 commit 9023362
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions test/suites/integration/networkinterfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var _ = Describe("NetworkInterfaces", func() {
subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": settings.FromContext(env.Context).ClusterName})
Expect(len(subnets)).ToNot(Equal(0))
allSubnets := lo.Flatten(lo.Values(subnets))
ExpectAssociatePublicIPAddressToBe(false, allSubnets...)
subnetCleanUpFn := ExpectAssociatePublicIPAddressToBe(false, allSubnets...)
defer subnetCleanUpFn()

provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{
AWS: v1alpha1.AWS{
Expand All @@ -58,7 +59,8 @@ var _ = Describe("NetworkInterfaces", func() {
subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": settings.FromContext(env.Context).ClusterName})
Expect(len(subnets)).ToNot(Equal(0))
allSubnets := lo.Flatten(lo.Values(subnets))
ExpectAssociatePublicIPAddressToBe(true, allSubnets...)
subnetCleanUpFn := ExpectAssociatePublicIPAddressToBe(true, allSubnets...)
defer subnetCleanUpFn()

provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{
AWS: v1alpha1.AWS{
Expand All @@ -82,7 +84,8 @@ var _ = Describe("NetworkInterfaces", func() {
subnets := env.GetSubnets(map[string]string{"karpenter.sh/discovery": settings.FromContext(env.Context).ClusterName})
Expect(len(subnets)).ToNot(Equal(0))
allSubnets := lo.Flatten(lo.Values(subnets))
ExpectAssociatePublicIPAddressToBe(false, allSubnets...)
subnetCleanUpFn := ExpectAssociatePublicIPAddressToBe(false, allSubnets...)
defer subnetCleanUpFn()

desc := "a test network interface"
provider := awstest.AWSNodeTemplate(v1alpha1.AWSNodeTemplateSpec{
Expand Down Expand Up @@ -160,14 +163,43 @@ var _ = Describe("NetworkInterfaces", func() {
})
})

func ExpectAssociatePublicIPAddressToBe(enabled bool, subnetIDs ...string) {
for subnetID := range subnetIDs {
func ExpectAssociatePublicIPAddressToBe(enabled bool, subnetIDs ...string) func() {
var subnetIDsToModify []string
// check if the subnet matches the desired "enabled" state
for _, subnetID := range subnetIDs {
subnetsOut, err := env.EC2API.DescribeSubnets(&ec2.DescribeSubnetsInput{
SubnetIds: []*string{&subnetID},

Check failure on line 171 in test/suites/integration/networkinterfaces_test.go

View workflow job for this annotation

GitHub Actions / ci

G601: Implicit memory aliasing in for loop. (gosec)
})
Expect(err).To(BeNil())
Expect(len(subnetsOut.Subnets)).To(Equal(1))
subnet := subnetsOut.Subnets[0]
if *subnet.MapPublicIpOnLaunch == enabled {
continue
}
subnetIDsToModify = append(subnetIDsToModify, subnetID)
}

// modify the subnets that do not match the desired "enabled" state
for _, subnetID := range subnetIDsToModify {
_, err := env.EC2API.ModifySubnetAttribute(&ec2.ModifySubnetAttributeInput{
MapPublicIpOnLaunch: &ec2.AttributeBooleanValue{
Value: aws.Bool(enabled),
},
SubnetId: aws.String(subnetIDs[subnetID]),
SubnetId: aws.String(subnetID),
})
Expect(err).To(BeNil())
}

// return a clean-up func to revert the modified subnets to the previous state
return func() {
for _, subnetID := range subnetIDsToModify {
_, err := env.EC2API.ModifySubnetAttribute(&ec2.ModifySubnetAttributeInput{
MapPublicIpOnLaunch: &ec2.AttributeBooleanValue{
Value: aws.Bool(!enabled),
},
SubnetId: aws.String(subnetID),
})
Expect(err).To(BeNil())
}
}
}

0 comments on commit 9023362

Please sign in to comment.