~prudhvikrishna/sqs/trunk

« back to all changes in this revision

Viewing changes to sqs_test.go

  • Committer: Prudhvi Krishna Surapaneni
  • Date: 2012-05-24 21:06:40 UTC
  • Revision ID: me@prudhvi.net-20120524210640-rt0mcxlp42v5kwze
Import SQS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package sqs_test
 
2
 
 
3
import (
 
4
        "../sqs"
 
5
        "launchpad.net/goamz/aws"
 
6
        . "launchpad.net/gocheck"
 
7
)
 
8
 
 
9
var _ = Suite(&S{})
 
10
 
 
11
type S struct {
 
12
        //testServer.PrepareResponse(200, nil, TestChangeMessageVisibilityXmlOK)
 
13
        HTTPSuite
 
14
        sqs *sqs.SQS
 
15
}
 
16
 
 
17
func (s *S) SetUpSuite(c *C) {
 
18
        s.HTTPSuite.SetUpSuite(c)
 
19
        auth := aws.Auth{"abc", "123"}
 
20
        s.sqs = sqs.New(auth, aws.Region{SQSEndpoint: testServer.URL})
 
21
}
 
22
 
 
23
func (s *S) TestCreateQueue(c *C) {
 
24
        testServer.PrepareResponse(200, nil, TestCreateQueueXmlOK)
 
25
 
 
26
        timeOutAttribute := sqs.Attribute{"VisibilityTimeout", "60"}
 
27
        maxMessageSizeAttribute := sqs.Attribute{"MaximumMessageSize", "65536"}
 
28
        messageRetentionAttribute := sqs.Attribute{"MessageRetentionPeriod", "345600"}
 
29
        q, err := s.sqs.CreateQueue("testQueue", []sqs.Attribute{timeOutAttribute, maxMessageSizeAttribute, messageRetentionAttribute})
 
30
        req := testServer.WaitRequest()
 
31
 
 
32
        c.Assert(req.Method, Equals, "GET")
 
33
        c.Assert(req.URL.Path, Equals, "/")
 
34
        c.Assert(req.Header["Date"], Not(Equals), "")
 
35
        c.Assert(q.Url, Equals, "http://sqs.us-east-1.amazonaws.com/123456789012/testQueue")
 
36
        c.Assert(err, IsNil)
 
37
}
 
38
 
 
39
func (s *S) TestListQueues(c *C) {
 
40
        testServer.PrepareResponse(200, nil, TestListQueuesXmlOK)
 
41
 
 
42
        resp, err := s.sqs.ListQueues()
 
43
        req := testServer.WaitRequest()
 
44
 
 
45
        c.Assert(req.Method, Equals, "GET")
 
46
        c.Assert(req.URL.Path, Equals, "/")
 
47
        c.Assert(req.Header["Date"], Not(Equals), "")
 
48
        c.Assert(len(resp.QueueUrl), Not(Equals), 0)
 
49
        c.Assert(resp.QueueUrl[0], Equals, "http://sqs.us-east-1.amazonaws.com/123456789012/testQueue")
 
50
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "725275ae-0b9b-4762-b238-436d7c65a1ac")
 
51
        c.Assert(err, IsNil)
 
52
}
 
53
 
 
54
func (s *S) TestGetQueueUrl(c *C) {
 
55
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
56
 
 
57
        resp, err := s.sqs.GetQueueUrl("testQueue")
 
58
        req := testServer.WaitRequest()
 
59
 
 
60
        c.Assert(req.Method, Equals, "GET")
 
61
        c.Assert(req.URL.Path, Equals, "/")
 
62
        c.Assert(req.Header["Date"], Not(Equals), "")
 
63
        c.Assert(resp.QueueUrl, Equals, "http://sqs.us-east-1.amazonaws.com/123456789012/testQueue")
 
64
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "470a6f13-2ed9-4181-ad8a-2fdea142988e")
 
65
        c.Assert(err, IsNil)
 
66
}
 
