Skip to content

Commit c8d4fc3

Browse files
Fix matchers to align with waiters SEP (#3242)
Fix matchers to align with waiters SEP
1 parent f9893b7 commit c8d4fc3

File tree

4 files changed

+466
-56
lines changed

4 files changed

+466
-56
lines changed

build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/waiter.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def acceptors(acceptors)
8282
acceptor['argument']
8383
)
8484
end
85+
acceptor['expected'] = convert_value(acceptor['expected'])
8586
end
8687
HashFormatter.new(
8788
wrap: false,
@@ -90,6 +91,29 @@ def acceptors(acceptors)
9091
).format(acceptors: acceptors)
9192
end
9293

94+
def convert_value(value)
95+
case value
96+
when Hash then convert_keys(value)
97+
when Array then convert_array(value)
98+
else value
99+
end
100+
end
101+
102+
# Underscore and symbolize all keys in hash
103+
def convert_keys(hash)
104+
hash.each_with_object({}) do |(key, value), result|
105+
result[convert_key(key)] = convert_value(value)
106+
end
107+
end
108+
109+
def convert_key(key)
110+
key.is_a?(String) ? Underscore.underscore(key).to_sym : key
111+
end
112+
113+
# Convert keys for every object in array
114+
def convert_array(array)
115+
array.collect { |elem| convert_value(elem) }
116+
end
93117
end
94118
end
95119
end

gems/aws-sdk-core/spec/aws/waiters_spec.rb

Lines changed: 180 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ module Waiters
8989
end
9090

9191
it 'returns when successful' do
92-
client.stub_responses(:waiter_operation, table: { table_status: 'ACTIVE' })
92+
client.stub_responses(:waiter_operation, complex_object: { string_member: 'expected' })
9393
expect { client.wait_until(:generic_waiter) }
9494
.not_to raise_error
9595
end
9696

9797
it 'returns the client response' do
98-
client.stub_responses(:waiter_operation, table: { table_status: 'ACTIVE' })
98+
client.stub_responses(:waiter_operation, complex_object: { string_member: 'expected' })
9999
resp = client.wait_until(:generic_waiter)
100-
expect(resp.table.table_status).to eq('ACTIVE')
100+
expect(resp.complex_object.string_member).to eq('expected')
101101
end
102102

103103
it 'raises an error when failed' do
@@ -231,22 +231,22 @@ module Waiters
231231
client.stub_responses(
232232
:waiter_operation,
233233
'ResourceNotFoundException',
234-
{ table: { table_status: 'ACTIVE' } }
234+
{ complex_object: { string_member: 'expected' } }
235235
)
236236
expect { client.wait_until(:error_matcher_with_false) }
237237
.not_to raise_error
238238
end
239239

240240
it 'fails when matched' do
241-
client.stub_responses(:waiter_operation, table: { table_status: 'ACTIVE' })
241+
client.stub_responses(:waiter_operation, complex_object: { string_member: 'expected' })
242242
expect { client.wait_until(:error_matcher_with_false_fails) }
243243
.to raise_error(Errors::WaiterFailed)
244244
end
245245

246246
it 'retries when matched' do
247247
client.stub_responses(
248248
:waiter_operation,
249-
{ table: { table_status: 'ACTIVE' } },
249+
{ complex_object: { string_member: 'expected' } },
250250
'ResourceNotFoundException'
251251
)
252252
retries = 0
@@ -261,8 +261,8 @@ module Waiters
261261
it 'retries and succeed when matched' do
262262
client.stub_responses(
263263
:waiter_operation,
264-
{ table: { table_status: 'UPDATING' } },
265-
{ table: { table_status: 'ACTIVE' } }
264+
{ complex_object: { string_member: 'retry' } },
265+
{ complex_object: { string_member: 'expected' } }
266266
)
267267
retries = 0
268268
expect do
@@ -272,18 +272,86 @@ module Waiters
272272
end
273273

