Skip to content

Commit 8b4a99a

Browse files
author
Will Beason
authoredOct 29, 2021
Test Local Driver (#145)
Add unit tests for local driver. Increases code coverage for the driver by 18%. * Test local driver Signed-off-by: Will Beason <[email protected]> * Continue testing local driver Signed-off-by: Will Beason <[email protected]> * Finish Local Driver unit tests Signed-off-by: Will Beason <[email protected]> * Revert unintentional changes Signed-off-by: Will Beason <[email protected]> * Register external_data builtin Signed-off-by: Will Beason <[email protected]> * Continue reverting unintentional changes Signed-off-by: Will Beason <[email protected]> * Minor refactorings Signed-off-by: Will Beason <[email protected]> * Make arg names consistent Signed-off-by: Will Beason <[email protected]> * Resolve nits Signed-off-by: Will Beason <[email protected]> * Re-add tracing Signed-off-by: Will Beason <[email protected]> * Resolve merge conflicts Signed-off-by: Will Beason <[email protected]> * Continue resolving merge conflicts Signed-off-by: Will Beason <[email protected]>
1 parent f478d8a commit 8b4a99a

28 files changed

+2707
-101
lines changed
 

‎constraint/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/mitchellh/mapstructure v1.4.1 // indirect
1717
github.com/onsi/gomega v1.10.5
1818
github.com/open-policy-agent/opa v0.29.4
19-
github.com/pkg/errors v0.9.1
19+
github.com/pkg/errors v0.9.1 // indirect
2020
github.com/prometheus/client_golang v1.10.0 // indirect
2121
github.com/prometheus/common v0.25.0 // indirect
2222
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect

‎constraint/pkg/client/backend.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,43 @@ func (b *Backend) NewClient(opts ...Opt) (*Client, error) {
4848
if b.hasClient {
4949
return nil, errors.New("currently only one client per backend is supported")
5050
}
51+
5152
var fields []string
5253
for k := range validDataFields {
5354
fields = append(fields, k)
5455
}
56+
5557
c := &Client{
5658
backend: b,
5759
constraints: make(map[schema.GroupKind]map[string]*unstructured.Unstructured),
5860
templates: make(map[templateKey]*templateEntry),
5961
allowedDataFields: fields,
6062
}
63+
6164
var errs Errors
6265
for _, opt := range opts {
6366
if err := opt(c); err != nil {
6467
errs = append(errs, err)
6568
}
6669
}
70+
if len(errs) > 0 {
71+
return nil, errs
72+
}
73+
6774
for _, field := range c.allowedDataFields {
6875
if !validDataFields[field] {
6976
return nil, fmt.Errorf("invalid data field %s", field)
7077
}
7178
}
72-
if len(errs) > 0 {
73-
return nil, errs
74-
}
79+
7580
if len(c.targets) == 0 {
7681
return nil, errors.New("no targets registered: please register a target via client.Targets()")
7782
}
83+
7884
if err := b.driver.Init(context.Background()); err != nil {
7985
return nil, err
8086
}
87+
8188
if err := c.init(); err != nil {
8289
return nil, err
8390
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package local
2+
3+
import (
4+
"github.com/open-policy-agent/frameworks/constraint/pkg/externaldata"
5+
"github.com/open-policy-agent/opa/ast"
6+
"github.com/open-policy-agent/opa/storage"
7+
"github.com/open-policy-agent/opa/storage/inmem"
8+
opatypes "github.com/open-policy-agent/opa/types"
9+
)
10+
11+
type Arg func(*driver)
12+
13+
func Defaults() Arg {
14+
return func(d *driver) {
15+
if d.compiler == nil {
16+
d.compiler = ast.NewCompiler()
17+
}
18+
19+
if d.modules == nil {
20+
d.modules = make(map[string]*ast.Module)
21+
}
22+
23+
if d.storage == nil {
24+
d.storage = inmem.New()
25+
}
26+
27+
if d.capabilities == nil {
28+
d.capabilities = ast.CapabilitiesForThisVersion()
29+
}
30+
31+
// adding external_data builtin otherwise capabilities get overridden
32+
// if a capability, like http.send, is disabled
33+
if d.providerCache != nil {
34+
d.capabilities.Builtins = append(d.capabilities.Builtins, &ast.Builtin{
35+
Name: "external_data",
36+
Decl: opatypes.NewFunction(opatypes.Args(opatypes.A), opatypes.A),
37+
})
38+
}
39+
}
40+
}
41+
42+
func Tracing(enabled bool) Arg {
43+
return func(d *driver) {
44+
d.traceEnabled = enabled
45+
}
46+
}
47+
48+
func Modules(modules map[string]*ast.Module) Arg {
49+
return func(d *driver) {
50+
d.modules = modules
51+
}
52+
}
53+
54+
func Storage(s storage.Store) Arg {
55+
return func(d *driver) {
56+
d.storage = s
57+
}
58+
}
59+
60+
func AddExternalDataProviderCache(providerCache *externaldata.ProviderCache) Arg {
61+
return func(d *driver) {
62+
d.providerCache = providerCache
63+
}
64+
}
65+
66+
func DisableBuiltins(builtins ...string) Arg {
67+
return func(d *driver) {
68+
if d.capabilities == nil {
69+
d.capabilities = ast.CapabilitiesForThisVersion()
70+
}
71+
72+
disableBuiltins := make(map[string]bool)
73+
for _, b := range builtins {
74+
disableBuiltins[b] = true
75+
}
76+
77+
var nb []*ast.Builtin
78+
builtins := d.capabilities.Builtins
79+
for i, b := range builtins {
80+
if !disableBuiltins[b.Name] {
81+
nb = append(nb, builtins[i])
82+
}
83+
}
84+
85+
d.capabilities.Builtins = nb
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package local
2+
3+
import "errors"
4+
5+
var (
6+
ErrModuleName = errors.New("invalid module name")
7+
ErrParse = errors.New("unable to parse module")
8+
ErrCompile = errors.New("unable to compile modules")
9+
ErrModulePrefix = errors.New("invalid module prefix")
10+
ErrPathInvalid = errors.New("invalid data path")
11+
ErrPathConflict = errors.New("conflicting path")
12+
ErrWrite = errors.New("error writing data")
13+
ErrRead = errors.New("error reading data")
14+
ErrTransaction = errors.New("error committing data")
15+
)

‎constraint/pkg/client/drivers/local/local.go

+115-97
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package local
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
func BenchmarkDriver_PutModule(b *testing.B) {
10+
for _, n := range []int{1, 2, 5, 10, 20, 50, 100, 200} {
11+
b.Run(fmt.Sprintf("%d templates", n), func(b *testing.B) {
12+
for i := 0; i < b.N; i++ {
13+
b.StopTimer()
14+
ctx := context.Background()
15+
d := New()
16+
b.StartTimer()
17+
18+
for j := 0; j < n; j++ {
19+
name := fmt.Sprintf("foo-%d", j)
20+
err := d.PutModule(ctx, name, Module)
21+
if err != nil {
22+
b.Fatal(err)
23+
}
24+
}
25+
}
26+
})
27+
}
28+
}

‎constraint/pkg/client/drivers/local/local_unit_test.go

+997
Large diffs are not rendered by default.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/equate.go

+148
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/errors_go113.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/errors_xerrors.go

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/ignore.go

+206
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/sort.go

+147
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/struct_filter.go

+187
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/github.com/google/go-cmp/cmp/cmpopts/xform.go

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/LICENSE

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/PATENTS

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/README

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/adaptor.go

+193
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/codereview.cfg

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/doc.go

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/errors.go

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/fmt.go

+187
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/format.go

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/frame.go

+56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/go.mod

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/internal/internal.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/golang.org/x/xerrors/wrap.go

+106
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎constraint/vendor/modules.txt

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ github.com/golang/protobuf/ptypes/timestamp
6262
# github.com/google/go-cmp v0.5.6
6363
## explicit
6464
github.com/google/go-cmp/cmp
65+
github.com/google/go-cmp/cmp/cmpopts
6566
github.com/google/go-cmp/cmp/internal/diff
6667
github.com/google/go-cmp/cmp/internal/flags
6768
github.com/google/go-cmp/cmp/internal/function
@@ -261,6 +262,9 @@ golang.org/x/text/unicode/norm
261262
golang.org/x/text/width
262263
# golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
263264
golang.org/x/time/rate
265+
# golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
266+
golang.org/x/xerrors
267+
golang.org/x/xerrors/internal
264268
# google.golang.org/appengine v1.6.7
265269
## explicit
266270
google.golang.org/appengine/internal

0 commit comments

Comments
 (0)
Please sign in to comment.