forked from gruntwork-io/cloud-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasg_test.go
116 lines (91 loc) · 3.31 KB
/
asg_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package aws
import (
"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/util"
"github.com/gruntwork-io/gruntwork-cli/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())
}
}
func TestListAutoScalingGroups(t *testing.T) {
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))
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))
if err != nil {
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups")
}
assert.Contains(t, awsgo.StringValueSlice(groupNames), uniqueTestID)
}
func TestNukeAutoScalingGroups(t *testing.T) {
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))
if err != nil {
assert.Fail(t, "Unable to fetch list of Auto Scaling Groups")
}
assert.NotContains(t, awsgo.StringValueSlice(groupNames), uniqueTestID)
}