Skip to content

Commit

Permalink
When creating a new Rego object, bundles are not allocated. This will…
Browse files Browse the repository at this point in the history
… cause an error when passing ParsedBundle option. This change initializes bundles at the beginning.

Signed-off-by: Xin Jin <xin@styra.com>
  • Loading branch information
xin-styra authored and patrick-east committed Feb 14, 2020
1 parent 9c2022e commit 71bbdc2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 1 addition & 4 deletions rego/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ func New(options ...func(r *Rego)) *Rego {
compiledQueries: map[queryType]compiledQuery{},
builtinDecls: map[string]*ast.Builtin{},
builtinFuncs: map[string]*topdown.Builtin{},
bundles: map[string]*bundle.Bundle{},
}

for _, option := range options {
Expand Down Expand Up @@ -877,10 +878,6 @@ func New(options ...func(r *Rego)) *Rego {
r.partialNamespace = defaultPartialNamespace
}

if r.bundles == nil {
r.bundles = map[string]*bundle.Bundle{}
}

return r
}

Expand Down
30 changes: 30 additions & 0 deletions rego/rego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/internal/storage/mock"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/storage"
Expand Down Expand Up @@ -1011,6 +1012,35 @@ func TestMissingLocation(t *testing.T) {
}
}

func TestBundlePassing(t *testing.T) {

opaBundle := bundle.Bundle{
Modules: []bundle.ModuleFile{
{
Path: "policy.rego",
Parsed: ast.MustParseModule(`package foo
allow = true`),
Raw: []byte(`package foo
allow = true`),
},
},
Manifest: bundle.Manifest{Revision: "test", Roots: &[]string{"/"}},
}

// Pass a bundle
r := New(
ParsedBundle("123", &opaBundle),
Query("x = data.foo.allow"),
)

res, err := r.Eval(context.Background())

if err != nil {
t.Fatal(err)
}
assertResultSet(t, res, `[[true]]`)
}

func TestModulePassing(t *testing.T) {

// This module will not be loaded since it has the same filename as the
Expand Down

0 comments on commit 71bbdc2

Please sign in to comment.