From 996c37c72b314e8e6c23cb467b9b26778985a667 Mon Sep 17 00:00:00 2001 From: Fulton Byrne Date: Wed, 3 Oct 2018 11:45:29 -0400 Subject: [PATCH] Unit test for aws_lb security group filtering kubernetes/kubernetes#60825 --- .../providers/aws/aws_loadbalancer_test.go | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/pkg/cloudprovider/providers/aws/aws_loadbalancer_test.go b/pkg/cloudprovider/providers/aws/aws_loadbalancer_test.go index 9f81ea75cfa..cd5d79ba7a9 100644 --- a/pkg/cloudprovider/providers/aws/aws_loadbalancer_test.go +++ b/pkg/cloudprovider/providers/aws/aws_loadbalancer_test.go @@ -17,9 +17,11 @@ limitations under the License. package aws import ( + "fmt" "testing" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" ) func TestElbProtocolsAreEqual(t *testing.T) { @@ -160,3 +162,63 @@ func TestIsNLB(t *testing.T) { } } } + +func TestSecurityGroupFiltering(t *testing.T) { + grid := []struct { + in []*ec2.SecurityGroup + name string + expected int + description string + }{ + { + in: []*ec2.SecurityGroup{ + { + IpPermissions: []*ec2.IpPermission{ + { + IpRanges: []*ec2.IpRange{ + { + Description: aws.String("an unmanaged"), + }, + }, + }, + }, + }, + }, + name: "unmanaged", + expected: 0, + description: "An environment without managed LBs should have %d, but found %d SecurityGroups", + }, + { + in: []*ec2.SecurityGroup{ + { + IpPermissions: []*ec2.IpPermission{ + { + IpRanges: []*ec2.IpRange{ + { + Description: aws.String("an unmanaged"), + }, + { + Description: aws.String(fmt.Sprintf("%s=%s", NLBClientRuleDescription, "managedlb")), + }, + { + Description: aws.String(fmt.Sprintf("%s=%s", NLBHealthCheckRuleDescription, "managedlb")), + }, + }, + }, + }, + }, + }, + name: "managedlb", + expected: 1, + description: "Found %d, but should have %d Security Groups", + }, + } + + for _, g := range grid { + actual := len(filterForIPRangeDescription(g.in, g.name)) + if actual != g.expected { + t.Errorf(g.description, actual, g.expected) + } + } + +}