Skip to content

Commit

Permalink
add test for variables in a Terraform here doc
Browse files Browse the repository at this point in the history
  • Loading branch information
lhitchon committed May 6, 2018
1 parent ae350a2 commit 91be358
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions linter/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,20 @@ func TestTerraformPolicies(t *testing.T) {
t.Errorf("TestTerraformPolicies returned %d violations, expecting 1", len(report.Violations))
}
}

func TestTerraformPoliciesWithVariables(t *testing.T) {
options := Options{
Tags: []string{},
RuleIDs: []string{},
}
filenames := []string{"./testdata/resources/policy_with_variables.tf"}
linter := FileLinter{Filenames: filenames, ValueSource: TestingValueSource{}, Loader: TerraformResourceLoader{}}
ruleSet := loadRulesForTest("./testdata/rules/policy_variable.yml", t)
report, err := linter.Validate(ruleSet, options)
if err != nil {
t.Error("Expecting TestTerraformPoliciesWithVariables to not return an error:" + err.Error())
}
if len(report.Violations) != 0 {
t.Errorf("TestTerraformPoliciesWithVariables returned %d violations, expecting 0", len(report.Violations))
}
}
20 changes: 20 additions & 0 deletions linter/testdata/resources/policy_with_variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "statement_effect" {
default = "Allow"
}

resource "aws_iam_role" "role_with_variable" {
name = "non_compliant"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "*",
"Principal": { "Service": "ec2.amazonaws.com" },
"Effect": "${var.statement_effect}",
"Resources": "*"
}
]
}
EOF
}
14 changes: 14 additions & 0 deletions linter/testdata/rules/policy_variable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 1
description: Rules for Terraform here documents with variables
type: Terraform
files:
- "*.tf"
rules:

- id: TEST_VARIABLE
message: Testing
resource: aws_iam_role
assertions:
- key: "assume_role_policy.Statement[].Effect | [0]"
op: eq
value: Allow

0 comments on commit 91be358

Please sign in to comment.