示例#1
0
func TestDataGetExplainFull(t *testing.T) {
	f := newFixture(t)

	f.v1("PUT", "/data/x", `{"a":1,"b":2}`, 204, "")

	req := newReqV1("GET", "/data/x?explain=full", "")
	f.reset()
	f.server.Handler.ServeHTTP(f.recorder, req)

	var result traceV1

	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&result); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	if len(result) != 3 {
		t.Fatalf("Expected exactly 3 events but got %d", len(result))
	}

	_, ok := result[2].Node.(ast.Body)
	if !ok {
		t.Fatalf("Expected body for node but got: %v", result[2].Node)
	}

	if len(result[2].Locals) != 1 {
		t.Fatalf("Expected one binding but got: %v", result[2].Locals)
	}

	req = newReqV1("GET", "/data/deadbeef?explain=full", "")
	f.reset()
	f.server.Handler.ServeHTTP(f.recorder, req)

	result = traceV1{}

	if f.recorder.Code != 404 {
		t.Fatalf("Expected status code to be 404 but got: %v", f.recorder.Code)
	}

	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&result); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	if len(result) != 3 {
		t.Fatalf("Expected exactly 3 events but got %d", len(result))
	}

	if result[2].Op != "Fail" {
		t.Fatalf("Expected last event to be 'Fail' but got: %v", result[2])
	}

}
示例#2
0
func TestPoliciesPutV1ParseError(t *testing.T) {
	f := newFixture(t)
	req := newReqV1("PUT", "/policies/test", `
    package a.b.c

    p ;- true
    `)

	f.server.Handler.ServeHTTP(f.recorder, req)

	if f.recorder.Code != 400 {
		t.Errorf("Expected bad request but got %v", f.recorder)
		return
	}

	errs := astErrorV1{}
	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&errs); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	expected := ast.NewLocation(nil, "test", 4, 8)

	if !reflect.DeepEqual(errs.Errors[0].Location, expected) {
		t.Errorf("Expected error location to be %v but got: %v", expected, errs)
	}
}
示例#3
0
func TestDataGetExplainTruth(t *testing.T) {
	f := newFixture(t)

	f.v1("PUT", "/policies/test", `package test
	p :- a = [1,2,3,4], a[_] = x, x > 1
	`, 204, "")

	req := newReqV1("GET", "/data/test/p?explain=truth", "")
	f.reset()
	f.server.Handler.ServeHTTP(f.recorder, req)

	var result traceV1

	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&result); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	if len(result) != 8 {
		t.Fatalf("Expected exactly 9 events but got %d", len(result))
	}

	req = newReqV1("GET", "/data/deadbeef?explain=truth", "")
	f.reset()
	f.server.Handler.ServeHTTP(f.recorder, req)

	if f.recorder.Code != 404 {
		t.Fatalf("Expected status code to be 404 but got: %v", f.recorder)
	}
}
示例#4
0
文件: server.go 项目: tsandall/opa
func (s *Server) v1DataPatch(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	vars := mux.Vars(r)

	ops := []patchV1{}
	if err := util.NewJSONDecoder(r.Body).Decode(&ops); err != nil {
		handleError(w, 400, err)
		return
	}

	txn, err := s.store.NewTransaction(ctx)
	if err != nil {
		handleErrorAuto(w, err)
		return
	}

	defer s.store.Close(ctx, txn)

	patches, err := s.prepareV1PatchSlice(vars["path"], ops)
	if err != nil {
		handleErrorAuto(w, err)
		return
	}

	for _, patch := range patches {
		if err := s.store.Write(ctx, txn, patch.op, patch.path, patch.value); err != nil {
			handleErrorAuto(w, err)
			return
		}
	}

	handleResponse(w, 204, nil)
}
示例#5
0
func (f *fixture) loadResponse() interface{} {
	var v interface{}
	err := util.NewJSONDecoder(f.recorder.Body).Decode(&v)
	if err != nil {
		panic(err)
	}
	return v
}
示例#6
0
func (f *fixture) loadPolicy() *policyV1 {
	policy := &policyV1{}
	err := util.NewJSONDecoder(f.recorder.Body).Decode(policy)
	if err != nil {
		panic(err)
	}
	return policy
}
示例#7
0
文件: datastore.go 项目: tsandall/opa
// NewDataStoreFromReader returns a new DataStore from a reader that produces a
// JSON serialized object. This function is for test purposes.
func NewDataStoreFromReader(r io.Reader) *DataStore {
	d := util.NewJSONDecoder(r)
	var data map[string]interface{}
	if err := d.Decode(&data); err != nil {
		panic(err)
	}
	return NewDataStoreFromJSONObject(data)
}
示例#8
0
文件: loader.go 项目: tsandall/opa
// unmarshalYAML re-implements yaml.Unmarshal so that the JSON decoder can have
// UseNumber set.
func unmarshalYAML(y []byte, o interface{}) error {
	bs, err := yaml.YAMLToJSON(y)
	if err != nil {
		return fmt.Errorf("error converting YAML to JSON: %v", err)
	}
	buf := bytes.NewBuffer(bs)
	decoder := util.NewJSONDecoder(buf)
	return decoder.Decode(o)
}
示例#9
0
文件: loader.go 项目: tsandall/opa
func jsonLoad(path string) (interface{}, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, errors.Wrapf(err, path)
	}
	defer f.Close()
	decoder := util.NewJSONDecoder(f)
	var x interface{}
	return x, decoder.Decode(&x)
}
示例#10
0
func TestQueryV1Explain(t *testing.T) {
	f := newFixture(t)
	get := newReqV1("GET", `/query?q=a=[1,2,3],a[i]=x&explain=full`, "")
	f.server.Handler.ServeHTTP(f.recorder, get)

	if f.recorder.Code != 200 {
		t.Fatalf("Expected 200 but got: %v", f.recorder)
	}

	var result traceV1

	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&result); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	if len(result) != 10 {
		t.Fatalf("Expected exactly 10 trace events for full query but got %d", len(result))
	}

	get = newReqV1("GET", "/query?q=a=[1,2,3],a[_]=x,x>1&explain=truth", "")
	f.reset()
	f.server.Handler.ServeHTTP(f.recorder, get)

	if f.recorder.Code != 200 {
		t.Fatalf("Expected 200 but got: %v", f.recorder)
	}

	result = traceV1{}

	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&result); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	if len(result) != 5 {
		t.Fatalf("Expected exactly 5 trace events for truth query but got %d", len(result))
	}
}
示例#11
0
文件: server.go 项目: tsandall/opa
func (s *Server) v1DataPut(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	vars := mux.Vars(r)

	var value interface{}
	if err := util.NewJSONDecoder(r.Body).Decode(&value); err != nil {
		handleError(w, 400, err)
		return
	}

	txn, err := s.store.NewTransaction(ctx)
	if err != nil {
		handleErrorAuto(w, err)
		return
	}

	defer s.store.Close(ctx, txn)

	path, ok := storage.ParsePath("/" + strings.Trim(vars["path"], "/"))
	if !ok {
		handleErrorf(w, 400, "bad path format %v", vars["path"])
		return
	}

	_, err = s.store.Read(ctx, txn, path)

	if err != nil {
		if !storage.IsNotFound(err) {
			handleErrorAuto(w, err)
			return
		}
		if err := s.makeDir(ctx, txn, path[:len(path)-1]); err != nil {
			handleErrorAuto(w, err)
			return
		}
	} else if r.Header.Get("If-None-Match") == "*" {
		handleResponse(w, 304, nil)
		return
	}

	if err := s.store.Write(ctx, txn, storage.AddOp, path, value); err != nil {
		handleErrorAuto(w, err)
		return
	}

	handleResponse(w, 204, nil)
}
示例#12
0
func TestPoliciesListV1(t *testing.T) {
	f := newFixture(t)
	put := newReqV1("PUT", "/policies/1", testMod)
	f.server.Handler.ServeHTTP(f.recorder, put)
	if f.recorder.Code != 200 {
		t.Errorf("Expected success but got %v", f.recorder)
		return
	}
	f.reset()
	list := newReqV1("GET", "/policies", "")

	f.server.Handler.ServeHTTP(f.recorder, list)

	if f.recorder.Code != 200 {
		t.Errorf("Expected success but got %v", f.recorder)
		return
	}

	var policies []*policyV1
	err := util.NewJSONDecoder(f.recorder.Body).Decode(&policies)
	if err != nil {
		t.Errorf("Expected policy list but got error: %v", err)
		return
	}

	expected := []*policyV1{
		newPolicy("1", testMod),
	}
	if len(expected) != len(policies) {
		t.Errorf("Expected %d policies but got: %v", len(expected), policies)
		return
	}
	for i := range expected {
		if !expected[i].Equal(policies[i]) {
			t.Errorf("Expected policies to be equal. Expected:\n\n%v\n\nGot:\n\n%v\n", expected[i], policies[i])
		}
	}
}
示例#13
0
func TestPoliciesPutV1CompileError(t *testing.T) {
	f := newFixture(t)
	req := newReqV1("PUT", "/policies/test", `
    package a.b.c
    p[x] :- q[x]
    q[x] :- p[x]
    `)

	f.server.Handler.ServeHTTP(f.recorder, req)

	if f.recorder.Code != 400 {
		t.Fatalf("Expected bad request but got %v", f.recorder)
	}

	errs := astErrorV1{}
	if err := util.NewJSONDecoder(f.recorder.Body).Decode(&errs); err != nil {
		t.Fatalf("Unexpected JSON decode error: %v", err)
	}

	expected := ast.NewLocation(nil, "test", 3, 5)

	if len(errs.Errors) != 2 {
		t.Fatalf("Expected exactly two errors but got %d: %v", len(errs.Errors), errs)
	}

	found := false

	for _, err := range errs.Errors {
		if reflect.DeepEqual(err.Location, expected) {
			found = true
		}
	}

	if !found {
		t.Fatalf("Missing expected error %v: %v", expected, errs)
	}
}