Skip to content

Commit

Permalink
refactor some of Terraform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Hitchon committed Jun 15, 2018
1 parent 62323d0 commit 074d836
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
21 changes: 21 additions & 0 deletions linter/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,24 @@ func loadRulesForTest(filename string, t *testing.T) assertion.RuleSet {
}
return ruleSet
}

func assertViolationsCount(testName string, count int, violations []assertion.Violation, t *testing.T) {
if len(violations) != count {
t.Errorf("TestTerraformDataObject returned %d violations, expecting %d", len(violations), count)
t.Errorf("Violations: %v", violations)
}
}

func assertViolationByRuleID(testName string, ruleID string, violations []assertion.Violation, t *testing.T) {
found := false
ruleIDsFound := []string{}
for _, v := range violations {
ruleIDsFound = append(ruleIDsFound, v.RuleID)
if v.RuleID == ruleID {
found = true
}
}
if !found {
t.Errorf("%s expected violation %s not found in %v", testName, ruleID, ruleIDsFound)
}
}
39 changes: 10 additions & 29 deletions linter/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ func TestTerraformLinter(t *testing.T) {
if len(report.FilesScanned) != 1 {
t.Errorf("TestTerraformLinter scanned %d files, expecting 1", len(report.FilesScanned))
}
if len(report.Violations) != 0 {
t.Errorf("TestTerraformLinter returned %d violations, expecting 0", len(report.Violations))
}
assertViolationsCount("TestTerraformLinter ", 0, report.Violations, t)
}

func TestTerraformVariables(t *testing.T) {
Expand Down Expand Up @@ -75,9 +73,7 @@ func TestTerraformVariablesInDifferentFile(t *testing.T) {
if len(report.FilesScanned) != 2 {
t.Errorf("TestTerraformVariablesInDifferentFile scanned %d files, expecting 2", len(report.FilesScanned))
}
if len(report.Violations) != 0 {
t.Errorf("TestTerraformVariablesInDifferentFile expecting no violations, found %v", report.Violations)
}
assertViolationsCount("TestTerraformVariablesInDifferentFile ", 0, report.Violations, t)
}

type TestingValueSource struct{}
Expand Down Expand Up @@ -107,9 +103,7 @@ func TestTerraformPolicies(t *testing.T) {
if len(report.FilesScanned) != 1 {
t.Errorf("TestTerraformPolicies scanned %d files, expecting 1", len(report.FilesScanned))
}
if len(report.Violations) != 1 {
t.Errorf("TestTerraformPolicies returned %d violations, expecting 1", len(report.Violations))
}
assertViolationsCount("TestTerraformPolicies ", 1, report.Violations, t)
}

func TestTerraformPoliciesWithVariables(t *testing.T) {
Expand All @@ -124,10 +118,7 @@ func TestTerraformPoliciesWithVariables(t *testing.T) {
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))
t.Errorf("Violations: %v", report.Violations)
}
assertViolationsCount("TestTerraformPoliciesWithVariables ", 0, report.Violations, t)
}

func TestTerraformHereDocWithExpression(t *testing.T) {
Expand All @@ -142,10 +133,7 @@ func TestTerraformHereDocWithExpression(t *testing.T) {
if err != nil {
t.Error("Expecting TestTerraformHereDocWithExpression to not return an error:" + err.Error())
}
if len(report.Violations) != 0 {
t.Errorf("TestTerraformPoliciesWithVariables returned %d violations, expecting 0", len(report.Violations))
t.Errorf("Violations: %v", report.Violations)
}
assertViolationsCount("TestTerraformHereDocWithExpression ", 0, report.Violations, t)
}

func TestTerraformDataLoader(t *testing.T) {
Expand All @@ -171,10 +159,8 @@ func TestTerraformDataObject(t *testing.T) {
if err != nil {
t.Error("Expecting TestTerraformDataObject to not return an error:" + err.Error())
}
if len(report.Violations) != 1 {
t.Errorf("TestTerraformDataObject returned %d violations, expecting 1", len(report.Violations))
t.Errorf("Violations: %v", report.Violations)
}
assertViolationsCount("TestTerraformDataObject", 1, report.Violations, t)
assertViolationByRuleID("TestTerraformDataObject", "DATA_NOT_CONTAINS", report.Violations, t)
}

func TestTerraformProvider(t *testing.T) {
Expand All @@ -189,10 +175,8 @@ func TestTerraformProvider(t *testing.T) {
if err != nil {
t.Error("Expecting TestTerraformProvider to not return an error:" + err.Error())
}
if len(report.Violations) != 1 {
t.Errorf("TestTerraformProvider returned %d violations, expecting 1", len(report.Violations))
t.Errorf("Violations: %v", report.Violations)
}
assertViolationsCount("TestTerraformProvider", 1, report.Violations, t)
assertViolationByRuleID("TestTerraformProvider", "AWS_PROVIDER", report.Violations, t)
}

func TestTerraformParseError(t *testing.T) {
Expand All @@ -213,8 +197,5 @@ func TestTerraformParseError(t *testing.T) {
t.Errorf("TestTerraformParseError returned %d violations, expecting 1", len(report.Violations))
t.Errorf("Violations: %v", report.Violations)
}
ruleID := report.Violations[0].RuleID
if ruleID != "FILE_LOAD" {
t.Errorf("TestTerraformParseError returned RuleID = %s, expecting FILE_LOAD", ruleID)
}
assertViolationByRuleID("TestTerraformParseError", "FILE_LOAD", report.Violations, t)
}
2 changes: 1 addition & 1 deletion linter/testdata/rules/terraform_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ files:
- "*.tf"
rules:

- id: TEST_1
- id: DATA_NOT_CONTAINS
message: Testing
resource: template_file
category: data
Expand Down

0 comments on commit 074d836

Please sign in to comment.