-
Notifications
You must be signed in to change notification settings - Fork 389
test: Improve code coverage of tests #3063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
josephschorr
wants to merge
12
commits into
authzed:main
Choose a base branch
from
josephschorr:test-cov-improvement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cae6747
test: add coverage for pkg/schema/errors.go
josephschorr 102a627
test: add coverage for internal/namespace/errors.go
josephschorr 49c9ad7
test: add coverage for internal/graph/errors.go
josephschorr 0c850d0
test: add coverage for internal/services/v1/errors.go
josephschorr 5f908df
test: cover wrapper methods on checkingStableReader
josephschorr 3c03cf8
test: cover strict replicated reader wrappers and validation
josephschorr 3fdc82d
test: cover relationship integrity proxy surface
josephschorr 059638e
test: cover schema v2 walk and flatten public wrappers
josephschorr af2fa8f
test: cover NewSchemaArrow and DatastoreIterator.ReplaceSubiterators
josephschorr e07f847
test: add coverage for pkg/datalayer
josephschorr b55c100
test: cover combined dispatcher option surface and upstream paths
josephschorr 47545b7
chore: lint fixes
josephschorr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,6 +285,301 @@ func TestWatchIntegrityFailureDueToInvalidHashSignature(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestNewRelationshipIntegrityProxyErrors(t *testing.T) { | ||
| validKey := DefaultKeyForTesting | ||
|
|
||
| expTime, err := time.Parse("2006-01-02", "2021-01-01") | ||
| require.NoError(t, err) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| current KeyConfig | ||
| expired []KeyConfig | ||
| errContains string | ||
| }{ | ||
| { | ||
| name: "empty current key bytes", | ||
| current: KeyConfig{ID: "k1", Bytes: nil}, | ||
| errContains: "current key file cannot be empty", | ||
| }, | ||
| { | ||
| name: "empty current key ID", | ||
| current: KeyConfig{ID: "", Bytes: validKey.Bytes}, | ||
| errContains: "current key ID cannot be empty", | ||
| }, | ||
| { | ||
| name: "expired key empty bytes", | ||
| current: validKey, | ||
| expired: []KeyConfig{ | ||
| {ID: "exp", Bytes: nil, ExpiredAt: &expTime}, | ||
| }, | ||
| errContains: "expired key cannot be empty", | ||
| }, | ||
| { | ||
| name: "expired key empty ID", | ||
| current: validKey, | ||
| expired: []KeyConfig{ | ||
| {ID: "", Bytes: validKey.Bytes, ExpiredAt: &expTime}, | ||
| }, | ||
| errContains: "expired key ID cannot be empty", | ||
| }, | ||
| { | ||
| name: "expired key missing expiration", | ||
| current: validKey, | ||
| expired: []KeyConfig{ | ||
| {ID: "exp", Bytes: validKey.Bytes, ExpiredAt: nil}, | ||
| }, | ||
| errContains: "expired key missing expiration time", | ||
| }, | ||
| { | ||
| name: "duplicate key ID", | ||
| current: validKey, | ||
| expired: []KeyConfig{ | ||
| {ID: validKey.ID, Bytes: validKey.Bytes, ExpiredAt: &expTime}, | ||
| }, | ||
| errContains: "found duplicate key ID", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = NewRelationshipIntegrityProxy(ds, tc.current, tc.expired) | ||
| require.Error(t, err) | ||
| require.ErrorContains(t, err, tc.errContains) | ||
| }) | ||
| } | ||
|
|
||
| // "Current key has an expiration" panics via MustBugf rather than | ||
| // returning an error, so test it separately. | ||
| t.Run("current key with expiration panics", func(t *testing.T) { | ||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Panics(t, func() { | ||
| _, _ = NewRelationshipIntegrityProxy(ds, KeyConfig{ | ||
| ID: "k1", | ||
| Bytes: validKey.Bytes, | ||
| ExpiredAt: &expTime, | ||
| }, nil) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func TestRelationshipIntegrityProxyPassThroughs(t *testing.T) { | ||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| ctx := t.Context() | ||
|
|
||
| metricsID, err := pds.MetricsID() | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, metricsID) | ||
|
|
||
| uniqueID, err := pds.UniqueID(ctx) | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, uniqueID) | ||
|
|
||
| features, err := pds.Features(ctx) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, features) | ||
|
|
||
| offline, err := pds.OfflineFeatures() | ||
| require.NoError(t, err) | ||
| require.NotNil(t, offline) | ||
|
|
||
| headRev, err := pds.HeadRevision(ctx) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, headRev) | ||
|
|
||
| require.NoError(t, pds.CheckRevision(ctx, headRev)) | ||
|
|
||
| optRev, err := pds.OptimizedRevision(ctx) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, optRev) | ||
|
|
||
| readyState, err := pds.ReadyState(ctx) | ||
| require.NoError(t, err) | ||
| require.True(t, readyState.IsReady) | ||
|
|
||
| roundTripped, err := pds.RevisionFromString(headRev.String()) | ||
| require.NoError(t, err) | ||
| require.True(t, roundTripped.Equal(headRev)) | ||
|
|
||
| _, err = pds.Statistics(ctx) | ||
| require.NoError(t, err) | ||
|
|
||
| unwrapper, ok := pds.(datastore.UnwrappableDatastore) | ||
| require.True(t, ok) | ||
| require.Equal(t, ds, unwrapper.Unwrap()) | ||
|
|
||
| require.NoError(t, pds.Close()) | ||
| } | ||
|
|
||
| func TestRelationshipIntegrityReaderPassThroughs(t *testing.T) { | ||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| headRev, err := pds.HeadRevision(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| reader := pds.SnapshotReader(headRev) | ||
|
|
||
| // Each of these simply delegates to the wrapped reader; memdb returns | ||
| // empty/zero results at head on a fresh database. | ||
| _, err = reader.CountRelationships(t.Context(), "nonexistent") | ||
| require.Error(t, err) // memdb errors on unknown counter | ||
|
|
||
| caveats, err := reader.LegacyListAllCaveats(t.Context()) | ||
| require.NoError(t, err) | ||
| require.Empty(t, caveats) | ||
|
|
||
| namespaces, err := reader.LegacyListAllNamespaces(t.Context()) | ||
| require.NoError(t, err) | ||
| require.Empty(t, namespaces) | ||
|
|
||
| lookupC, err := reader.LegacyLookupCaveatsWithNames(t.Context(), []string{"missing"}) | ||
| require.NoError(t, err) | ||
| require.Empty(t, lookupC) | ||
|
|
||
| counters, err := reader.LookupCounters(t.Context()) | ||
| require.NoError(t, err) | ||
| require.Empty(t, counters) | ||
|
|
||
| lookupN, err := reader.LegacyLookupNamespacesWithNames(t.Context(), []string{"missing"}) | ||
| require.NoError(t, err) | ||
| require.Empty(t, lookupN) | ||
|
|
||
| _, _, err = reader.LegacyReadCaveatByName(t.Context(), "missing") | ||
| require.Error(t, err) | ||
|
|
||
| _, _, err = reader.LegacyReadNamespaceByName(t.Context(), "missing") | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestRelationshipIntegrityReverseQueryValidatesHash(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we already have a test for this scenario: |
||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| // Write a valid relationship through the proxy. | ||
| _, err = pds.ReadWriteTx(t.Context(), func(ctx context.Context, tx datastore.ReadWriteTransaction) error { | ||
| return tx.WriteRelationships(t.Context(), []tuple.RelationshipUpdate{ | ||
| tuple.Create(tuple.MustParse("resource:foo#viewer@user:tom")), | ||
| }) | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Bypass the proxy to insert a relationship with an invalid hash. | ||
| _, err = ds.ReadWriteTx(t.Context(), func(ctx context.Context, tx datastore.ReadWriteTransaction) error { | ||
| invalid := tuple.MustParse("resource:foo#viewer@user:fred") | ||
| invalid.OptionalIntegrity = &core.RelationshipIntegrity{ | ||
| KeyId: "defaultfortest", | ||
| Hash: append([]byte{0x01}, []byte("someinvalidhashaasd")[0:hashLength]...), | ||
| HashedAt: timestamppb.Now(), | ||
| } | ||
| return tx.WriteRelationships(t.Context(), []tuple.RelationshipUpdate{ | ||
| tuple.Create(invalid), | ||
| }) | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| headRev, err := pds.HeadRevision(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| reader := pds.SnapshotReader(headRev) | ||
| iter, err := reader.ReverseQueryRelationships(t.Context(), datastore.SubjectsFilter{ | ||
| SubjectType: "user", | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = datastore.IteratorToSlice(iter) | ||
| require.Error(t, err) | ||
| require.ErrorContains(t, err, "has invalid integrity hash") | ||
| } | ||
|
|
||
| // stubBulkSource is a trivial BulkWriteRelationshipSource used to verify | ||
| // that the integrity proxy decorates the iterator correctly. | ||
| type stubBulkSource struct { | ||
| rels []tuple.Relationship | ||
| idx int | ||
| } | ||
|
|
||
| func (s *stubBulkSource) Next(_ context.Context) (*tuple.Relationship, error) { | ||
| if s.idx >= len(s.rels) { | ||
| return nil, nil | ||
| } | ||
| rel := s.rels[s.idx] | ||
| s.idx++ | ||
| return &rel, nil | ||
| } | ||
|
|
||
| func TestRelationshipIntegrityBulkLoad(t *testing.T) { | ||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| src := &stubBulkSource{rels: []tuple.Relationship{ | ||
| tuple.MustParse("resource:foo#viewer@user:tom"), | ||
| tuple.MustParse("resource:foo#viewer@user:fred"), | ||
| }} | ||
|
|
||
| _, err = pds.ReadWriteTx(t.Context(), func(ctx context.Context, tx datastore.ReadWriteTransaction) error { | ||
| loaded, err := tx.BulkLoad(ctx, src) | ||
| require.NoError(t, err) | ||
| require.Equal(t, uint64(2), loaded) | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // Integrity metadata should have been added and verifiable on readback. | ||
| headRev, err := pds.HeadRevision(t.Context()) | ||
| require.NoError(t, err) | ||
|
|
||
| iter, err := pds.SnapshotReader(headRev).QueryRelationships( | ||
| t.Context(), | ||
| datastore.RelationshipsFilter{OptionalResourceType: "resource"}, | ||
| ) | ||
| require.NoError(t, err) | ||
| rels, err := datastore.IteratorToSlice(iter) | ||
| require.NoError(t, err) | ||
| require.Len(t, rels, 2) | ||
| } | ||
|
|
||
| func TestRelationshipIntegrityBulkLoadRejectsPrehashed(t *testing.T) { | ||
| ds, err := dsfortesting.NewMemDBDatastoreForTesting(t, 0, 5*time.Second, 1*time.Hour) | ||
| require.NoError(t, err) | ||
|
|
||
| pds, err := NewRelationshipIntegrityProxy(ds, DefaultKeyForTesting, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| prehashed := tuple.MustParse("resource:foo#viewer@user:tom") | ||
| prehashed.OptionalIntegrity = &core.RelationshipIntegrity{KeyId: "other"} | ||
|
|
||
| src := &stubBulkSource{rels: []tuple.Relationship{prehashed}} | ||
|
|
||
| // spiceerrors.MustBugf panics; BulkLoad propagates that through the iterator's | ||
| // Next call. | ||
| require.Panics(t, func() { | ||
| _, _ = pds.ReadWriteTx(t.Context(), func(ctx context.Context, tx datastore.ReadWriteTransaction) error { | ||
| _, _ = tx.BulkLoad(ctx, src) | ||
| return nil | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func BenchmarkQueryRelsWithIntegrity(b *testing.B) { | ||
| for _, withIntegrity := range []bool{true, false} { | ||
| b.Run(fmt.Sprintf("withIntegrity=%t", withIntegrity), func(b *testing.B) { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a godoc in the prod code that says
is it possible to encode this knowledge in a test?
Similarly, for
NewStrictReplicatedDatastore:can we encode that in a test?
(see #2525 (comment) for more context)