mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-25 01:35:18 -06:00
283b1e11f8
This has been semi-public anyways: people depend on the JSON structure to be kept as-is. So we might as well make the structs public, and make working with this easier from golang. No need to copy the struct definitions manually. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
72 lines
1.1 KiB
Go
72 lines
1.1 KiB
Go
package planner
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/open-policy-agent/opa/ast"
|
|
"github.com/open-policy-agent/opa/internal/planner"
|
|
"github.com/open-policy-agent/opa/ir"
|
|
)
|
|
|
|
func TestRoundTrip(t *testing.T) {
|
|
|
|
c, err := ast.CompileModules(map[string]string{
|
|
"test.rego": `
|
|
package test
|
|
|
|
p {
|
|
input.foo == 7
|
|
}
|
|
`,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
modules := []*ast.Module{}
|
|
|
|
for _, m := range c.Modules {
|
|
modules = append(modules, m)
|
|
}
|
|
|
|
planner := planner.New().
|
|
WithQueries([]planner.QuerySet{
|
|
{
|
|
Name: "main",
|
|
Queries: []ast.Body{
|
|
ast.MustParseBody("data.test.p = true"),
|
|
},
|
|
},
|
|
}).
|
|
WithModules(modules).
|
|
WithBuiltinDecls(ast.BuiltinMap)
|
|
|
|
plan, err := planner.Plan()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
bs, err := json.MarshalIndent(plan, "", " ")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var cpy ir.Policy
|
|
err = json.Unmarshal(bs, &cpy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
bs2, err := json.MarshalIndent(plan, "", " ")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !bytes.Equal(bs, bs2) {
|
|
t.Fatal("expected bytes to be equal")
|
|
}
|
|
}
|