mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
12dad370d5
- REST APIs
* CRUDL on policy modules
* Ad-hoc queries
* Query and patch base documents
* Query virtual documents
- Add PolicyStore to manage policy definition/module CRUDL operations.
* Supports persistence of policy definitons.
* Serve REST API CRUDL operations.
* Manage install/uninstall of rules into data store.
* Manage persistence of policy definitions.
- Misc. refactoring
* Move storage creation into runtime Init.
* Make AST types JSON serializable. Tweaked ast.Import to use Term instead
of Value for the path.
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
// Copyright 2016 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package eval
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
type mockTracer struct {
|
|
buf []string
|
|
}
|
|
|
|
func (t *mockTracer) Enabled() bool { return true }
|
|
|
|
func (t *mockTracer) Trace(ctx *TopDownContext, f string, a ...interface{}) {
|
|
t.buf = append(t.buf, fmt.Sprintf(f, a))
|
|
}
|
|
|
|
func TestTracer(t *testing.T) {
|
|
|
|
data := loadSmallTestData()
|
|
|
|
mods := compileRules([]string{"data.a"}, []string{
|
|
"p[x] :- q[x] = y",
|
|
"q[i] = j :- a[i] = j",
|
|
})
|
|
|
|
store := NewStorageFromJSONObject(data)
|
|
policyStore := NewPolicyStore(store, "")
|
|
|
|
for id, mod := range mods {
|
|
err := policyStore.Add(id, mod, []byte(""), false)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
tracer := &mockTracer{[]string{}}
|
|
|
|
params := &TopDownQueryParams{
|
|
Store: store,
|
|
Tracer: tracer,
|
|
Path: []string{"p"}}
|
|
|
|
result, err := TopDownQuery(params)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error in tracing test: %v", err)
|
|
return
|
|
}
|
|
|
|
expected := []interface{}{float64(0), float64(1), float64(2), float64(3)}
|
|
|
|
if Compare(result, expected) != 0 {
|
|
t.Errorf("Unexpected result in tracing test: %v", result)
|
|
return
|
|
}
|
|
|
|
// ((try success finish * 2) * 4) + 2
|
|
// 2 rules
|
|
// 4 elements in a
|
|
if len(tracer.buf) != 26 {
|
|
t.Errorf("Unexpected output from tracer:\n%v", tracer.buf)
|
|
}
|
|
}
|