Skip to content

Commit

Permalink
Test Local Driver (#145)
Browse files Browse the repository at this point in the history
Add unit tests for local driver. Increases code coverage for the driver by 18%.

* Test local driver

Signed-off-by: Will Beason <willbeason@google.com>

* Continue testing local driver

Signed-off-by: Will Beason <willbeason@google.com>

* Finish Local Driver unit tests

Signed-off-by: Will Beason <willbeason@google.com>

* Revert unintentional changes

Signed-off-by: Will Beason <willbeason@google.com>

* Register external_data builtin

Signed-off-by: Will Beason <willbeason@google.com>

* Continue reverting unintentional changes

Signed-off-by: Will Beason <willbeason@google.com>

* Minor refactorings

Signed-off-by: Will Beason <willbeason@google.com>

* Make arg names consistent

Signed-off-by: Will Beason <willbeason@google.com>

* Resolve nits

Signed-off-by: Will Beason <willbeason@google.com>

* Re-add tracing

Signed-off-by: Will Beason <willbeason@google.com>

* Resolve merge conflicts

Signed-off-by: Will Beason <willbeason@google.com>

* Continue resolving merge conflicts

Signed-off-by: Will Beason <willbeason@google.com>
  • Loading branch information
Will Beason committed Oct 29, 2021
1 parent f478d8a commit 8b4a99a
Show file tree
Hide file tree
Showing 28 changed files with 2,707 additions and 101 deletions.
2 changes: 1 addition & 1 deletion constraint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/onsi/gomega v1.10.5
github.com/open-policy-agent/opa v0.29.4
github.com/pkg/errors v0.9.1
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.10.0 // indirect
github.com/prometheus/common v0.25.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
Expand Down
13 changes: 10 additions & 3 deletions constraint/pkg/client/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,43 @@ func (b *Backend) NewClient(opts ...Opt) (*Client, error) {
if b.hasClient {
return nil, errors.New("currently only one client per backend is supported")
}

var fields []string
for k := range validDataFields {
fields = append(fields, k)
}

c := &Client{
backend: b,
constraints: make(map[schema.GroupKind]map[string]*unstructured.Unstructured),
templates: make(map[templateKey]*templateEntry),
allowedDataFields: fields,
}

var errs Errors
for _, opt := range opts {
if err := opt(c); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return nil, errs
}

for _, field := range c.allowedDataFields {
if !validDataFields[field] {
return nil, fmt.Errorf("invalid data field %s", field)
}
}
if len(errs) > 0 {
return nil, errs
}

if len(c.targets) == 0 {
return nil, errors.New("no targets registered: please register a target via client.Targets()")
}

if err := b.driver.Init(context.Background()); err != nil {
return nil, err
}

if err := c.init(); err != nil {
return nil, err
}
Expand Down
87 changes: 87 additions & 0 deletions constraint/pkg/client/drivers/local/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package local

import (
"github.com/open-policy-agent/frameworks/constraint/pkg/externaldata"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
opatypes "github.com/open-policy-agent/opa/types"
)

type Arg func(*driver)

func Defaults() Arg {
return func(d *driver) {
if d.compiler == nil {
d.compiler = ast.NewCompiler()
}

if d.modules == nil {
d.modules = make(map[string]*ast.Module)
}

if d.storage == nil {
d.storage = inmem.New()
}

if d.capabilities == nil {
d.capabilities = ast.CapabilitiesForThisVersion()
}

// adding external_data builtin otherwise capabilities get overridden
// if a capability, like http.send, is disabled
if d.providerCache != nil {
d.capabilities.Builtins = append(d.capabilities.Builtins, &ast.Builtin{
Name: "external_data",
Decl: opatypes.NewFunction(opatypes.Args(opatypes.A), opatypes.A),
})
}
}
}

func Tracing(enabled bool) Arg {
return func(d *driver) {
d.traceEnabled = enabled
}
}

func Modules(modules map[string]*ast.Module) Arg {
return func(d *driver) {
d.modules = modules
}
}

func Storage(s storage.Store) Arg {
return func(d *driver) {
d.storage = s
}
}

func AddExternalDataProviderCache(providerCache *externaldata.ProviderCache) Arg {
return func(d *driver) {
d.providerCache = providerCache
}
}

func DisableBuiltins(builtins ...string) Arg {
return func(d *driver) {
if d.capabilities == nil {
d.capabilities = ast.CapabilitiesForThisVersion()
}

disableBuiltins := make(map[string]bool)
for _, b := range builtins {
disableBuiltins[b] = true
}

var nb []*ast.Builtin
builtins := d.capabilities.Builtins
for i, b := range builtins {
if !disableBuiltins[b.Name] {
nb = append(nb, builtins[i])
}
}

d.capabilities.Builtins = nb
}
}
15 changes: 15 additions & 0 deletions constraint/pkg/client/drivers/local/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package local

import "errors"

var (
ErrModuleName = errors.New("invalid module name")
ErrParse = errors.New("unable to parse module")
ErrCompile = errors.New("unable to compile modules")
ErrModulePrefix = errors.New("invalid module prefix")
ErrPathInvalid = errors.New("invalid data path")
ErrPathConflict = errors.New("conflicting path")
ErrWrite = errors.New("error writing data")
ErrRead = errors.New("error reading data")
ErrTransaction = errors.New("error committing data")
)
Loading

0 comments on commit 8b4a99a

Please sign in to comment.