From 073f48f2d023cc58e8862b6f99a97009eb19de4a Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Sun, 23 Dec 2018 16:41:40 +0100 Subject: [PATCH] Inline all mock* functions invocation and remove them It's more transparent to set all mocked methods without any mediators. --- pkg/actuators/machine/actuator_test.go | 130 ++++++++++--------------- 1 file changed, 51 insertions(+), 79 deletions(-) diff --git a/pkg/actuators/machine/actuator_test.go b/pkg/actuators/machine/actuator_test.go index 9f9610f951..7bb1b86abc 100644 --- a/pkg/actuators/machine/actuator_test.go +++ b/pkg/actuators/machine/actuator_test.go @@ -2,7 +2,6 @@ package machine import ( "context" - "errors" "fmt" "strings" "testing" @@ -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) { @@ -71,6 +69,7 @@ func TestMachineEvents(t *testing.T) { runInstancesErr error terminateInstancesErr error lbErr error + regInstancesWithLbErr error }{ { name: "Create machine event failed (invalid configuration)", @@ -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) @@ -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 @@ -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 {