67
 
 
68
func (s *S) TestChangeMessageVisibility(c *C) {
 
69
        //testServer.PrepareResponse(200, nil, TestChangeMessageVisibilityXmlOK)
 
70
        testServer.PrepareResponse(200, nil, TestCreateQueueXmlOK)
 
71
 
 
72
        timeOutAttribute := sqs.Attribute{"VisibilityTimeout", "60"}
 
73
        maxMessageSizeAttribute := sqs.Attribute{"MaximumMessageSize", "65536"}
 
74
        messageRetentionAttribute := sqs.Attribute{"MessageRetentionPeriod", "345600"}
 
75
        q, err := s.sqs.CreateQueue("testQueue", []sqs.Attribute{timeOutAttribute, maxMessageSizeAttribute, messageRetentionAttribute})
 
76
        req := testServer.WaitRequest()
 
77
 
 
78
        c.Assert(req.Method, Equals, "GET")
 
79
        testServer.PrepareResponse(200, nil, TestChangeMessageVisibilityXmlOK)
 
80
        resp, err := q.ChangeMessageVisibility("MbZj6wDWli%2BJvwwJaBV%2B3dcjk2YW2vA3%2BSTFFljT", 0)
 
81
        testServer.WaitRequest()
 
82
        c.Assert(err, IsNil)
 
83
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "6a7a282a-d013-4a59-aba9-335b0fa48bed")
 
84
}
 
85
 
 
86
func (s *S) TestChangeMessageVisibilityBatch(c *C) {
 
87
        testServer.PrepareResponse(200, nil, TestCreateQueueXmlOK)
 
88
 
 
89
        timeOutAttribute := sqs.Attribute{"VisibilityTimeout", "60"}
 
90
        maxMessageSizeAttribute := sqs.Attribute{"MaximumMessageSize", "65536"}
 
91
        messageRetentionAttribute := sqs.Attribute{"MessageRetentionPeriod", "345600"}
 
92
        q, err := s.sqs.CreateQueue("testQueue", []sqs.Attribute{timeOutAttribute, maxMessageSizeAttribute, messageRetentionAttribute})
 
93
        testServer.WaitRequest()
 
94
 
 
95
        testServer.PrepareResponse(200, nil, TestChangeMessaveVisibilityBatchXmlOK)
 
96
 
 
97
        messageVisibilityBatch := []sqs.ChangeMessageVisibilityBatchEntry{sqs.ChangeMessageVisibilityBatchEntry{"change_visibility_msg_2", "gfk0T0R0waama4fVFffkjKzmhMCymjQvfTFk2LxT33G4ms5subrE0deLKWSscPU1oD3J9zgeS4PQQ3U30qOumIE6AdAv3w%2F%2Fa1IXW6AqaWhGsEPaLm3Vf6IiWqdM8u5imB%2BNTwj3tQRzOWdTOePjOjPcTpRxBtXix%2BEvwJOZUma9wabv%2BSw6ZHjwmNcVDx8dZXJhVp16Bksiox%2FGrUvrVTCJRTWTLc59oHLLF8sEkKzRmGNzTDGTiV%2BYjHfQj60FD3rVaXmzTsoNxRhKJ72uIHVMGVQiAGgBX6HGv9LDmYhPXw4hy%2FNgIg%3D%3D", 45}, sqs.ChangeMessageVisibilityBatchEntry{"change_visibility_msg_3", "gfk0T0R0waama4fVFffkjKzmhMCymjQvfTFk2LxT33FUgBz3%2BnougdeLKWSscPU1%2FXgx%2BxcNnjnQQ3U30qOumIE6AdAv3w%2F%2Fa1IXW6AqaWhGsEPaLm3Vf6IiWqdM8u5imB%2BNTwj3tQRzOWdTOePjOsogjZM%2F7kzn4Ew27XLU9I%2FYaWYmKvDbq%2Fk3HKVB9HfB43kE49atP2aWrzNL4yunG41Q4cfRRtfJdcGQGNHQ2%2Byd0Usf5qR1dZr1iDo5xk946eQat83AxTRP%2BY4Qi0V7FAeSLH9su9xpX6HGv9LDmYhPXw4hy%2FNgIg%3D%3D", 45}}
 
98
        resp, err := q.ChangeMessageVisibilityBatch(messageVisibilityBatch)
 
99
        testServer.WaitRequest()
 
100
        c.Assert(err, IsNil)
 
101
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "ca9668f7-ab1b-4f7a-8859-f15747ab17a7")
 