274274
it 'fails when matched' do
275-
client.stub_responses(:waiter_operation, table: { table_status: 'FAILED' })
275+
client.stub_responses(:waiter_operation, complex_object: { string_member: 'unexpected' })
276276
expect { client.wait_until(:path_matcher) }
277277
.to raise_error(Errors::FailureStateError)
278278
end
279+
280+
it 'can match objects' do
281+
object = {
282+
object_member: { string_member: 'string' },
283+
string_member: 'string',
284+
number_member: 1,
285+
boolean_member: true,
286+
array_member: [1, 2, 3],
287+
object_array_member: [
288+
{
289+
object_member: { string_member: 'string' },
290+
string_member: 'string'
291+
}
292+
]
293+
}
294+
client.stub_responses(:waiter_operation, complex_object: object)
295+
expect { client.wait_until(:path_matcher_object) }
296+
.to_not raise_error
297+
end
298+
299+
it 'can match numbers' do
300+
resp = {
301+
object_list: [
302+
{ string_member: 'expected' },
303+
{ string_member: 'expected' },
304+
{ string_member: 'expected' }
305+
]
306+
}
307+
client.stub_responses(:waiter_operation, resp)
308+
expect { client.wait_until(:path_matcher_number) }
309+
.to_not raise_error
310+
end
311+
312+
it 'can match arrays' do
313+
client.stub_responses(:waiter_operation, number_list: [1, 2, 3])
314+
expect { client.wait_until(:path_matcher_array) }
315+
.to_not raise_error
316+
end
317+
318+
it 'can match array of objects' do
319+
list = [
320+
{
321+
object_member: { string_member: 'string' },
322+
string_member: 'string',
323+
number_member: 1,
324+
boolean_member: true,
325+
array_member: [1, 2, 3],
326+
object_array_member: [
327+
{
328+
object_member: { string_member: 'string' },
329+
string_member: 'string'
330+
}
331+
]
332+
},
333+
{
334+
object_member: { string_member: 'string' }
335+
}
336+
]
337+
client.stub_responses(:waiter_operation, object_list: list)
338+
expect { client.wait_until(:path_matcher_object_array) }
339+
.to_not raise_error
340+
end
341+
342+
it 'can match booleans' do
343+
client.stub_responses(:waiter_operation, boolean: false)
344+
expect { client.wait_until(:path_matcher_boolean) }
345+
.to_not raise_error
346+
end
279347
end
280348

281349
context 'pathAll' do
282350
it 'retries and succeed when matched' do
283351
client.stub_responses(
284352
:waiter_operation,
285-
{ table_list: [{ table_status: 'UPDATING' }] },
286-
{ table_list: [{ table_status: 'ACTIVE' }] }
353+
{ object_list: [{ string_member: 'retry' }] },
354+
{ object_list: [{ string_member: 'expected' }] }
287355
)
288356
retries = 0
289357
expect do
@@ -292,10 +360,37 @@ module Waiters
292360
expect(retries).to be(1)
293361
end
294362

363+
it 'can match array of objects' do
364+
client.stub_responses(
365+
:waiter_operation,
366+
object_list: [{ string_member: 'expected' }, { string_member: 'expected' }]
367+
)
368+
expect { client.wait_until(:path_all_matcher_object) }
369+
.to_not raise_error
370+
end
371+
372+
it 'can match array of numbers' do
373+
client.stub_responses(:waiter_operation, number_list: [123, 123, 123])
374+
expect { client.wait_until(:path_all_matcher_number) }
375+
.to_not raise_error
376+
end
377+
378+
it 'can match array of arrays' do
379+
client.stub_responses(:waiter_operation, nested_list: [[1, 2, 3], [1, 2, 3], [1, 2, 3]])
380+
expect { client.wait_until(:path_all_matcher_array) }
381+
.to_not raise_error
382+
end
383+
384+
it 'can match array of booleans' do
385+
client.stub_responses(:waiter_operation, boolean_list: [false, false, false])
386+
expect { client.wait_until(:path_all_matcher_boolean) }
387+
.to_not raise_error
388+
end
389+
295390
it 'fails when matched' do
296391
client.stub_responses(
297392
:waiter_operation,
298-
table_list: [{ table_status: 'FAILED' }]
393+
object_list: [{ string_member: 'unexpected' }]
299394
)
300395
expect { client.wait_until(:path_all_matcher) }
301396
.to raise_error(Errors::FailureStateError)
@@ -304,7 +399,7 @@ module Waiters
304399
it 'fails when none are matched' do
305400
client.stub_responses(
306401
:waiter_operation,
307-
table_list: [{ table_status: 'UPDATING' }, { table_status: 'ACTIVE' }]
402+
object_list: [{ string_member: 'retry' }, { string_member: 'expected' }]
308403
)
309404
expect { client.wait_until(:path_all_matcher) }
310405
.to raise_error(Errors::TooManyAttemptsError)
@@ -313,7 +408,27 @@ module Waiters
313408
it 'fails when none of the path matches' do
314409
client.stub_responses(
315410
:waiter_operation,
316-
table_list: [{ table_status: 'UPDATING' }, { table_status: 'ACTIVE' }]
411+
object_list: [{ string_member: 'retry' }, { string_member: 'expected' }]
412+
)
413+
expect do
414+
client.wait_until(:path_all_matcher) { |w| w.max_attempts = 1 }
415+
end.to raise_error(Errors::TooManyAttemptsError)
416+
end
417+
418+
it 'fails when array is empty' do
419+
client.stub_responses(
420+
:waiter_operation,
421+
object_list: []
422+
)
423+
expect do
424+
client.wait_until(:path_all_matcher) { |w| w.max_attempts = 1 }
425+
end.to raise_error(Errors::TooManyAttemptsError)
426+
end
427+
428+
it 'fails when array is nil' do
429+
client.stub_responses(
430+
:waiter_operation,
431+
object_list: nil
317432
)
318433
expect do
319434
client.wait_until(:path_all_matcher) { |w| w.max_attempts = 1 }
@@ -325,8 +440,8 @@ module Waiters
325440
it 'retries and succeed when matched' do
326441
client.stub_responses(
327442
:waiter_operation,
328-
{ table_list: [{ table_status: 'UPDATING' }, { table_status: 'CREATING' }] },
329-
{ table_list: [{ table_status: 'ACTIVE' }, { table_status: 'CREATING' }] }
443+
{ object_list: [{ string_member: 'retry' }, { string_member: 'other' }] },
444+
{ object_list: [{ string_member: 'expected' }, { string_member: 'retry' }] }
330445
)
331446
retries = 0
332447
expect do
@@ -335,10 +450,37 @@ module Waiters
335450
expect(retries).to be(1)
336451
end
337452

