Skip to content

Commit

Permalink
pkg/actuators/machine/stubs: Add 'state' argument to stubDescribeInst…
Browse files Browse the repository at this point in the history
…ancesOutput

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#269).
  • Loading branch information
wking committed Nov 14, 2019
1 parent f68fb22 commit 5980690
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
18 changes: 9 additions & 9 deletions pkg/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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{
Expand All @@ -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()),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
24 changes: 2 additions & 22 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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()),
Expand Down

0 comments on commit 5980690

Please sign in to comment.