| 1 |
# |
| 2 |
# (c) 2024 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from contextlib import nullcontext as does_not_raise |
| 8 |
from copy import deepcopy |
| 9 |
from unittest.mock import MagicMock |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from ansible_collections.amazon.aws.plugins.modules.lambda_event import get_qualifier |
| 15 |
from ansible_collections.amazon.aws.plugins.modules.lambda_event import lambda_event_stream |
| 16 |
from ansible_collections.amazon.aws.plugins.modules.lambda_event import set_default_values |
| 17 |
from ansible_collections.amazon.aws.plugins.modules.lambda_event import validate_params |
| 18 |
|
| 19 |
mock_get_qualifier = "ansible_collections.amazon.aws.plugins.modules.lambda_event.get_qualifier" |
| 20 |
mock_camel_dict_to_snake_dict = "ansible_collections.amazon.aws.plugins.modules.lambda_event.camel_dict_to_snake_dict" |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture(name="ansible_aws_module") |
| 24 |
def fixture_ansible_aws_module(): |
| 25 |
module = MagicMock() |
| 26 |
module.check_mode = False |
| 27 |
module.params = { |
| 28 |
"state": "present", |
| 29 |
"lambda_function_arn": None, |
| 30 |
"event_source": "sqs", |
| 31 |
"source_params": { |
| 32 |
"source_arn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 33 |
"batch_size": 200, |
| 34 |
"starting_position": "LATEST", |
| 35 |
}, |
| 36 |
"alias": None, |
| 37 |
"version": 0, |
| 38 |
} |
| 39 |
module.exit_json = MagicMock() |
| 40 |
module.exit_json.side_effect = SystemExit(1) |
| 41 |
module.fail_json_aws = MagicMock() |
| 42 |
module.fail_json_aws.side_effect = SystemExit(2) |
| 43 |
module.fail_json = MagicMock() |
| 44 |
module.fail_json.side_effect = SystemExit(2) |
| 45 |
module.client = MagicMock() |
| 46 |
module.client.return_value = MagicMock() |
| 47 |
module.boolean = MagicMock() |
| 48 |
module.boolean.side_effect = lambda x: x.lower() in ["true", "1", "t", "y", "yes"] |
| 49 |
return module |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
"module_params,expected", |
| 54 |
[ |
| 55 |
({"version": 1}, "1"), |
| 56 |
({"alias": "ansible-test"}, "ansible-test"), |
| 57 |
({"version": 1, "alias": "ansible-test"}, "1"), |
| 58 |
({}, None), |
| 59 |
], |
| 60 |
) |
| 61 |
def test_get_qualifier(ansible_aws_module, module_params, expected): |
| 62 |
ansible_aws_module.params.update(module_params) |
| 63 |
assert get_qualifier(ansible_aws_module) == expected |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize( |
| 67 |
"function_name,error_msg", |
| 68 |
[ |
| 69 |
( |
| 70 |
"invalid+function+name", |
| 71 |
"Function name invalid+function+name is invalid. Names must contain only alphanumeric characters and hyphens.", |
| 72 |
), |
| 73 |
( |
| 74 |
"this_invalid_function_name_has_more_than_64_character_limit_this_is_not_allowed_by_the_module", |
| 75 |
'Function name "this_invalid_function_name_has_more_than_64_character_limit_this_is_not_allowed_by_the_module" exceeds 64 character limit', |
| 76 |
), |
| 77 |
( |
| 78 |
"arn:aws:lambda:us-east-2:123456789012:function:ansible-test-ansible-test-ansible-test-sqs-lambda-function:" |
| 79 |
"ansible-test-ansible-test-ansible-test-sqs-lambda-function-alias", |
| 80 |
'ARN "arn:aws:lambda:us-east-2:123456789012:function:ansible-test-ansible-test-ansible-test-sqs-lambda-function:' |
| 81 |
'ansible-test-ansible-test-ansible-test-sqs-lambda-function-alias" exceeds 140 character limit', |
| 82 |
), |
| 83 |
], |
| 84 |
) |
| 85 |
def test_validate_params_function_name_errors(ansible_aws_module, function_name, error_msg): |
| 86 |
ansible_aws_module.params.update({"lambda_function_arn": function_name}) |
| 87 |
client = MagicMock() |
| 88 |
client.get_function = MagicMock() |
| 89 |
client.get_function.return_value = {} |
| 90 |
with pytest.raises(SystemExit): |
| 91 |
validate_params(ansible_aws_module, client) |
| 92 |
|
| 93 |
ansible_aws_module.fail_json.assert_called_once_with(msg=error_msg) |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.parametrize( |
| 97 |
"qualifier", |
| 98 |
[None, "ansible-test"], |
| 99 |
) |
| 100 |
@patch(mock_get_qualifier) |
| 101 |
def test_validate_params_with_function_arn(m_get_qualifier, ansible_aws_module, qualifier): |
| 102 |
function_name = "arn:aws:lambda:us-east-2:123456789012:function:sqs_consumer" |
| 103 |
ansible_aws_module.params.update({"lambda_function_arn": function_name}) |
| 104 |
m_get_qualifier.return_value = qualifier |
| 105 |
|
| 106 |
client = MagicMock() |
| 107 |
client.get_function = MagicMock() |
| 108 |
client.get_function.return_value = {} |
| 109 |
|
| 110 |
params = deepcopy(ansible_aws_module.params) |
| 111 |
params["lambda_function_arn"] = f"{function_name}:{qualifier}" if qualifier else function_name |
| 112 |
|
| 113 |
validate_params(ansible_aws_module, client) |
| 114 |
assert params == ansible_aws_module.params |
| 115 |
m_get_qualifier.assert_called_once() |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.parametrize( |
| 119 |
"qualifier", |
| 120 |
[None, "ansible-test"], |
| 121 |
) |
| 122 |
@patch(mock_get_qualifier) |
| 123 |
def test_validate_params_with_function_name(m_get_qualifier, ansible_aws_module, qualifier): |
| 124 |
function_arn = "arn:aws:lambda:us-east-2:123456789012:function:sqs_consumer" |
| 125 |
function_name = "sqs_consumer" |
| 126 |
ansible_aws_module.params.update({"lambda_function_arn": function_name}) |
| 127 |
m_get_qualifier.return_value = qualifier |
| 128 |
|
| 129 |
client = MagicMock() |
| 130 |
client.get_function = MagicMock() |
| 131 |
client.get_function.return_value = { |
| 132 |
"Configuration": {"FunctionArn": function_arn}, |
| 133 |
} |
| 134 |
|
| 135 |
params = deepcopy(ansible_aws_module.params) |
| 136 |
params["lambda_function_arn"] = function_arn |
| 137 |
|
| 138 |
validate_params(ansible_aws_module, client) |
| 139 |
|
| 140 |
assert params == ansible_aws_module.params |
| 141 |
m_get_qualifier.assert_called_once() |
| 142 |
api_params = {"FunctionName": function_name} |
| 143 |
if qualifier: |
| 144 |
api_params.update({"Qualifier": qualifier}) |
| 145 |
client.get_function.assert_called_once_with(**api_params) |
| 146 |
|
| 147 |
|
| 148 |
EventSourceMappings = [ |
| 149 |
{ |
| 150 |
"BatchSize": 10, |
| 151 |
"EventSourceArn": "arn:aws:sqs:us-east-2:123456789012:sqs_consumer", |
| 152 |
"FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:sqs_consumer", |
| 153 |
"LastModified": "2024-02-08T15:24:57.014000+01:00", |
| 154 |
"MaximumBatchingWindowInSeconds": 0, |
| 155 |
"State": "Enabled", |
| 156 |
"StateTransitionReason": "USER_INITIATED", |
| 157 |
"UUID": "3ab96d4c-b0c4-4885-87d0-f58cb9c0a4cc", |
| 158 |
} |
| 159 |
] |
| 160 |
|
| 161 |
|
| 162 |
@pytest.mark.parametrize( |
| 163 |
"check_mode", |
| 164 |
[True, False], |
| 165 |
) |
| 166 |
@pytest.mark.parametrize( |
| 167 |
"existing_event_source", |
| 168 |
[True, False], |
| 169 |
) |
| 170 |
@patch(mock_camel_dict_to_snake_dict) |
| 171 |
def test_lambda_event_stream_with_state_absent( |
| 172 |
m_camel_dict_to_snake_dict, ansible_aws_module, check_mode, existing_event_source |
| 173 |
): |
| 174 |
function_name = "sqs_consumer" |
| 175 |
ansible_aws_module.params.update({"lambda_function_arn": function_name, "state": "absent"}) |
| 176 |
ansible_aws_module.check_mode = check_mode |
| 177 |
|
| 178 |
client = MagicMock() |
| 179 |
client.list_event_source_mappings = MagicMock() |
| 180 |
|
| 181 |
client.list_event_source_mappings.return_value = { |
| 182 |
"EventSourceMappings": EventSourceMappings if existing_event_source else [] |
| 183 |
} |
| 184 |
client.delete_event_source_mapping = MagicMock() |
| 185 |
event_source_deleted = {"msg": "event source successfully deleted."} |
| 186 |
client.delete_event_source_mapping.return_value = event_source_deleted |
| 187 |
m_camel_dict_to_snake_dict.side_effect = lambda x: x |
| 188 |
|
| 189 |
events = [] |
| 190 |
changed = False |
| 191 |
result = lambda_event_stream(ansible_aws_module, client) |
| 192 |
changed = existing_event_source |
| 193 |
if existing_event_source: |
| 194 |
events = EventSourceMappings |
| 195 |
if not check_mode: |
| 196 |
events = event_source_deleted |
| 197 |
client.delete_event_source_mapping.assert_called_once_with(UUID=EventSourceMappings[0]["UUID"]) |
| 198 |
else: |
| 199 |
client.delete_event_source_mapping.assert_not_called() |
| 200 |
assert dict(changed=changed, events=events) == result |
| 201 |
|
| 202 |
|
| 203 |
def test_lambda_event_stream_create_event_missing_starting_position(ansible_aws_module): |
| 204 |
ansible_aws_module.params = { |
| 205 |
"state": "present", |
| 206 |
"lambda_function_arn": "sqs_consumer", |
| 207 |
"event_source": "stream", |
| 208 |
"source_params": { |
| 209 |
"source_arn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 210 |
"maximum_batching_window_in_seconds": 1, |
| 211 |
"batch_size": 200, |
| 212 |
}, |
| 213 |
"alias": None, |
| 214 |
"version": 0, |
| 215 |
} |
| 216 |
|
| 217 |
client = MagicMock() |
| 218 |
client.list_event_source_mappings = MagicMock() |
| 219 |
client.list_event_source_mappings.return_value = {"EventSourceMappings": []} |
| 220 |
|
| 221 |
error_message = "Source parameter 'starting_position' is required for stream event notification." |
| 222 |
with pytest.raises(SystemExit): |
| 223 |
lambda_event_stream(ansible_aws_module, client) |
| 224 |
ansible_aws_module.fail_json.assert_called_once_with(msg=error_message) |
| 225 |
|
| 226 |
|
| 227 |
@pytest.mark.parametrize( |
| 228 |
"check_mode", |
| 229 |
[True, False], |
| 230 |
) |
| 231 |
@pytest.mark.parametrize( |
| 232 |
"module_params,api_params", |
| 233 |
[ |
| 234 |
( |
| 235 |
{ |
| 236 |
"state": "present", |
| 237 |
"lambda_function_arn": "sqs_consumer", |
| 238 |
"event_source": "stream", |
| 239 |
"source_params": { |
| 240 |
"source_arn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 241 |
"maximum_batching_window_in_seconds": 1, |
| 242 |
"batch_size": 250, |
| 243 |
"starting_position": "END", |
| 244 |
"function_response_types": ["ReportBatchItemFailures"], |
| 245 |
}, |
| 246 |
"alias": None, |
| 247 |
"version": 0, |
| 248 |
}, |
| 249 |
{ |
| 250 |
"FunctionName": "sqs_consumer", |
| 251 |
"EventSourceArn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 252 |
"StartingPosition": "END", |
| 253 |
"Enabled": True, |
| 254 |
"MaximumBatchingWindowInSeconds": 1, |
| 255 |
"BatchSize": 250, |
| 256 |
"FunctionResponseTypes": ["ReportBatchItemFailures"], |
| 257 |
}, |
| 258 |
), |
| 259 |
( |
| 260 |
{ |
| 261 |
"state": "present", |
| 262 |
"lambda_function_arn": "sqs_consumer", |
| 263 |
"event_source": "stream", |
| 264 |
"source_params": { |
| 265 |
"source_arn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 266 |
"maximum_batching_window_in_seconds": 1, |
| 267 |
"batch_size": 250, |
| 268 |
"starting_position": "END", |
| 269 |
"function_response_types": ["ReportBatchItemFailures"], |
| 270 |
"enabled": "no", |
| 271 |
}, |
| 272 |
"alias": None, |
| 273 |
"version": 0, |
| 274 |
}, |
| 275 |
{ |
| 276 |
"FunctionName": "sqs_consumer", |
| 277 |
"EventSourceArn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 278 |
"StartingPosition": "END", |
| 279 |
"Enabled": False, |
| 280 |
"MaximumBatchingWindowInSeconds": 1, |
| 281 |
"BatchSize": 250, |
| 282 |
"FunctionResponseTypes": ["ReportBatchItemFailures"], |
| 283 |
}, |
| 284 |
), |
| 285 |
( |
| 286 |
{ |
| 287 |
"state": "present", |
| 288 |
"lambda_function_arn": "sqs_consumer", |
| 289 |
"event_source": "sqs", |
| 290 |
"source_params": { |
| 291 |
"source_arn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 292 |
"maximum_batching_window_in_seconds": 1, |
| 293 |
"batch_size": 101, |
| 294 |
}, |
| 295 |
"alias": None, |
| 296 |
"version": 0, |
| 297 |
}, |
| 298 |
{ |
| 299 |
"FunctionName": "sqs_consumer", |
| 300 |
"EventSourceArn": "arn:aws:sqs:us-east-2:123456789012:ansible-test-sqs", |
| 301 |
"Enabled": True, |
| 302 |
"MaximumBatchingWindowInSeconds": 1, |
| 303 |
"BatchSize": 101, |
| 304 |
}, |
| 305 |
), |
| 306 |
], |
| 307 |
) |
| 308 |
@patch(mock_camel_dict_to_snake_dict) |
| 309 |
def test_lambda_event_stream_create_event( |
| 310 |
m_camel_dict_to_snake_dict, ansible_aws_module, check_mode, module_params, api_params |
| 311 |
): |
| 312 |
ansible_aws_module.params = module_params |
| 313 |
ansible_aws_module.check_mode = check_mode |
| 314 |
|
| 315 |
client = MagicMock() |
| 316 |
client.list_event_source_mappings = MagicMock() |
| 317 |
client.list_event_source_mappings.return_value = {"EventSourceMappings": []} |
| 318 |
|
| 319 |
client.create_event_source_mapping = MagicMock() |
| 320 |
event_source_created = {"msg": "event source successfully created."} |
| 321 |
client.create_event_source_mapping.return_value = event_source_created |
| 322 |
m_camel_dict_to_snake_dict.side_effect = lambda x: x |
| 323 |
|
| 324 |
result = lambda_event_stream(ansible_aws_module, client) |
| 325 |
|
| 326 |
events = [] |
| 327 |
|
| 328 |
if not check_mode: |
| 329 |
events = event_source_created |
| 330 |
client.create_event_source_mapping.assert_called_once_with(**api_params) |
| 331 |
else: |
| 332 |
client.create_event_source_mapping.assert_not_called() |
| 333 |
|
| 334 |
assert dict(changed=True, events=events) == result |
| 335 |
|
| 336 |
|
| 337 |
@pytest.mark.parametrize( |
| 338 |
"check_mode", |
| 339 |
[True, False], |
| 340 |
) |
| 341 |
@pytest.mark.parametrize( |
| 342 |
"module_source_params,current_mapping,api_params", |
| 343 |
[ |
| 344 |
( |
| 345 |
{"batch_size": 100, "enabled": "false"}, |
| 346 |
{"BatchSize": 120, "State": "Enabled"}, |
| 347 |
{"BatchSize": 100, "Enabled": False}, |
| 348 |
), |
| 349 |
( |
| 350 |
{"batch_size": 100, "enabled": "true"}, |
| 351 |
{"BatchSize": 100, "State": "Enabled"}, |
| 352 |
{}, |
| 353 |
), |
| 354 |
], |
| 355 |
) |
| 356 |
@patch(mock_camel_dict_to_snake_dict) |
| 357 |
def test_lambda_event_stream_update_event( |
| 358 |
m_camel_dict_to_snake_dict, ansible_aws_module, check_mode, module_source_params, current_mapping, api_params |
| 359 |
): |
| 360 |
function_name = "ansible-test-update-event-function" |
| 361 |
ansible_aws_module.params.update({"lambda_function_arn": function_name}) |
| 362 |
ansible_aws_module.params["source_params"].update(module_source_params) |
| 363 |
ansible_aws_module.check_mode = check_mode |
| 364 |
|
| 365 |
client = MagicMock() |
| 366 |
client.list_event_source_mappings = MagicMock() |
| 367 |
existing_event_source = deepcopy(EventSourceMappings) |
| 368 |
existing_event_source[0].update(current_mapping) |
| 369 |
client.list_event_source_mappings.return_value = {"EventSourceMappings": existing_event_source} |
| 370 |
|
| 371 |
client.update_event_source_mapping = MagicMock() |
| 372 |
event_source_updated = {"msg": "event source successfully updated."} |
| 373 |
client.update_event_source_mapping.return_value = event_source_updated |
| 374 |
m_camel_dict_to_snake_dict.side_effect = lambda x: x |
| 375 |
|
| 376 |
result = lambda_event_stream(ansible_aws_module, client) |
| 377 |
if not api_params: |
| 378 |
assert dict(changed=False, events=existing_event_source) == result |
| 379 |
client.update_event_source_mapping.assert_not_called() |
| 380 |
elif check_mode: |
| 381 |
assert dict(changed=True, events=existing_event_source) == result |
| 382 |
client.update_event_source_mapping.assert_not_called() |
| 383 |
else: |
| 384 |
api_params.update({"FunctionName": function_name, "UUID": existing_event_source[0]["UUID"]}) |
| 385 |
assert dict(changed=True, events=event_source_updated) == result |
| 386 |
client.update_event_source_mapping.assert_called_once_with(**api_params) |
| 387 |
|
| 388 |
|
| 389 |
@pytest.mark.parametrize( |
| 390 |
"params, expected, exception, message, source_type", |
| 391 |
[ |
| 392 |
( |
| 393 |
{ |
| 394 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052.fifo", |
| 395 |
"enabled": True, |
| 396 |
"batch_size": 100, |
| 397 |
"starting_position": None, |
| 398 |
"function_response_types": None, |
| 399 |
"maximum_batching_window_in_seconds": None, |
| 400 |
}, |
| 401 |
None, |
| 402 |
pytest.raises(SystemExit), |
| 403 |
"For FIFO queues the maximum batch_size is 10.", |
| 404 |
"sqs", |
| 405 |
), |
| 406 |
( |
| 407 |
{ |
| 408 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052.fifo", |
| 409 |
"enabled": True, |
| 410 |
"batch_size": 10, |
| 411 |
"starting_position": None, |
| 412 |
"function_response_types": None, |
| 413 |
"maximum_batching_window_in_seconds": 1, |
| 414 |
}, |
| 415 |
None, |
| 416 |
pytest.raises(SystemExit), |
| 417 |
"maximum_batching_window_in_seconds is not supported by Amazon SQS FIFO event sources.", |
| 418 |
"sqs", |
| 419 |
), |
| 420 |
( |
| 421 |
{ |
| 422 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052.fifo", |
| 423 |
"enabled": True, |
| 424 |
"batch_size": 10, |
| 425 |
"starting_position": None, |
| 426 |
"function_response_types": None, |
| 427 |
"maximum_batching_window_in_seconds": None, |
| 428 |
}, |
| 429 |
{ |
| 430 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052.fifo", |
| 431 |
"enabled": True, |
| 432 |
"batch_size": 10, |
| 433 |
"starting_position": None, |
| 434 |
"function_response_types": None, |
| 435 |
"maximum_batching_window_in_seconds": None, |
| 436 |
}, |
| 437 |
does_not_raise(), |
| 438 |
None, |
| 439 |
"sqs", |
| 440 |
), |
| 441 |
( |
| 442 |
{ |
| 443 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 444 |
"enabled": True, |
| 445 |
"batch_size": 11000, |
| 446 |
"starting_position": None, |
| 447 |
"function_response_types": None, |
| 448 |
"maximum_batching_window_in_seconds": None, |
| 449 |
}, |
| 450 |
None, |
| 451 |
pytest.raises(SystemExit), |
| 452 |
"For standard queue batch_size must be lower than 10000.", |
| 453 |
"sqs", |
| 454 |
), |
| 455 |
( |
| 456 |
{ |
| 457 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 458 |
"enabled": True, |
| 459 |
"batch_size": 100, |
| 460 |
"starting_position": None, |
| 461 |
"function_response_types": None, |
| 462 |
"maximum_batching_window_in_seconds": None, |
| 463 |
}, |
| 464 |
{ |
| 465 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 466 |
"enabled": True, |
| 467 |
"batch_size": 100, |
| 468 |
"starting_position": None, |
| 469 |
"function_response_types": None, |
| 470 |
"maximum_batching_window_in_seconds": 1, |
| 471 |
}, |
| 472 |
does_not_raise(), |
| 473 |
None, |
| 474 |
"sqs", |
| 475 |
), |
| 476 |
( |
| 477 |
{ |
| 478 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 479 |
"enabled": True, |
| 480 |
"starting_position": None, |
| 481 |
"function_response_types": None, |
| 482 |
"maximum_batching_window_in_seconds": None, |
| 483 |
}, |
| 484 |
{ |
| 485 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 486 |
"enabled": True, |
| 487 |
"batch_size": 100, |
| 488 |
"starting_position": None, |
| 489 |
"function_response_types": None, |
| 490 |
"maximum_batching_window_in_seconds": 1, |
| 491 |
}, |
| 492 |
does_not_raise(), |
| 493 |
None, |
| 494 |
"stream", |
| 495 |
), |
| 496 |
( |
| 497 |
{ |
| 498 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 499 |
"enabled": True, |
| 500 |
"starting_position": None, |
| 501 |
"function_response_types": None, |
| 502 |
}, |
| 503 |
{ |
| 504 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 505 |
"enabled": True, |
| 506 |
"batch_size": 10, |
| 507 |
"starting_position": None, |
| 508 |
"function_response_types": None, |
| 509 |
}, |
| 510 |
does_not_raise(), |
| 511 |
None, |
| 512 |
"sqs", |
| 513 |
), |
| 514 |
( |
| 515 |
{ |
| 516 |
"source_arn": "arn:aws:sqs:us-east-1:123456789012:ansible-test-28277052", |
| 517 |
"enabled": True, |
| 518 |
"batch_size": 10, |
| 519 |
"starting_position": None, |
| 520 |
"function_response_types": None, |
| 521 |
"maximum_batching_window_in_seconds": None, |
| 522 |
}, |
| 523 |
None, |
| 524 |
pytest.raises(SystemExit), |
| 525 |
"batch_size for streams must be between 100 and 10000", |
| 526 |
"stream", |
| 527 |
), |
| 528 |
], |
| 529 |
) |
| 530 |
def test__set_default_values(params, expected, exception, message, source_type): |
| 531 |
result = None |
| 532 |
module = MagicMock() |
| 533 |
module.check_mode = False |
| 534 |
module.params = { |
| 535 |
"event_source": source_type, |
| 536 |
"source_params": params, |
| 537 |
} |
| 538 |
module.fail_json = MagicMock() |
| 539 |
module.fail_json.side_effect = SystemExit(message) |
| 540 |
with exception as e: |
| 541 |
result = set_default_values(module, params) |
| 542 |
assert message is None or message in str(e) |
| 543 |
if expected is not None: |
| 544 |
assert result == expected |