102
        c.Assert(resp.Id[0], Equals, "change_visibility_msg_2")
 
103
        c.Assert(resp.Id[1], Equals, "change_visibility_msg_3")
 
104
}
 
105
 
 
106
func (s *S) TestReceiveMessage(c *C) {
 
107
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
108
 
 
109
        q, err := s.sqs.GetQueue("testQueue")
 
110
        testServer.WaitRequest()
 
111
 
 
112
        testServer.PrepareResponse(200, nil, TestReceiveMessageXmlOK)
 
113
 
 
114
        resp, err := q.ReceiveMessage([]string{"All"}, 5, 15)
 
115
        testServer.WaitRequest()
 
116
        c.Assert(err, IsNil)
 
117
        c.Assert(len(resp.Messages), Not(Equals), 0)
 
118
}
 
119
 
 
120
func (s *S) TestDeleteMessage(c *C) {
 
121
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
122
 
 
123
        q, err := s.sqs.GetQueue("testQueue")
 
124
        testServer.WaitRequest()
 
125
 
 
126
        testServer.PrepareResponse(200, nil, TestDeleteMessageXmlOK)
 
127
 
 
128
        resp, err := q.DeleteMessage("MbZj6wDWli%2BJvwwJaBV%2B3dcjk2YW2vA3%2BSTFFljTM8tJJg6HRG6PYSasuWXPJB%2BCwLj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ%2BQEauMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=")
 
129
        testServer.WaitRequest()
 
130
        c.Assert(err, IsNil)
 
131
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "b5293cb5-d306-4a17-9048-b263635abe42")
 
132
}
 
133
 
 
134
func (s *S) TestDeleteMessageBatch(c *C) {
 
135
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
136
 
 
137
        q, err := s.sqs.GetQueue("testQueue")
 
138
        testServer.WaitRequest()
 
139
 
 
140
        testServer.PrepareResponse(200, nil, TestDeleteMessageBatchXmlOK)
 
141
 
 
142
        deleteMessageBatch := []sqs.DeleteMessageBatch{sqs.DeleteMessageBatch{Id: "msg1", ReceiptHandle: "gfk0T0R0waama4fVFffkjPQrrvzMrOg0fTFk2LxT33EuB8wR0ZCFgKWyXGWFoqqpCIiprQUEhir%2F5LeGPpYTLzjqLQxyQYaQALeSNHb0us3uE84uujxpBhsDkZUQkjFFkNqBXn48xlMcVhTcI3YLH%2Bd%2BIqetIOHgBCZAPx6r%2B09dWaBXei6nbK5Ygih21DCDdAwFV68Jo8DXhb3ErEfoDqx7vyvC5nCpdwqv%2BJhU%2FTNGjNN8t51v5c%2FAXvQsAzyZVNapxUrHIt4NxRhKJ72uICcxruyE8eRXlxIVNgeNP8ZEDcw7zZU1Zw%3D%3D"}, sqs.DeleteMessageBatch{Id: "msg2", ReceiptHandle: "gfk0T0R0waama4fVFffkjKzmhMCymjQvfTFk2LxT33G4ms5subrE0deLKWSscPU1oD3J9zgeS4PQQ3U30qOumIE6AdAv3w%2F%2Fa1IXW6AqaWhGsEPaLm3Vf6IiWqdM8u5imB%2BNTwj3tQRzOWdTOePjOjPcTpRxBtXix%2BEvwJOZUma9wabv%2BSw6ZHjwmNcVDx8dZXJhVp16Bksiox%2FGrUvrVTCJRTWTLc59oHLLF8sEkKzRmGNzTDGTiV%2BYjHfQj60FD3rVaXmzTsoNxRhKJ72uIHVMGVQiAGgB%2BqAbSqfKHDQtVOmJJgkHug%3D%3D"}}
 
143
        resp, err := q.DeleteMessageBatch(deleteMessageBatch)
 
144
        testServer.WaitRequest()
 
145
 
 
146
        c.Assert(err, IsNil)
 
147
        c.Assert(len(resp.DeleteMessageBatchResult.Ids), Equals, 2)
 
148
        c.Assert(resp.DeleteMessageBatchResult.Ids[0], Equals, "msg1")
 
149
        c.Assert(resp.DeleteMessageBatchResult.Ids[1], Equals, "msg2")
 
150
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "d6f86b7a-74d1-4439-b43f-196a1e29cd85")
 
