Skip to content

Commit

Permalink
Inline all mock* functions invocation and remove them
Browse files Browse the repository at this point in the history
It's more transparent to set all mocked methods without any mediators.
  • Loading branch information
ingvagabund committed Jan 7, 2019
1 parent 1f5dcd9 commit 073f48f
Showing 1 changed file with 51 additions and 79 deletions.
130 changes: 51 additions & 79 deletions pkg/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package machine

import (
"context"
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -35,7 +34,6 @@ const (
noError = ""
awsServiceError = "error creating aws service"
launchInstanceError = "error launching instance"
lbError = "error updating load balancers"
)

func TestMachineEvents(t *testing.T) {
Expand Down Expand Up @@ -71,6 +69,7 @@ func TestMachineEvents(t *testing.T) {
runInstancesErr error
terminateInstancesErr error
lbErr error
regInstancesWithLbErr error
}{
{
name: "Create machine event failed (invalid configuration)",
Expand Down Expand Up @@ -171,8 +170,8 @@ func TestMachineEvents(t *testing.T) {
mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(tc.describeInstancesOutput, tc.describeInstancesErr).AnyTimes()
}

mockTerminateInstances(mockAWSClient)
mockRegisterInstancesWithLoadBalancer(mockAWSClient, tc.error == lbError)
mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(&ec2.TerminateInstancesOutput{}, nil)
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any()).Return(nil, nil).AnyTimes()
mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(&ec2.TerminateInstancesOutput{}, tc.terminateInstancesErr).AnyTimes()
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any()).Return(nil, tc.lbErr).AnyTimes()
mockAWSClient.EXPECT().ELBv2DescribeLoadBalancers(gomock.Any()).Return(stubDescribeLoadBalancersOutput(), tc.lbErr)
Expand Down Expand Up @@ -562,50 +561,6 @@ func TestActuator(t *testing.T) {
}
}

func mockDescribeInstances(mockAWSClient *mockaws.MockClient, genError bool) {
var err error

if genError {
err = errors.New("requested RunInstances error")
}

mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(
&ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
{
ImageId: aws.String("ami-a9acbbd6"),
InstanceId: aws.String("i-02fcb933c5da7085c"),
State: &ec2.InstanceState{
Name: aws.String("Running"),
Code: aws.Int64(16),
},
LaunchTime: aws.Time(time.Now()),
},
},
},
},
}, err).AnyTimes()
}

func mockTerminateInstances(mockAWSClient *mockaws.MockClient) {
mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(
&ec2.TerminateInstancesOutput{}, nil)
}

func mockRegisterInstancesWithLoadBalancer(mockAWSClient *mockaws.MockClient, createError bool) {
if createError {
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any()).Return(nil, fmt.Errorf("error")).AnyTimes()
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 TestAvailabiltyZone(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -684,41 +639,58 @@ func TestAvailabiltyZone(t *testing.T) {
t.Fatalf("Could not create AWS machine actuator: %v", err)
}

mockRunInstancesForPlacement(mockAWSClient, tc.availabilityZone, tc.subnet)
mockDescribeInstances(mockAWSClient, false)
mockTerminateInstances(mockAWSClient)
mockRegisterInstancesWithLoadBalancer(mockAWSClient, false)
mockDescribeSubnets(mockAWSClient)
var placement *ec2.Placement
if tc.availabilityZone != "" && tc.subnet == "" {
placement = &ec2.Placement{AvailabilityZone: aws.String(tc.availabilityZone)}
}

actuator.Create(context.TODO(), cluster, machine)
})
}
}
mockAWSClient.EXPECT().RunInstances(Placement(placement)).Return(
&ec2.Reservation{
Instances: []*ec2.Instance{
{
ImageId: aws.String("ami-a9acbbd6"),
InstanceId: aws.String("i-02fcb933c5da7085c"),
State: &ec2.InstanceState{
Name: aws.String("Running"),
Code: aws.Int64(16),
},
LaunchTime: aws.Time(time.Now()),
},
},
}, nil)

func mockDescribeSubnets(mockAWSClient *mockaws.MockClient) {
mockAWSClient.EXPECT().DescribeSubnets(gomock.Any()).Return(&ec2.DescribeSubnetsOutput{}, nil)
}
mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(
&ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
{
ImageId: aws.String("ami-a9acbbd6"),
InstanceId: aws.String("i-02fcb933c5da7085c"),
State: &ec2.InstanceState{
Name: aws.String("Running"),
Code: aws.Int64(16),
},
LaunchTime: aws.Time(time.Now()),
},
},
},
},
}, nil).AnyTimes()

func mockRunInstancesForPlacement(mockAWSClient *mockaws.MockClient, availabilityZone, subnet string) {
var placement *ec2.Placement
if availabilityZone != "" && subnet == "" {
placement = &ec2.Placement{AvailabilityZone: aws.String(availabilityZone)}
}
mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(&ec2.TerminateInstancesOutput{}, nil)

mockAWSClient.EXPECT().RunInstances(Placement(placement)).Return(
&ec2.Reservation{
Instances: []*ec2.Instance{
{
ImageId: aws.String("ami-a9acbbd6"),
InstanceId: aws.String("i-02fcb933c5da7085c"),
State: &ec2.InstanceState{
Name: aws.String("Running"),
Code: aws.Int64(16),
},
LaunchTime: aws.Time(time.Now()),
},
},
}, nil)
// 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())
}

mockAWSClient.EXPECT().DescribeSubnets(gomock.Any()).Return(&ec2.DescribeSubnetsOutput{}, nil)

actuator.Create(context.TODO(), cluster, machine)
})
}
}

type placementMatcher struct {
Expand Down

0 comments on commit 073f48f

Please sign in to comment.