aws 1.10.1 → 1.10.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/right_aws'
@@ -0,0 +1,291 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSqs < Test::Unit::TestCase
4
+
5
+ GRANTEE_EMAIL_ADDRESS = 'madhur@amazon.com'
6
+ RIGHT_MESSAGE_TEXT = 'Right test message'
7
+
8
+
9
+ def setup
10
+ @sqs = Rightscale::SqsInterface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
11
+ @queue_name = 'right_sqs_test_awesome_queue'
12
+ # for classes
13
+ @s = Rightscale::Sqs.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
14
+ end
15
+
16
+ # Wait for the queue to appears in the queues list.
17
+ # Amazon needs some time to after the queue creation to place
18
+ # it to the accessible queues list. If we dont want to get
19
+ # the additional faults then wait a bit...
20
+ def wait_for_queue_url(queue_name)
21
+ queue_url = nil
22
+ until queue_url
23
+ queue_url = @sqs.queue_url_by_name(queue_name)
24
+ unless queue_url
25
+ print '-'
26
+ STDOUT.flush
27
+ sleep 1
28
+ end
29
+ end
30
+ queue_url
31
+ end
32
+
33
+
34
+
35
+
36
+ def assert_eventually_equal(value, timeout=30, failmsg="", &block)
37
+ start_time = Time.now.to_i
38
+ tries = 0
39
+ while(yield != value) do
40
+ tries += 1
41
+ print '-'
42
+ STDOUT.flush
43
+ s = Time.now.to_i - start_time
44
+ flunk("Timeout: #{failmsg}: did not equal \"#{value}\" after #{tries} tries in #{s}s.") if s > timeout
45
+ sleep(1)
46
+ setup if (tries % 10) == 0
47
+ end
48
+ end
49
+
50
+
51
+ #---------------------------
52
+ # Rightscale::SqsInterface
53
+ #---------------------------
54
+
55
+ def test_01_create_queue
56
+ queue_url = @sqs.create_queue @queue_name
57
+ assert queue_url[/http.*#{@queue_name}/], 'New queue creation fail'
58
+ end
59
+
60
+ def test_02_list_queues
61
+ wait_for_queue_url(@queue_name)
62
+ queues = @sqs.list_queues('right_')
63
+ assert queues.size>0, 'Must more that 0 queues in list'
64
+ end
65
+
66
+ def test_03_set_and_get_queue_attributes
67
+ queue_url = @sqs.queue_url_by_name(@queue_name)
68
+ assert queue_url[/http.*#{@queue_name}/], "#{@queue_name} must exist!"
69
+ assert @sqs.set_queue_attributes(queue_url, 'VisibilityTimeout', 111), 'Set_queue_attributes fail'
70
+ sleep 20 # Amazon needs some time to change attribute
71
+ assert_equal '111', @sqs.get_queue_attributes(queue_url)['VisibilityTimeout'], 'New VisibilityTimeout must be equal to 111'
72
+ end
73
+
74
+ def test_04_set_and_get_visibility_timeout
75
+ queue_url = @sqs.queue_url_by_name(@queue_name)
76
+ assert @sqs.set_visibility_timeout(queue_url, 222), 'Set_visibility_timeout fail'
77
+ sleep 20 # Amazon needs some time to change attribute
78
+ #assert_equal 222, @sqs.get_visibility_timeout(queue_url), 'Get_visibility_timeout must return to 222'
79
+ assert_eventually_equal(222, 60, 'Get_visibility_timeout must return to 222') do
80
+ @sqs.get_visibility_timeout(queue_url)
81
+ end
82
+ end
83
+
84
+ def test_05_add_test_remove_grant
85
+ queue_url = @sqs.queue_url_by_name(@queue_name)
86
+ assert @sqs.add_grant(queue_url, GRANTEE_EMAIL_ADDRESS, 'FULLCONTROL'), 'Add grant fail'
87
+ grants_list = @sqs.list_grants(queue_url, GRANTEE_EMAIL_ADDRESS)
88
+ assert grants_list.size>0, 'List_grants must return at least 1 record for user #{GRANTEE_EMAIL_ADDRESS}'
89
+ assert @sqs.remove_grant(queue_url, GRANTEE_EMAIL_ADDRESS, 'FULLCONTROL'), 'Remove_grant fail'
90
+ end
91
+
92
+ def test_06_send_message
93
+ queue_url = @sqs.queue_url_by_name(@queue_name)
94
+ # send 5 messages for the tests below
95
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
96
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
97
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
98
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
99
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
100
+ end
101
+
102
+ def test_07_get_queue_length
103
+ queue_url = @sqs.queue_url_by_name(@queue_name)
104
+ assert_equal 5, @sqs.get_queue_length(queue_url), 'Queue must have 5 messages'
105
+ end
106
+
107
+ def test_08_receive_message
108
+ queue_url = @sqs.queue_url_by_name(@queue_name)
109
+ r_message = @sqs.receive_message(queue_url, 1)
110
+ assert_equal RIGHT_MESSAGE_TEXT, r_message[:body], 'Receive message get wron message text'
111
+ p_message = @sqs.peek_message(queue_url, r_message[:id])
112
+ assert_equal r_message[:body], p_message[:body], 'Received and Peeked messages must be equal'
113
+ assert @sqs.change_message_visibility(queue_url, r_message[:id], 0), 'Change_message_visibility fail'
114
+ end
115
+
116
+ def test_09_delete_message
117
+ queue_url = @sqs.queue_url_by_name(@queue_name)
118
+ message = @sqs.receive_message(queue_url)
119
+ assert @sqs.delete_message(queue_url, message[:id]), 'Delete_message fail'
120
+ assert @sqs.pop_message(queue_url), 'Pop_message fail'
121
+ end
122
+
123
+ def test_10_clear_and_delete_queue
124
+ queue_url = @sqs.queue_url_by_name(@queue_name)
125
+ assert_raise(Rightscale::AwsError) { @sqs.delete_queue(queue_url) }
126
+ ## oops, force_clear_queue does not work any more - amazon expects for 60 secs timeout between
127
+ ## queue deletion and recreation...
128
+ ## assert @sqs.force_clear_queue(queue_url), 'Force_clear_queue fail'
129
+ assert @sqs.clear_queue(queue_url), 'Clear_queue fail'
130
+ assert @sqs.delete_queue(queue_url), 'Delete_queue fail'
131
+ end
132
+
133
+ #---------------------------
134
+ # Rightscale::Sqs classes
135
+ #---------------------------
136
+
137
+ def test_20_sqs_create_delete_queue
138
+ assert @s, 'Rightscale::Sqs must exist'
139
+ # get queues list
140
+ queues_size = @s.queues.size
141
+ # create new queue
142
+ queue = @s.queue("#{@queue_name}_20", true)
143
+ # check that it is created
144
+ assert queue.is_a?(Rightscale::Sqs::Queue)
145
+ wait_for_queue_url(@queue_name)
146
+ # check that amount of queues has increased
147
+ assert_eventually_equal(queues_size + 1, 60, "The number of queues did not increase by one") do
148
+ @s.queues.size
149
+ end
150
+ # delete queue
151
+ assert queue.delete
152
+ end
153
+
154
+ def test_21_queue_create
155
+ # create new queue
156
+ queue = Rightscale::Sqs::Queue.create(@s, "#{@queue_name}_21", true)
157
+ # check that it is created
158
+ assert queue.is_a?(Rightscale::Sqs::Queue)
159
+ wait_for_queue_url(@queue_name)
160
+ end
161
+
162
+ def test_22_queue_attributes
163
+ queue = Rightscale::Sqs::Queue.create(@s, "#{@queue_name}_21", false)
164
+ # get a list of attrinutes
165
+ attributes = queue.get_attribute
166
+ assert attributes.is_a?(Hash) && attributes.size>0
167
+ # get attribute value and increase it by 10
168
+ v = (queue.get_attribute('VisibilityTimeout').to_i + 10).to_s
169
+ # set attribute
170
+ assert queue.set_attribute('VisibilityTimeout', v)
171
+ # wait a bit
172
+ sleep 20
173
+ # check that attribute has changed
174
+ assert_equal v, queue.get_attribute('VisibilityTimeout')
175
+ # get queue visibility timeout
176
+ assert v.to_i, queue.visibility
177
+ # change it
178
+ queue.visibility += 10
179
+ # make sure that it is changed
180
+ assert v.to_i + 10, queue.visibility
181
+ end
182
+
183
+ def test_23_grantees
184
+ queue = Rightscale::Sqs::Queue.create(@s, "#{@queue_name}_21", false)
185
+ # get a list of grantees
186
+ grantees = queue.grantees
187
+ # well, queue must exist at least some seconds before we could add grantees to it....
188
+ # otherwise we get "Queue does not exists" message. Hence we use the queue
189
+ # has been created at previous step.
190
+ #
191
+ # create new grantee
192
+ grantee = Rightscale::Sqs::Grantee.new(queue, GRANTEE_EMAIL_ADDRESS)
193
+ assert grantee.perms.empty?
194
+ # grant perms
195
+ assert grantee.grant('FULLCONTROL')
196
+ assert grantee.grant('RECEIVEMESSAGE')
197
+ assert_equal 2, grantee.perms.size
198
+ # make sure that amount of grantees has increased
199
+ assert grantees.size < queue.grantees.size
200
+ # revoke perms
201
+ assert grantee.revoke('RECEIVEMESSAGE')
202
+ assert_equal 1, grantee.perms.size
203
+ # remove grantee
204
+ assert grantee.drop
205
+ # Don't test this - just for cleanup purposes
206
+ queue.delete
207
+ end
208
+
209
+ def test_24_send_size
210
+ queue_url = @sqs.queue_url_by_name("#{@queue_name}_24")
211
+ @sqs.delete_queue(queue_url)
212
+ queue = Rightscale::Sqs::Queue.create(@s, "#{@queue_name}_24", true)
213
+ # send 5 messages
214
+ assert queue.push('a1')
215
+ assert queue.push('a2')
216
+ assert queue.push('a3')
217
+ assert queue.push('a4')
218
+ assert queue.push('a5')
219
+ # check queue size
220
+ assert_equal 5, queue.size
221
+ # send one more
222
+ assert queue.push('a6')
223
+ # check queue size again
224
+ assert_equal 6, queue.size
225
+ end
226
+
227
+ def test_25_message_receive_pop_peek_delete
228
+ queue = Rightscale::Sqs::Queue.create(@s, "#{@queue_name}_24", false)
229
+ # get queue size
230
+ size = queue.size
231
+ # get first message
232
+ m1 = queue.receive(10)
233
+ assert m1.is_a?(Rightscale::Sqs::Message)
234
+ # pop second message
235
+ m2 = queue.pop
236
+ assert m2.is_a?(Rightscale::Sqs::Message)
237
+ # make sure that queue size has decreased
238
+ assert_equal size-1, queue.size
239
+ # peek message 1
240
+ m1p = queue.peek(m1.id)
241
+ assert m1p.is_a?(Rightscale::Sqs::Message)
242
+ assert_equal m1.id, m1p.id
243
+ assert_equal m1.body, m1p.body
244
+ # change message visibility
245
+ assert m1.visibility = 30
246
+ # delete messsage
247
+ assert m1.delete
248
+ # make sure that queue size has decreased again
249
+ assert_equal size-2, queue.size
250
+ end
251
+
252
+ def test_26
253
+ queue = Rightscale::Sqs::Queue.create(@s, "#{@queue_name}_24", false)
254
+ # lock message
255
+ queue.receive(100)
256
+ # clear queue
257
+ assert queue.clear
258
+ # queue size is greater than zero
259
+ assert queue.size>0
260
+ queue.push('123456')
261
+ assert_raise(Rightscale::AwsError) { queue.delete }
262
+ assert queue.delete(true)
263
+ end
264
+
265
+ def test_27_set_amazon_problems
266
+ original_problems = Rightscale::SqsInterface.amazon_problems
267
+ assert(original_problems.length > 0)
268
+ Rightscale::SqsInterface.amazon_problems= original_problems << "A New Problem"
269
+ new_problems = Rightscale::SqsInterface.amazon_problems
270
+ assert_equal(new_problems, original_problems)
271
+
272
+ Rightscale::SqsInterface.amazon_problems= nil
273
+ assert_nil(Rightscale::SqsInterface.amazon_problems)
274
+ end
275
+
276
+ def test_28_check_threading_model
277
+ assert(!@sqs.multi_thread)
278
+ newsqs = Rightscale::SqsInterface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, {:multi_thread => true})
279
+ assert(newsqs.multi_thread)
280
+ end
281
+
282
+ def test_29_signature_version_0
283
+ sqs = Rightscale::SqsInterface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, :signature_version => '0')
284
+ assert_nothing_raised do
285
+ sqs.list_queues
286
+ end
287
+ # check that the request has correct signature version
288
+ assert sqs.last_request.path.include?('SignatureVersion=0')
289
+ end
290
+
291
+ end
@@ -0,0 +1,209 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSqsGen2 < Test::Unit::TestCase
4
+
5
+ GRANTEE_EMAIL_ADDRESS = 'fester@example.com'
6
+ RIGHT_MESSAGE_TEXT = 'Right test message'
7
+
8
+
9
+ def setup
10
+ @sqs = Rightscale::SqsGen2Interface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
11
+ @queue_name = 'right_sqs_test_gen2_queue'
12
+ # for classes
13
+ @s = Rightscale::SqsGen2.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key)
14
+ end
15
+
16
+ # Wait for the queue to appear in the queues list.
17
+ # Amazon needs some time to after the queue creation to place
18
+ # it to the accessible queues list. If we dont want to get
19
+ # the additional faults then wait a bit...
20
+ def wait_for_queue_url(queue_name)
21
+ queue_url = nil
22
+ until queue_url
23
+ queue_url = @sqs.queue_url_by_name(queue_name)
24
+ unless queue_url
25
+ print '-'
26
+ STDOUT.flush
27
+ sleep 1
28
+ end
29
+ end
30
+ queue_url
31
+ end
32
+
33
+
34
+ #---------------------------
35
+ # Rightscale::SqsInterface
36
+ #---------------------------
37
+
38
+ def test_01_create_queue
39
+ queue_url = @sqs.create_queue @queue_name
40
+ assert queue_url[/http.*#{@queue_name}/], 'New queue creation failed'
41
+ end
42
+
43
+ def test_02_list_queues
44
+ wait_for_queue_url(@queue_name)
45
+ queues = @sqs.list_queues('right_')
46
+ assert queues.size>0, 'Must more that 0 queues in list'
47
+ end
48
+
49
+ def test_03_set_and_get_queue_attributes
50
+ queue_url = @sqs.queue_url_by_name(@queue_name)
51
+ assert queue_url[/http.*#{@queue_name}/], "#{@queue_name} must exist!"
52
+ assert @sqs.set_queue_attributes(queue_url, 'VisibilityTimeout', 111), 'Set_queue_attributes fail'
53
+ sleep 20 # Amazon needs some time to change attribute
54
+ assert_equal '111', @sqs.get_queue_attributes(queue_url)['VisibilityTimeout'], 'New VisibilityTimeout must be equal to 111'
55
+ end
56
+
57
+ def test_06_send_message
58
+ queue_url = @sqs.queue_url_by_name(@queue_name)
59
+ # send 5 messages for the tests below
60
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
61
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
62
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
63
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
64
+ assert @sqs.send_message(queue_url, RIGHT_MESSAGE_TEXT)
65
+ end
66
+
67
+ def test_07_get_queue_length
68
+ queue_url = @sqs.queue_url_by_name(@queue_name)
69
+ assert_equal 5, @sqs.get_queue_length(queue_url), 'Queue must have 5 messages'
70
+ end
71
+
72
+ def test_08_receive_message
73
+ queue_url = @sqs.queue_url_by_name(@queue_name)
74
+ r_message = @sqs.receive_message(queue_url, 1)[0]
75
+ assert r_message, "Receive returned no message(s), but this is not necessarily incorrect"
76
+ assert_equal RIGHT_MESSAGE_TEXT, r_message['Body'], 'Receive message got wrong message text'
77
+ end
78
+
79
+ def test_09_delete_message
80
+ queue_url = @sqs.queue_url_by_name(@queue_name)
81
+ message = @sqs.receive_message(queue_url)[0]
82
+ assert @sqs.delete_message(queue_url, message['ReceiptHandle']), 'Delete_message fail'
83
+ assert @sqs.pop_message(queue_url), 'Pop_message fail'
84
+ end
85
+
86
+ def test_10_clear_and_delete_queue
87
+ queue_url = @sqs.queue_url_by_name(@queue_name)
88
+ assert @sqs.delete_queue(queue_url)
89
+ end
90
+
91
+ #---------------------------
92
+ # Rightscale::Sqs classes
93
+ #---------------------------
94
+
95
+ def test_20_sqs_create_delete_queue
96
+ assert @s, 'Rightscale::SqsGen2 must exist'
97
+ # get queues list
98
+ queues_size = @s.queues.size
99
+ # create new queue
100
+ queue = @s.queue("#{@queue_name}_20", true)
101
+ # check that it is created
102
+ assert queue.is_a?(Rightscale::SqsGen2::Queue)
103
+ wait_for_queue_url(@queue_name)
104
+ # check that amount of queues has increased
105
+ assert_equal queues_size + 1, @s.queues.size
106
+ # delete queue
107
+ assert queue.delete
108
+ end
109
+
110
+ def test_21_queue_create
111
+ # create new queue
112
+ queue = Rightscale::SqsGen2::Queue.create(@s, "#{@queue_name}_21", true)
113
+ # check that it is created
114
+ assert queue.is_a?(Rightscale::SqsGen2::Queue)
115
+ wait_for_queue_url(@queue_name)
116
+ end
117
+
118
+ def test_22_queue_attributes
119
+ queue = Rightscale::SqsGen2::Queue.create(@s, "#{@queue_name}_21", false)
120
+ # get a list of attrinutes
121
+ attributes = queue.get_attribute
122
+ assert attributes.is_a?(Hash) && attributes.size>0
123
+ # get attribute value and increase it by 10
124
+ v = (queue.get_attribute('VisibilityTimeout').to_i + 10).to_s
125
+ # set attribute
126
+ assert queue.set_attribute('VisibilityTimeout', v)
127
+ # wait a bit
128
+ sleep 20
129
+ # check that attribute has changed
130
+ assert_equal v, queue.get_attribute('VisibilityTimeout')
131
+ # get queue visibility timeout
132
+ assert_equal v, queue.visibility
133
+ # change it
134
+ queue.visibility = queue.visibility.to_i + 10
135
+ # make sure that it is changed
136
+ assert v.to_i + 10, queue.visibility
137
+ end
138
+
139
+ def test_24_send_size
140
+ queue = Rightscale::SqsGen2::Queue.create(@s, "#{@queue_name}_24", true)
141
+ # send 5 messages
142
+ assert queue.push('a1')
143
+ assert queue.push('a2')
144
+ assert queue.push('a3')
145
+ assert queue.push('a4')
146
+ assert queue.push('a5')
147
+ # check queue size
148
+ assert_equal 5, queue.size
149
+ # send one more
150
+ assert queue.push('a6')
151
+ # check queue size again
152
+ assert_equal 6, queue.size
153
+ end
154
+
155
+ def test_25_message_receive_pop_delete
156
+ queue = Rightscale::SqsGen2::Queue.create(@s, "#{@queue_name}_24", false)
157
+ # get queue size
158
+ size = queue.size
159
+ # get first message
160
+ m1 = queue.receive(10)
161
+ assert m1.is_a?(Rightscale::SqsGen2::Message)
162
+ # pop second message
163
+ m2 = queue.pop
164
+ assert m2.is_a?(Rightscale::SqsGen2::Message)
165
+ # make sure that queue size has decreased
166
+ assert_equal size-1, queue.size
167
+ # delete messsage
168
+ assert m1.delete
169
+ # make sure that queue size has decreased again
170
+ assert_equal size-2, queue.size
171
+ end
172
+
173
+ def test_26
174
+ queue = Rightscale::SqsGen2::Queue.create(@s, "#{@queue_name}_24", false)
175
+ # lock message
176
+ queue.receive(100)
177
+ # clear queue
178
+ assert queue.clear
179
+ # queue size is greater than zero
180
+ assert queue.size>0
181
+ end
182
+
183
+ def test_27_set_amazon_problems
184
+ original_problems = Rightscale::SqsGen2Interface.amazon_problems
185
+ assert(original_problems.length > 0)
186
+ Rightscale::SqsGen2Interface.amazon_problems= original_problems << "A New Problem"
187
+ new_problems = Rightscale::SqsGen2Interface.amazon_problems
188
+ assert_equal(new_problems, original_problems)
189
+
190
+ Rightscale::SqsGen2Interface.amazon_problems= nil
191
+ assert_nil(Rightscale::SqsGen2Interface.amazon_problems)
192
+ end
193
+
194
+ def test_28_check_threading_model
195
+ assert(!@sqs.multi_thread)
196
+ newsqs = Rightscale::SqsGen2Interface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, {:multi_thread => true})
197
+ assert(newsqs.multi_thread)
198
+ end
199
+
200
+ def test_29_signature_version_0
201
+ sqs = Rightscale::SqsInterface.new(TestCredentials.aws_access_key_id, TestCredentials.aws_secret_access_key, :signature_version => '0')
202
+ assert_nothing_raised do
203
+ sqs.list_queues
204
+ end
205
+ # check that the request has correct signature version
206
+ assert sqs.last_request.path.include?('SignatureVersion=0')
207
+ end
208
+
209
+ end