151
}
 
152
 
 
153
func (s *S) TestAddPermission(c *C) {
 
154
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
155
 
 
156
        q, err := s.sqs.GetQueue("testQueue")
 
157
        testServer.WaitRequest()
 
158
 
 
159
        testServer.PrepareResponse(200, nil, TestAddPermissionXmlOK)
 
160
        resp, err := q.AddPermission("testLabel", []sqs.AccountPermission{sqs.AccountPermission{"125074342641", "SendMessage"}, sqs.AccountPermission{"125074342642", "ReceiveMessage"}})
 
161
        testServer.WaitRequest()
 
162
 
 
163
        c.Assert(err, IsNil)
 
164
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "9a285199-c8d6-47c2-bdb2-314cb47d599d")
 
165
}
 
166
 
 
167
func (s *S) TestRemovePermission(c *C) {
 
168
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
169
 
 
170
        q, err := s.sqs.GetQueue("testQueue")
 
171
        testServer.WaitRequest()
 
172
 
 
173
        testServer.PrepareResponse(200, nil, TestRemovePermissionXmlOK)
 
174
        resp, err := q.RemovePermission("testLabel")
 
175
        testServer.WaitRequest()
 
176
 
 
177
        c.Assert(err, IsNil)
 
178
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "f8bdb362-6616-42c0-977a-ce9a8bcce3bb")
 
179
}
 
180
 
 
181
func (s *S) TestSendMessage(c *C) {
 
182
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
183
 
 
184
        q, err := s.sqs.GetQueue("testQueue")
 
185
        testServer.WaitRequest()
 
186
 
 
187
        testServer.PrepareResponse(200, nil, TestSendMessageXmlOK)
 
188
 
 
189
        resp, err := q.SendMessage("This is a Message")
 
190
        testServer.WaitRequest()
 
191
        c.Assert(err, IsNil)
 
192
        c.Assert(resp.SendMessageResult.MD5OfMessageBody, Equals, "fafb00f5732ab283681e124bf8747ed1")
 
193
        c.Assert(resp.SendMessageResult.MessageId, Equals, "5fea7756-0ea4-451a-a703-a558b933e274")
 
194
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "27daac76-34dd-47df-bd01-1f6e873584a0")
 
195
}
 
196
 
 
197
func (s *S) TestSendMessageWithDelay(c *C) {
 
198
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
199
 
 
200
        q, err := s.sqs.GetQueue("testQueue")
 
201
        testServer.WaitRequest()
 
202
 
 
203
        testServer.PrepareResponse(200, nil, TestSendMessageXmlOK)
 
204
 
 
205
        resp, err := q.SendMessageWithDelay("This is a Message", 60)
 
206
        testServer.WaitRequest()
 
207
        c.Assert(err, IsNil)
 
208
        c.Assert(resp.SendMessageResult.MD5OfMessageBody, Equals, "fafb00f5732ab283681e124bf8747ed1")
 
209
        c.Assert(resp.SendMessageResult.MessageId, Equals, "5fea7756-0ea4-451a-a703-a558b933e274")
 
210
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "27daac76-34dd-47df-bd01-1f6e873584a0")
 
211
}
 
212
 
 
213
func (s *S) TestSendMessageBatch(c *C) {
 
214
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
215
 
 
216
        q, err := s.sqs.GetQueue("testQueue")
 
217
        testServer.WaitRequest()
 
218
 
 
219
        testServer.PrepareResponse(200, nil, TestSendMessageBatchXmlOK)
 
220
 
 
221
        sendMessageBatchRequests := []sqs.SendMessageBatchRequestEntry{sqs.SendMessageBatchRequestEntry{Id: "test_msg_001", MessageBody: "test message body 1", DelaySeconds: 30}}
 
222
        resp, err := q.SendMessageBatch(sendMessageBatchRequests)
 
223
        testServer.WaitRequest()
 
224
        c.Assert(err, IsNil)
 
225
        c.Assert(len(resp.SendMessageBatchResult.Entries), Equals, 2)
 
226
        c.Assert(resp.SendMessageBatchResult.Entries[0].Id, Equals, "test_msg_001")
 
227
}
 
