Skip to content

Commit 850b771

Browse files
kftsehkkddejong
andauthored
Fix custom rule inequality comparison (#2612) (#2614)
* Fix custom rule inequality comparison (#2612) --------- Co-authored-by: Kevin DeJong <[email protected]>
1 parent 3ffc242 commit 850b771

File tree

6 files changed

+156
-4
lines changed

6 files changed

+156
-4
lines changed

src/cfnlint/rules/custom/Operators.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ def rule_func(value, expected_values, path):
132132
def CreateGreaterRule(rule_id, resourceType, prop, value, error_message):
133133
def rule_func(value, expected_value, path):
134134
matches = []
135-
if checkInt(value.strip()) and checkInt(str(expected_value).strip()):
136-
if value.strip().lower() < str(expected_value).strip().lower():
135+
if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
136+
if int(str(value).strip()) < int(str(expected_value).strip()):
137137
matches.append(
138138
cfnlint.rules.RuleMatch(
139139
path, error_message or "Greater than check failed"
@@ -163,8 +163,8 @@ def rule_func(value, expected_value, path):
163163
def CreateLesserRule(rule_id, resourceType, prop, value, error_message):
164164
def rule_func(value, expected_value, path):
165165
matches = []
166-
if checkInt(value.strip()) and checkInt(str(expected_value).strip()):
167-
if value.strip().lower() > str(expected_value).strip().lower():
166+
if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
167+
if int(str(value).strip()) > int(str(expected_value).strip()):
168168
matches.append(
169169
cfnlint.rules.RuleMatch(
170170
path, error_message or "Lesser than check failed"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
AWSTemplateFormatVersion: "2010-09-09"
3+
Description: >
4+
Testing for custom inequality rules
5+
Resources:
6+
LambdaExecutionRole:
7+
Type: AWS::IAM::Role
8+
Properties:
9+
AssumeRolePolicyDocument:
10+
Version: "2012-10-17"
11+
Statement:
12+
-
13+
Effect: Allow
14+
Principal:
15+
Service:
16+
- lambda.amazonaws.com
17+
- ec2.amazonaws.com
18+
Action:
19+
- sts:AssumeRole
20+
Path: "/"
21+
TimeoutInNumericsFunction:
22+
Type: AWS::Lambda::Function
23+
Properties:
24+
Handler: index.handler
25+
Role: !GetAtt LambdaExecutionRole.Arn
26+
Code: ./
27+
Runtime: nodejs4.3
28+
Timeout: 100
29+
TimeoutInStringFunction:
30+
Type: AWS::Lambda::Function
31+
Properties:
32+
Handler: index.handler
33+
Role: !GetAtt LambdaExecutionRole.Arn
34+
Code: ./
35+
Runtime: nodejs4.3
36+
Timeout: '100'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
AWSTemplateFormatVersion: "2010-09-09"
3+
Description: >
4+
Testing for custom inequality rules
5+
Resources:
6+
LambdaExecutionRole:
7+
Type: AWS::IAM::Role
8+
Properties:
9+
AssumeRolePolicyDocument:
10+
Version: "2012-10-17"
11+
Statement:
12+
-
13+
Effect: Allow
14+
Principal:
15+
Service:
16+
- lambda.amazonaws.com
17+
- ec2.amazonaws.com
18+
Action:
19+
- sts:AssumeRole
20+
Path: "/"
21+
TimeoutInNumericsFunction:
22+
Type: AWS::Lambda::Function
23+
Properties:
24+
Handler: index.handler
25+
Role: !GetAtt LambdaExecutionRole.Arn
26+
Code: ./
27+
Runtime: nodejs4.3
28+
Timeout: 9
29+
TimeoutInStringFunction:
30+
Type: AWS::Lambda::Function
31+
Properties:
32+
Handler: index.handler
33+
Role: !GetAtt LambdaExecutionRole.Arn
34+
Code: ./
35+
Runtime: nodejs4.3
36+
Timeout: '9'

test/unit/rules/custom/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
from test.unit.rules import BaseRuleTestCase
6+
7+
from cfnlint.rules.custom.Operators import CreateGreaterRule # pylint: disable=E0401
8+
9+
10+
class TestGreaterOperator(BaseRuleTestCase):
11+
"""Test template mapping configurations"""
12+
13+
def setUp(self):
14+
"""Setup"""
15+
super(TestGreaterOperator, self).setUp()
16+
self.collection.register(
17+
CreateGreaterRule(
18+
"E9001",
19+
"AWS::Lambda::Function",
20+
"Timeout",
21+
"50",
22+
"Timeout should be greater than 50",
23+
)
24+
)
25+
26+
success_templates = [
27+
"test/fixtures/templates/good/custom/numeric-inequalities-large.yaml"
28+
]
29+
30+
def test_file_positive(self):
31+
"""Test Positive"""
32+
self.helper_file_positive()
33+
34+
def test_file_negative(self):
35+
"""Test failure"""
36+
self.helper_file_negative(
37+
"test/fixtures/templates/good/custom/numeric-inequalities-small.yaml", 2
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
from test.unit.rules import BaseRuleTestCase
6+
7+
from cfnlint.rules.custom.Operators import CreateLesserRule # pylint: disable=E0401
8+
9+
10+
class TestLesserOperator(BaseRuleTestCase):
11+
"""Test template mapping configurations"""
12+
13+
def setUp(self):
14+
"""Setup"""
15+
super(TestLesserOperator, self).setUp()
16+
self.collection.register(
17+
CreateLesserRule(
18+
"E9001",
19+
"AWS::Lambda::Function",
20+
"Timeout",
21+
"50",
22+
"Timeout should be less than 50",
23+
)
24+
)
25+
26+
success_templates = [
27+
"test/fixtures/templates/good/custom/numeric-inequalities-small.yaml"
28+
]
29+
30+
def test_file_positive(self):
31+
"""Test Positive"""
32+
self.helper_file_positive()
33+
34+
def test_file_negative(self):
35+
"""Test failure"""
36+
self.helper_file_negative(
37+
"test/fixtures/templates/good/custom/numeric-inequalities-large.yaml", 2
38+
)

0 commit comments

Comments
 (0)