Skip to content

Commit c6b1803

Browse files
committed
Add unit tests for map constraint traversal
- Add shapes_test.go with tests for extractShapeName, getTarget, and traverseToMapConstraints - Cover map, list, and structure shape types - Test case variations (Key/Value vs key/value vs Name/Value) - Test edge cases (non-string members, mixed members, list values) - Rename traverseToTagConstraints to traverseToMapConstraints
1 parent e8f25d2 commit c6b1803

File tree

57 files changed

+6168
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+6168
-50
lines changed

docs/rules/README.md

Lines changed: 53 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// This file generated by `generator/`. DO NOT EDIT
2+
3+
package models
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
9+
"github.com/terraform-linters/tflint-plugin-sdk/logger"
10+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
11+
)
12+
13+
// AwsAppconfigConfigurationProfileInvalidValidatorRule checks the pattern is valid
14+
type AwsAppconfigConfigurationProfileInvalidValidatorRule struct {
15+
tflint.DefaultRule
16+
17+
resourceType string
18+
attributeName string
19+
keyMax int
20+
}
21+
22+
// NewAwsAppconfigConfigurationProfileInvalidValidatorRule returns new rule with default attributes
23+
func NewAwsAppconfigConfigurationProfileInvalidValidatorRule() *AwsAppconfigConfigurationProfileInvalidValidatorRule {
24+
return &AwsAppconfigConfigurationProfileInvalidValidatorRule{
25+
resourceType: "aws_appconfig_configuration_profile",
26+
attributeName: "validator",
27+
keyMax: 32768,
28+
}
29+
}
30+
31+
// Name returns the rule name
32+
func (r *AwsAppconfigConfigurationProfileInvalidValidatorRule) Name() string {
33+
return "aws_appconfig_configuration_profile_invalid_validator"
34+
}
35+
36+
// Enabled returns whether the rule is enabled by default
37+
func (r *AwsAppconfigConfigurationProfileInvalidValidatorRule) Enabled() bool {
38+
return true
39+
}
40+
41+
// Severity returns the rule severity
42+
func (r *AwsAppconfigConfigurationProfileInvalidValidatorRule) Severity() tflint.Severity {
43+
return tflint.ERROR
44+
}
45+
46+
// Link returns the rule reference link
47+
func (r *AwsAppconfigConfigurationProfileInvalidValidatorRule) Link() string {
48+
return ""
49+
}
50+
51+
// Check checks the pattern is valid
52+
func (r *AwsAppconfigConfigurationProfileInvalidValidatorRule) Check(runner tflint.Runner) error {
53+
logger.Trace("Check `%s` rule", r.Name())
54+
55+
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
56+
Attributes: []hclext.AttributeSchema{
57+
{Name: r.attributeName},
58+
},
59+
}, nil)
60+
if err != nil {
61+
return err
62+
}
63+
64+
for _, resource := range resources.Blocks {
65+
attribute, exists := resource.Body.Attributes[r.attributeName]
66+
if !exists {
67+
continue
68+
}
69+
70+
err := runner.EvaluateExpr(attribute.Expr, func(val map[string]string) error {
71+
for k, _ := range val {
72+
if len(k) > r.keyMax {
73+
runner.EmitIssue(
74+
r,
75+
fmt.Sprintf("tag key %q must be 32768 characters or less", truncateLongMessage(k)),
76+
attribute.Expr.Range(),
77+
)
78+
}
79+
}
80+
return nil
81+
}, nil)
82+
if err != nil {
83+
return err
84+
}
85+
}
86+
87+
return nil
88+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// This file generated by `generator/`. DO NOT EDIT
2+
3+
package models
4+
5+
import (
6+
"fmt"
7+
"regexp"
8+
9+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
10+
"github.com/terraform-linters/tflint-plugin-sdk/logger"
11+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
12+
)
13+
14+
// AwsAppconfigEnvironmentInvalidMonitorRule checks the pattern is valid
15+
type AwsAppconfigEnvironmentInvalidMonitorRule struct {
16+
tflint.DefaultRule
17+
18+
resourceType string
19+
attributeName string
20+
keyMax int
21+
keyMin int
22+
valueMax int
23+
valueMin int
24+
valuePattern *regexp.Regexp
25+
}
26+
27+
// NewAwsAppconfigEnvironmentInvalidMonitorRule returns new rule with default attributes
28+
func NewAwsAppconfigEnvironmentInvalidMonitorRule() *AwsAppconfigEnvironmentInvalidMonitorRule {
29+
return &AwsAppconfigEnvironmentInvalidMonitorRule{
30+
resourceType: "aws_appconfig_environment",
31+
attributeName: "monitor",
32+
keyMax: 2048,
33+
keyMin: 1,
34+
valueMax: 2048,
35+
valueMin: 20,
36+
valuePattern: regexp.MustCompile(`^((arn):(aws|aws-cn|aws-iso|aws-iso-[a-z]{1}|aws-us-gov|aws-eusc):(iam)::\d{12}:role[/].*)$`),
37+
}
38+
}
39+
40+
// Name returns the rule name
41+
func (r *AwsAppconfigEnvironmentInvalidMonitorRule) Name() string {
42+
return "aws_appconfig_environment_invalid_monitor"
43+
}
44+
45+
// Enabled returns whether the rule is enabled by default
46+
func (r *AwsAppconfigEnvironmentInvalidMonitorRule) Enabled() bool {
47+
return true
48+
}
49+
50+
// Severity returns the rule severity
51+
func (r *AwsAppconfigEnvironmentInvalidMonitorRule) Severity() tflint.Severity {
52+
return tflint.ERROR
53+
}
54+
55+
// Link returns the rule reference link
56+
func (r *AwsAppconfigEnvironmentInvalidMonitorRule) Link() string {
57+
return ""
58+
}
59+
60+
// Check checks the pattern is valid
61+
func (r *AwsAppconfigEnvironmentInvalidMonitorRule) Check(runner tflint.Runner) error {
62+
logger.Trace("Check `%s` rule", r.Name())
63+
64+
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
65+
Attributes: []hclext.AttributeSchema{
66+
{Name: r.attributeName},
67+
},
68+
}, nil)
69+
if err != nil {
70+
return err
71+
}
72+
73+
for _, resource := range resources.Blocks {
74+
attribute, exists := resource.Body.Attributes[r.attributeName]
75+
if !exists {
76+
continue
77+
}
78+
79+
err := runner.EvaluateExpr(attribute.Expr, func(val map[string]string) error {
80+
for k, v := range val {
81+
if len(k) > r.keyMax {
82+
runner.EmitIssue(
83+
r,
84+
fmt.Sprintf("tag key %q must be 2048 characters or less", truncateLongMessage(k)),
85+
attribute.Expr.Range(),
86+
)
87+
}
88+
if len(k) < r.keyMin {
89+
runner.EmitIssue(
90+
r,
91+
fmt.Sprintf("tag key %q must be 1 characters or higher", truncateLongMessage(k)),
92+
attribute.Expr.Range(),
93+
)
94+
}
95+
if len(v) > r.valueMax {
96+
runner.EmitIssue(
97+
r,
98+
fmt.Sprintf("tag value for key %q must be 2048 characters or less", truncateLongMessage(k)),
99+
attribute.Expr.Range(),
100+
)
101+
}
102+
if len(v) < r.valueMin {
103+
runner.EmitIssue(
104+
r,
105+
fmt.Sprintf("tag value for key %q must be 20 characters or higher", truncateLongMessage(k)),
106+
attribute.Expr.Range(),
107+
)
108+
}
109+
if !r.valuePattern.MatchString(v) {
110+
runner.EmitIssue(
111+
r,
112+
fmt.Sprintf(`tag value %q for key %q does not match valid pattern %s`, truncateLongMessage(v), truncateLongMessage(k), `^((arn):(aws|aws-cn|aws-iso|aws-iso-[a-z]{1}|aws-us-gov|aws-eusc):(iam)::\d{12}:role[/].*)$`),
113+
attribute.Expr.Range(),
114+
)
115+
}
116+
}
117+
return nil
118+
}, nil)
119+
if err != nil {
120+
return err
121+
}
122+
}
123+
124+
return nil
125+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// This file generated by `generator/`. DO NOT EDIT
2+
3+
package models
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
9+
"github.com/terraform-linters/tflint-plugin-sdk/logger"
10+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
11+
)
12+
13+
// AwsApprunnerServiceInvalidHealthCheckConfigurationRule checks the pattern is valid
14+
type AwsApprunnerServiceInvalidHealthCheckConfigurationRule struct {
15+
tflint.DefaultRule
16+
17+
resourceType string
18+
attributeName string
19+
keyMin int
20+
}
21+
22+
// NewAwsApprunnerServiceInvalidHealthCheckConfigurationRule returns new rule with default attributes
23+
func NewAwsApprunnerServiceInvalidHealthCheckConfigurationRule() *AwsApprunnerServiceInvalidHealthCheckConfigurationRule {
24+
return &AwsApprunnerServiceInvalidHealthCheckConfigurationRule{
25+
resourceType: "aws_apprunner_service",
26+
attributeName: "health_check_configuration",
27+
keyMin: 1,
28+
}
29+
}
30+
31+
// Name returns the rule name
32+
func (r *AwsApprunnerServiceInvalidHealthCheckConfigurationRule) Name() string {
33+
return "aws_apprunner_service_invalid_health_check_configuration"
34+
}
35+
36+
// Enabled returns whether the rule is enabled by default
37+
func (r *AwsApprunnerServiceInvalidHealthCheckConfigurationRule) Enabled() bool {
38+
return true
39+
}
40+
41+
// Severity returns the rule severity
42+
func (r *AwsApprunnerServiceInvalidHealthCheckConfigurationRule) Severity() tflint.Severity {
43+
return tflint.ERROR
44+
}
45+
46+
// Link returns the rule reference link
47+
func (r *AwsApprunnerServiceInvalidHealthCheckConfigurationRule) Link() string {
48+
return ""
49+
}
50+
51+
// Check checks the pattern is valid
52+
func (r *AwsApprunnerServiceInvalidHealthCheckConfigurationRule) Check(runner tflint.Runner) error {
53+
logger.Trace("Check `%s` rule", r.Name())
54+
55+
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
56+
Attributes: []hclext.AttributeSchema{
57+
{Name: r.attributeName},
58+
},
59+
}, nil)
60+
if err != nil {
61+
return err
62+
}
63+
64+
for _, resource := range resources.Blocks {
65+
attribute, exists := resource.Body.Attributes[r.attributeName]
66+
if !exists {
67+
continue
68+
}
69+
70+
err := runner.EvaluateExpr(attribute.Expr, func(val map[string]string) error {
71+
for k, _ := range val {
72+
if len(k) < r.keyMin {
73+
runner.EmitIssue(
74+
r,
75+
fmt.Sprintf("tag key %q must be 1 characters or higher", truncateLongMessage(k)),
76+
attribute.Expr.Range(),
77+
)
78+
}
79+
}
80+
return nil
81+
}, nil)
82+
if err != nil {
83+
return err
84+
}
85+
}
86+
87+
return nil
88+
}

0 commit comments

Comments
 (0)