228
 
 
229
func (s *S) TestGetQueueAttributes(c *C) {
 
230
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
231
 
 
232
        q, err := s.sqs.GetQueue("testQueue")
 
233
        testServer.WaitRequest()
 
234
 
 
235
        testServer.PrepareResponse(200, nil, TestGetQueueAttributesAllXmlOK)
 
236
 
 
237
        resp, err := q.GetQueueAttributes([]string{"ALL"})
 
238
        testServer.WaitRequest()
 
239
 
 
240
        c.Assert(err, IsNil)
 
241
        c.Assert(len(resp.Attributes), Equals, 8)
 
242
        c.Assert(resp.Attributes[0].Name, Equals, "VisibilityTimeout")
 
243
        c.Assert(resp.Attributes[0].Value, Equals, "30")
 
244
}
 
245
 
 
246
func (s *S) TestGetQueueAttributesSelective(c *C) {
 
247
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
248
 
 
249
        q, err := s.sqs.GetQueue("testQueue")
 
250
        testServer.WaitRequest()
 
251
 
 
252
        testServer.PrepareResponse(200, nil, TestGetQueueAttributesSelectiveXmlOK)
 
253
 
 
254
        resp, err := q.GetQueueAttributes([]string{"VisibilityTimeout", "DelaySeconds"})
 
255
        testServer.WaitRequest()
 
256
 
 
257
        c.Assert(err, IsNil)
 
258
        c.Assert(len(resp.Attributes), Equals, 2)
 
259
        c.Assert(resp.Attributes[0].Name, Equals, "VisibilityTimeout")
 
260
        c.Assert(resp.Attributes[0].Value, Equals, "30")
 
261
        c.Assert(resp.Attributes[1].Name, Equals, "DelaySeconds")
 
262
        c.Assert(resp.Attributes[1].Value, Equals, "0")
 
263
}
 
264
 
 
265
func (s *S) TestDeleteQueue(c *C) {
 
266
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
267
 
 
268
        q, err := s.sqs.GetQueue("testQueue")
 
269
        testServer.WaitRequest()
 
270
 
 
271
        testServer.PrepareResponse(200, nil, TestDeleteQueueXmlOK)
 
272
        resp, err := q.Delete()
 
273
        testServer.WaitRequest()
 
274
 
 
275
        c.Assert(err, IsNil)
 
276
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "6fde8d1e-52cd-4581-8cd9-c512f4c64223")
 
277
}
 
278
 
 
279
func (s *S) TestSetQueueAttributes(c *C) {
 
280
        testServer.PrepareResponse(200, nil, TestGetQueueUrlXmlOK)
 
281
 
 
282
        q, err := s.sqs.GetQueue("testQueue")
 
283
        testServer.WaitRequest()
 
284
 
 
285
        testServer.PrepareResponse(200, nil, TestSetQueueAttributesXmlOK)
 
286
        var policyStr = `
 
287
  {
 
288
        "Version":"2008-10-17",
 
289
        "Id":"/123456789012/testQueue/SQSDefaultPolicy",
 
290
        "Statement":  [
 
291
             {
 
292
             "Sid":"Queue1ReceiveMessage",
 
293
             "Effect":"Allow",
 
294
             "Principal":{"AWS":"*"},
 
295
             "Action":"SQS:ReceiveMessage",
 
296
             "Resource":"arn:aws:sqs:us-east-1:123456789012:testQueue"
 
297
              }
 
298
         ]    
 
299
   }
 
300
  `
 
301
        resp, err := q.SetQueueAttributes(sqs.Attribute{"Policy", policyStr})
 
302
        testServer.WaitRequest()
 
303
 
 
304
        c.Assert(err, IsNil)
 
305
        c.Assert(resp.ResponseMetadata.RequestId, Equals, "e5cca473-4fc0-4198-a451-8abb94d02c75")
 
306
}