Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cfnlint/rules/mappings/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self):
)
self.rule_set = {
"maxProperties": "E7010",
"propertyNames": "E7002",
"maxLength": "E7002",
}
self.child_rules = dict.fromkeys(list(self.rule_set.values()))
Expand Down
1 change: 1 addition & 0 deletions src/cfnlint/rules/parameters/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self):
all_matches=True,
)
self.rule_set = {
"propertyNames": "E2011",
"maxLength": "E2011",
"maxProperties": "E2010",
"minProperties": "E2010",
Expand Down
89 changes: 89 additions & 0 deletions test/unit/rules/mappings/test_max_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

import json

import pytest

from cfnlint.api import ManualArgs, lint

# A full lint, because I7002 is only reached when E7001 wires in its child rules.

_RULE_IDS = ("E7002", "I7002")


def _template(mappings):
template = {"Resources": {"Topic": {"Type": "AWS::SNS::Topic"}}}
if mappings is not None:
template["Mappings"] = mappings
return template


def _approaching(name):
return ("I7002", f"{name!r} is approaching the max length of 255")


def _longer(name):
return ("E7002", f"{name!r} is longer than 255")


@pytest.mark.parametrize(
"name,mappings,expected",
[
("No Mappings section", None, []),
("Short names", {"MyMapping": {"us-east-1": {"Name": "one"}}}, []),
(
"Mapping name just under the threshold",
{"M" * 229: {"us-east-1": {"Name": "one"}}},
[],
),
(
"Mapping name just over the threshold",
{"M" * 230: {"us-east-1": {"Name": "one"}}},
[_approaching("M" * 230)],
),
(
"Mapping name at the limit",
{"M" * 255: {"us-east-1": {"Name": "one"}}},
[_approaching("M" * 255)],
),
(
"Mapping name over the limit",
{"M" * 256: {"us-east-1": {"Name": "one"}}},
[_longer("M" * 256)],
),
(
"Attribute name just under the threshold",
{"MyMapping": {"us-east-1": {"A" * 229: "one"}}},
[],
),
(
"Attribute name just over the threshold",
{"MyMapping": {"us-east-1": {"A" * 230: "one"}}},
[_approaching("A" * 230)],
),
(
"Attribute name at the limit",
{"MyMapping": {"us-east-1": {"A" * 255: "one"}}},
[_approaching("A" * 255)],
),
(
"Attribute name over the limit",
{"MyMapping": {"us-east-1": {"A" * 256: "one"}}},
[_longer("A" * 256)],
),
],
)
def test_mapping_name_length(name, mappings, expected):
matches = lint(
json.dumps(_template(mappings)),
config=ManualArgs(regions=["us-east-1"], include_checks=["I"]),
)

assert [
(match.rule.id, match.message)
for match in matches
if match.rule.id in _RULE_IDS
] == expected, f"Test {name!r} failed with {matches!r}"
78 changes: 78 additions & 0 deletions test/unit/rules/parameters/test_property_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

import json

import pytest

from cfnlint.api import ManualArgs, lint

# A full lint, because I2011 is only reached when E2001 wires in its child rules.

_RULE_IDS = ("E2001", "E2011", "I2011")


def _template(parameters):
template = {"Resources": {"Topic": {"Type": "AWS::SNS::Topic"}}}
if parameters is not None:
template["Parameters"] = parameters
return template


def _approaching(name):
return ("I2011", f"{name!r} is approaching the max length of 255")


def _longer(name):
return ("E2011", f"{name!r} is longer than 255")


# E2001 is only checked for attribution; its message lists every parameter attribute.
_E2001 = ("E2001", None)


def _key(match):
if match.rule.id == "E2001":
return _E2001
return (match.rule.id, match.message)


@pytest.mark.parametrize(
"name,parameters,expected",
[
("No Parameters section", None, []),
("Short name", {"MyParameter": {"Type": "String"}}, []),
("Just under the threshold", {"P" * 229: {"Type": "String"}}, []),
(
"Just over the threshold",
{"P" * 230: {"Type": "String"}},
[_approaching("P" * 230)],
),
(
"At the limit",
{"P" * 255: {"Type": "String"}},
[_approaching("P" * 255)],
),
(
"Over the limit",
{"P" * 256: {"Type": "String"}},
[_longer("P" * 256)],
),
(
"Unknown attribute on a long name is still reported by E2001",
{"P" * 240: {"Type": "String", "Foo": "bar"}},
[_E2001, _E2001, _approaching("P" * 240)],
),
],
)
def test_parameter_name_length(name, parameters, expected):
matches = lint(
json.dumps(_template(parameters)),
config=ManualArgs(regions=["us-east-1"], include_checks=["I"]),
)

assert [
_key(match) for match in matches if match.rule.id in _RULE_IDS
] == expected, f"Test {name!r} failed with {matches!r}"
Loading