|
| 1 | +""" |
| 2 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MIT-0 |
| 4 | +""" |
| 5 | + |
| 6 | +from collections import deque |
| 7 | + |
| 8 | +import jsonpatch |
| 9 | +import pytest |
| 10 | + |
| 11 | +from cfnlint.jsonschema import ValidationError |
| 12 | +from cfnlint.rules.resources.lmbd.EventSourceMappingToSqsTimeout import ( |
| 13 | + EventSourceMappingToSqsTimeout, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="module") |
| 18 | +def rule(): |
| 19 | + rule = EventSourceMappingToSqsTimeout() |
| 20 | + yield rule |
| 21 | + |
| 22 | + |
| 23 | +_template = { |
| 24 | + "Parameters": { |
| 25 | + "BatchSize": {"Type": "String"}, |
| 26 | + }, |
| 27 | + "Resources": { |
| 28 | + "MyFifoQueue": { |
| 29 | + "Type": "AWS::SQS::Queue", |
| 30 | + "Properties": { |
| 31 | + "VisibilityTimeout": 300, |
| 32 | + "MessageRetentionPeriod": 7200, |
| 33 | + }, |
| 34 | + }, |
| 35 | + "SQSBatch": { |
| 36 | + "Type": "AWS::Lambda::EventSourceMapping", |
| 37 | + "Properties": { |
| 38 | + "BatchSize": {"Ref": "BatchSize"}, |
| 39 | + "Enabled": True, |
| 40 | + "EventSourceArn": {"Fn::GetAtt": "MyFifoQueue.Arn"}, |
| 41 | + "FunctionName": {"Ref": "Lambda"}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + "Lambda": { |
| 45 | + "Type": "AWS::Lambda::Function", |
| 46 | + "Properties": {"Role": {"Fn::GetAtt": "Role.Arn"}}, |
| 47 | + }, |
| 48 | + "CustomResource": { |
| 49 | + "Type": "AWS::CloudFormation::CustomResource", |
| 50 | + "Properties": {"Key": {"Fn::GetAtt": "Lambda.Arn"}}, |
| 51 | + }, |
| 52 | + }, |
| 53 | + "Outputs": { |
| 54 | + "LambdaArn": {"Value": {"Fn::GetAtt": "Lambda.Arn"}}, |
| 55 | + "SourceMapping": {"Value": {"Ref": "SQSBatch"}}, |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "instance,template,path,expected", |
| 62 | + [ |
| 63 | + ( |
| 64 | + "100", |
| 65 | + _template, |
| 66 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 67 | + [], |
| 68 | + ), |
| 69 | + ( |
| 70 | + "a", |
| 71 | + _template, |
| 72 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 73 | + [], |
| 74 | + ), |
| 75 | + ( |
| 76 | + {"Ref": "AWS::Region"}, |
| 77 | + _template, |
| 78 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 79 | + [], |
| 80 | + ), |
| 81 | + ( |
| 82 | + "100", |
| 83 | + jsonpatch.apply_patch( |
| 84 | + _template, |
| 85 | + [ |
| 86 | + { |
| 87 | + "op": "add", |
| 88 | + "path": ( |
| 89 | + "/Resources/MyFifoQueue/" "Properties/VisibilityTimeout" |
| 90 | + ), |
| 91 | + "value": "300", |
| 92 | + }, |
| 93 | + ], |
| 94 | + ), |
| 95 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 96 | + [], |
| 97 | + ), |
| 98 | + ( |
| 99 | + "600", |
| 100 | + jsonpatch.apply_patch( |
| 101 | + _template, |
| 102 | + [ |
| 103 | + { |
| 104 | + "op": "add", |
| 105 | + "path": ( |
| 106 | + "/Resources/MyFifoQueue/" "Properties/VisibilityTimeout" |
| 107 | + ), |
| 108 | + "value": {"Ref": "AWS::Region"}, |
| 109 | + }, |
| 110 | + ], |
| 111 | + ), |
| 112 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 113 | + [], |
| 114 | + ), |
| 115 | + ( |
| 116 | + "600", |
| 117 | + jsonpatch.apply_patch( |
| 118 | + _template, |
| 119 | + [ |
| 120 | + { |
| 121 | + "op": "add", |
| 122 | + "path": ( |
| 123 | + "/Resources/MyFifoQueue/" "Properties/VisibilityTimeout" |
| 124 | + ), |
| 125 | + "value": "a", |
| 126 | + }, |
| 127 | + ], |
| 128 | + ), |
| 129 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 130 | + [], |
| 131 | + ), |
| 132 | + ( |
| 133 | + "600", |
| 134 | + _template, |
| 135 | + {"path": deque(["Resources"])}, |
| 136 | + [], |
| 137 | + ), |
| 138 | + ( |
| 139 | + "600", |
| 140 | + _template, |
| 141 | + {"path": deque(["Resources", "Lambda", "Properties", "Timeout"])}, |
| 142 | + [ |
| 143 | + ValidationError( |
| 144 | + ( |
| 145 | + "Queue visibility timeout (300) is less " |
| 146 | + "than Function timeout (600) seconds" |
| 147 | + ), |
| 148 | + rule=EventSourceMappingToSqsTimeout(), |
| 149 | + ) |
| 150 | + ], |
| 151 | + ), |
| 152 | + ], |
| 153 | + indirect=["template", "path"], |
| 154 | +) |
| 155 | +def test_lambda_runtime(instance, template, path, expected, rule, validator): |
| 156 | + errs = list(rule.validate(validator, "", instance, {})) |
| 157 | + assert errs == expected, f"Expected {expected} got {errs}" |
0 commit comments