Skip to content

Commit

Permalink
beef up tests
Browse files Browse the repository at this point in the history
Signed-off-by: Simar <[email protected]>
  • Loading branch information
simar7 committed May 18, 2023
1 parent 3ddb789 commit bfe0f01
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/rego/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (s *Scanner) loadPoliciesFromDirs(target fs.FS, paths []string) (map[string
ProcessAnnotation: true,
})
if err != nil {
return err
s.debug.Log("Failed to load module: %s, err: %s", filepath.ToSlash(path), err.Error())
return nil
}
modules[path] = module
return nil
Expand Down
46 changes: 46 additions & 0 deletions pkg/rego/load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package rego

import (
"bytes"
"embed"
"testing"

"github.com/stretchr/testify/assert"

"github.com/aquasecurity/defsec/pkg/types"
"github.com/stretchr/testify/require"
)

//go:embed testdata/policies
var testEmbedFS embed.FS

func Test_RegoScanning_WithSomeInvalidPolicies(t *testing.T) {
t.Run("allow no errors", func(t *testing.T) {
var debugBuf bytes.Buffer
scanner := NewScanner(types.SourceDockerfile)
scanner.SetRegoErrorLimit(0)
scanner.SetDebugWriter(&debugBuf)
p, _ := RecurseEmbeddedModules(testEmbedFS, ".")
scanner.policies = p

err := scanner.compilePolicies(testEmbedFS, []string{"policies"})
require.ErrorContains(t, err, `want (one of): ["Cmd" "EndLine" "Flags" "JSON" "Original" "Path" "Stage" "StartLine" "SubCmd" "Value"]`)
assert.Contains(t, debugBuf.String(), "Error(s) occurred while loading policies")
})

t.Run("allow up to max 1 error", func(t *testing.T) {
var debugBuf bytes.Buffer
scanner := NewScanner(types.SourceDockerfile)
scanner.SetRegoErrorLimit(1)
scanner.SetDebugWriter(&debugBuf)

p, _ := RecurseEmbeddedModules(testEmbedFS, ".")
scanner.policies = p

err := scanner.compilePolicies(testEmbedFS, []string{"policies"})
require.NoError(t, err)

assert.Contains(t, debugBuf.String(), "Error occurred while parsing: testdata/policies/invalid.rego, testdata/policies/invalid.rego:7")
})

}
8 changes: 8 additions & 0 deletions pkg/rego/testdata/policies/invalid.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# METADATA
# schemas:
# - input: schema["input"]
package defsec.test

deny {
input.Stages[0].Commands[0].FooBarNothingBurger == "lol"
}
8 changes: 8 additions & 0 deletions pkg/rego/testdata/policies/valid.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# METADATA
# schemas:
# - input: schema["input"]
package defsec.test

deny {
input.Stages[0].Commands[0].Cmd == "lol"
}

0 comments on commit bfe0f01

Please sign in to comment.