|
3 | 3 | SPDX-License-Identifier: MIT-0
|
4 | 4 | """
|
5 | 5 |
|
| 6 | +from collections import deque |
| 7 | + |
6 | 8 | import pytest
|
7 | 9 |
|
8 |
| -from cfnlint.jsonschema import CfnTemplateValidator |
| 10 | +from cfnlint.jsonschema import CfnTemplateValidator, ValidationError |
| 11 | +from cfnlint.rules.functions.Cidr import Cidr |
| 12 | +from cfnlint.rules.functions.Join import Join |
| 13 | +from cfnlint.rules.functions.Ref import Ref |
9 | 14 | from cfnlint.rules.outputs.Value import Value # pylint: disable=E0401
|
10 | 15 |
|
11 | 16 |
|
| 17 | +@pytest.fixture |
| 18 | +def template(): |
| 19 | + return { |
| 20 | + "Parameters": { |
| 21 | + "additionalVpcCidr": { |
| 22 | + "Type": "String", |
| 23 | + "Default": "", |
| 24 | + }, |
| 25 | + "badAdditionalVpcCidr": { |
| 26 | + "Type": "String", |
| 27 | + "Default": "1", |
| 28 | + }, |
| 29 | + }, |
| 30 | + "Conditions": { |
| 31 | + "isAdditionalVpc": { |
| 32 | + "Fn::Not": [ |
| 33 | + { |
| 34 | + "Fn::Equals": [ |
| 35 | + {"Ref": "additionalVpcCidr"}, |
| 36 | + "", |
| 37 | + ] |
| 38 | + } |
| 39 | + ] |
| 40 | + }, |
| 41 | + "isBadAdditionalVpc": { |
| 42 | + "Fn::Not": [ |
| 43 | + { |
| 44 | + "Fn::Equals": [ |
| 45 | + {"Ref": "badAdditionalVpcCidr"}, |
| 46 | + "", |
| 47 | + ] |
| 48 | + } |
| 49 | + ] |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def validator(cfn): |
| 57 | + yield CfnTemplateValidator(schema={}).extend( |
| 58 | + validators={ |
| 59 | + "fn_join": Join().fn_join, |
| 60 | + "ref": Ref().ref, |
| 61 | + "fn_cidr": Cidr().fn_cidr, |
| 62 | + } |
| 63 | + )( |
| 64 | + schema={}, |
| 65 | + cfn=cfn, |
| 66 | + ) |
| 67 | + |
| 68 | + |
12 | 69 | @pytest.mark.parametrize(
|
13 | 70 | "input,expected",
|
14 | 71 | [
|
15 |
| - ("foo", 0), |
16 |
| - (1.0, 1), |
17 |
| - (1, 1), |
18 |
| - (True, 1), |
19 |
| - ([{}], 1), |
20 |
| - ({"foo": "bar"}, 1), |
| 72 | + ( |
| 73 | + { |
| 74 | + "Value": "foo", |
| 75 | + }, |
| 76 | + [], |
| 77 | + ), |
| 78 | + ( |
| 79 | + { |
| 80 | + "Value": 1.0, |
| 81 | + }, |
| 82 | + [ |
| 83 | + ValidationError( |
| 84 | + "1.0 is not of type 'array', 'string'", |
| 85 | + validator="type", |
| 86 | + schema_path=deque(["type"]), |
| 87 | + path=deque(["Value"]), |
| 88 | + rule=Value(), |
| 89 | + ) |
| 90 | + ], |
| 91 | + ), |
| 92 | + ( |
| 93 | + { |
| 94 | + "Value": 1, |
| 95 | + }, |
| 96 | + [ |
| 97 | + ValidationError( |
| 98 | + "1 is not of type 'array', 'string'", |
| 99 | + validator="type", |
| 100 | + schema_path=deque(["type"]), |
| 101 | + path=deque(["Value"]), |
| 102 | + rule=Value(), |
| 103 | + ) |
| 104 | + ], |
| 105 | + ), |
| 106 | + ( |
| 107 | + {"Value": True}, |
| 108 | + [ |
| 109 | + ValidationError( |
| 110 | + "True is not of type 'array', 'string'", |
| 111 | + validator="type", |
| 112 | + schema_path=deque(["type"]), |
| 113 | + path=deque(["Value"]), |
| 114 | + rule=Value(), |
| 115 | + ) |
| 116 | + ], |
| 117 | + ), |
| 118 | + ( |
| 119 | + {"Value": [{}]}, |
| 120 | + [ |
| 121 | + ValidationError( |
| 122 | + "{} is not of type 'string'", |
| 123 | + validator="type", |
| 124 | + schema_path=deque(["items", "type"]), |
| 125 | + path=deque(["Value", 0]), |
| 126 | + rule=Value(), |
| 127 | + ) |
| 128 | + ], |
| 129 | + ), |
| 130 | + ( |
| 131 | + {"Value": {"foo": "bar"}}, |
| 132 | + [ |
| 133 | + ValidationError( |
| 134 | + "{'foo': 'bar'} is not of type 'array', 'string'", |
| 135 | + validator="type", |
| 136 | + schema_path=deque(["type"]), |
| 137 | + path=deque(["Value"]), |
| 138 | + rule=Value(), |
| 139 | + ) |
| 140 | + ], |
| 141 | + ), |
| 142 | + ( |
| 143 | + { |
| 144 | + "Condition": "isAdditionalVpc", |
| 145 | + "Value": { |
| 146 | + "Fn::Join": [ |
| 147 | + ",", |
| 148 | + {"Fn::Cidr": [{"Ref": "additionalVpcCidr"}, 3, 8]}, |
| 149 | + ] |
| 150 | + }, |
| 151 | + }, |
| 152 | + [], |
| 153 | + ), |
| 154 | + ( |
| 155 | + { |
| 156 | + "Condition": "isBadAdditionalVpc", |
| 157 | + "Value": { |
| 158 | + "Fn::Join": [ |
| 159 | + ",", |
| 160 | + {"Fn::Cidr": [{"Ref": "badAdditionalVpcCidr"}, 3, 8]}, |
| 161 | + ] |
| 162 | + }, |
| 163 | + }, |
| 164 | + [ |
| 165 | + ValidationError( |
| 166 | + ( |
| 167 | + "{'Ref': 'badAdditionalVpcCidr'} does not match " |
| 168 | + "'^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\\\/([0-9]|[1-2][0-9]|3[0-2]))$'" |
| 169 | + " when 'Ref' is resolved" |
| 170 | + ), |
| 171 | + validator="ref", |
| 172 | + schema_path=deque( |
| 173 | + ["fn_join", "fn_items", "fn_cidr", "fn_items", "ref", "pattern"] |
| 174 | + ), |
| 175 | + path=deque(["Value", "Fn::Join", 1, "Fn::Cidr", 0, "Ref"]), |
| 176 | + ) |
| 177 | + ], |
| 178 | + ), |
21 | 179 | ],
|
22 | 180 | )
|
23 |
| -def test_output_value(input, expected): |
| 181 | +def test_output_value(input, expected, validator): |
24 | 182 | rule = Value()
|
25 |
| - validator = CfnTemplateValidator() |
26 |
| - |
27 | 183 | results = list(rule.validate(validator, {}, input, {}))
|
28 | 184 |
|
29 |
| - assert len(results) == expected, f"Expected {expected} results, got {results}" |
| 185 | + assert results == expected, f"Expected {expected} results, got {results}" |
0 commit comments