forked from gruntwork-io/cloud-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs_cluster_test.go
117 lines (89 loc) · 4.07 KB
/
ecs_cluster_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/gruntwork-io/cloud-nuke/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test we can create a cluster, tag it, and then find the tag
func TestCanTagEcsClusters(t *testing.T) {
t.Parallel()
region := getRandomFargateSupportedRegion()
awsSession, err := session.NewSession(&awsgo.Config{
Region: awsgo.String(region),
})
require.NoError(t, err)
cluster := createEcsFargateCluster(t, awsSession, "cloud-nuke-test-" + util.UniqueID())
defer deleteEcsCluster(awsSession, cluster)
tagValue := time.Now().UTC()
tagErr := tagEcsClusterWhenFirstSeen(awsSession, cluster.ClusterArn, tagValue)
require.NoError(t, tagErr)
returnedTag, err := getFirstSeenEcsClusterTag(awsSession, cluster.ClusterArn)
require.NoError(t, err)
parsedTagValue, parseErr1 := parseTimestampTag(formatTimestampTag(tagValue))
require.NoError(t, parseErr1)
parsedReturnValue, parseErr2 := parseTimestampTag(formatTimestampTag(returnedTag))
require.NoError(t, parseErr2)
//compare that the tags' Time values after formatting are equal
assert.Equal(t, parsedTagValue, parsedReturnValue)
}
// Test we can get all ECS clusters younger than < X time based on tags
func TestCanListAllEcsClustersOlderThan24hours(t *testing.T) {
t.Parallel()
region := getRandomFargateSupportedRegion()
awsSession, err := session.NewSession(&awsgo.Config{
Region: awsgo.String(region),
})
require.NoError(t, err)
cluster1 := createEcsFargateCluster(t, awsSession, util.UniqueID())
defer deleteEcsCluster(awsSession, cluster1)
cluster2 := createEcsFargateCluster(t, awsSession, util.UniqueID())
defer deleteEcsCluster(awsSession, cluster2)
now := time.Now().UTC()
var olderClusterTagValue = now.Add(time.Hour * time.Duration(-48))
var youngerClusterTagValue = now.Add(time.Hour * time.Duration(-23))
err1 := tagEcsClusterWhenFirstSeen(awsSession, cluster1.ClusterArn, olderClusterTagValue)
require.NoError(t, err1)
err2 := tagEcsClusterWhenFirstSeen(awsSession, cluster2.ClusterArn, youngerClusterTagValue)
require.NoError(t, err2)
last24Hours := now.Add(time.Hour * time.Duration(-24))
filteredClusterArns, err := getAllEcsClustersOlderThan(awsSession, region, last24Hours)
require.NoError(t, err)
assert.Contains(t, awsgo.StringValueSlice(filteredClusterArns), awsgo.StringValue(cluster1.ClusterArn))
}
// Test we can nuke all ECS clusters older than 24hrs
func TestCanNukeAllEcsClustersOlderThan24Hours(t *testing.T) {
t.Parallel()
region := getRandomFargateSupportedRegion()
awsSession, err := session.NewSession(&awsgo.Config{
Region: awsgo.String(region),
})
require.NoError(t, err)
cluster1 := createEcsFargateCluster(t, awsSession, "cloud-nuke-test-" + util.UniqueID())
defer deleteEcsCluster(awsSession, cluster1)
cluster2 := createEcsFargateCluster(t, awsSession, "cloud-nuke-test-" + util.UniqueID())
defer deleteEcsCluster(awsSession, cluster2)
cluster3 := createEcsFargateCluster(t, awsSession, "cloud-nuke-test-" + util.UniqueID())
defer deleteEcsCluster(awsSession, cluster3)
now := time.Now().UTC()
var oldClusterTagValue1 = now.Add(time.Hour * time.Duration(-48))
var youngClusterTagValue = now
var oldClusterTagValue2 = now.Add(time.Hour * time.Duration(-27))
err1 := tagEcsClusterWhenFirstSeen(awsSession, cluster1.ClusterArn, oldClusterTagValue1)
require.NoError(t, err1)
err2 := tagEcsClusterWhenFirstSeen(awsSession, cluster2.ClusterArn, youngClusterTagValue)
require.NoError(t, err2)
err3 := tagEcsClusterWhenFirstSeen(awsSession, cluster3.ClusterArn, oldClusterTagValue2)
require.NoError(t, err3)
last24Hours := now.Add(time.Hour * time.Duration(-24))
filteredClusterArns, err := getAllEcsClustersOlderThan(awsSession, region, last24Hours)
require.NoError(t, err)
nukeErr := nukeEcsClusters(awsSession, filteredClusterArns)
require.NoError(t, nukeErr)
allLeftClusterArns, err := getAllEcsClusters(awsSession)
require.NoError(t, err)
assert.Contains(t, awsgo.StringValueSlice(allLeftClusterArns), awsgo.StringValue(cluster2.ClusterArn))
}