mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
0d7e509613
https://github.com/golangci/golangci-lint/releases/tag/v2.9.0 Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
73 lines
1.1 KiB
Go
73 lines
1.1 KiB
Go
package encoding
|
|
|
|
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) {
|
|
|
|
// Note: v0 module
|
|
c, err := ast.CompileModules(map[string]string{
|
|
"test.rego": `
|
|
package test
|
|
|
|
p {
|
|
input.foo == 7
|
|
}
|
|
`,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
modules := make([]*ast.Module, 0, len(c.Modules))
|
|
|
|
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")
|
|
}
|
|
}
|