forked from gruntwork-io/terratest
/
sqs.go
283 lines (229 loc) · 8.21 KB
/
sqs.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
package aws
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/google/uuid"
"github.com/gruntwork-io/terratest/modules/logger"
)
// CreateRandomQueue creates a new SQS queue with a random name that starts with the given prefix and return the queue URL.
func CreateRandomQueue(t *testing.T, awsRegion string, prefix string) string {
url, err := CreateRandomQueueE(t, awsRegion, prefix)
if err != nil {
t.Fatal(err)
}
return url
}
// CreateRandomQueueE creates a new SQS queue with a random name that starts with the given prefix and return the queue URL.
func CreateRandomQueueE(t *testing.T, awsRegion string, prefix string) (string, error) {
logger.Logf(t, "Creating randomly named SQS queue with prefix %s", prefix)
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return "", err
}
channel, err := uuid.NewUUID()
if err != nil {
return "", err
}
channelName := fmt.Sprintf("%s-%s", prefix, channel.String())
queue, err := sqsClient.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(channelName),
})
if err != nil {
return "", err
}
return aws.StringValue(queue.QueueUrl), nil
}
// CreateRandomFifoQueue creates a new FIFO SQS queue with a random name that starts with the given prefix and return the queue URL.
func CreateRandomFifoQueue(t *testing.T, awsRegion string, prefix string) string {
url, err := CreateRandomFifoQueueE(t, awsRegion, prefix)
if err != nil {
t.Fatal(err)
}
return url
}
// CreateRandomFifoQueueE creates a new FIFO SQS queue with a random name that starts with the given prefix and return the queue URL.
func CreateRandomFifoQueueE(t *testing.T, awsRegion string, prefix string) (string, error) {
logger.Logf(t, "Creating randomly named FIFO SQS queue with prefix %s", prefix)
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return "", err
}
channel, err := uuid.NewUUID()
if err != nil {
return "", err
}
channelName := fmt.Sprintf("%s-%s.fifo", prefix, channel.String())
queue, err := sqsClient.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(channelName),
Attributes: map[string]*string{
"ContentBasedDeduplication": aws.String("true"),
"FifoQueue": aws.String("true"),
},
})
if err != nil {
return "", err
}
return aws.StringValue(queue.QueueUrl), nil
}
// DeleteQueue deletes the SQS queue with the given URL.
func DeleteQueue(t *testing.T, awsRegion string, queueURL string) {
err := DeleteQueueE(t, awsRegion, queueURL)
if err != nil {
t.Fatal(err)
}
}
// DeleteQueueE deletes the SQS queue with the given URL.
func DeleteQueueE(t *testing.T, awsRegion string, queueURL string) error {
logger.Logf(t, "Deleting SQS Queue %s", queueURL)
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return err
}
_, err = sqsClient.DeleteQueue(&sqs.DeleteQueueInput{
QueueUrl: aws.String(queueURL),
})
return err
}
// DeleteMessageFromQueue deletes the message with the given receipt from the SQS queue with the given URL.
func DeleteMessageFromQueue(t *testing.T, awsRegion string, queueURL string, receipt string) {
err := DeleteMessageFromQueueE(t, awsRegion, queueURL, receipt)
if err != nil {
t.Fatal(err)
}
}
// DeleteMessageFromQueueE deletes the message with the given receipt from the SQS queue with the given URL.
func DeleteMessageFromQueueE(t *testing.T, awsRegion string, queueURL string, receipt string) error {
logger.Logf(t, "Deleting message from queue %s (%s)", queueURL, receipt)
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return err
}
_, err = sqsClient.DeleteMessage(&sqs.DeleteMessageInput{
ReceiptHandle: &receipt,
QueueUrl: &queueURL,
})
return err
}
// SendMessageToQueue sends the given message to the SQS queue with the given URL.
func SendMessageToQueue(t *testing.T, awsRegion string, queueURL string, message string) {
err := SendMessageToQueueE(t, awsRegion, queueURL, message)
if err != nil {
t.Fatal(err)
}
}
// SendMessageToQueueE sends the given message to the SQS queue with the given URL.
func SendMessageToQueueE(t *testing.T, awsRegion string, queueURL string, message string) error {
logger.Logf(t, "Sending message %s to queue %s", message, queueURL)
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return err
}
res, err := sqsClient.SendMessage(&sqs.SendMessageInput{
MessageBody: &message,
QueueUrl: &queueURL,
})
if err != nil {
if strings.Contains(err.Error(), "AWS.SimpleQueueService.NonExistentQueue") {
logger.Logf(t, fmt.Sprintf("WARN: Client has stopped listening on queue %s", queueURL))
return nil
}
return err
}
logger.Logf(t, "Message id %s sent to queue %s", aws.StringValue(res.MessageId), queueURL)
return nil
}
// SendMessageToFifoQueue sends the given message to the FIFO SQS queue with the given URL.
func SendMessageFifoToQueue(t *testing.T, awsRegion string, queueURL string, message string, messageGroupID string) {
err := SendMessageToFifoQueueE(t, awsRegion, queueURL, message, messageGroupID)
if err != nil {
t.Fatal(err)
}
}
// SendMessageToFifoQueueE sends the given message to the FIFO SQS queue with the given URL.
func SendMessageToFifoQueueE(t *testing.T, awsRegion string, queueURL string, message string, messageGroupID string) error {
logger.Logf(t, "Sending message %s to queue %s", message, queueURL)
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return err
}
res, err := sqsClient.SendMessage(&sqs.SendMessageInput{
MessageBody: &message,
QueueUrl: &queueURL,
MessageGroupId: &messageGroupID,
})
if err != nil {
if strings.Contains(err.Error(), "AWS.SimpleQueueService.NonExistentQueue") {
logger.Logf(t, fmt.Sprintf("WARN: Client has stopped listening on queue %s", queueURL))
return nil
}
return err
}
logger.Logf(t, "Message id %s sent to queue %s", aws.StringValue(res.MessageId), queueURL)
return nil
}
// QueueMessageResponse contains a queue message.
type QueueMessageResponse struct {
ReceiptHandle string
MessageBody string
Error error
}
// WaitForQueueMessage waits to receive a message from on the queueURL. Since the API only allows us to wait a max 20 seconds for a new
// message to arrive, we must loop TIMEOUT/20 number of times to be able to wait for a total of TIMEOUT seconds
func WaitForQueueMessage(t *testing.T, awsRegion string, queueURL string, timeout int) QueueMessageResponse {
sqsClient, err := NewSqsClientE(t, awsRegion)
if err != nil {
return QueueMessageResponse{Error: err}
}
cycles := timeout
cycleLength := 1
if timeout >= 20 {
cycleLength = 20
cycles = timeout / cycleLength
}
for i := 0; i < cycles; i++ {
logger.Logf(t, "Waiting for message on %s (%ss)", queueURL, strconv.Itoa(i*cycleLength))
result, err := sqsClient.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: aws.String(queueURL),
AttributeNames: aws.StringSlice([]string{"SentTimestamp"}),
MaxNumberOfMessages: aws.Int64(1),
MessageAttributeNames: aws.StringSlice([]string{"All"}),
WaitTimeSeconds: aws.Int64(int64(cycleLength)),
})
if err != nil {
return QueueMessageResponse{Error: err}
}
if len(result.Messages) > 0 {
logger.Logf(t, "Message %s received on %s", *result.Messages[0].MessageId, queueURL)
return QueueMessageResponse{ReceiptHandle: *result.Messages[0].ReceiptHandle, MessageBody: *result.Messages[0].Body}
}
}
return QueueMessageResponse{Error: ReceiveMessageTimeout{QueueUrl: queueURL, TimeoutSec: timeout}}
}
// NewSqsClient creates a new SQS client.
func NewSqsClient(t *testing.T, region string) *sqs.SQS {
client, err := NewSqsClientE(t, region)
if err != nil {
t.Fatal(err)
}
return client
}
// NewSqsClientE creates a new SQS client.
func NewSqsClientE(t *testing.T, region string) (*sqs.SQS, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return sqs.New(sess), nil
}
// ReceiveMessageTimeout is an error that occurs if receiving a message times out.
type ReceiveMessageTimeout struct {
QueueUrl string
TimeoutSec int
}
func (err ReceiveMessageTimeout) Error() string {
return fmt.Sprintf("Failed to receive messages on %s within %s seconds", err.QueueUrl, strconv.Itoa(err.TimeoutSec))
}