Skip to content

Commit

Permalink
server: Add test case for bundle update - query API handler scenario
Browse files Browse the repository at this point in the history
This test case exercises the scenario when a bundle is being written
to the store (ie. active write transaction) and the OPA server handling
a policy eval request at the same time. The former should not block
the later.

Fixes: open-policy-agent#4792

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
  • Loading branch information
ashutosh-narkar committed Sep 26, 2023
1 parent 83a295f commit 5d089fe
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,43 @@ func TestDataGetExplainFull(t *testing.T) {
}
}

func TestDataPostWithActiveStoreWriteTxn(t *testing.T) {

f := newFixture(t)

err := f.v1(http.MethodPut, "/policies/test", `package test
p = [1, 2, 3, 4] { true }`, 200, "")
if err != nil {
t.Fatal(err)
}

// open write transaction on the store and execute a query.
// Then check the query is processed
ctx := context.Background()
_ = storage.NewTransactionOrDie(ctx, f.server.store, storage.WriteParams)

req := newReqV1(http.MethodPost, "/data/test/p", "")
f.reset()
f.server.Handler.ServeHTTP(f.recorder, req)

var result types.DataResponseV1

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

var expected interface{}

if err := util.UnmarshalJSON([]byte(`[1,2,3,4]`), &expected); err != nil {
panic(err)
}

if result.Result == nil || !reflect.DeepEqual(*result.Result, expected) {
t.Fatalf("Expected %v but got: %v", expected, result.Result)
}
}

func TestDataPostExplain(t *testing.T) {
f := newFixture(t)

Expand Down

0 comments on commit 5d089fe

Please sign in to comment.