|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + hcl "github.com/hashicorp/hcl/v2" |
| 7 | + "github.com/terraform-linters/tflint-plugin-sdk/helper" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_AwsSecurityGroupInlineRulesRule(t *testing.T) { |
| 11 | + cases := []struct { |
| 12 | + Name string |
| 13 | + Content string |
| 14 | + Expected helper.Issues |
| 15 | + }{ |
| 16 | + { |
| 17 | + Name: "finds deprecated ingress block", |
| 18 | + Content: ` |
| 19 | +resource "aws_security_group" "test" { |
| 20 | + name = "test" |
| 21 | +
|
| 22 | + ingress { |
| 23 | + from_port = 0 |
| 24 | + to_port = 0 |
| 25 | + protocol = "-1" |
| 26 | + cidr_blocks = ["0.0.0.0/0"] |
| 27 | + } |
| 28 | +} |
| 29 | +`, |
| 30 | + Expected: helper.Issues{ |
| 31 | + { |
| 32 | + Rule: NewAwsSecurityGroupInlineRulesRule(), |
| 33 | + Message: "Replace this ingress block with aws_vpc_security_group_ingress_rule.", |
| 34 | + Range: hcl.Range{ |
| 35 | + Filename: "resource.tf", |
| 36 | + Start: hcl.Pos{Line: 5, Column: 3}, |
| 37 | + End: hcl.Pos{Line: 5, Column: 10}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "finds deprecated egress block", |
| 44 | + Content: ` |
| 45 | +resource "aws_security_group" "test" { |
| 46 | + name = "test" |
| 47 | +
|
| 48 | + egress { |
| 49 | + from_port = 0 |
| 50 | + to_port = 0 |
| 51 | + protocol = "-1" |
| 52 | + cidr_blocks = ["0.0.0.0/0"] |
| 53 | + } |
| 54 | +} |
| 55 | +`, |
| 56 | + Expected: helper.Issues{ |
| 57 | + { |
| 58 | + Rule: NewAwsSecurityGroupInlineRulesRule(), |
| 59 | + Message: "Replace this egress block with aws_vpc_security_group_egress_rule.", |
| 60 | + Range: hcl.Range{ |
| 61 | + Filename: "resource.tf", |
| 62 | + Start: hcl.Pos{Line: 5, Column: 3}, |
| 63 | + End: hcl.Pos{Line: 5, Column: 9}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + { |
| 69 | + Name: "everything is fine", |
| 70 | + Content: ` |
| 71 | +resource "aws_security_group" "test" { |
| 72 | + name = "test" |
| 73 | +} |
| 74 | +`, |
| 75 | + Expected: helper.Issues{}, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + rule := NewAwsSecurityGroupInlineRulesRule() |
| 80 | + |
| 81 | + for _, tc := range cases { |
| 82 | + runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) |
| 83 | + |
| 84 | + if err := rule.Check(runner); err != nil { |
| 85 | + t.Fatalf("Unexpected error occurred: %s", err) |
| 86 | + } |
| 87 | + |
| 88 | + helper.AssertIssues(t, tc.Expected, runner.Issues) |
| 89 | + } |
| 90 | +} |
0 commit comments