Skip to content

Commit

Permalink
start adding tests for cli directory
Browse files Browse the repository at this point in the history
  • Loading branch information
lhitchon committed Mar 31, 2018
1 parent 6419f32 commit 97b4d4e
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 0 deletions.
File renamed without changes.
22 changes: 22 additions & 0 deletions cli/helpers_test.go
@@ -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.
23 changes: 23 additions & 0 deletions cli/terraform_test.go
@@ -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))
}
}
42 changes: 42 additions & 0 deletions cli/testdata/resources/generic.config
@@ -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
4 changes: 4 additions & 0 deletions cli/testdata/resources/terraform_instance.tf
@@ -0,0 +1,4 @@
resource "aws_instance" "first" {
ami = "ami-f2d3638a"
instance_type = "t2.micro"
}
69 changes: 69 additions & 0 deletions cli/testdata/rules/generic.yml
@@ -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

17 changes: 17 additions & 0 deletions cli/testdata/rules/terraform_instance.yml
@@ -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
23 changes: 23 additions & 0 deletions cli/yaml_linter_test.go
@@ -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))
}
}

0 comments on commit 97b4d4e

Please sign in to comment.