Skip to content

Commit

Permalink
PR(WIP): Add policy testing framework working
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Jan 23, 2024
1 parent 49b97af commit 5c9133c
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 16 deletions.
12 changes: 2 additions & 10 deletions acp/acp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package acp

import (
"context"
"fmt"

cosmosDB "github.com/cosmos/cosmos-db"
protoTypes "github.com/cosmos/gogoproto/types"
Expand Down Expand Up @@ -124,15 +123,12 @@ func (l *ACPLocalEmbedded) Start(ctx context.Context, rootDir string) error {
return err
}

fmt.Println("222222222222222222222222222222222222")
fmt.Println("storePath : ", l.pathToStore)
fmt.Println("222222222222222222222222222222222222")

l.ctx = acp.GetCtx() // TODO: probably dont need this
l.messageService = acp.GetMsgService()
l.queryService = acp.GetQueryService()
l.underlyingDB = acp.GetUnderlyingDB()
l.pathToStore = acpStorePath

return nil
}

Expand All @@ -146,7 +142,6 @@ func (l *ACPLocalEmbedded) AddPolicy(
creator string,
isYAML bool,
) (string, error) {

var createPolicy types.MsgCreatePolicy

if isYAML {
Expand All @@ -165,7 +160,7 @@ func (l *ACPLocalEmbedded) AddPolicy(
}
}

createPolicyResponse, err := l.messageService.CreatePolicy(ctx, &createPolicy)
createPolicyResponse, err := l.messageService.CreatePolicy(l.ctx, &createPolicy)
if err != nil {
log.FatalE(ctx, "failed to add/create policy with local acp module", err)
return "", err
Expand All @@ -184,7 +179,6 @@ func (l *ACPLocalEmbedded) RegisterDocCreation(
collection string,
docID string,
) error {

registerDoc := types.MsgRegisterObject{
Creator: creator,
PolicyId: policyID,
Expand All @@ -207,7 +201,6 @@ func (l *ACPLocalEmbedded) RegisterDocCreation(
}

switch registerDocResponse.Result {

case types.RegistrationResult_NoOp:
log.Error(
ctx,
Expand Down Expand Up @@ -253,7 +246,6 @@ func (l *ACPLocalEmbedded) CheckDocAccess(
docID string,
permission string,
) (bool, error) {

checkDoc := types.QueryVerifyAccessRequestRequest{
PolicyId: policyID,
AccessRequest: &types.AccessRequest{
Expand Down
3 changes: 2 additions & 1 deletion client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (

blockstore "github.com/ipfs/boxo/blockstore"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/events"
"github.com/sourcenetwork/immutable"
)

type CollectionName = string
Expand Down
3 changes: 2 additions & 1 deletion db/txn_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ package db
import (
"context"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/immutable"
)

var _ client.DB = (*implicitTxnDB)(nil)
Expand Down
3 changes: 2 additions & 1 deletion http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
blockstore "github.com/ipfs/boxo/blockstore"
sse "github.com/vito/go-sse/sse"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/events"
"github.com/sourcenetwork/immutable"
)

var _ client.DB = (*Client)(nil)
Expand Down
3 changes: 2 additions & 1 deletion tests/clients/cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
blockstore "github.com/ipfs/boxo/blockstore"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/cli"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/events"
"github.com/sourcenetwork/defradb/http"
"github.com/sourcenetwork/defradb/net"
"github.com/sourcenetwork/immutable"
)

var _ client.P2P = (*Wrapper)(nil)
Expand Down
3 changes: 2 additions & 1 deletion tests/clients/http/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
blockstore "github.com/ipfs/boxo/blockstore"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/acp"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/events"
"github.com/sourcenetwork/defradb/http"
"github.com/sourcenetwork/defradb/net"
"github.com/sourcenetwork/immutable"
)

var _ client.P2P = (*Wrapper)(nil)
Expand Down
80 changes: 80 additions & 0 deletions tests/integration/acp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package tests

import (
"github.com/sourcenetwork/immutable"
"github.com/stretchr/testify/require"
)

// AddPolicy will attempt to add the given policy using defraDB's ACP module.
type AddPolicy struct {
// NodeID may hold the ID (index) of the node we want to add policy to.
//
// If a value is not provided the policy will be added in all nodes.
NodeID immutable.Option[int]

// The raw policy string.
Policy string

// The policy creator, i.e. actor creating the policy.
Creator string

// If true then assumes YAML format for the policy, otherwise assumes JSON format.
IsYAML bool

// The expected policyID generated based on the Policy loaded in to acp module.
ExpectedPolicyID string

// Any error expected from the action. Optional.
//
// String can be a partial, and the test will pass if an error is returned that
// contains this string.
ExpectedError string
}

// addPolicyACP will attempt to add the given policy using defraDB's ACP module.
func addPolicyACP(
s *state,
action AddPolicy,
) {
if s.dbt == badgerIMType || s.dbt == defraIMType {
s.t.Skip("ACP test (adding policy) is not supported on in-memory database type(s)")
}

// If we expect an error, then ExpectedPolicyID should be empty.
if action.ExpectedError != "" && action.ExpectedPolicyID != "" {
require.Fail(s.t, "Expected error should not have an expected policyID with it.", s.testCase.Description)
}

// for nodeID, node := range getNodes(action.NodeID, s.nodes) {
for _, node := range getNodes(action.NodeID, s.nodes) {
if !node.ACPModule().HasValue() {
require.Fail(s.t, "Failed to add policy because ACP module was not found", s.testCase.Description)
}

policyID, err := node.ACPModule().Value().AddPolicy(
s.ctx,
action.Policy,
action.Creator,
action.IsYAML,
)

if err == nil {
require.Equal(s.t, action.ExpectedError, "")
require.Equal(s.t, action.ExpectedPolicyID, policyID)
// s.policyIDs[nodeID] = append(s.policyIDs[nodeID], policyID)
}

expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)
}
}
95 changes: 95 additions & 0 deletions tests/integration/acp/simple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package test_acp

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestACP_CreateAndRead(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple acp create and read",
Actions: []any{
testUtils.AddPolicy{
Policy: `
description: a test policy which marks a collection in a database as a resource
resources:
users:
permissions:
read:
expr: owner + reader
write:
expr: owner
relations:
owner:
types:
- actor
reader:
types:
- actor
admin:
manages:
- reader
types:
- actor
actor:
name: actor
`,
IsYAML: true,
Creator: "cosmos1zzg43wdrhmmk89z3pmejwete2kkd4a3vn7w969",
ExpectedPolicyID: "53980e762616fcffbe76307995895e862f87ef3f21d509325d1dc772a770b001",
ExpectedError: "",
},

testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
age: Int
}
`,
},

testUtils.CreateDoc{
Doc: `{
"name": "John",
"age": 27
}`,
},

testUtils.Request{
Request: `
query {
Users {
_docID
name
age
}
}
`,
Results: []map[string]any{
{
"_docID": "bae-88b63198-7d38-5714-a9ff-21ba46374fd1",
"name": "John",
"age": int64(27),
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}
4 changes: 4 additions & 0 deletions tests/integration/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func setupDatabase(s *state) (impl client.DB, path string, err error) {
db.WithLensPoolSize(lensPoolSize),
}

// TODO-ACP FIX BEFORE PR
// s.dbt = badgerFileType
// TODO-ACP FIX BEFORE PR

switch s.dbt {
case badgerIMType:
impl, err = NewBadgerMemoryDB(s.ctx, dbopts...)
Expand Down
13 changes: 12 additions & 1 deletion tests/integration/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ type state struct {
// nodes.
documents [][]*client.Document

// PolicyIDs by index, by node's index,
// for example: [nodeID][policyID].
//
// Stored in the order they were added.
//
// The IDs are order dependent (only for testing) to ensure the policy ID can be accessed
// by the desired index, because it will be a random generated string each time.
// policyIDs [][]string

// Indexes, by index, by collection index, by node index.
indexes [][][]client.IndexDescription

Expand Down Expand Up @@ -116,6 +125,8 @@ func newState(
collections: [][]client.Collection{},
collectionNames: collectionNames,
documents: [][]*client.Document{},
indexes: [][][]client.IndexDescription{},
// policyIDs: [][]string{},
indexes: [][][]client.IndexDescription{},
isBench: false,
}
}
6 changes: 6 additions & 0 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func executeTestCase(
// as tests run. This is particularly important for file based datastores.
defer closeNodes(s)

// Initialize policyIDs per node, assuming each node has it's own local acp module.
// s.policyIDs = make([][]string, len(s.nodes))

// Documents and Collections may already exist in the database if actions have been split
// by the change detector so we should fetch them here at the start too (if they exist).
// collections are by node (index), as they are specific to nodes.
Expand Down Expand Up @@ -272,6 +275,9 @@ func performAction(
case GetMigrations:
getMigrations(s, action)

case AddPolicy:
addPolicyACP(s, action)

case CreateDoc:
createDoc(s, action)

Expand Down

0 comments on commit 5c9133c

Please sign in to comment.