This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqs_test.go
250 lines (224 loc) · 5.88 KB
/
sqs_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
package aws_utils
import (
"errors"
"flag"
"fmt"
"log"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/twinj/uuid"
"github.com/twitchscience/aws_utils/listener"
"github.com/twitchscience/aws_utils/notifier"
)
var (
useRealAWS = flag.Bool("realAWS", false, "Test against real sqs+sns resources")
)
type handler struct {
msgs []*sqs.Message
}
func (h *handler) Handle(msg *sqs.Message) error {
h.msgs = append(h.msgs, msg)
return nil
}
func TestSqs(t *testing.T) {
if *useRealAWS == false {
t.Skip("Skipping test because --realAWS not specified")
}
const numMessages = 3
client := sqs.New(session.New())
queueName := "aws_utils_test_" + uuid.NewV4().String() // Get a random name for the queue
_, err := client.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(queueName),
})
if err != nil {
t.Errorf("Error creating queue: %v", err)
}
log.Printf("SQS Queue %s created!", queueName)
defer func() {
o, err := client.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(queueName),
})
_, err = client.DeleteQueue(&sqs.DeleteQueueInput{
QueueUrl: o.QueueUrl,
})
if err != nil {
t.Errorf("Error deleting queue: %v", err)
}
log.Printf("SQS Queue %s destroyed!", queueName)
}()
n := notifier.BuildSQSClient(client)
n.Signer.RegisterMessageType("number", func(args ...interface{}) (string, error) {
if len(args) != 1 {
return "", errors.New("Missing correct number of args ")
}
return fmt.Sprintf("{\"number\":%d}", args...), nil
})
h := &handler{}
l := listener.BuildSQSListener(h, 1*time.Second, client, nil)
if l == nil {
t.Error("Unable to create SQSListener")
}
go func() {
l.Listen(queueName)
}()
defer l.Close()
for i := 0; i < numMessages; i++ {
err = n.SendMessage("number", queueName, i)
if err != nil {
t.Errorf("Error trying to send message %d: %v", i, err)
}
}
timeout := time.NewTicker(4 * time.Second)
defer timeout.Stop()
RunLoop:
for {
select {
case <-timeout.C:
t.Error("Timeout waiting for sqs events")
break RunLoop
default:
}
if len(h.msgs) == numMessages {
break RunLoop
}
}
}
func TestSns(t *testing.T) {
if *useRealAWS == false {
t.Skip("Skipping test because --realAWS not specified")
}
const numMessages = 3
config := &aws.Config{Region: aws.String("us-west-2")}
session := session.New(config)
sqsClient := sqs.New(session)
queueName := "aws_utils_test_" + uuid.NewV4().String() // Get a random name for the queue
sqsOutput, err := sqsClient.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(queueName),
})
if err != nil {
t.Errorf("Error creating queue: %v", err)
}
sqsURL := sqsOutput.QueueUrl
log.Printf("SQS Queue %s created!", queueName)
qAttributes, err := sqsClient.GetQueueAttributes(&sqs.GetQueueAttributesInput{
AttributeNames: []*string{aws.String("QueueArn")},
QueueUrl: sqsURL,
})
if err != nil {
t.Errorf("Error getting queue's ARN: %v", err)
}
sqsARN := qAttributes.Attributes["QueueArn"]
defer func() {
_, err = sqsClient.DeleteQueue(&sqs.DeleteQueueInput{
QueueUrl: sqsURL,
})
if err != nil {
t.Errorf("Error deleting queue: %v", err)
}
log.Printf("SQS Queue %s destroyed!", queueName)
}()
snsClient := sns.New(session)
snsOutput, err := snsClient.CreateTopic(&sns.CreateTopicInput{
Name: aws.String(queueName),
})
if err != nil {
t.Errorf("Error creating SNS topic: %s", err)
}
snsARN := snsOutput.TopicArn
log.Printf("SNS Topic %s created!", *snsARN)
defer func() {
_, err := snsClient.DeleteTopic(&sns.DeleteTopicInput{
TopicArn: snsARN,
})
if err != nil {
t.Errorf("Error deleting sns topic: %v", err)
}
log.Printf("SNS Topic %s destroyed!", *snsARN)
}()
_, err = sqsClient.SetQueueAttributes(&sqs.SetQueueAttributesInput{
QueueUrl: sqsURL,
Attributes: map[string]*string{
"Policy": aws.String(`
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal": {
"AWS": "*"
},
"Action":"SQS:SendMessage",
"Resource":"` + *sqsARN + `",
"Condition":{
"ArnEquals":{
"aws:SourceArn":"` + *snsARN + `"
}
}
}
]
}`),
},
})
if err != nil {
t.Errorf("Error setting policy on sqs queue: %v", err)
}
subOut, err := snsClient.Subscribe(&sns.SubscribeInput{
TopicArn: snsARN,
Protocol: aws.String("sqs"),
Endpoint: sqsARN,
})
if err != nil {
t.Errorf("Error subscribing sqs to sns: %v", err)
}
if subOut.SubscriptionArn == nil {
t.Errorf("Expected automatic subscription of the sqs queue to the sns topic, but no subscription arn was returned.")
}
_, err = snsClient.SetSubscriptionAttributes(&sns.SetSubscriptionAttributesInput{
SubscriptionArn: subOut.SubscriptionArn,
AttributeName: aws.String("RawMessageDelivery"),
AttributeValue: aws.String("true"),
})
if err != nil {
t.Errorf("Error setting raw message delivery setting on the SNS topic: %v", err)
}
n := notifier.BuildSNSClient(snsClient)
n.Signer.RegisterMessageType("number", func(args ...interface{}) (string, error) {
if len(args) != 1 {
return "", errors.New("Missing correct number of args ")
}
return fmt.Sprintf("{\"number\":%d}", args...), nil
})
h := &handler{}
l := listener.BuildSQSListener(h, 1*time.Second, sqsClient, nil)
if l == nil {
t.Error("Unable to create SQSListener")
}
go func() {
l.Listen(queueName)
}()
defer l.Close()
for i := 0; i < numMessages; i++ {
err = n.SendMessage("number", *snsARN, i)
if err != nil {
t.Errorf("Error trying to send message %d: %v", i, err)
}
}
timeout := time.NewTicker(4 * time.Second)
defer timeout.Stop()
RunLoop:
for {
select {
case <-timeout.C:
t.Error("Timeout waiting for sqs events")
break RunLoop
default:
}
if len(h.msgs) == numMessages {
break RunLoop
}
}
}