Skip to content

Commit

Permalink
attempt to create test cases for asg with mixed instances configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bplessis-swi committed Oct 4, 2023
1 parent 8317e85 commit 6860178
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
5 changes: 3 additions & 2 deletions pkg/lookable/asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ func (asg AutoScalingGroup) doLookupIPs(as ASGAPI, ec EC2API, ctx context.Contex
// Make a list of healthy instance ID in the ASG
instances := make([]string, 0, numInstances)
for _, inst := range resp2.AutoScalingGroups[0].Instances {
if (*inst.HealthStatus == "Healthy" && inst.LifecycleState == "InService") {
if (*inst.HealthStatus == "Healthy" && inst.LifecycleState == asgtypes.LifecycleStateInService) {
instances = append(instances, *inst.InstanceId)
}
}

// No healthy instances
if len(instances) == 0 {
return nil, nil

}

// Find running instances IP
params3 := &ec2.DescribeInstancesInput{
InstanceIds: instances,
Expand Down
39 changes: 22 additions & 17 deletions pkg/lookable/asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func TestLookupASG(t *testing.T) {
/* Single instance result */
{
client: func(t *testing.T) (ASGAPI,EC2API) {
instanceId1 := "inst-2016584701"
instanceId2 := "inst-2016584702"
instanceId2 := "inst-2016584703"

return &MockASGAPI{
DescribeAutoScalingGroupsMethod: func(ctx context.Context, params *autoscaling.DescribeAutoScalingGroupsInput, optFns...func(*autoscaling.Options)) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
Expand All @@ -40,19 +37,19 @@ func TestLookupASG(t *testing.T) {
{
Instances: []asgtypes.Instance{
{
InstanceId: &instanceId1,
HealthStatus: "Healthy",
LifeCycleState: "InService",
InstanceId: aws.String("inst-2016584701"),
HealthStatus: aws.String("Healthy"),
LifecycleState: asgtypes.LifecycleStateInService,
},
{
InstanceId: &instanceId2,
HealthStatus: "Degraded",
LifeCycleState: "Pending",
InstanceId: aws.String("inst-2016584702"),
HealthStatus: aws.String("Degraded"),
LifecycleState: asgtypes.LifecycleStateTerminating,
},
{
InstanceId: &instanceId3,
HealthStatus: "Healthy",
LifeCycleState: "Terminating",
InstanceId: aws.String("inst-2016584703"),
HealthStatus: aws.String("Healthy"),
LifecycleState: asgtypes.LifecycleStatePending,
},
},
},
Expand All @@ -73,14 +70,22 @@ func TestLookupASG(t *testing.T) {
t.Fatal("expect InstancesIds to not be nil")
}

instances := make([]ec2types.Instance, len(params.InstanceIds))
for _, id := range params.InstanceIds {
ipv4Address := "10.0.0." + id[len(id)-1:]
ipv6Address := "f00:ba5:10:0:0:" + id[len(id)-1:]
t.Log("Instance Id:"+id+" got ipv4:"+ipv4Address+" ipv6:"+ipv6Address)
instances = append(instances,
ec2types.Instance{
PrivateIpAddress: aws.String(ipv4Address),
Ipv6Address: aws.String(ipv6Address),
})
}

return &ec2.DescribeInstancesOutput{
Reservations: []ec2types.Reservation{
{
Instances: []ec2types.Instance{
{
PrivateIpAddress: aws.String("10.0.0.1"),
},
},
Instances: instances,
},
},

Expand Down

0 comments on commit 6860178

Please sign in to comment.