From 5980690374c21797f58519696fef14169b4e9735 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 13 Nov 2019 20:56:00 -0800 Subject: [PATCH] pkg/actuators/machine/stubs: Add 'state' argument to stubDescribeInstancesOutput So we can create pending, terminated, etc. instances for testing. This should scale better than stub{State}InstanceDescribeInstancesOutput from openshift/cluster-api-provider-aws@c8e341f22f (Stop retieving instances by state on API call, 2019-11-14, openshift/cluster-api-provider-aws#269). --- pkg/actuators/machine/actuator_test.go | 18 +++++++++--------- pkg/actuators/machine/stubs.go | 24 ++---------------------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/pkg/actuators/machine/actuator_test.go b/pkg/actuators/machine/actuator_test.go index 0e65699b5e..d0816a4971 100644 --- a/pkg/actuators/machine/actuator_test.go +++ b/pkg/actuators/machine/actuator_test.go @@ -169,7 +169,7 @@ func TestMachineEvents(t *testing.T) { mockAWSClient.EXPECT().RunInstances(gomock.Any()).Return(stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"), tc.runInstancesErr).AnyTimes() if tc.describeInstancesOutput == nil { - mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c"), tc.describeInstancesErr).AnyTimes() + mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c", ec2.InstanceStateNameRunning), tc.describeInstancesErr).AnyTimes() } else { mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(tc.describeInstancesOutput, tc.describeInstancesErr).AnyTimes() } @@ -546,7 +546,7 @@ func TestActuator(t *testing.T) { mockAWSClient.EXPECT().RunInstances(gomock.Any()).Return(stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"), tc.runInstancesErr).AnyTimes() if tc.describeInstancesOutput == nil { - mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c"), tc.describeInstancesErr).AnyTimes() + mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c", ec2.InstanceStateNameRunning), tc.describeInstancesErr).AnyTimes() } else { mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(tc.describeInstancesOutput, tc.describeInstancesErr).AnyTimes() } @@ -656,7 +656,7 @@ func TestAvailabilityZone(t *testing.T) { ImageId: aws.String("ami-a9acbbd6"), InstanceId: aws.String("i-02fcb933c5da7085c"), State: &ec2.InstanceState{ - Name: aws.String("Running"), + Name: aws.String(ec2.InstanceStateNameRunning), }, LaunchTime: aws.Time(time.Now()), Placement: &ec2.Placement{ @@ -675,7 +675,7 @@ func TestAvailabilityZone(t *testing.T) { ImageId: aws.String("ami-a9acbbd6"), InstanceId: aws.String("i-02fcb933c5da7085c"), State: &ec2.InstanceState{ - Name: aws.String("Running"), + Name: aws.String(ec2.InstanceStateNameRunning), Code: aws.Int64(16), }, LaunchTime: aws.Time(time.Now()), @@ -804,7 +804,7 @@ func TestCreate(t *testing.T) { mockAWSClient.EXPECT().DescribeSecurityGroups(gomock.Any()).Return(nil, fmt.Errorf("describeSecurityGroups error")).AnyTimes() mockAWSClient.EXPECT().DescribeAvailabilityZones(gomock.Any()).Return(nil, fmt.Errorf("describeAvailabilityZones error")).AnyTimes() mockAWSClient.EXPECT().DescribeImages(gomock.Any()).Return(nil, fmt.Errorf("describeImages error")).AnyTimes() - mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c"), nil).AnyTimes() + mockAWSClient.EXPECT().DescribeInstances(gomock.Any()).Return(stubDescribeInstancesOutput("ami-a9acbbd6", "i-02fcb933c5da7085c", ec2.InstanceStateNameRunning), nil).AnyTimes() mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(&ec2.TerminateInstancesOutput{}, nil).AnyTimes() mockAWSClient.EXPECT().RunInstances(gomock.Any()).Return(stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"), nil).AnyTimes() mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any()).Return(nil, nil).AnyTimes() @@ -1175,7 +1175,7 @@ func TestGetMachineInstances(t *testing.T) { } mockAWSClient.EXPECT().DescribeInstances(request).Return( - stubDescribeInstancesOutput(imageID, instanceID), + stubDescribeInstancesOutput(imageID, instanceID, ec2.InstanceStateNameRunning), nil, ).Times(1) @@ -1196,7 +1196,7 @@ func TestGetMachineInstances(t *testing.T) { } mockAWSClient.EXPECT().DescribeInstances(request).Return( - stubDescribeInstancesOutput(imageID, instanceID), + stubDescribeInstancesOutput(imageID, instanceID, ec2.InstanceStateNameRunning), nil, ).Times(1) @@ -1217,7 +1217,7 @@ func TestGetMachineInstances(t *testing.T) { } mockAWSClient.EXPECT().DescribeInstances(request).Return( - stubTerminatedInstanceDescribeInstancesOutput(imageID, instanceID), + stubDescribeInstancesOutput(imageID, instanceID, ec2.InstanceStateNameTerminated), nil, ).Times(1) @@ -1233,7 +1233,7 @@ func TestGetMachineInstances(t *testing.T) { } mockAWSClient.EXPECT().DescribeInstances(request2).Return( - stubTerminatedInstanceDescribeInstancesOutput(imageID, instanceID), + stubDescribeInstancesOutput(imageID, instanceID, ec2.InstanceStateNameTerminated), nil, ).Times(1) diff --git a/pkg/actuators/machine/stubs.go b/pkg/actuators/machine/stubs.go index 4f0c673470..be750d644f 100644 --- a/pkg/actuators/machine/stubs.go +++ b/pkg/actuators/machine/stubs.go @@ -273,7 +273,7 @@ func stubReservation(imageID, instanceID string) *ec2.Reservation { } } -func stubDescribeInstancesOutput(imageID, instanceID string) *ec2.DescribeInstancesOutput { +func stubDescribeInstancesOutput(imageID, instanceID string, state string) *ec2.DescribeInstancesOutput { return &ec2.DescribeInstancesOutput{ Reservations: []*ec2.Reservation{ { @@ -282,27 +282,7 @@ func stubDescribeInstancesOutput(imageID, instanceID string) *ec2.DescribeInstan ImageId: aws.String(imageID), InstanceId: aws.String(instanceID), State: &ec2.InstanceState{ - Name: aws.String("Running"), - Code: aws.Int64(16), - }, - LaunchTime: aws.Time(time.Now()), - }, - }, - }, - }, - } -} - -func stubTerminatedInstanceDescribeInstancesOutput(imageID, instanceID string) *ec2.DescribeInstancesOutput { - return &ec2.DescribeInstancesOutput{ - Reservations: []*ec2.Reservation{ - { - Instances: []*ec2.Instance{ - { - ImageId: aws.String(imageID), - InstanceId: aws.String(instanceID), - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameTerminated), + Name: aws.String(state), Code: aws.Int64(16), }, LaunchTime: aws.Time(time.Now()),