Skip to content

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Aug 1, 2024
1 parent aa6309c commit 5ce49ce
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cfnlint/rules/resources/ecs/TaskDefinitionAwsVpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _get_port_mappings(
container_definition,
path=deque(["ContainerPort"]),
):
if not isinstance(host_port, (str, int)):
if not isinstance(container_port, (str, int)):
continue
if str(host_port) != str(container_port):
yield host_port, container_port, host_port_validator
Expand Down
116 changes: 116 additions & 0 deletions test/unit/rules/resources/ecs/test_service_fargate.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,122 @@ def rule():
deque(["Resources", "Service", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": jsonpatch.apply_patch(
dict(_task_definition),
[
{
"op": "remove",
"path": "/Properties/RequiresCompatibilities",
},
],
),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Fn::Sub": "${TaskDefinition.Arn}"},
},
],
),
}
},
deque(["Resources", "Service", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": jsonpatch.apply_patch(
dict(_task_definition),
[
{
"op": "remove",
"path": "/Properties/RequiresCompatibilities",
},
],
),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Fn::GetAtt": "TaskDefinition.Arn"},
},
],
),
}
},
deque(["Resources", "Service", "Properties"]),
[
ValidationError(
("'RequiresCompatibilities' is a required property"),
validator="required",
rule=ServiceFargate(),
path_override=deque(["Resources", "TaskDefinition", "Properties"]),
)
],
),
(
{
"Parameters": {"MyTask": {"Type": "String"}},
"Resources": {
"TaskDefinition": jsonpatch.apply_patch(
dict(_task_definition),
[
{
"op": "remove",
"path": "/Properties/RequiresCompatibilities",
},
],
),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Ref": "MyTask"},
},
],
),
},
},
deque(["Resources", "Service", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": jsonpatch.apply_patch(
dict(_task_definition),
[
{
"op": "remove",
"path": "/Properties/RequiresCompatibilities",
},
],
),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": "MyTask",
},
],
),
}
},
deque(["Resources", "Service", "Properties"]),
[],
),
(
{
"Resources": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,105 @@ def rule():
)
],
),
(
{
"Resources": {
"TaskDefinition": dict(_task_definition),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "remove",
"path": "/Properties/NetworkConfiguration",
},
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Fn::GetAtt": "TaskDefinition.Arn"},
},
],
),
}
},
deque(["Resources", "Service", "Properties"]),
[
ValidationError(
("'NetworkConfiguration' is a required property"),
validator="required",
rule=ServiceNetworkConfiguration(),
)
],
),
(
{
"Parameters": {"MyTargetGroup": {"Type": "String"}},
"Resources": {
"TaskDefinition": dict(_task_definition),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "remove",
"path": "/Properties/NetworkConfiguration",
},
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Ref": "MyTargetGroup"},
},
],
),
},
},
deque(["Resources", "Service", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": dict(_task_definition),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "remove",
"path": "/Properties/NetworkConfiguration",
},
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Foo": "Bar"},
},
],
),
},
},
deque(["Resources", "Service", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": dict(_task_definition),
"Service": jsonpatch.apply_patch(
dict(_service),
[
{
"op": "remove",
"path": "/Properties/NetworkConfiguration",
},
{
"op": "replace",
"path": "/Properties/TaskDefinition",
"value": {"Fn::Sub": "${TaskDefinition.Arn}"},
},
],
),
},
},
deque(["Resources", "Service", "Properties"]),
[],
),
],
indirect=["template"],
)
Expand Down
55 changes: 55 additions & 0 deletions test/unit/rules/resources/ecs/test_task_definition_aws_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ def rule():
deque(["Resources", "TaskDefinition", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": jsonpatch.apply_patch(
dict(_task_definition),
[
{
"op": "add",
"path": (
"/Properties/ContainerDefinitions/"
"0/PortMappings/0/HostPort"
),
"value": "8080",
},
{
"op": "replace",
"path": (
"/Properties/ContainerDefinitions/"
"0/PortMappings/0/ContainerPort"
),
"value": {"Fn::Sub": "8080"},
},
],
),
}
},
deque(["Resources", "TaskDefinition", "Properties"]),
[],
),
(
{
"Parameters": {
Expand Down Expand Up @@ -106,6 +135,32 @@ def rule():
deque(["Resources", "TaskDefinition", "Properties"]),
[],
),
(
{
"Resources": {
"TaskDefinition": jsonpatch.apply_patch(
dict(_task_definition),
[
{
"op": "add",
"path": (
"/Properties/ContainerDefinitions/0"
"/PortMappings/0/HostPort"
),
"value": "80",
},
{
"op": "replace",
"path": ("/Properties/NetworkMode"),
"value": "bridge",
},
],
),
}
},
deque(["Resources", "TaskDefinition", "Properties"]),
[],
),
(
{
"Resources": {
Expand Down

0 comments on commit 5ce49ce

Please sign in to comment.