Files
releases/runtime/runtime_test.go
T
Torin Sandall 12dad370d5 Add basic REST API support to server mode
- 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.
2016-05-13 15:34:02 -07:00

126 lines
2.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 runtime
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/eval"
)
func TestInit(t *testing.T) {
tmp1, err := ioutil.TempFile("", "docFile")
if err != nil {
panic(err)
}
defer os.Remove(tmp1.Name())
doc1 := `{"foo": "bar", "a": {"b": {"d": [1]}}}`
if _, err := tmp1.Write([]byte(doc1)); err != nil {
panic(err)
}
if err := tmp1.Close(); err != nil {
panic(err)
}
tmp2, err := ioutil.TempFile("", "policyFile")
if err != nil {
panic(err)
}
defer os.Remove(tmp2.Name())
mod1 := `
package a.b.c
import data.foo
p = true :- foo = "bar"
p = true :- 1 = 2
`
if _, err := tmp2.Write([]byte(mod1)); err != nil {
panic(err)
}
if err := tmp2.Close(); err != nil {
panic(err)
}
tmp3, err := ioutil.TempDir("", "policyDir")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp3)
tmp4 := filepath.Join(tmp3, "existingPolicy")
err = ioutil.WriteFile(tmp4, []byte(`
package a.b.c
q = true :- p
`), 0644)
if err != nil {
panic(err)
}
rt := Runtime{}
err = rt.Init(&Params{
Paths: []string{tmp1.Name(), tmp2.Name()},
PolicyDir: tmp3,
})
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
node, err := rt.Store.Get(path("foo"))
if eval.Compare(node, "bar") != 0 || err != nil {
t.Errorf("Expected %v but got %v (err: %v)", "bar", node, err)
return
}
node, err = rt.Store.Get(path("a.b.c.p"))
rules, ok := node.([]*ast.Rule)
if !ok {
t.Errorf("Expected rules but got: %v", node)
return
}
if !rules[0].Name.Equal(ast.Var("p")) {
t.Errorf("Expected rule p but got: %v", rules[0])
return
}
node, err = rt.Store.Get(path("a.b.c.q"))
rules, ok = node.([]*ast.Rule)
if !ok {
t.Errorf("Expected rules but got: %v", node)
return
}
if !rules[0].Name.Equal(ast.Var("q")) {
t.Errorf("Expected rule q but got: %v", rules[0])
return
}
}
func path(input interface{}) []interface{} {
switch input := input.(type) {
case []interface{}:
return input
case string:
switch v := ast.MustParseTerm(input).Value.(type) {
case ast.Var:
return []interface{}{string(v)}
case ast.Ref:
path, err := v.Underlying()
if err != nil {
panic(err)
}
return path
}
}
panic(fmt.Sprintf("illegal value: %v", input))
}