453+
it 'can match array of objects' do
454+
client.stub_responses(
455+
:waiter_operation,
456+
object_list: [{ string_member: 'unexpected' }, { string_member: 'expected' }]
457+
)
458+
expect { client.wait_until(:path_any_matcher_object) }
459+
.to_not raise_error
460+
end
461+
462+
it 'can match array of numbers' do
463+
client.stub_responses(:waiter_operation, number_list: [456, 789, 123])
464+
expect { client.wait_until(:path_any_matcher_number) }
465+
.to_not raise_error
466+
end
467+
468+
it 'can match array of arrays' do
469+
client.stub_responses(:waiter_operation, nested_list: [[4, 5, 6], [1, 2, 3], [7, 8, 9]])
470+
expect { client.wait_until(:path_any_matcher_array) }
471+
.to_not raise_error
472+
end
473+
474+
it 'can match array of booleans' do
475+
client.stub_responses(:waiter_operation, boolean_list: [true, false, true])
476+
expect { client.wait_until(:path_any_matcher_boolean) }
477+
.to_not raise_error
478+
end
479+
338480
it 'fails when matched' do
339481
client.stub_responses(
340482
:waiter_operation,
341-
{ table_list: [{ table_status: 'FAILED' }, { table_status: 'CREATING' }] }
483+
{ object_list: [{ string_member: 'unexpected' }, { string_member: 'retry' }] }
342484
)
343485
expect { client.wait_until(:path_any_matcher) }
344486
.to raise_error(Errors::FailureStateError)
@@ -347,11 +489,31 @@ module Waiters
347489
it 'fails when none of the path matches' do
348490
client.stub_responses(
349491
:waiter_operation,
350-
table_list: [{ table_status: 'CREATING' }, { table_status: 'FOO' }]
492+
object_list: [{ string_member: 'other' }, { string_member: 'other' }]
351493
)
352494
expect { client.wait_until(:path_any_matcher) }
353495
.to raise_error(Errors::TooManyAttemptsError)
354496
end
497+
498+
it 'fails when array is empty' do
499+
client.stub_responses(
500+
:waiter_operation,
501+
object_list: []
502+
)
503+
expect do
504+
client.wait_until(:path_any_matcher) { |w| w.max_attempts = 1 }
505+
end.to raise_error(Errors::TooManyAttemptsError)
506+
end
507+
508+
it 'fails when array is nil' do
509+
client.stub_responses(
510+
:waiter_operation,
511+
object_list: nil
512+
)
513+
expect do
514+
client.wait_until(:path_any_matcher) { |w| w.max_attempts = 1 }
515+
end.to raise_error(Errors::TooManyAttemptsError)
516+
end
355517
end
356518
end
357519
end

0 commit comments

Comments
 (0)