-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
363 lines (317 loc) · 9.22 KB
/
main_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"syscall"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/cw-sakamoto/sample/localstack"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/require"
)
type testcase struct {
setupS3 bool
setupDDB bool
setupSQS bool
okLabels []labels
ngLabels []labels
}
func TestRun(t *testing.T) {
t.Run("ok", func(t *testing.T) {
checkRun(t, testcase{
setupS3: true,
setupDDB: true,
setupSQS: true,
okLabels: []labels{
{"S3", "GetObject", "Success"},
{"SQS", "ReceiveMessage", "Success"},
{"DynamoDB", "Scan", "Success"},
},
ngLabels: []labels{
{"S3", "GetObject", "Failure"},
{"SQS", "ReceiveMessage", "Failure"},
{"DynamoDB", "Scan", "Failure"},
},
})
})
t.Run("s3 failing", func(t *testing.T) {
checkRun(t, testcase{
setupS3: false,
setupDDB: true,
setupSQS: true,
okLabels: []labels{
{"S3", "GetObject", "Failure"},
{"SQS", "ReceiveMessage", "Success"},
{"DynamoDB", "Scan", "Success"},
},
ngLabels: []labels{
{"S3", "GetObject", "Success"},
{"SQS", "ReceiveMessage", "Failure"},
{"DynamoDB", "Scan", "Failure"},
},
})
})
t.Run("sqs failing", func(t *testing.T) {
checkRun(t, testcase{
setupS3: true,
setupDDB: true,
setupSQS: false,
okLabels: []labels{
{"S3", "GetObject", "Success"},
{"SQS", "ReceiveMessage", "Failure"},
{"DynamoDB", "Scan", "Success"},
},
ngLabels: []labels{
{"S3", "GetObject", "Failure"},
{"SQS", "ReceiveMessage", "Success"},
{"DynamoDB", "Scan", "Failure"},
},
})
})
t.Run("DynamoDB failing", func(t *testing.T) {
checkRun(t, testcase{
setupS3: true,
setupDDB: false,
setupSQS: true,
okLabels: []labels{
{"S3", "GetObject", "Success"},
{"SQS", "ReceiveMessage", "Success"},
{"DynamoDB", "Scan", "Failure"},
},
ngLabels: []labels{
{"S3", "GetObject", "Failure"},
{"SQS", "ReceiveMessage", "Failure"},
{"DynamoDB", "Scan", "Success"},
},
})
})
}
func checkRun(t *testing.T, tc testcase) {
t.Helper()
var (
runErr = make(chan error, 1)
sigs = make(chan os.Signal, 1)
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
awsConfig, err := config.LoadDefaultConfig(ctx)
require.NoError(t, err)
s3EndpointResolver := localstack.S3EndpointResolver()
sqsEndpointResolver := localstack.SQSEndpointResolver()
dynamodbEndpointResolver := localstack.DynamoDBEndpointResolver()
if tc.setupS3 {
setupS3BucketAndObject(t, ctx, awsConfig, s3EndpointResolver)
}
if tc.setupDDB {
setupDynamoDBTable(t, ctx, awsConfig, dynamodbEndpointResolver)
}
if tc.setupSQS {
preservedQueueURL := os.Getenv("SQS_QUEUE_URL")
queueURL := setupSQSQueue(t, ctx, awsConfig, sqsEndpointResolver)
os.Setenv("SQS_QUEUE_URL", queueURL)
defer func() {
os.Setenv("SQS_QUEUE_URL", preservedQueueURL)
}()
}
go func() {
runErr <- Run(ContextWithSignal(ctx, sigs), func(c *checker) {
// Use localstack for S3 and DynamoDB
c.s3Opts = append(c.s3Opts, s3.WithEndpointResolverV2(s3EndpointResolver))
c.sqsOpts = append(c.sqsOpts, sqs.WithEndpointResolverV2(sqsEndpointResolver))
c.dynamodbOpts = append(c.dynamodbOpts, dynamodb.WithEndpointResolverV2(dynamodbEndpointResolver))
})
cancel()
}()
//
// Wait for the server to start exposing metrics
//
metrics := make(chan map[labels]struct{}, 1)
go func() {
for {
time.Sleep(100 * time.Millisecond)
m := httpGetStr(t, "http://localhost:8080/metrics")
mm, err := parseMetrics(t, m)
if err != nil {
t.Logf("Error: %v", err)
continue
}
if len(mm) == len(tc.okLabels) {
metrics <- mm
break
}
}
}()
select {
case mm := <-metrics:
for _, l := range tc.okLabels {
require.Contains(t, mm, l)
}
for _, l := range tc.ngLabels {
require.NotContains(t, mm, l)
}
case <-time.After(3 * time.Second):
// We assume that the server is expected to start and expose metrics within 2 seconds.
// Otherwise, we consider it as a failure, and you may need to fix the server implementation,
// or you may need to increase the timeout if the runtime environment is soooo slow.
t.Fatal("timed out waiting for metrics")
}
sigs <- syscall.SIGINT
select {
case <-ctx.Done():
require.NoError(t, <-runErr)
case <-time.After(1 * time.Second):
// We assume the server can gracefully shut down within 5 seconds.
// Otherwise, we consider it as a failure, and you may need to fix the server implementation.
t.Fatal("timeout")
}
}
type labels struct {
service, method, status string
}
func parseMetrics(t *testing.T, m string) (map[labels]struct{}, error) {
p := expfmt.TextParser{}
mf, err := p.TextToMetricFamilies(strings.NewReader(m))
require.NoError(t, err)
mt, ok := mf["aws_request_duration_seconds"]
if !ok {
return nil, fmt.Errorf("metric family aws_request_duration_seconds not found")
}
mm := make(map[labels]struct{})
for _, m := range mt.Metric {
t.Logf("Metric: %v", m)
var labels labels
for _, l := range m.Label {
switch *l.Name {
case "service":
labels.service = *l.Value
case "method":
labels.method = *l.Value
case "status":
labels.status = *l.Value
}
}
mm[labels] = struct{}{}
}
return mm, nil
}
// Put s3 object for testing
func setupS3BucketAndObject(t *testing.T, ctx context.Context, awsConfig aws.Config, s3EndpointResolver s3.EndpointResolverV2) {
s3Client := s3.NewFromConfig(awsConfig, s3.WithEndpointResolverV2(s3EndpointResolver))
// We assume that S3_BUCKET and S3_KEY are set in the environment variables
// before running the tests.
s3Bucket := os.Getenv("S3_BUCKET")
s3Key := os.Getenv("S3_KEY")
_, err := s3Client.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: &s3Bucket,
// LocationConstraint is required when AWS_DEFAULT_REGION is not us-east-1
// Otherwise, you will get the following error:
// IllegalLocationConstraintException: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.
CreateBucketConfiguration: &s3types.CreateBucketConfiguration{
LocationConstraint: s3types.BucketLocationConstraint(os.Getenv("AWS_DEFAULT_REGION")),
},
})
require.NoError(t, err)
t.Cleanup(func() {
_, err = s3Client.DeleteBucket(context.Background(), &s3.DeleteBucketInput{
Bucket: &s3Bucket,
})
if err != nil {
t.Logf("Error: %v", err)
}
})
_, err = s3Client.PutObject(ctx, &s3.PutObjectInput{
Bucket: &s3Bucket,
Key: &s3Key,
Body: strings.NewReader("hello"),
})
require.NoError(t, err)
t.Cleanup(func() {
_, err := s3Client.DeleteObject(context.Background(), &s3.DeleteObjectInput{
Bucket: &s3Bucket,
Key: &s3Key,
})
if err != nil {
t.Logf("Error: %v", err)
}
})
}
func setupSQSQueue(t *testing.T, ctx context.Context, awsConfig aws.Config, sqsEndpointResolver sqs.EndpointResolverV2) string {
sqsClient := sqs.NewFromConfig(awsConfig, sqs.WithEndpointResolverV2(sqsEndpointResolver))
// We assume that SQS_QUEUE_URL is set in the environment variables
// before running the tests.
sqsQueueURL := os.Getenv("SQS_QUEUE_URL")
queueName := sqsQueueURL[strings.LastIndex(sqsQueueURL, "/")+1:]
res, err := sqsClient.CreateQueue(ctx, &sqs.CreateQueueInput{
QueueName: aws.String(queueName),
})
require.NoError(t, err)
sqsQueueURL = *res.QueueUrl
t.Cleanup(func() {
_, err = sqsClient.DeleteQueue(context.Background(), &sqs.DeleteQueueInput{
QueueUrl: &sqsQueueURL,
})
if err != nil {
t.Logf("Error: %v", err)
}
})
return sqsQueueURL
}
func setupDynamoDBTable(t *testing.T, ctx context.Context, awsConfig aws.Config, dynamodbEndpointResolver dynamodb.EndpointResolverV2) {
dynamodbClient := dynamodb.NewFromConfig(awsConfig, dynamodb.WithEndpointResolverV2(dynamodbEndpointResolver))
// We assume that DYNAMODB_TABLE is set in the environment variables
// before running the tests.
dynamodbTable := os.Getenv("DYNAMODB_TABLE")
_, err := dynamodbClient.CreateTable(ctx, &dynamodb.CreateTableInput{
TableName: &dynamodbTable,
KeySchema: []dynamodbtypes.KeySchemaElement{
{
AttributeName: aws.String("id"),
KeyType: dynamodbtypes.KeyTypeHash,
},
},
AttributeDefinitions: []dynamodbtypes.AttributeDefinition{
{
AttributeName: aws.String("id"),
AttributeType: dynamodbtypes.ScalarAttributeTypeS,
},
},
ProvisionedThroughput: &dynamodbtypes.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(1),
WriteCapacityUnits: aws.Int64(1),
},
})
require.NoError(t, err)
t.Cleanup(func() {
_, err = dynamodbClient.DeleteTable(context.Background(), &dynamodb.DeleteTableInput{
TableName: &dynamodbTable,
})
if err != nil {
t.Logf("Error: %v", err)
}
})
}
func httpGetStr(t *testing.T, url string) string {
resp, err := http.Get(url)
if err != nil {
t.Logf("Error: %v", err)
return ""
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Logf("Error: %v", err)
return ""
}
return string(body)
}