-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor auto scaling group resource type
- Loading branch information
1 parent
1b6398c
commit ecfe9ad
Showing
4 changed files
with
78 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,197 +1,84 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface" | ||
"github.com/gruntwork-io/cloud-nuke/telemetry" | ||
"regexp" | ||
"testing" | ||
"time" | ||
|
||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/autoscaling" | ||
"github.com/gruntwork-io/cloud-nuke/config" | ||
"github.com/gruntwork-io/cloud-nuke/logging" | ||
"github.com/gruntwork-io/cloud-nuke/util" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func createTestAutoScalingGroup(t *testing.T, session *session.Session, name string) { | ||
svc := autoscaling.New(session) | ||
instance := createTestEC2Instance(t, session, name, false) | ||
|
||
param := &autoscaling.CreateAutoScalingGroupInput{ | ||
AutoScalingGroupName: &name, | ||
InstanceId: instance.InstanceId, | ||
MinSize: awsgo.Int64(1), | ||
MaxSize: awsgo.Int64(2), | ||
} | ||
|
||
_, err := svc.CreateAutoScalingGroup(param) | ||
if err != nil { | ||
assert.Failf(t, "Could not create test ASG", errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
err = svc.WaitUntilGroupExists(&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{&name}, | ||
}) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
type mockedASGroups struct { | ||
autoscalingiface.AutoScalingAPI | ||
DescribeAutoScalingGroupsResp autoscaling.DescribeAutoScalingGroupsOutput | ||
DeleteAutoScalingGroupResp autoscaling.DeleteAutoScalingGroupOutput | ||
} | ||
|
||
func TestListAutoScalingGroups(t *testing.T) { | ||
telemetry.InitTelemetry("cloud-nuke", "") | ||
t.Parallel() | ||
|
||
region, err := getRandomRegion() | ||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
session, err := session.NewSession(&awsgo.Config{ | ||
Region: awsgo.String(region)}, | ||
) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
uniqueTestID := "cloud-nuke-test-" + util.UniqueID() | ||
createTestAutoScalingGroup(t, session, uniqueTestID) | ||
// clean up after this test | ||
defer nukeAllAutoScalingGroups(session, []*string{&uniqueTestID}) | ||
defer nukeAllEc2Instances(session, findEC2InstancesByNameTag(t, session, uniqueTestID)) | ||
|
||
groupNames, err := getAllAutoScalingGroups(session, region, time.Now().Add(1*time.Hour*-1), config.Config{}) | ||
if err != nil { | ||
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups") | ||
} | ||
|
||
assert.NotContains(t, awsgo.StringValueSlice(groupNames), uniqueTestID) | ||
|
||
groupNames, err = getAllAutoScalingGroups(session, region, time.Now().Add(1*time.Hour), config.Config{}) | ||
if err != nil { | ||
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups") | ||
} | ||
|
||
assert.Contains(t, awsgo.StringValueSlice(groupNames), uniqueTestID) | ||
func (m mockedASGroups) DescribeAutoScalingGroups(input *autoscaling.DescribeAutoScalingGroupsInput) (*autoscaling.DescribeAutoScalingGroupsOutput, error) { | ||
return &m.DescribeAutoScalingGroupsResp, nil | ||
} | ||
|
||
func TestNukeAutoScalingGroups(t *testing.T) { | ||
telemetry.InitTelemetry("cloud-nuke", "") | ||
t.Parallel() | ||
|
||
region, err := getRandomRegion() | ||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
session, err := session.NewSession(&awsgo.Config{ | ||
Region: awsgo.String(region)}, | ||
) | ||
svc := autoscaling.New(session) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
uniqueTestID := "cloud-nuke-test-" + util.UniqueID() | ||
createTestAutoScalingGroup(t, session, uniqueTestID) | ||
|
||
// clean up ec2 instance created by the above call | ||
defer nukeAllEc2Instances(session, findEC2InstancesByNameTag(t, session, uniqueTestID)) | ||
|
||
_, err = svc.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ | ||
AutoScalingGroupNames: []*string{&uniqueTestID}, | ||
}) | ||
|
||
if err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
if err := nukeAllAutoScalingGroups(session, []*string{&uniqueTestID}); err != nil { | ||
assert.Fail(t, errors.WithStackTrace(err).Error()) | ||
} | ||
|
||
groupNames, err := getAllAutoScalingGroups(session, region, time.Now().Add(1*time.Hour), config.Config{}) | ||
if err != nil { | ||
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups") | ||
} | ||
func (m mockedASGroups) DeleteAutoScalingGroup(input *autoscaling.DeleteAutoScalingGroupInput) (*autoscaling.DeleteAutoScalingGroupOutput, error) { | ||
return &m.DeleteAutoScalingGroupResp, nil | ||
} | ||
|
||
assert.NotContains(t, awsgo.StringValueSlice(groupNames), uniqueTestID) | ||
func (m mockedASGroups) WaitUntilGroupNotExists(input *autoscaling.DescribeAutoScalingGroupsInput) error { | ||
return nil | ||
} | ||
|
||
// Test config file filtering works as expected | ||
func TestShouldIncludeAutoScalingGroup(t *testing.T) { | ||
func TestAutoScalingGroupGetAll(t *testing.T) { | ||
telemetry.InitTelemetry("cloud-nuke", "") | ||
mockAutoScalingGroup := &autoscaling.Group{ | ||
AutoScalingGroupName: awsgo.String("cloud-nuke-test"), | ||
CreatedTime: awsgo.Time(time.Now()), | ||
} | ||
|
||
mockExpression, err := regexp.Compile("^cloud-nuke-*") | ||
if err != nil { | ||
logging.Logger.Fatalf("There was an error compiling regex expression %v", err) | ||
} | ||
t.Parallel() | ||
|
||
mockExcludeConfig := config.Config{ | ||
testName := "cloud-nuke-test" | ||
now := time.Now() | ||
ag := ASGroups{ | ||
Client: mockedASGroups{ | ||
DescribeAutoScalingGroupsResp: autoscaling.DescribeAutoScalingGroupsOutput{ | ||
AutoScalingGroups: []*autoscaling.Group{{ | ||
AutoScalingGroupName: awsgo.String(testName), | ||
CreatedTime: awsgo.Time(now), | ||
}}}}} | ||
|
||
// empty filter | ||
groups, err := ag.getAll(config.Config{}) | ||
assert.NoError(t, err) | ||
assert.Contains(t, awsgo.StringValueSlice(groups), testName) | ||
|
||
// name filter | ||
groups, err = ag.getAll(config.Config{ | ||
AutoScalingGroup: config.ResourceType{ | ||
ExcludeRule: config.FilterRule{ | ||
NamesRegExp: []config.Expression{ | ||
{ | ||
RE: *mockExpression, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
mockIncludeConfig := config.Config{ | ||
NamesRegExp: []config.Expression{{ | ||
RE: *regexp.MustCompile("^cloud-nuke-*"), | ||
}}}}}) | ||
assert.NoError(t, err) | ||
assert.NotContains(t, awsgo.StringValueSlice(groups), testName) | ||
|
||
// time filter | ||
groups, err = ag.getAll(config.Config{ | ||
AutoScalingGroup: config.ResourceType{ | ||
IncludeRule: config.FilterRule{ | ||
NamesRegExp: []config.Expression{ | ||
{ | ||
RE: *mockExpression, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
ExcludeRule: config.FilterRule{ | ||
TimeAfter: awsgo.Time(now.Add(-1)), | ||
}}}) | ||
assert.NoError(t, err) | ||
assert.NotContains(t, awsgo.StringValueSlice(groups), testName) | ||
} | ||
|
||
func TestAutoScalingGroupNukeAll(t *testing.T) { | ||
telemetry.InitTelemetry("cloud-nuke", "") | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
Name string | ||
AutoScalingGroup *autoscaling.Group | ||
Config config.Config | ||
ExcludeAfter time.Time | ||
Expected bool | ||
}{ | ||
{ | ||
Name: "ConfigExclude", | ||
AutoScalingGroup: mockAutoScalingGroup, | ||
Config: mockExcludeConfig, | ||
ExcludeAfter: time.Now().Add(1 * time.Hour), | ||
Expected: false, | ||
}, | ||
{ | ||
Name: "ConfigInclude", | ||
AutoScalingGroup: mockAutoScalingGroup, | ||
Config: mockIncludeConfig, | ||
ExcludeAfter: time.Now().Add(1 * time.Hour), | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "NotOlderThan", | ||
AutoScalingGroup: mockAutoScalingGroup, | ||
Config: config.Config{}, | ||
ExcludeAfter: time.Now().Add(1 * time.Hour * -1), | ||
Expected: false, | ||
}, | ||
} | ||
ag := ASGroups{ | ||
Client: mockedASGroups{ | ||
DeleteAutoScalingGroupResp: autoscaling.DeleteAutoScalingGroupOutput{}, | ||
}} | ||
|
||
for _, c := range cases { | ||
t.Run(c.Name, func(t *testing.T) { | ||
result := shouldIncludeAutoScalingGroup(c.AutoScalingGroup, c.ExcludeAfter, c.Config) | ||
assert.Equal(t, c.Expected, result) | ||
}) | ||
} | ||
err := ag.nukeAll([]*string{awsgo.String("cloud-nuke-test")}) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.