Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start adding tests for cli directory
- Loading branch information
lhitchon
committed
Mar 31, 2018
1 parent
6419f32
commit 97b4d4e
Showing
9 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stelligent/config-lint/assertion" | ||
"testing" | ||
) | ||
|
||
func testLogger(string) {} | ||
|
||
func loadRulesForTest(filename string, t *testing.T) assertion.RuleSet { | ||
rulesContent, err := assertion.LoadRules(filename) | ||
if err != nil { | ||
t.Errorf("Unable to load rules file: %s", filename) | ||
return assertion.RuleSet{} | ||
} | ||
ruleSet, err := assertion.ParseRules(rulesContent) | ||
if err != nil { | ||
t.Errorf("Unable to parse rules file: %s", filename) | ||
return assertion.RuleSet{} | ||
} | ||
return ruleSet | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTerraformLinter(t *testing.T) { | ||
emptyTags := []string{} | ||
emptyIds := []string{} | ||
linter := TerraformLinter{Log: testLogger} | ||
ruleSet := loadRulesForTest("./testdata/rules/terraform_instance.yml", t) | ||
filenames := []string{"./testdata/resources/terraform_instance.tf"} | ||
filesScanned, violations, err := linter.Validate(filenames, ruleSet, emptyTags, emptyIds) | ||
if err != nil { | ||
t.Error("Expecting TestTerraformLinter to not return an error") | ||
} | ||
if len(filesScanned) != 1 { | ||
t.Errorf("TestTerraformLinter scanned %d files, expecting 1", len(filesScanned)) | ||
} | ||
if len(violations) != 0 { | ||
t.Errorf("TestTerraformLinter returned %d violations, expecting 0", len(violations)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# | ||
widgets: | ||
- id: W1 | ||
name: Foo | ||
- id: W2 | ||
name: Bar | ||
- id: W3 | ||
key: Baz | ||
|
||
gadgets: | ||
- name: first_gadget | ||
color: red | ||
- name: second_gadget | ||
color: blue | ||
- name: third_gadget | ||
color: green | ||
- name: fourth_gadget | ||
color: yellow | ||
|
||
other_stuff: | ||
contraptions: | ||
- ids: | ||
serial_number: S1000 | ||
sku: S1234 | ||
size: 10 | ||
locations: | ||
- city: Seattle | ||
- city: San Francisco | ||
- ids: | ||
serial_number: S2000 | ||
sku: S5678 | ||
size: 20 | ||
locations: | ||
- city: New York | ||
- ids: | ||
serial_number: S3000 | ||
sku: S0101 | ||
size: 4000 | ||
locations: | ||
- city: Paris | ||
- city: Munich | ||
- city: Florence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "aws_instance" "first" { | ||
ami = "ami-f2d3638a" | ||
instance_type = "t2.micro" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
version: 1 | ||
description: Rules for generic YAML file | ||
type: YAML | ||
files: | ||
- "*.config" | ||
|
||
# For generic YAML linting, we need a list of resources | ||
# Each entry in the list describes the resource type, how to discover it in the file, and how to get its ID | ||
|
||
resources: | ||
- type: widget | ||
key: widgets[] | ||
id: id | ||
- type: gadget | ||
key: gadgets[] | ||
id: name | ||
- type: contraption | ||
key: other_stuff.contraptions[] | ||
id: ids.serial_number | ||
|
||
rules: | ||
|
||
- id: WIDGET_NAME | ||
message: Widget needs a name | ||
severity: FAILURE | ||
resource: widget | ||
assertions: | ||
- key: name | ||
op: present | ||
|
||
- id: GADGET_COLOR | ||
message: Gadget has missing or invalid color | ||
severity: FAILURE | ||
resource: gadget | ||
assertions: | ||
- key: color | ||
op: in | ||
value: red,blue,green | ||
|
||
- id: GADGET_PROPERTIES | ||
message: Gadget has missing properties | ||
severity: FAILURE | ||
resource: gadget | ||
assertions: | ||
- key: "@" | ||
op: has-properties | ||
value: name,color | ||
|
||
- id: CONTRAPTION_SIZE | ||
message: Contraption size should be less than 1000 | ||
resource: contraption | ||
severity: FAILURE | ||
assertions: | ||
- key: size | ||
op: lt | ||
value: 1000 | ||
value_type: integer | ||
|
||
- id: CONTRAPTION_LOCATIONS | ||
message: Contraption location must have city | ||
resource: contraption | ||
severity: FAILURE | ||
assertions: | ||
- every: | ||
key: locations | ||
assertions: | ||
- key: city | ||
op: present | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: 1 | ||
description: Rules for Terraform configuration files | ||
type: Terraform | ||
files: | ||
- "*.tf" | ||
rules: | ||
|
||
- id: TEST_1 | ||
message: Testing | ||
resource: aws_instance | ||
assertions: | ||
- key: instance_type | ||
op: in | ||
value: t2.micro,m3.medium | ||
severity: WARNING | ||
tags: | ||
- ec2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestYAMLLinter(t *testing.T) { | ||
emptyTags := []string{} | ||
emptyIds := []string{} | ||
linter := YAMLLinter{Log: testLogger} | ||
ruleSet := loadRulesForTest("./testdata/rules/generic.yml", t) | ||
filenames := []string{"./testdata/resources/generic.config"} | ||
filesScanned, violations, err := linter.Validate(filenames, ruleSet, emptyTags, emptyIds) | ||
if err != nil { | ||
t.Error("Expecting TestYAMLLinter to not return an error") | ||
} | ||
if len(filesScanned) != 1 { | ||
t.Errorf("TestYAMLLinter scanned %d files, expecting 1", len(filesScanned)) | ||
} | ||
if len(violations) != 3 { | ||
t.Errorf("TestYAMLLinter returned %d violations, expecting 3", len(violations)) | ||
} | ||
} |