-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
28 changed files
with
2,707 additions
and
101 deletions.
There are no files selected for viewing
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
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
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,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 | ||
} | ||
} |
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,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") | ||
) |
Oops, something went wrong.