Skip to content

Commit

Permalink
Change tests to use a new entrypoint for creating memdb for testing
Browse files Browse the repository at this point in the history
This will allow us to centrally register additional datastore validation that only runs at test time
  • Loading branch information
josephschorr committed Nov 6, 2024
1 parent 11031c7 commit 23edcef
Show file tree
Hide file tree
Showing 29 changed files with 111 additions and 72 deletions.
7 changes: 4 additions & 3 deletions internal/caveats/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/authzed/spicedb/internal/caveats"
"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/testfixtures"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
Expand Down Expand Up @@ -447,7 +448,7 @@ func TestRunCaveatExpressions(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
req := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
req.NoError(err)

ds, _ := testfixtures.DatastoreFromSchemaAndTestRelationships(rawDS, `
Expand Down Expand Up @@ -506,7 +507,7 @@ func TestRunCaveatExpressions(t *testing.T) {
func TestRunCaveatWithMissingMap(t *testing.T) {
req := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
req.NoError(err)

ds, _ := testfixtures.DatastoreFromSchemaAndTestRelationships(rawDS, `
Expand Down Expand Up @@ -535,7 +536,7 @@ func TestRunCaveatWithMissingMap(t *testing.T) {
func TestRunCaveatWithEmptyMap(t *testing.T) {
req := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
req.NoError(err)

ds, _ := testfixtures.DatastoreFromSchemaAndTestRelationships(rawDS, `
Expand Down
16 changes: 16 additions & 0 deletions internal/datastore/dsfortesting/dsfortesting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dsfortesting

import (
"time"

"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/pkg/datastore"
)

func NewMemDBDatastoreForTesting(
watchBufferLength uint16,
revisionQuantization,
gcWindow time.Duration,
) (datastore.Datastore, error) {
return memdb.NewMemdbDatastore(watchBufferLength, revisionQuantization, gcWindow)
}
3 changes: 2 additions & 1 deletion internal/datastore/proxy/observable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/test"
Expand All @@ -12,7 +13,7 @@ import (
type observableTest struct{}

func (obs observableTest) New(revisionQuantization, _, gcWindow time.Duration, watchBufferLength uint16) (datastore.Datastore, error) {
db, err := memdb.NewMemdbDatastore(watchBufferLength, revisionQuantization, gcWindow)
db, err := dsfortesting.NewMemDBDatastoreForTesting(watchBufferLength, revisionQuantization, gcWindow)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions internal/datastore/proxy/relationshipintegrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/pkg/datastore"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/tuple"
Expand Down Expand Up @@ -58,7 +58,7 @@ var expiredKeyForTesting = KeyConfig{
}

func TestWriteWithPredefinedIntegrity(t *testing.T) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(t, err)

pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil)
Expand All @@ -76,7 +76,7 @@ func TestWriteWithPredefinedIntegrity(t *testing.T) {
}

func TestReadWithMissingIntegrity(t *testing.T) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(t, err)

// Write a relationship to the underlying datastore without integrity information.
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestReadWithMissingIntegrity(t *testing.T) {
}

func TestBasicIntegrityFailureDueToInvalidHashVersion(t *testing.T) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(t, err)

pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil)
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestBasicIntegrityFailureDueToInvalidHashVersion(t *testing.T) {
}

func TestBasicIntegrityFailureDueToInvalidHashSignature(t *testing.T) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(t, err)

pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil)
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestBasicIntegrityFailureDueToInvalidHashSignature(t *testing.T) {
}

func TestBasicIntegrityFailureDueToWriteWithExpiredKey(t *testing.T) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(t, err)

// Create a proxy with the to-be-expired key and write some relationships.
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestBasicIntegrityFailureDueToWriteWithExpiredKey(t *testing.T) {
}

func TestWatchIntegrityFailureDueToInvalidHashSignature(t *testing.T) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(t, err)

headRev, err := ds.HeadRevision(context.Background())
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestWatchIntegrityFailureDueToInvalidHashSignature(t *testing.T) {
func BenchmarkQueryRelsWithIntegrity(b *testing.B) {
for _, withIntegrity := range []bool{true, false} {
b.Run(fmt.Sprintf("withIntegrity=%t", withIntegrity), func(b *testing.B) {
ds, err := memdb.NewMemdbDatastore(0, 5*time.Second, 1*time.Hour)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 5*time.Second, 1*time.Hour)
require.NoError(b, err)

pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil)
Expand Down
3 changes: 2 additions & 1 deletion internal/datastore/proxy/schemacaching/estimatedsize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ccoveille/go-safecast"
"github.com/stretchr/testify/require"

"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/validationfile"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestEstimatedDefinitionSizes(t *testing.T) {
filePath := filePath
t.Run(path.Base(filePath), func(t *testing.T) {
require := require.New(t)
ds, err := memdb.NewMemdbDatastore(0, 1*time.Second, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 1*time.Second, memdb.DisableGC)
require.NoError(err)

fullyResolved, _, err := validationfile.PopulateFromFiles(context.Background(), ds, []string{filePath})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"

"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/datastore/proxy/proxy_test"
"github.com/authzed/spicedb/internal/datastore/revisions"
Expand Down Expand Up @@ -366,7 +367,7 @@ func TestSnapshotCachingRealDatastore(t *testing.T) {
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(t, err)

ctx := context.Background()
Expand Down
3 changes: 2 additions & 1 deletion internal/dispatch/combined/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
"github.com/authzed/spicedb/internal/testfixtures"
Expand All @@ -22,7 +23,7 @@ func TestCombinedRecursiveCall(t *testing.T) {

ctx := datastoremw.ContextWithHandle(context.Background())

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(t, err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(rawDS, `
Expand Down
15 changes: 8 additions & 7 deletions internal/dispatch/graph/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/dispatch"
"github.com/authzed/spicedb/internal/dispatch/caching"
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestMaxDepth(t *testing.T) {
t.Parallel()
require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, _ := testfixtures.StandardDatastoreWithSchema(rawDS, require)
Expand Down Expand Up @@ -1322,7 +1323,7 @@ func TestCheckPermissionOverSchema(t *testing.T) {

dispatcher := NewLocalOnlyDispatcher(10, 100)

ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(ds, tc.schema, tc.relationships, require)
Expand Down Expand Up @@ -1823,7 +1824,7 @@ func TestCheckWithHints(t *testing.T) {

dispatcher := NewLocalOnlyDispatcher(10, 100)

ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(ds, tc.schema, tc.relationships, require)
Expand Down Expand Up @@ -1863,7 +1864,7 @@ func TestCheckHintsPartialApplication(t *testing.T) {

dispatcher := NewLocalOnlyDispatcher(10, 100)

ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(ds, `
Expand Down Expand Up @@ -1909,7 +1910,7 @@ func TestCheckHintsPartialApplicationOverArrow(t *testing.T) {

dispatcher := NewLocalOnlyDispatcher(10, 100)

ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(ds, `
Expand Down Expand Up @@ -1955,7 +1956,7 @@ func TestCheckHintsPartialApplicationOverArrow(t *testing.T) {
}

func newLocalDispatcherWithConcurrencyLimit(t testing.TB, concurrencyLimit uint16) (context.Context, dispatch.Dispatcher, datastore.Revision) {
rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(t, err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require.New(t))
Expand All @@ -1977,7 +1978,7 @@ func newLocalDispatcher(t testing.TB) (context.Context, dispatch.Dispatcher, dat
}

func newLocalDispatcherWithSchemaAndRels(t testing.TB, schema string, rels []tuple.Relationship) (context.Context, dispatch.Dispatcher, datastore.Revision) {
rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(t, err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(rawDS, schema, rels, require.New(t))
Expand Down
3 changes: 2 additions & 1 deletion internal/dispatch/graph/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/protobuf/testing/protocmp"

"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
expand "github.com/authzed/spicedb/internal/graph"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
Expand Down Expand Up @@ -280,7 +281,7 @@ func TestMaxDepthExpand(t *testing.T) {

require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, _ := testfixtures.StandardDatastoreWithSchema(rawDS, require)
Expand Down
11 changes: 6 additions & 5 deletions internal/dispatch/graph/lookupresources2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.uber.org/goleak"
"google.golang.org/protobuf/types/known/structpb"

"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/dispatch"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
Expand Down Expand Up @@ -310,7 +311,7 @@ func TestMaxDepthLookup2(t *testing.T) {
t.Parallel()
require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require)
Expand Down Expand Up @@ -754,7 +755,7 @@ func TestLookupResources2OverSchemaWithCursors(t *testing.T) {

dispatcher := NewLocalOnlyDispatcher(10, 100)

ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(ds, tc.schema, tc.relationships, require)
Expand Down Expand Up @@ -830,7 +831,7 @@ func TestLookupResources2ImmediateTimeout(t *testing.T) {

require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require)
Expand Down Expand Up @@ -865,7 +866,7 @@ func TestLookupResources2WithError(t *testing.T) {

require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require)
Expand Down Expand Up @@ -1341,7 +1342,7 @@ func TestLookupResources2EnsureCheckHints(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(rawDS, tc.schema, tc.relationships, require)
Expand Down
9 changes: 5 additions & 4 deletions internal/dispatch/graph/lookupresources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ccoveille/go-safecast"
"github.com/stretchr/testify/require"

"github.com/authzed/spicedb/internal/datastore/dsfortesting"
"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/dispatch"
datastoremw "github.com/authzed/spicedb/internal/middleware/datastore"
Expand Down Expand Up @@ -302,7 +303,7 @@ func TestMaxDepthLookup(t *testing.T) {
t.Parallel()
require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require)
Expand Down Expand Up @@ -606,7 +607,7 @@ func TestLookupResourcesOverSchemaWithCursors(t *testing.T) {

dispatcher := NewLocalOnlyDispatcher(10, 100)

ds, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
ds, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.DatastoreFromSchemaAndTestRelationships(ds, tc.schema, tc.relationships, require)
Expand Down Expand Up @@ -664,7 +665,7 @@ func TestLookupResourcesImmediateTimeout(t *testing.T) {

require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require)
Expand Down Expand Up @@ -697,7 +698,7 @@ func TestLookupResourcesWithError(t *testing.T) {

require := require.New(t)

rawDS, err := memdb.NewMemdbDatastore(0, 0, memdb.DisableGC)
rawDS, err := dsfortesting.NewMemDBDatastoreForTesting(0, 0, memdb.DisableGC)
require.NoError(err)

ds, revision := testfixtures.StandardDatastoreWithData(rawDS, require)
Expand Down
Loading

0 comments on commit 23edcef

Please sign in to comment.