Skip to content

Commit

Permalink
add unit test for buildEC2Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed Oct 29, 2018
1 parent d3e61ce commit 599071b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
9 changes: 5 additions & 4 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (a *Actuator) removeStoppedMachine(machine *clusterv1.Machine, client awscl
return TerminateInstances(client, instances, mLog)
}

func buildEc2Filters(inputFilters []providerconfigv1.Filter) []*ec2.Filter {
func buildEC2Filters(inputFilters []providerconfigv1.Filter) []*ec2.Filter {
filters := make([]*ec2.Filter, len(inputFilters))
for i, f := range inputFilters {
values := make([]*string, len(f.Values))
Expand All @@ -216,7 +216,7 @@ func getSecurityGroupsIDs(securityGroups []providerconfigv1.AWSResourceReference
mLog.Debug("Describing security groups based on filters")
// Get groups based on filters
describeSecurityGroupsRequest := ec2.DescribeSecurityGroupsInput{
Filters: buildEc2Filters(g.Filters),
Filters: buildEC2Filters(g.Filters),
}
describeSecurityGroupsResult, err := client.DescribeSecurityGroups(&describeSecurityGroupsRequest)
if err != nil {
Expand All @@ -242,10 +242,11 @@ func getSubnetIDs(subnet providerconfigv1.AWSResourceReference, client awsclient
// ID has priority
if subnet.ID != nil {
subnetIDs = append(subnetIDs, subnet.ID)
subnetIDs = append(subnetIDs, subnet.ID)
} else {
mLog.Debug("Describing subnets based on filters")
describeSubnetRequest := ec2.DescribeSubnetsInput{
Filters: buildEc2Filters(subnet.Filters),
Filters: buildEC2Filters(subnet.Filters),
}
describeSubnetResult, err := client.DescribeSubnets(&describeSubnetRequest)
if err != nil {
Expand Down Expand Up @@ -301,7 +302,7 @@ func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
} else if len(machineProviderConfig.AMI.Filters) > 0 {
mLog.Debug("Describing AMI based on filters")
describeImagesRequest := ec2.DescribeImagesInput{
Filters: buildEc2Filters(machineProviderConfig.AMI.Filters),
Filters: buildEC2Filters(machineProviderConfig.AMI.Filters),
}
describeAMIResult, err := client.DescribeImages(&describeImagesRequest)
if err != nil {
Expand Down
51 changes: 43 additions & 8 deletions pkg/cloud/aws/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
//"os"

"sigs.k8s.io/cluster-api-provider-aws/test/utils"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -330,6 +330,17 @@ func mockTerminateInstances(mockAWSClient *mockaws.MockClient) {
&ec2.TerminateInstancesOutput{}, nil)
}

func mockRegisterInstancesWithLoadBalancer(mockAWSClient *mockaws.MockClient, createError bool) {
if createError {
return
}
// RegisterInstancesWithLoadBalancer should be called for every load balancer name in the machine
// spec for create and for update (3 * 2 = 6)
for i := 0; i < 6; i++ {
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any())
}
}

func TestRemoveDuplicatedTags(t *testing.T) {
cases := []struct {
tagList []*ec2.Tag
Expand Down Expand Up @@ -371,13 +382,37 @@ func TestRemoveDuplicatedTags(t *testing.T) {
}
}

func mockRegisterInstancesWithLoadBalancer(mockAWSClient *mockaws.MockClient, createError bool) {
if createError {
return
func TestBuildEC2Filters(t *testing.T) {
filter1 := "filter1"
filter2 := "filter2"
value1 := "A"
value2 := "B"
value3 := "C"

inputFilters := []providerconfigv1.Filter{
{
Name: filter1,
Values: []string{value1, value2},
},
{
Name: filter2,
Values: []string{value3},
},
}
// RegisterInstancesWithLoadBalancer should be called for every load balancer name in the machine
// spec for create and for update (3 * 2 = 6)
for i := 0; i < 6; i++ {
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any())

expected := []*ec2.Filter{
{
Name: &filter1,
Values: []*string{&value1, &value2},
},
{
Name: &filter2,
Values: []*string{&value3},
},
}

got := buildEC2Filters(inputFilters)
if !reflect.DeepEqual(expected, got) {
t.Errorf("failed to buildEC2Filters. Expected: %+v, got: %+v", expected, got)
}
}

0 comments on commit 599071b

Please sign in to comment.