-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaws_test.go
207 lines (175 loc) · 5 KB
/
aws_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package aws_test
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/segmentio/ebs-backup/internal/handler"
)
func TestEBSBackup(t *testing.T) {
sess := getAWSSession()
defer terraform.Destroy(t, tfOpts())
terraform.InitAndApply(t, tfOpts())
backupFunctionName := terraform.Output(t, tfOpts(), "backup_function_name")
backupFunctionARN := terraform.Output(t, tfOpts(), "backup_function_arn")
tests := map[string]func(t *testing.T){
"CloudWatch Scheduled Event created": testScheduledEvent(sess, backupFunctionARN),
"Create snapshot": testCreateSnapshot(sess, backupFunctionName),
}
t.Run("AWS", func(t *testing.T) {
for name, test := range tests {
t.Run(name, test)
}
})
}
func testScheduledEvent(sess client.ConfigProvider, lambdaARN string) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
client := cloudwatchevents.New(sess)
output, err := client.ListRuleNamesByTarget(
&cloudwatchevents.ListRuleNamesByTargetInput{
TargetArn: aws.String(lambdaARN),
},
)
if err != nil {
t.Fatal(err)
}
for _, name := range output.RuleNames {
output, err := client.DescribeRule(
&cloudwatchevents.DescribeRuleInput{
Name: name,
},
)
if err != nil {
t.Fatal(err)
}
if aws.StringValue(output.ScheduleExpression) != "" {
return
}
}
t.Error("Could not find scheduled backup CloudWatch event")
}
}
func testCreateSnapshot(sess client.ConfigProvider, functionName string) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
lambdaClient := lambda.New(sess)
output, err := lambdaClient.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(functionName),
InvocationType: aws.String("RequestResponse"),
})
if err != nil {
t.Fatal(err)
}
if output.FunctionError != nil {
t.Fatalf("Lambda returned function error: %s", aws.StringValue(output.FunctionError))
}
var resp handler.Response
if err := json.Unmarshal(output.Payload, &resp); err != nil {
t.Fatal(err)
}
for i := range resp {
defer deleteSnapshot(sess, resp[i].SnapshotID)
}
t.Run("Single snapshot created", func(t *testing.T) {
if len(resp) != 1 {
t.Errorf("Expected 1 snapshot created, actual %d", len(resp))
}
})
ec2Client := ec2.New(sess)
t.Run("Snapshot exists", func(t *testing.T) {
resp, err := ec2Client.DescribeSnapshots(
&ec2.DescribeSnapshotsInput{
SnapshotIds: aws.StringSlice([]string{resp[0].SnapshotID}),
},
)
if err != nil {
t.Errorf("DescribeSnapshots: %v", err)
}
if len(resp.Snapshots) != 1 {
t.Errorf("Expected 1 snapshot, actual %d", len(resp.Snapshots))
}
t.Run("Snapshot has proper tags", func(t *testing.T) {
tags := map[string]string{
"Name": nameTag(),
"Creator": buildUser(),
}
for k, v := range tags {
var found bool
for _, tag := range resp.Snapshots[0].Tags {
if aws.StringValue(tag.Key) == k && aws.StringValue(tag.Value) == v {
found = true
}
}
if !found {
t.Errorf("Snapshot missing expected tag %s: %s", k, v)
}
}
})
})
}
}
func deleteSnapshot(sess client.ConfigProvider, snapshotID string) {
ec2Client := ec2.New(sess)
ec2Client.DeleteSnapshot(&ec2.DeleteSnapshotInput{
SnapshotId: aws.String(snapshotID),
})
}
func envMust(keys ...string) (val string) {
for _, key := range keys {
if val = os.Getenv(key); val != "" {
return val
}
}
panic(fmt.Sprintf("%s must be set", strings.Join(keys, " or ")))
}
func nameTag() string {
return "EBSBackupTest-" + envMust("CIRCLE_WORKFLOW_ID")
}
func buildUser() string {
return envMust("CIRCLE_USERNAME", "LOGNAME")
}
func tfOpts() *terraform.Options {
return &terraform.Options{
Vars: map[string]interface{}{
"build_username": buildUser(),
"workflow_id": envMust("CIRCLE_WORKFLOW_ID"),
"lambda_s3_bucket": envMust("LAMBDA_S3_BUCKET"),
"lambda_s3_key": envMust("LAMBDA_S3_KEY"),
"name": nameTag(),
"assume_role_arn": os.Getenv("ASSUME_ROLE_ARN"),
},
}
}
func getAWSSession() *session.Session {
var region string
sess := session.Must(session.NewSession())
// Detect region automatically if possible
client := ec2metadata.New(sess)
doc, err := client.GetInstanceIdentityDocument()
if err == nil {
sess.Config.Region = aws.String(doc.Region)
}
assumeRoleARN := os.Getenv("ASSUME_ROLE_ARN")
if assumeRoleARN != "" {
// Replace session with one configured to assume role
sess = session.Must(
session.NewSession(
aws.NewConfig().WithCredentials(
stscreds.NewCredentials(sess, assumeRoleARN),
).WithRegion(region),
),
)
}
return sess
}