Skip to content

Commit

Permalink
add has-properties operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Hitchon committed Mar 31, 2018
1 parent bc91ddb commit 4f7a0b0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 12 deletions.
21 changes: 14 additions & 7 deletions assertion/assertion_test.go
Expand Up @@ -462,13 +462,20 @@ func loadTestCasesFromFixture(filename string, t *testing.T) FixtureTestCases {
return testCases
}

func TestCollectionAssertion(t *testing.T) {
fixture := loadTestCasesFromFixture("./fixtures/collection-assertions.yaml", t)
for _, testCase := range fixture.TestCases {
status, err := CheckAssertion(testCase.Rule, testCase.Rule.Assertions[0], testCase.Resource, testLogging)
failTestIfError(err, testCase.Name, t)
if status != testCase.Result {
t.Errorf("Test case %s returned %s expecting %s", testCase.Name, status, testCase.Result)
func TestUsingFixtures(t *testing.T) {
fixtureFilenames := []string{
"./fixtures/collection-assertions.yaml",
"./fixtures/has-properties.yaml",
}

for _, filename := range fixtureFilenames {
fixture := loadTestCasesFromFixture(filename, t)
for _, testCase := range fixture.TestCases {
status, err := CheckAssertion(testCase.Rule, testCase.Rule.Assertions[0], testCase.Resource, testLogging)
failTestIfError(err, testCase.Name, t)
if status != testCase.Result {
t.Errorf("Test case %s returned %s expecting %s", testCase.Name, status, testCase.Result)
}
}
}
}
41 changes: 41 additions & 0 deletions assertion/fixtures/has-properties.yaml
@@ -0,0 +1,41 @@
---
description: Test has-properties operator
test_cases:

- name: has-properties_OK
rule:
id: PROPERTIES_1
message: Missing properties
severity: FAILURE
resource: sample
assertions:
- key: example
op: has-properties
value: name,id
resource:
id: p1
type: sample
properties:
example:
name: first
id: 1
result: OK

- name: has-properties_FAILURE
rule:
id: PROPERTIES_2
message: Missing properties
severity: FAILURE
resource: sample
assertions:
- key: example
op: has-properties
value: name,id,description
resource:
id: p1
type: sample
properties:
example:
name: first
id: 1
result: FAILURE
16 changes: 16 additions & 0 deletions assertion/has_properties.go
@@ -0,0 +1,16 @@
package assertion

import (
"strings"
)

func has_properties(data interface{}, list string) (bool, error) {
for _, key := range strings.Split(list, ",") {
if m, ok := data.(map[string]interface{}); ok {
if _, ok := m[key]; !ok {
return false, nil
}
}
}
return true, nil
}
7 changes: 2 additions & 5 deletions assertion/match.go
Expand Up @@ -9,11 +9,6 @@ func isNil(data interface{}) bool {
return data == nil
}

func isString(data interface{}) bool {
_, ok := data.(string)
return ok
}

func convertToString(data interface{}) (string, bool) {
s, ok := data.(string)
return s, ok
Expand Down Expand Up @@ -106,6 +101,8 @@ func isMatch(data interface{}, op string, value string, valueType string) (bool,
return true, nil
}
return false, nil
case "has-properties":
return has_properties(data, value)
}
return false, nil
}
9 changes: 9 additions & 0 deletions example-files/rules/generic.yml
Expand Up @@ -37,6 +37,15 @@ rules:
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
Expand Down

0 comments on commit 4f7a0b0

Please sign in to comment.