From 948529a600f7c57516dbdbf28d9f7359e672b4d0 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Mon, 26 Apr 2021 12:35:44 +0300 Subject: [PATCH] satellite/metabase/metabasetest: move test utils to a separate package Change-Id: Iaa607996478497d5c97c6a6973d099b315b3abf0 --- satellite/metabase/alias_test.go | 29 +- satellite/metabase/aliascache_test.go | 7 +- satellite/metabase/bench_expired_test.go | 5 +- satellite/metabase/bench_test.go | 5 +- satellite/metabase/commit_object_test.go | 177 ++--- satellite/metabase/commit_test.go | 687 ++++++++---------- satellite/metabase/db_migrate_test.go | 9 +- satellite/metabase/delete_bucket_test.go | 75 +- satellite/metabase/delete_expired_test.go | 61 +- satellite/metabase/delete_test.go | 590 ++++++--------- satellite/metabase/get_test.go | 289 ++++---- satellite/metabase/iterator_test.go | 315 ++++---- satellite/metabase/list_segments_test.go | 143 ++-- satellite/metabase/loop_test.go | 131 ++-- satellite/metabase/metabasetest/common.go | 76 ++ satellite/metabase/metabasetest/create.go | 255 +++++++ satellite/metabase/metabasetest/defaults.go | 22 + satellite/metabase/metabasetest/invalid.go | 166 +++++ .../{db_test.go => metabasetest/run.go} | 58 +- satellite/metabase/metabasetest/run_test.go | 25 + .../{test_test.go => metabasetest/test.go} | 289 ++------ satellite/metabase/streamstat_test.go | 43 +- satellite/metabase/update_test.go | 103 +-- 23 files changed, 1858 insertions(+), 1702 deletions(-) create mode 100644 satellite/metabase/metabasetest/common.go create mode 100644 satellite/metabase/metabasetest/create.go create mode 100644 satellite/metabase/metabasetest/defaults.go create mode 100644 satellite/metabase/metabasetest/invalid.go rename satellite/metabase/{db_test.go => metabasetest/run.go} (59%) create mode 100644 satellite/metabase/metabasetest/run_test.go rename satellite/metabase/{test_test.go => metabasetest/test.go} (73%) diff --git a/satellite/metabase/alias_test.go b/satellite/metabase/alias_test.go index 91973c65f..47b0fdfbd 100644 --- a/satellite/metabase/alias_test.go +++ b/satellite/metabase/alias_test.go @@ -13,18 +13,19 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestNodeAliases(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { t.Run("Zero", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) nodes := []storj.NodeID{ testrand.NodeID(), {}, } - EnsureNodeAliases{ + metabasetest.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{ Nodes: nodes, }, @@ -34,14 +35,14 @@ func TestNodeAliases(t *testing.T) { }) t.Run("Empty", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - aliasesAfter := ListNodeAliases{}.Check(ctx, t, db) + aliasesAfter := metabasetest.ListNodeAliases{}.Check(ctx, t, db) require.Len(t, aliasesAfter, 0) }) t.Run("Valid", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) nodes := []storj.NodeID{ testrand.NodeID(), @@ -50,19 +51,19 @@ func TestNodeAliases(t *testing.T) { } nodes = append(nodes, nodes...) // add duplicates to our slice - EnsureNodeAliases{ + metabasetest.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{ Nodes: nodes, }, }.Check(ctx, t, db) - EnsureNodeAliases{ + metabasetest.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{ Nodes: nodes, }, }.Check(ctx, t, db) - aliases := ListNodeAliases{}.Check(ctx, t, db) + aliases := metabasetest.ListNodeAliases{}.Check(ctx, t, db) require.Len(t, aliases, 3) for _, entry := range aliases { @@ -70,18 +71,18 @@ func TestNodeAliases(t *testing.T) { require.LessOrEqual(t, int(entry.Alias), 3) } - EnsureNodeAliases{ + metabasetest.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{ Nodes: []storj.NodeID{testrand.NodeID()}, }, }.Check(ctx, t, db) - aliasesAfter := ListNodeAliases{}.Check(ctx, t, db) + aliasesAfter := metabasetest.ListNodeAliases{}.Check(ctx, t, db) require.Len(t, aliasesAfter, 4) }) t.Run("Concurrent", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) nodes := make([]storj.NodeID, 128) for i := range nodes { @@ -99,7 +100,7 @@ func TestNodeAliases(t *testing.T) { } require.NoError(t, group.Wait()) - aliases := ListNodeAliases{}.Check(ctx, t, db) + aliases := metabasetest.ListNodeAliases{}.Check(ctx, t, db) seen := map[metabase.NodeAlias]bool{} require.Len(t, aliases, len(nodes)) for _, entry := range aliases { @@ -112,7 +113,7 @@ func TestNodeAliases(t *testing.T) { }) t.Run("Stress Concurrent", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) nodes := make([]storj.NodeID, 128) for i := range nodes { diff --git a/satellite/metabase/aliascache_test.go b/satellite/metabase/aliascache_test.go index 0ad20cb58..2316df33a 100644 --- a/satellite/metabase/aliascache_test.go +++ b/satellite/metabase/aliascache_test.go @@ -18,6 +18,7 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestNodeAliasCache(t *testing.T) { @@ -138,9 +139,9 @@ func TestNodeAliasCache(t *testing.T) { } func TestNodeAliasCache_DB(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { t.Run("missing aliases", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) cache := metabase.NewNodeAliasCache(db) nodes, err := cache.Nodes(ctx, []metabase.NodeAlias{1, 2, 3}) @@ -149,7 +150,7 @@ func TestNodeAliasCache_DB(t *testing.T) { }) t.Run("auto add nodes", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) cache := metabase.NewNodeAliasCache(db) diff --git a/satellite/metabase/bench_expired_test.go b/satellite/metabase/bench_expired_test.go index dd64ad60a..2a2b0d7fb 100644 --- a/satellite/metabase/bench_expired_test.go +++ b/satellite/metabase/bench_expired_test.go @@ -14,6 +14,7 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) var letters = []rune("abcdefghijklmnopqrstuvwxyz") @@ -84,7 +85,7 @@ func (g *expirationDateGenerator) getDeadline() time.Time { // Run runs the scenario as a subtest. func (s expiredScenario) Run(b *testing.B) { - b.Run(s.name(), func(b *testing.B) { Bench(b, s.run) }) + b.Run(s.name(), func(b *testing.B) { metabasetest.Bench(b, s.run) }) } // name returns the scenario arguments as a string. @@ -119,7 +120,7 @@ func (s *expiredScenario) run(ctx *testcontext.Context, b *testing.B, db *metaba b.Run("Delete expired objects", func(b *testing.B) { for i := 0; i < b.N; i++ { // wipe data so we can do the exact same test - DeleteAll{}.Check(ctx, b, db) + metabasetest.DeleteAll{}.Check(ctx, b, db) s.objectStream = nil var expiredGenerator expirationDateGenerator diff --git a/satellite/metabase/bench_test.go b/satellite/metabase/bench_test.go index c0d89f9b0..aec341943 100644 --- a/satellite/metabase/bench_test.go +++ b/satellite/metabase/bench_test.go @@ -18,6 +18,7 @@ import ( "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func Benchmark(b *testing.B) { @@ -42,7 +43,7 @@ type scenario struct { // Run runs the scenario as a subtest. func (s scenario) Run(b *testing.B) { - b.Run(s.name(), func(b *testing.B) { Bench(b, s.run) }) + b.Run(s.name(), func(b *testing.B) { metabasetest.Bench(b, s.run) }) } // name returns the scenario arguments as a string. @@ -98,7 +99,7 @@ func (s *scenario) run(ctx *testcontext.Context, b *testing.B, db *metabase.DB) for i := 0; i < b.N; i++ { // wipe data so we can do the exact same test b.StopTimer() - DeleteAll{}.Check(ctx, b, db) + metabasetest.DeleteAll{}.Check(ctx, b, db) if err := db.EnsureNodeAliases(ctx, metabase.EnsureNodeAliases{Nodes: nodes}); err != nil { require.NoError(b, err) } diff --git a/satellite/metabase/commit_object_test.go b/satellite/metabase/commit_object_test.go index 9ba7f1d4b..832d8bc90 100644 --- a/satellite/metabase/commit_object_test.go +++ b/satellite/metabase/commit_object_test.go @@ -11,35 +11,36 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestCommitObjectWithSegments(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - CommitObjectWithSegments{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: test.ObjectStream, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("invalid order", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) pos00 := metabase.SegmentPosition{Part: 0, Index: 0} pos01 := metabase.SegmentPosition{Part: 0, Index: 1} pos10 := metabase.SegmentPosition{Part: 1, Index: 0} - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -51,7 +52,7 @@ func TestCommitObjectWithSegments(t *testing.T) { ErrText: "segments not in ascending order, got {0 1} before {0 0}", }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -63,7 +64,7 @@ func TestCommitObjectWithSegments(t *testing.T) { ErrText: "segments not in ascending order, got {1 0} before {0 0}", }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -75,13 +76,13 @@ func TestCommitObjectWithSegments(t *testing.T) { ErrText: "segments not in ascending order, got {0 0} before {0 0}", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("version without pending", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -94,13 +95,13 @@ func TestCommitObjectWithSegments(t *testing.T) { ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -109,7 +110,7 @@ func TestCommitObjectWithSegments(t *testing.T) { Version: 5, StreamID: obj.StreamID, }, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 5, }.Check(ctx, t, db) @@ -119,7 +120,7 @@ func TestCommitObjectWithSegments(t *testing.T) { encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -135,7 +136,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }.Check(ctx, t, db) // disallow for double commit - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -149,7 +150,7 @@ func TestCommitObjectWithSegments(t *testing.T) { ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -166,26 +167,26 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedMetadata: encryptedMetadata, EncryptedMetadataEncryptedKey: encryptedMetadataKey, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("segments missing in database", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) now := time.Now() pos00 := metabase.SegmentPosition{Part: 0, Index: 0} - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -196,26 +197,26 @@ func TestCommitObjectWithSegments(t *testing.T) { ErrText: "segments and database does not match: {0 0}: segment not committed", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("delete segments that are not in proofs", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -233,7 +234,7 @@ func TestCommitObjectWithSegments(t *testing.T) { encryptedKey01 := testrand.Bytes(32) encryptedKeyNonce01 := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -247,11 +248,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -265,11 +266,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -281,7 +282,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -293,7 +294,7 @@ func TestCommitObjectWithSegments(t *testing.T) { TotalEncryptedSize: 1024, FixedSegmentSize: -1, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -310,7 +311,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 0, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces01, }, @@ -319,12 +320,12 @@ func TestCommitObjectWithSegments(t *testing.T) { }) t.Run("delete inline segments that are not in proofs", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -340,7 +341,7 @@ func TestCommitObjectWithSegments(t *testing.T) { encryptedKey01 := testrand.Bytes(32) encryptedKeyNonce01 := testrand.Bytes(32) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: pos00, @@ -355,7 +356,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: pos01, @@ -370,7 +371,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }, }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -380,7 +381,7 @@ func TestCommitObjectWithSegments(t *testing.T) { Deleted: nil, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -392,7 +393,7 @@ func TestCommitObjectWithSegments(t *testing.T) { TotalEncryptedSize: 1024, FixedSegmentSize: -1, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -415,12 +416,12 @@ func TestCommitObjectWithSegments(t *testing.T) { }) t.Run("updated plain offset", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -438,7 +439,7 @@ func TestCommitObjectWithSegments(t *testing.T) { encryptedKey01 := testrand.Bytes(32) encryptedKeyNonce01 := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -452,11 +453,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -470,11 +471,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -484,7 +485,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -496,7 +497,7 @@ func TestCommitObjectWithSegments(t *testing.T) { TotalEncryptedSize: 2048, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -513,7 +514,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 0, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces00, }, @@ -530,7 +531,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 512, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces01, }, @@ -539,12 +540,12 @@ func TestCommitObjectWithSegments(t *testing.T) { }) t.Run("fixed segment size", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -562,7 +563,7 @@ func TestCommitObjectWithSegments(t *testing.T) { encryptedKey10 := testrand.Bytes(32) encryptedKeyNonce10 := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -576,11 +577,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -594,11 +595,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -608,7 +609,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -620,7 +621,7 @@ func TestCommitObjectWithSegments(t *testing.T) { TotalEncryptedSize: 2048, FixedSegmentSize: -1, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -637,7 +638,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 0, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces00, }, @@ -654,7 +655,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 512, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces10, }, @@ -663,12 +664,12 @@ func TestCommitObjectWithSegments(t *testing.T) { }) t.Run("skipped fixed segment size", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -686,7 +687,7 @@ func TestCommitObjectWithSegments(t *testing.T) { encryptedKey02 := testrand.Bytes(32) encryptedKeyNonce02 := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -700,11 +701,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, @@ -718,11 +719,11 @@ func TestCommitObjectWithSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, Segments: []metabase.SegmentPosition{ @@ -732,7 +733,7 @@ func TestCommitObjectWithSegments(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -744,7 +745,7 @@ func TestCommitObjectWithSegments(t *testing.T) { TotalEncryptedSize: 2048, FixedSegmentSize: -1, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -761,7 +762,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 0, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces00, }, @@ -778,7 +779,7 @@ func TestCommitObjectWithSegments(t *testing.T) { PlainOffset: 512, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces02, }, @@ -787,31 +788,31 @@ func TestCommitObjectWithSegments(t *testing.T) { }) t.Run("no segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) now := time.Now() - CommitObjectWithSegments{ + metabasetest.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{ ObjectStream: obj, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) diff --git a/satellite/metabase/commit_test.go b/satellite/metabase/commit_test.go index 37efffade..009dd1bbb 100644 --- a/satellite/metabase/commit_test.go +++ b/satellite/metabase/commit_test.go @@ -8,120 +8,31 @@ import ( "testing" "time" - "github.com/zeebo/errs" - "storj.io/common/storj" "storj.io/common/testcontext" "storj.io/common/testrand" - "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) -var defaultTestRedundancy = storj.RedundancyScheme{ - Algorithm: storj.ReedSolomon, - ShareSize: 2048, - RequiredShares: 1, - RepairShares: 1, - OptimalShares: 1, - TotalShares: 1, -} - -var defaultTestEncryption = storj.EncryptionParameters{ - CipherSuite: storj.EncAESGCM, - BlockSize: 29 * 256, -} - -func randObjectStream() metabase.ObjectStream { - return metabase.ObjectStream{ - ProjectID: testrand.UUID(), - BucketName: testrand.BucketName(), - ObjectKey: metabase.ObjectKey(testrand.Bytes(16)), - Version: 1, - StreamID: testrand.UUID(), - } -} - -type invalidObjectStream struct { - Name string - ObjectStream metabase.ObjectStream - ErrClass *errs.Class - ErrText string -} - -func invalidObjectStreams(base metabase.ObjectStream) []invalidObjectStream { - var tests []invalidObjectStream - { - stream := base - stream.ProjectID = uuid.UUID{} - tests = append(tests, invalidObjectStream{ - Name: "ProjectID missing", - ObjectStream: stream, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ProjectID missing", - }) - } - { - stream := base - stream.BucketName = "" - tests = append(tests, invalidObjectStream{ - Name: "BucketName missing", - ObjectStream: stream, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "BucketName missing", - }) - } - { - stream := base - stream.ObjectKey = "" - tests = append(tests, invalidObjectStream{ - Name: "ObjectKey missing", - ObjectStream: stream, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ObjectKey missing", - }) - } - { - stream := base - stream.Version = -1 - tests = append(tests, invalidObjectStream{ - Name: "Version invalid", - ObjectStream: stream, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "Version invalid: -1", - }) - } - { - stream := base - stream.StreamID = uuid.UUID{} - tests = append(tests, invalidObjectStream{ - Name: "StreamID missing", - ObjectStream: stream, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "StreamID missing", - }) - } - - return tests -} - func TestBeginObjectNextVersion(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - BeginObjectNextVersion{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: test.ObjectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: -1, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } @@ -133,14 +44,14 @@ func TestBeginObjectNextVersion(t *testing.T) { } t.Run("disallow exact version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) objectStream.Version = 5 - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: -1, ErrClass: &metabase.ErrInvalidRequest, @@ -149,31 +60,31 @@ func TestBeginObjectNextVersion(t *testing.T) { }) t.Run("NextVersion", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = metabase.NextVersion - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) now2 := time.Now() - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 2, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -186,7 +97,7 @@ func TestBeginObjectNextVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: metabase.ObjectStream{ @@ -199,7 +110,7 @@ func TestBeginObjectNextVersion(t *testing.T) { CreatedAt: now2, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -209,20 +120,20 @@ func TestBeginObjectNextVersion(t *testing.T) { // TODO: zombie deletion deadline t.Run("older committed version exists", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = metabase.NextVersion - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -235,15 +146,15 @@ func TestBeginObjectNextVersion(t *testing.T) { }.Check(ctx, t, db) now2 := time.Now() - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 2, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -255,7 +166,7 @@ func TestBeginObjectNextVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -268,7 +179,7 @@ func TestBeginObjectNextVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: metabase.ObjectStream{ @@ -281,37 +192,37 @@ func TestBeginObjectNextVersion(t *testing.T) { CreatedAt: now2, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("newer committed version exists", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = metabase.NextVersion - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) now2 := time.Now() - BeginObjectNextVersion{ + metabasetest.BeginObjectNextVersion{ Opts: metabase.BeginObjectNextVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 2, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -323,7 +234,7 @@ func TestBeginObjectNextVersion(t *testing.T) { }, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -335,7 +246,7 @@ func TestBeginObjectNextVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -348,7 +259,7 @@ func TestBeginObjectNextVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: metabase.ObjectStream{ @@ -361,7 +272,7 @@ func TestBeginObjectNextVersion(t *testing.T) { CreatedAt: now2, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -370,23 +281,23 @@ func TestBeginObjectNextVersion(t *testing.T) { } func TestBeginObjectExactVersion(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: test.ObjectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: -1, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } @@ -398,14 +309,14 @@ func TestBeginObjectExactVersion(t *testing.T) { } t.Run("disallow NextVersion", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) objectStream.Version = metabase.NextVersion - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: -1, ErrClass: &metabase.ErrInvalidRequest, @@ -414,21 +325,21 @@ func TestBeginObjectExactVersion(t *testing.T) { }) t.Run("Specific version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = 5 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 5, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -441,38 +352,38 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Duplicate pending version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = 5 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 5, }.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: -1, ErrClass: &metabase.ErrConflict, ErrText: "object already exists", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -485,44 +396,44 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Duplicate committed version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = 5 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 5, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: objectStream, }, }.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: -1, ErrClass: &metabase.ErrConflict, ErrText: "object already exists", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -535,7 +446,7 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -544,21 +455,21 @@ func TestBeginObjectExactVersion(t *testing.T) { // TODO: zombie deletion deadline t.Run("Older committed version exists", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = 1 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: objectStream, }, @@ -566,21 +477,21 @@ func TestBeginObjectExactVersion(t *testing.T) { objectStream.Version = 3 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 3, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: objectStream, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -593,7 +504,7 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: metabase.ObjectStream{ @@ -606,28 +517,28 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Newer committed version exists", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() objectStream.Version = 3 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 3, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: objectStream, }, @@ -635,21 +546,21 @@ func TestBeginObjectExactVersion(t *testing.T) { objectStream.Version = 1 - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: objectStream, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: objectStream, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -662,7 +573,7 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: metabase.ObjectStream{ @@ -675,7 +586,7 @@ func TestBeginObjectExactVersion(t *testing.T) { CreatedAt: now1, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -684,28 +595,28 @@ func TestBeginObjectExactVersion(t *testing.T) { } func TestBeginSegment(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: test.ObjectStream, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("RootPieceID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Pieces: []metabase.Piece{{ @@ -716,13 +627,13 @@ func TestBeginSegment(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "RootPieceID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Pieces missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, RootPieceID: storj.PieceID{1}, @@ -730,13 +641,13 @@ func TestBeginSegment(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "pieces missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("StorageNode in pieces missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Pieces: []metabase.Piece{{ @@ -747,13 +658,13 @@ func TestBeginSegment(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "piece number 1 is missing storage node id", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Piece number 2 is duplicated", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Pieces: []metabase.Piece{ @@ -770,13 +681,13 @@ func TestBeginSegment(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "duplicated piece number 1", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Pieces should be ordered", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Pieces: []metabase.Piece{ @@ -793,13 +704,13 @@ func TestBeginSegment(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "pieces should be ordered", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("pending object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, RootPieceID: storj.PieceID{1}, @@ -811,28 +722,28 @@ func TestBeginSegment(t *testing.T) { ErrClass: &metabase.Error, ErrText: "pending object missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("pending object missing when object committed", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, RootPieceID: storj.PieceID{1}, @@ -845,32 +756,32 @@ func TestBeginSegment(t *testing.T) { ErrText: "pending object missing", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("begin segment successfully", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, RootPieceID: storj.PieceID{1}, @@ -881,33 +792,33 @@ func TestBeginSegment(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("multiple begin segment successfully", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) for i := 0; i < 5; i++ { - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, RootPieceID: storj.PieceID{1}, @@ -919,14 +830,14 @@ func TestBeginSegment(t *testing.T) { }.Check(ctx, t, db) } - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -935,38 +846,38 @@ func TestBeginSegment(t *testing.T) { } func TestCommitSegment(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - CommitSegment{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: test.ObjectStream, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("invalid request", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Pieces: metabase.Pieces{{ @@ -978,7 +889,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "RootPieceID missing", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, }, @@ -986,7 +897,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "pieces missing", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Pieces: []metabase.Piece{{ @@ -998,7 +909,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "piece number 1 is missing storage node id", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Pieces: []metabase.Piece{ @@ -1016,7 +927,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "duplicated piece number 1", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Pieces: []metabase.Piece{ @@ -1034,7 +945,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "pieces should be ordered", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: testrand.PieceID(), @@ -1047,7 +958,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "EncryptedKey missing", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: testrand.PieceID(), @@ -1063,7 +974,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "EncryptedKeyNonce missing", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: testrand.PieceID(), @@ -1083,7 +994,7 @@ func TestCommitSegment(t *testing.T) { }.Check(ctx, t, db) if metabase.ValidatePlainSize { - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: testrand.PieceID(), @@ -1104,7 +1015,7 @@ func TestCommitSegment(t *testing.T) { }.Check(ctx, t, db) } - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: testrand.PieceID(), @@ -1125,7 +1036,7 @@ func TestCommitSegment(t *testing.T) { ErrText: "PlainOffset negative", }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: testrand.PieceID(), @@ -1150,7 +1061,7 @@ func TestCommitSegment(t *testing.T) { OptimalShares: 2, } - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Pieces: []metabase.Piece{ @@ -1172,27 +1083,27 @@ func TestCommitSegment(t *testing.T) { ErrText: "number of pieces is less than redundancy optimal shares value", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("duplicate", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -1202,7 +1113,7 @@ func TestCommitSegment(t *testing.T) { encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1211,7 +1122,7 @@ func TestCommitSegment(t *testing.T) { }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1224,11 +1135,11 @@ func TestCommitSegment(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1241,20 +1152,20 @@ func TestCommitSegment(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, ErrClass: &metabase.ErrConflict, ErrText: "segment already exists", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now1, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -1271,7 +1182,7 @@ func TestCommitSegment(t *testing.T) { PlainOffset: 0, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces, }, @@ -1280,14 +1191,14 @@ func TestCommitSegment(t *testing.T) { }) t.Run("commit segment of missing object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) rootPieceID := testrand.PieceID() pieces := metabase.Pieces{{Number: 0, StorageNode: testrand.NodeID()}} encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: rootPieceID, @@ -1299,17 +1210,17 @@ func TestCommitSegment(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, ErrClass: &metabase.Error, ErrText: "pending object missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("commit segment of committed object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) rootPieceID := testrand.PieceID() pieces := metabase.Pieces{{Number: 0, StorageNode: testrand.NodeID()}} @@ -1317,21 +1228,21 @@ func TestCommitSegment(t *testing.T) { encryptedKeyNonce := testrand.Bytes(32) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: rootPieceID, @@ -1343,27 +1254,27 @@ func TestCommitSegment(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, ErrClass: &metabase.Error, ErrText: "pending object missing", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("commit segment of pending object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) rootPieceID := testrand.PieceID() pieces := metabase.Pieces{{Number: 0, StorageNode: testrand.NodeID()}} @@ -1372,15 +1283,15 @@ func TestCommitSegment(t *testing.T) { encryptedETag := testrand.Bytes(32) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, RootPieceID: rootPieceID, @@ -1392,19 +1303,19 @@ func TestCommitSegment(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, EncryptedETag: encryptedETag, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -1421,7 +1332,7 @@ func TestCommitSegment(t *testing.T) { PlainSize: 512, EncryptedETag: encryptedETag, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces, }, @@ -1431,37 +1342,37 @@ func TestCommitSegment(t *testing.T) { } func TestCommitInlineSegment(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - CommitInlineSegment{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: test.ObjectStream, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("invalid request", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, }, @@ -1469,7 +1380,7 @@ func TestCommitInlineSegment(t *testing.T) { ErrText: "EncryptedKey missing", }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, InlineData: []byte{1, 2, 3}, @@ -1478,7 +1389,7 @@ func TestCommitInlineSegment(t *testing.T) { ErrText: "EncryptedKey missing", }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, @@ -1491,7 +1402,7 @@ func TestCommitInlineSegment(t *testing.T) { }.Check(ctx, t, db) if metabase.ValidatePlainSize { - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, @@ -1507,7 +1418,7 @@ func TestCommitInlineSegment(t *testing.T) { }.Check(ctx, t, db) } - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, @@ -1525,13 +1436,13 @@ func TestCommitInlineSegment(t *testing.T) { }) t.Run("duplicate", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now1 := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -1539,7 +1450,7 @@ func TestCommitInlineSegment(t *testing.T) { encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1553,7 +1464,7 @@ func TestCommitInlineSegment(t *testing.T) { }, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1569,14 +1480,14 @@ func TestCommitInlineSegment(t *testing.T) { ErrText: "segment already exists", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now1, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -1599,12 +1510,12 @@ func TestCommitInlineSegment(t *testing.T) { }) t.Run("commit inline segment of missing object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, InlineData: []byte{1, 2, 3}, @@ -1619,19 +1530,19 @@ func TestCommitInlineSegment(t *testing.T) { ErrText: "pending object missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("commit segment of committed object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) now := time.Now() - createObject(ctx, t, db, obj, 0) - CommitInlineSegment{ + metabasetest.CreateObject(ctx, t, db, obj, 0) + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, InlineData: []byte{1, 2, 3}, @@ -1646,35 +1557,35 @@ func TestCommitInlineSegment(t *testing.T) { ErrText: "pending object missing", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("commit empty segment of pending object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) encryptedETag := testrand.Bytes(32) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, @@ -1687,13 +1598,13 @@ func TestCommitInlineSegment(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -1715,22 +1626,22 @@ func TestCommitInlineSegment(t *testing.T) { }) t.Run("commit segment of pending object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) encryptedETag := testrand.Bytes(32) now := time.Now() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, InlineData: []byte{1, 2, 3}, @@ -1744,13 +1655,13 @@ func TestCommitInlineSegment(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -1775,28 +1686,28 @@ func TestCommitInlineSegment(t *testing.T) { } func TestCommitObject(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - CommitObject{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: test.ObjectStream, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("version without pending", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -1809,13 +1720,13 @@ func TestCommitObject(t *testing.T) { ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -1824,7 +1735,7 @@ func TestCommitObject(t *testing.T) { Version: 5, StreamID: obj.StreamID, }, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 5, }.Check(ctx, t, db) @@ -1834,7 +1745,7 @@ func TestCommitObject(t *testing.T) { encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -1850,7 +1761,7 @@ func TestCommitObject(t *testing.T) { }.Check(ctx, t, db) // disallow for double commit - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -1864,7 +1775,7 @@ func TestCommitObject(t *testing.T) { ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: metabase.ObjectStream{ @@ -1881,19 +1792,19 @@ func TestCommitObject(t *testing.T) { EncryptedMetadata: encryptedMetadata, EncryptedMetadataEncryptedKey: encryptedMetadataKey, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("assign plain_offset", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -1904,7 +1815,7 @@ func TestCommitObject(t *testing.T) { encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Index: 0}, @@ -1918,11 +1829,11 @@ func TestCommitObject(t *testing.T) { PlainSize: 512, PlainOffset: 999999, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Index: 1}, @@ -1936,17 +1847,17 @@ func TestCommitObject(t *testing.T) { PlainSize: 512, PlainOffset: 999999, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Segments: []metabase.RawSegment{ { StreamID: obj.StreamID, @@ -1961,7 +1872,7 @@ func TestCommitObject(t *testing.T) { PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces, }, @@ -1978,7 +1889,7 @@ func TestCommitObject(t *testing.T) { PlainSize: 512, PlainOffset: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces, }, @@ -1994,19 +1905,19 @@ func TestCommitObject(t *testing.T) { TotalPlainSize: 2 * 512, TotalEncryptedSize: 2 * 1024, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("large object over 2 GB", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -2017,7 +1928,7 @@ func TestCommitObject(t *testing.T) { encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Index: 0}, @@ -2029,11 +1940,11 @@ func TestCommitObject(t *testing.T) { EncryptedSize: math.MaxInt32, PlainSize: math.MaxInt32, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Index: 1}, @@ -2045,17 +1956,17 @@ func TestCommitObject(t *testing.T) { EncryptedSize: math.MaxInt32, PlainSize: math.MaxInt32, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Segments: []metabase.RawSegment{ { StreamID: obj.StreamID, @@ -2069,7 +1980,7 @@ func TestCommitObject(t *testing.T) { EncryptedSize: math.MaxInt32, PlainSize: math.MaxInt32, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces, }, @@ -2086,7 +1997,7 @@ func TestCommitObject(t *testing.T) { PlainSize: math.MaxInt32, PlainOffset: math.MaxInt32, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: pieces, }, @@ -2102,16 +2013,16 @@ func TestCommitObject(t *testing.T) { TotalPlainSize: 2 * math.MaxInt32, TotalEncryptedSize: 2 * math.MaxInt32, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("commit with encryption", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, }, @@ -2120,7 +2031,7 @@ func TestCommitObject(t *testing.T) { now := time.Now() - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, Encryption: storj.EncryptionParameters{}, @@ -2129,7 +2040,7 @@ func TestCommitObject(t *testing.T) { ErrText: "Encryption is missing", }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, Encryption: storj.EncryptionParameters{ @@ -2140,7 +2051,7 @@ func TestCommitObject(t *testing.T) { ErrText: "Encryption.BlockSize is negative or zero", }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, Encryption: storj.EncryptionParameters{ @@ -2152,7 +2063,7 @@ func TestCommitObject(t *testing.T) { ErrText: "Encryption.BlockSize is negative or zero", }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, Encryption: storj.EncryptionParameters{ @@ -2162,7 +2073,7 @@ func TestCommitObject(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -2181,19 +2092,19 @@ func TestCommitObject(t *testing.T) { }) t.Run("commit with encryption (no override)", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) now := time.Now() - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, // set different encryption than with BeginObjectExactVersion @@ -2204,7 +2115,7 @@ func TestCommitObject(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -2212,7 +2123,7 @@ func TestCommitObject(t *testing.T) { Status: metabase.Committed, SegmentCount: 0, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -2221,29 +2132,29 @@ func TestCommitObject(t *testing.T) { } func TestUpdateObjectMetadata(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() - for _, test := range invalidObjectStreams(obj) { + for _, test := range metabasetest.InvalidObjectStreams(obj) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - UpdateObjectMetadata{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.UpdateObjectMetadata{ Opts: metabase.UpdateObjectMetadata{ ObjectStream: test.ObjectStream, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Version invalid", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateObjectMetadata{ + metabasetest.UpdateObjectMetadata{ Opts: metabase.UpdateObjectMetadata{ ObjectStream: metabase.ObjectStream{ ProjectID: obj.ProjectID, @@ -2256,43 +2167,43 @@ func TestUpdateObjectMetadata(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "Version invalid: 0", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateObjectMetadata{ + metabasetest.UpdateObjectMetadata{ Opts: metabase.UpdateObjectMetadata{ ObjectStream: obj, }, ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: object with specified version and committed status is missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Update metadata", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - CreateTestObject{}.Run(ctx, t, db, obj, 0) + metabasetest.CreateTestObject{}.Run(ctx, t, db, obj, 0) encryptedMetadata := testrand.Bytes(1024) encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) - UpdateObjectMetadata{ + metabasetest.UpdateObjectMetadata{ Opts: metabase.UpdateObjectMetadata{ ObjectStream: obj, EncryptedMetadata: encryptedMetadata, @@ -2301,13 +2212,13 @@ func TestUpdateObjectMetadata(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, EncryptedMetadata: encryptedMetadata, EncryptedMetadataNonce: encryptedMetadataNonce[:], diff --git a/satellite/metabase/db_migrate_test.go b/satellite/metabase/db_migrate_test.go index 1a3b13ada..430c23653 100644 --- a/satellite/metabase/db_migrate_test.go +++ b/satellite/metabase/db_migrate_test.go @@ -14,21 +14,22 @@ import ( "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" "storj.io/storj/satellite/satellitedb/satellitedbtest" ) func TestMigrateToAliases(t *testing.T) { - for _, info := range databaseInfos() { + for _, info := range metabasetest.DatabaseEntries() { info := info - t.Run(info.name, func(t *testing.T) { + t.Run(info.Name, func(t *testing.T) { t.Parallel() ctx := testcontext.New(t) defer ctx.Cleanup() db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{ - Name: info.name, - URL: info.connstr, + Name: info.Name, + URL: info.ConnStr, Message: "", }) require.NoError(t, err) diff --git a/satellite/metabase/delete_bucket_test.go b/satellite/metabase/delete_bucket_test.go index 392aef759..44bd84f69 100644 --- a/satellite/metabase/delete_bucket_test.go +++ b/satellite/metabase/delete_bucket_test.go @@ -13,15 +13,16 @@ import ( "storj.io/common/testcontext" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestDeleteBucketObjects(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj1 := randObjectStream() - obj2 := randObjectStream() - obj3 := randObjectStream() - objX := randObjectStream() - objY := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj1 := metabasetest.RandObjectStream() + obj2 := metabasetest.RandObjectStream() + obj3 := metabasetest.RandObjectStream() + objX := metabasetest.RandObjectStream() + objY := metabasetest.RandObjectStream() obj2.ProjectID, obj2.BucketName = obj1.ProjectID, obj1.BucketName obj3.ProjectID, obj3.BucketName = obj1.ProjectID, obj1.BucketName @@ -29,9 +30,9 @@ func TestDeleteBucketObjects(t *testing.T) { objY.BucketName = obj1.BucketName t.Run("invalid options", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: metabase.BucketLocation{ ProjectID: uuid.UUID{}, @@ -42,7 +43,7 @@ func TestDeleteBucketObjects(t *testing.T) { ErrText: "ProjectID missing", }.Check(ctx, t, db) - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: metabase.BucketLocation{ ProjectID: uuid.UUID{1}, @@ -53,13 +54,13 @@ func TestDeleteBucketObjects(t *testing.T) { ErrText: "BucketName missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("empty bucket", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: obj1.Location().Bucket(), DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error { @@ -69,15 +70,15 @@ func TestDeleteBucketObjects(t *testing.T) { Deleted: 0, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("one object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj1, 2) + metabasetest.CreateObject(ctx, t, db, obj1, 2) - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: obj1.Location().Bucket(), DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error { @@ -95,15 +96,15 @@ func TestDeleteBucketObjects(t *testing.T) { Deleted: 1, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("empty object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj1, 0) + metabasetest.CreateObject(ctx, t, db, obj1, 0) - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: obj1.Location().Bucket(), DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error { @@ -115,17 +116,17 @@ func TestDeleteBucketObjects(t *testing.T) { Deleted: 0, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("three objects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj1, 2) - createObject(ctx, t, db, obj2, 2) - createObject(ctx, t, db, obj3, 2) + metabasetest.CreateObject(ctx, t, db, obj1, 2) + metabasetest.CreateObject(ctx, t, db, obj2, 2) + metabasetest.CreateObject(ctx, t, db, obj3, 2) - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: obj1.Location().Bucket(), BatchSize: 2, @@ -144,25 +145,25 @@ func TestDeleteBucketObjects(t *testing.T) { Deleted: 3, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("don't delete non-exact match", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj1, 1) - createObject(ctx, t, db, objX, 1) - createObject(ctx, t, db, objY, 1) + metabasetest.CreateObject(ctx, t, db, obj1, 1) + metabasetest.CreateObject(ctx, t, db, objX, 1) + metabasetest.CreateObject(ctx, t, db, objY, 1) now := time.Now() - DeleteBucketObjects{ + metabasetest.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{ Bucket: obj1.Location().Bucket(), }, Deleted: 1, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: objX, @@ -173,7 +174,7 @@ func TestDeleteBucketObjects(t *testing.T) { TotalPlainSize: 512, TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: objY, @@ -184,7 +185,7 @@ func TestDeleteBucketObjects(t *testing.T) { TotalPlainSize: 512, TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -203,7 +204,7 @@ func TestDeleteBucketObjects(t *testing.T) { PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, { StreamID: objY.StreamID, @@ -220,7 +221,7 @@ func TestDeleteBucketObjects(t *testing.T) { PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }, }.Check(ctx, t, db) diff --git a/satellite/metabase/delete_expired_test.go b/satellite/metabase/delete_expired_test.go index 56670c42a..3d1999836 100644 --- a/satellite/metabase/delete_expired_test.go +++ b/satellite/metabase/delete_expired_test.go @@ -10,75 +10,76 @@ import ( "storj.io/common/storj" "storj.io/common/testcontext" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestDeleteExpiredObjects(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj1 := randObjectStream() - obj2 := randObjectStream() - obj3 := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj1 := metabasetest.RandObjectStream() + obj2 := metabasetest.RandObjectStream() + obj3 := metabasetest.RandObjectStream() now := time.Now() pastTime := now.Add(-1 * time.Hour) futureTime := now.Add(1 * time.Hour) t.Run("none", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteExpiredObjects{ + metabasetest.DeleteExpiredObjects{ Opts: metabase.DeleteExpiredObjects{ ExpiredBefore: time.Now(), }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("partial objects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) // pending object without expiration time - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj1, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) // pending object with expiration time in the past - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj2, ExpiresAt: &pastTime, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) // pending object with expiration time in the future - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj3, ExpiresAt: &futureTime, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeleteExpiredObjects{ + metabasetest.DeleteExpiredObjects{ Opts: metabase.DeleteExpiredObjects{ ExpiredBefore: time.Now(), }, }.Check(ctx, t, db) - Verify{ // the object with expiration time in the past is gone + metabasetest.Verify{ // the object with expiration time in the past is gone Objects: []metabase.RawObject{ { ObjectStream: obj1, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: obj3, @@ -86,7 +87,7 @@ func TestDeleteExpiredObjects(t *testing.T) { ExpiresAt: &futureTime, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) @@ -95,34 +96,34 @@ func TestDeleteExpiredObjects(t *testing.T) { t.Run("batch size", func(t *testing.T) { expiresAt := time.Now().Add(-30 * 24 * time.Hour) for i := 0; i < 32; i++ { - _ = createExpiredObject(ctx, t, db, randObjectStream(), 3, expiresAt) + _ = metabasetest.CreateExpiredObject(ctx, t, db, metabasetest.RandObjectStream(), 3, expiresAt) } - DeleteExpiredObjects{ + metabasetest.DeleteExpiredObjects{ Opts: metabase.DeleteExpiredObjects{ ExpiredBefore: time.Now().Add(time.Hour), BatchSize: 4, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("committed objects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object1 := CreateTestObject{}.Run(ctx, t, db, obj1, 1) - CreateTestObject{ + object1 := metabasetest.CreateTestObject{}.Run(ctx, t, db, obj1, 1) + metabasetest.CreateTestObject{ BeginObjectExactVersion: &metabase.BeginObjectExactVersion{ ObjectStream: obj2, ExpiresAt: &pastTime, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }.Run(ctx, t, db, obj2, 1) - object3 := CreateTestObject{ + object3 := metabasetest.CreateTestObject{ BeginObjectExactVersion: &metabase.BeginObjectExactVersion{ ObjectStream: obj3, ExpiresAt: &futureTime, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }.Run(ctx, t, db, obj3, 1) @@ -136,19 +137,19 @@ func TestDeleteExpiredObjects(t *testing.T) { EncryptedSize: 1060, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } expectedObj3Segment := expectedObj1Segment expectedObj3Segment.StreamID = obj3.StreamID - DeleteExpiredObjects{ + metabasetest.DeleteExpiredObjects{ Opts: metabase.DeleteExpiredObjects{ ExpiredBefore: time.Now(), }, }.Check(ctx, t, db) - Verify{ // the object with expiration time in the past is gone + metabasetest.Verify{ // the object with expiration time in the past is gone Objects: []metabase.RawObject{ metabase.RawObject(object1), metabase.RawObject(object3), diff --git a/satellite/metabase/delete_test.go b/satellite/metabase/delete_test.go index 01c7d1f90..1cd3e0299 100644 --- a/satellite/metabase/delete_test.go +++ b/satellite/metabase/delete_test.go @@ -12,21 +12,22 @@ import ( "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestDeletePendingObject(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - DeletePendingObject{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 1, @@ -35,14 +36,14 @@ func TestDeletePendingObject(t *testing.T) { ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Version invalid", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 0, @@ -51,13 +52,13 @@ func TestDeletePendingObject(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "Version invalid: 0", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("StreamID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: uuid.UUID{}, Version: 1, @@ -66,13 +67,13 @@ func TestDeletePendingObject(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 1, @@ -81,21 +82,21 @@ func TestDeletePendingObject(t *testing.T) { ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete non existing object version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 33, @@ -104,25 +105,25 @@ func TestDeletePendingObject(t *testing.T) { ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Delete committed object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 0) + object := metabasetest.CreateObject(ctx, t, db, obj, 0) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: object.StreamID, Version: 1, @@ -132,31 +133,31 @@ func TestDeletePendingObject(t *testing.T) { ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Delete pending object without segments with wrong StreamID", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: uuid.UUID{33}, Version: 1, @@ -167,26 +168,26 @@ func TestDeletePendingObject(t *testing.T) { ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Delete pending object without segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -195,9 +196,9 @@ func TestDeletePendingObject(t *testing.T) { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 1, @@ -208,20 +209,20 @@ func TestDeletePendingObject(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete pending object with segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createPendingObject(ctx, t, db, obj, 2) + metabasetest.CreatePendingObject(ctx, t, db, obj, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 1, @@ -233,28 +234,28 @@ func TestDeletePendingObject(t *testing.T) { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.DeletedSegmentInfo{expectedSegmentInfo, expectedSegmentInfo}, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete pending object with inline segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -269,7 +270,7 @@ func TestDeletePendingObject(t *testing.T) { }, }.Check(ctx, t, db) - DeletePendingObject{ + metabasetest.DeletePendingObject{ Opts: metabase.DeletePendingObject{ StreamID: obj.StreamID, Version: 1, @@ -281,44 +282,44 @@ func TestDeletePendingObject(t *testing.T) { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) }) } func TestDeleteObjectExactVersion(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - DeleteObjectExactVersion{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: test.ObjectLocation, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Version invalid", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 0, @@ -326,13 +327,13 @@ func TestDeleteObjectExactVersion(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "Version invalid: 0", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -340,13 +341,13 @@ func TestDeleteObjectExactVersion(t *testing.T) { ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete non existing object version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 33, @@ -354,21 +355,21 @@ func TestDeleteObjectExactVersion(t *testing.T) { ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete partial object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -377,27 +378,27 @@ func TestDeleteObjectExactVersion(t *testing.T) { ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Delete object without segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedMetadata := testrand.Bytes(1024) encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - object := CreateTestObject{ + object := metabasetest.CreateTestObject{ CommitObject: &metabase.CommitObject{ ObjectStream: obj, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -406,7 +407,7 @@ func TestDeleteObjectExactVersion(t *testing.T) { }, }.Run(ctx, t, db, obj, 0) - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -416,20 +417,20 @@ func TestDeleteObjectExactVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 2) + object := metabasetest.CreateObject(ctx, t, db, obj, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -440,21 +441,21 @@ func TestDeleteObjectExactVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with inline segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -469,13 +470,13 @@ func TestDeleteObjectExactVersion(t *testing.T) { }, }.Check(ctx, t, db) - object := CommitObject{ + object := metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - DeleteObjectExactVersion{ + metabasetest.DeleteObjectExactVersion{ Opts: metabase.DeleteObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -485,94 +486,94 @@ func TestDeleteObjectExactVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) }) } func TestDeleteObjectLatestVersion(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - DeleteObjectLatestVersion{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ ObjectLocation: test.ObjectLocation, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ObjectLocation: location}, ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete non existing object version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ObjectLocation: location}, ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete partial object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ObjectLocation: obj.Location()}, ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Delete object without segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedMetadata := testrand.Bytes(1024) encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - object := CreateTestObject{ + object := metabasetest.CreateTestObject{ CommitObject: &metabase.CommitObject{ ObjectStream: obj, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -581,7 +582,7 @@ func TestDeleteObjectLatestVersion(t *testing.T) { }, }.Run(ctx, t, db, obj, 0) - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ ObjectLocation: obj.Location(), }, @@ -590,20 +591,20 @@ func TestDeleteObjectLatestVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 2) + object := metabasetest.CreateObject(ctx, t, db, obj, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ ObjectLocation: location, }, @@ -613,21 +614,21 @@ func TestDeleteObjectLatestVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with inline segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -642,13 +643,13 @@ func TestDeleteObjectLatestVersion(t *testing.T) { }, }.Check(ctx, t, db) - object := CommitObject{ + object := metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ ObjectLocation: obj.Location(), }, @@ -657,17 +658,17 @@ func TestDeleteObjectLatestVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete latest from multiple versions", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - obj := randObjectStream() + obj := metabasetest.RandObjectStream() // first version obj.Version = metabase.Version(10) - createObject(ctx, t, db, obj, 1) + metabasetest.CreateObject(ctx, t, db, obj, 1) // second version, to delete secondObject := metabase.ObjectStream{ @@ -677,14 +678,14 @@ func TestDeleteObjectLatestVersion(t *testing.T) { Version: 11, StreamID: testrand.UUID(), } - object := createObject(ctx, t, db, secondObject, 1) + object := metabasetest.CreateObject(ctx, t, db, secondObject, 1) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectLatestVersion{ + metabasetest.DeleteObjectLatestVersion{ Opts: metabase.DeleteObjectLatestVersion{ ObjectLocation: obj.Location(), }, @@ -696,7 +697,7 @@ func TestDeleteObjectLatestVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -707,7 +708,7 @@ func TestDeleteObjectLatestVersion(t *testing.T) { TotalPlainSize: 512, TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -726,7 +727,7 @@ func TestDeleteObjectLatestVersion(t *testing.T) { PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }, }.Check(ctx, t, db) @@ -735,60 +736,60 @@ func TestDeleteObjectLatestVersion(t *testing.T) { } func TestDeleteObjectAnyStatusAllVersions(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - DeleteObjectAnyStatusAllVersions{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: test.ObjectLocation}, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: obj.Location()}, ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete non existing object version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: obj.Location()}, ErrClass: &storj.ErrObjectNotFound, ErrText: "metabase: no rows deleted", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete partial object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: obj.Location()}, Result: metabase.DeleteObjectResult{ Objects: []metabase.Object{{ @@ -796,22 +797,22 @@ func TestDeleteObjectAnyStatusAllVersions(t *testing.T) { CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }}, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object without segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedMetadata := testrand.Bytes(1024) encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - object := CreateTestObject{ + object := metabasetest.CreateTestObject{ CommitObject: &metabase.CommitObject{ ObjectStream: obj, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -820,27 +821,27 @@ func TestDeleteObjectAnyStatusAllVersions(t *testing.T) { }, }.Run(ctx, t, db, obj, 0) - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: obj.Location()}, Result: metabase.DeleteObjectResult{ Objects: []metabase.Object{object}, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 2) + object := metabasetest.CreateObject(ctx, t, db, obj, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ ObjectLocation: location, }, @@ -850,21 +851,21 @@ func TestDeleteObjectAnyStatusAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with inline segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -879,77 +880,77 @@ func TestDeleteObjectAnyStatusAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - object := CommitObject{ + object := metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: obj.Location()}, Result: metabase.DeleteObjectResult{ Objects: []metabase.Object{object}, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete multiple versions of the same object at once", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) expected := metabase.DeleteObjectResult{} - obj := randObjectStream() + obj := metabasetest.RandObjectStream() for i := 1; i <= 10; i++ { obj.StreamID = testrand.UUID() obj.Version = metabase.Version(i) - expected.Objects = append(expected.Objects, createObject(ctx, t, db, obj, 1)) + expected.Objects = append(expected.Objects, metabasetest.CreateObject(ctx, t, db, obj, 1)) expected.Segments = append(expected.Segments, metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, }) } - DeleteObjectAnyStatusAllVersions{ + metabasetest.DeleteObjectAnyStatusAllVersions{ Opts: metabase.DeleteObjectAnyStatusAllVersions{ObjectLocation: obj.Location()}, Result: expected, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) }) } func TestDeleteObjectsAllVersions(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - DeleteObjectsAllVersions{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{test.ObjectLocation}, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Delete two objects from different projects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - obj2 := randObjectStream() + obj2 := metabasetest.RandObjectStream() - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location, obj2.Location()}, }, @@ -957,16 +958,16 @@ func TestDeleteObjectsAllVersions(t *testing.T) { ErrText: "all objects must be in the same bucket", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete two objects from same project, but different buckets", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - obj2 := randObjectStream() + obj2 := metabasetest.RandObjectStream() obj2.ProjectID = obj.ProjectID - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location, obj2.Location()}, }, @@ -974,63 +975,63 @@ func TestDeleteObjectsAllVersions(t *testing.T) { ErrText: "all objects must be in the same bucket", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete empty list of objects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectsAllVersions{}.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.DeleteObjectsAllVersions{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location}, }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete partial object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location}, }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Delete object without segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 0) + object := metabasetest.CreateObject(ctx, t, db, obj, 0) - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location}, }, @@ -1039,20 +1040,20 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 2) + object := metabasetest.CreateObject(ctx, t, db, obj, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location}, }, @@ -1062,25 +1063,25 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete two objects with segments from same bucket", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - obj2 := randObjectStream() + obj2 := metabasetest.RandObjectStream() obj2.ProjectID = obj.ProjectID obj2.BucketName = obj.BucketName - object1 := createObject(ctx, t, db, obj, 1) - object2 := createObject(ctx, t, db, obj2, 2) + object1 := metabasetest.CreateObject(ctx, t, db, obj, 1) + object2 := metabasetest.CreateObject(ctx, t, db, obj2, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location, obj2.Location()}, }, @@ -1090,21 +1091,21 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with inline segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1119,13 +1120,13 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - object := CommitObject{ + object := metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location}, }, @@ -1134,21 +1135,21 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete object with inline segment and object with remote segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -1163,24 +1164,24 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - object1 := CommitObject{ + object1 := metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, }.Check(ctx, t, db) - obj2 := randObjectStream() + obj2 := metabasetest.RandObjectStream() obj2.ProjectID = obj.ProjectID obj2.BucketName = obj.BucketName - object2 := createObject(ctx, t, db, obj2, 2) + object2 := metabasetest.CreateObject(ctx, t, db, obj2, 2) expectedSegmentInfo := metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, } - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location, object2.Location()}, }, @@ -1190,171 +1191,32 @@ func TestDeleteObjectsAllVersions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Delete multiple versions of the same object at once", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) expected := metabase.DeleteObjectResult{} for i := 1; i <= 10; i++ { obj.StreamID = testrand.UUID() obj.Version = metabase.Version(i) - expected.Objects = append(expected.Objects, createObject(ctx, t, db, obj, 1)) + expected.Objects = append(expected.Objects, metabasetest.CreateObject(ctx, t, db, obj, 1)) expected.Segments = append(expected.Segments, metabase.DeletedSegmentInfo{ RootPieceID: storj.PieceID{1}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, }) } - DeleteObjectsAllVersions{ + metabasetest.DeleteObjectsAllVersions{ Opts: metabase.DeleteObjectsAllVersions{ Locations: []metabase.ObjectLocation{location}, }, Result: expected, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) }) } - -func createPendingObject(ctx *testcontext.Context, t *testing.T, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte) { - BeginObjectExactVersion{ - Opts: metabase.BeginObjectExactVersion{ - ObjectStream: obj, - Encryption: defaultTestEncryption, - }, - Version: obj.Version, - }.Check(ctx, t, db) - - for i := byte(0); i < numberOfSegments; i++ { - BeginSegment{ - Opts: metabase.BeginSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{i + 1}, - Pieces: []metabase.Piece{{ - Number: 1, - StorageNode: testrand.NodeID(), - }}, - }, - }.Check(ctx, t, db) - - CommitSegment{ - Opts: metabase.CommitSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{1}, - Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - - EncryptedKey: []byte{3}, - EncryptedKeyNonce: []byte{4}, - EncryptedETag: []byte{5}, - - EncryptedSize: 1024, - PlainSize: 512, - PlainOffset: 0, - Redundancy: defaultTestRedundancy, - }, - }.Check(ctx, t, db) - } -} - -func createObject(ctx *testcontext.Context, t *testing.T, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte) metabase.Object { - BeginObjectExactVersion{ - Opts: metabase.BeginObjectExactVersion{ - ObjectStream: obj, - Encryption: defaultTestEncryption, - }, - Version: obj.Version, - }.Check(ctx, t, db) - - for i := byte(0); i < numberOfSegments; i++ { - BeginSegment{ - Opts: metabase.BeginSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{i + 1}, - Pieces: []metabase.Piece{{ - Number: 1, - StorageNode: testrand.NodeID(), - }}, - }, - }.Check(ctx, t, db) - - CommitSegment{ - Opts: metabase.CommitSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{1}, - Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - - EncryptedKey: []byte{3}, - EncryptedKeyNonce: []byte{4}, - EncryptedETag: []byte{5}, - - EncryptedSize: 1024, - PlainSize: 512, - PlainOffset: 0, - Redundancy: defaultTestRedundancy, - }, - }.Check(ctx, t, db) - } - - return CommitObject{ - Opts: metabase.CommitObject{ - ObjectStream: obj, - }, - }.Check(ctx, t, db) -} - -func createExpiredObject(ctx *testcontext.Context, t *testing.T, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte, expiresAt time.Time) metabase.Object { - BeginObjectExactVersion{ - Opts: metabase.BeginObjectExactVersion{ - ObjectStream: obj, - Encryption: defaultTestEncryption, - ExpiresAt: &expiresAt, - }, - Version: obj.Version, - }.Check(ctx, t, db) - - for i := byte(0); i < numberOfSegments; i++ { - BeginSegment{ - Opts: metabase.BeginSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{i + 1}, - Pieces: []metabase.Piece{{ - Number: 1, - StorageNode: testrand.NodeID(), - }}, - }, - }.Check(ctx, t, db) - - CommitSegment{ - Opts: metabase.CommitSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{1}, - Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - - EncryptedKey: []byte{3}, - EncryptedKeyNonce: []byte{4}, - EncryptedETag: []byte{5}, - - EncryptedSize: 1024, - PlainSize: 512, - PlainOffset: 0, - Redundancy: defaultTestRedundancy, - }, - }.Check(ctx, t, db) - } - - return CommitObject{ - Opts: metabase.CommitObject{ - ObjectStream: obj, - }, - }.Check(ctx, t, db) -} diff --git a/satellite/metabase/get_test.go b/satellite/metabase/get_test.go index 3c4c8d8cc..9a31444cf 100644 --- a/satellite/metabase/get_test.go +++ b/satellite/metabase/get_test.go @@ -11,21 +11,22 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestGetObjectExactVersion(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - GetObjectExactVersion{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{ ObjectLocation: test.ObjectLocation, }, @@ -33,14 +34,14 @@ func TestGetObjectExactVersion(t *testing.T) { ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Version invalid", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetObjectExactVersion{ + metabasetest.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{ ObjectLocation: location, Version: 0, @@ -49,13 +50,13 @@ func TestGetObjectExactVersion(t *testing.T) { ErrText: "Version invalid: 0", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetObjectExactVersion{ + metabasetest.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -64,15 +65,15 @@ func TestGetObjectExactVersion(t *testing.T) { ErrText: "metabase: sql: no rows in result set", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Get not existing version", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj, 0) + metabasetest.CreateObject(ctx, t, db, obj, 0) - GetObjectExactVersion{ + metabasetest.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{ ObjectLocation: location, Version: 11, @@ -81,32 +82,32 @@ func TestGetObjectExactVersion(t *testing.T) { ErrText: "metabase: sql: no rows in result set", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Get pending object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - GetObjectExactVersion{ + metabasetest.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -115,25 +116,25 @@ func TestGetObjectExactVersion(t *testing.T) { ErrText: "metabase: sql: no rows in result set", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Get object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj, 0) + metabasetest.CreateObject(ctx, t, db, obj, 0) - GetObjectExactVersion{ + metabasetest.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{ ObjectLocation: location, Version: 1, @@ -143,17 +144,17 @@ func TestGetObjectExactVersion(t *testing.T) { CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }.Check(ctx, t, db) - Verify{Objects: []metabase.RawObject{ + metabasetest.Verify{Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }}.Check(ctx, t, db) }) @@ -161,18 +162,18 @@ func TestGetObjectExactVersion(t *testing.T) { } func TestGetObjectLatestVersion(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - GetObjectLatestVersion{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{ ObjectLocation: test.ObjectLocation, }, @@ -180,14 +181,14 @@ func TestGetObjectLatestVersion(t *testing.T) { ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetObjectLatestVersion{ + metabasetest.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{ ObjectLocation: location, }, @@ -195,22 +196,22 @@ func TestGetObjectLatestVersion(t *testing.T) { ErrText: "metabase: sql: no rows in result set", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Get pending object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - GetObjectLatestVersion{ + metabasetest.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{ ObjectLocation: location, }, @@ -218,27 +219,27 @@ func TestGetObjectLatestVersion(t *testing.T) { ErrText: "metabase: sql: no rows in result set", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("Get object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) encryptedMetadata := testrand.Bytes(1024) encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - CreateTestObject{ + metabasetest.CreateTestObject{ CommitObject: &metabase.CommitObject{ ObjectStream: obj, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -247,7 +248,7 @@ func TestGetObjectLatestVersion(t *testing.T) { }, }.Run(ctx, t, db, obj, 0) - GetObjectLatestVersion{ + metabasetest.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{ ObjectLocation: location, }, @@ -256,7 +257,7 @@ func TestGetObjectLatestVersion(t *testing.T) { CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadata: encryptedMetadata, @@ -264,13 +265,13 @@ func TestGetObjectLatestVersion(t *testing.T) { }, }.Check(ctx, t, db) - Verify{Objects: []metabase.RawObject{ + metabasetest.Verify{Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadata: encryptedMetadata, @@ -280,10 +281,10 @@ func TestGetObjectLatestVersion(t *testing.T) { }) t.Run("Get latest object version from multiple", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) firstVersion := obj - createObject(ctx, t, db, firstVersion, 0) + metabasetest.CreateObject(ctx, t, db, firstVersion, 0) secondVersion := metabase.ObjectStream{ ProjectID: obj.ProjectID, BucketName: obj.BucketName, @@ -291,9 +292,9 @@ func TestGetObjectLatestVersion(t *testing.T) { Version: 2, StreamID: obj.StreamID, } - createObject(ctx, t, db, secondVersion, 0) + metabasetest.CreateObject(ctx, t, db, secondVersion, 0) - GetObjectLatestVersion{ + metabasetest.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{ ObjectLocation: location, }, @@ -302,24 +303,24 @@ func TestGetObjectLatestVersion(t *testing.T) { CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }.Check(ctx, t, db) - Verify{Objects: []metabase.RawObject{ + metabasetest.Verify{Objects: []metabase.RawObject{ { ObjectStream: firstVersion, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectStream: secondVersion, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }}.Check(ctx, t, db) }) @@ -327,8 +328,8 @@ func TestGetObjectLatestVersion(t *testing.T) { } func TestGetSegmentByLocation(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() @@ -338,11 +339,11 @@ func TestGetSegmentByLocation(t *testing.T) { ObjectKey: obj.ObjectKey, } - for _, test := range invalidSegmentLocations(location) { + for _, test := range metabasetest.InvalidSegmentLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - GetSegmentByLocation{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{ SegmentLocation: test.SegmentLocation, }, @@ -350,14 +351,14 @@ func TestGetSegmentByLocation(t *testing.T) { ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Object missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetSegmentByLocation{ + metabasetest.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{ SegmentLocation: location, }, @@ -365,13 +366,13 @@ func TestGetSegmentByLocation(t *testing.T) { ErrText: "metabase: object or segment missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Get segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj, 1) + metabasetest.CreateObject(ctx, t, db, obj, 1) expectedSegment := metabase.Segment{ StreamID: obj.StreamID, @@ -386,10 +387,10 @@ func TestGetSegmentByLocation(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } - GetSegmentByLocation{ + metabasetest.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{ SegmentLocation: location, }, @@ -397,7 +398,7 @@ func TestGetSegmentByLocation(t *testing.T) { }.Check(ctx, t, db) // check non existing segment in existing object - GetSegmentByLocation{ + metabasetest.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{ SegmentLocation: metabase.SegmentLocation{ ProjectID: obj.ProjectID, @@ -412,7 +413,7 @@ func TestGetSegmentByLocation(t *testing.T) { ErrText: "metabase: object or segment missing", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -424,7 +425,7 @@ func TestGetSegmentByLocation(t *testing.T) { TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -436,27 +437,27 @@ func TestGetSegmentByLocation(t *testing.T) { } func TestGetSegmentByPosition(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() t.Run("StreamID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetSegmentByPosition{ + metabasetest.GetSegmentByPosition{ Opts: metabase.GetSegmentByPosition{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Segment missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetSegmentByPosition{ + metabasetest.GetSegmentByPosition{ Opts: metabase.GetSegmentByPosition{ StreamID: obj.StreamID, }, @@ -464,13 +465,13 @@ func TestGetSegmentByPosition(t *testing.T) { ErrText: "segment missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Get segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj, 1) + metabasetest.CreateObject(ctx, t, db, obj, 1) expectedSegment := metabase.Segment{ StreamID: obj.StreamID, @@ -485,10 +486,10 @@ func TestGetSegmentByPosition(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } - GetSegmentByPosition{ + metabasetest.GetSegmentByPosition{ Opts: metabase.GetSegmentByPosition{ StreamID: obj.StreamID, Position: metabase.SegmentPosition{ @@ -499,7 +500,7 @@ func TestGetSegmentByPosition(t *testing.T) { }.Check(ctx, t, db) // check non existing segment in existing object - GetSegmentByPosition{ + metabasetest.GetSegmentByPosition{ Opts: metabase.GetSegmentByPosition{ StreamID: obj.StreamID, Position: metabase.SegmentPosition{ @@ -510,7 +511,7 @@ func TestGetSegmentByPosition(t *testing.T) { ErrText: "segment missing", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -522,7 +523,7 @@ func TestGetSegmentByPosition(t *testing.T) { TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -534,16 +535,16 @@ func TestGetSegmentByPosition(t *testing.T) { } func TestGetLatestObjectLastSegment(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - GetLatestObjectLastSegment{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.GetLatestObjectLastSegment{ Opts: metabase.GetLatestObjectLastSegment{ ObjectLocation: test.ObjectLocation, }, @@ -551,14 +552,14 @@ func TestGetLatestObjectLastSegment(t *testing.T) { ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Object or segment missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetLatestObjectLastSegment{ + metabasetest.GetLatestObjectLastSegment{ Opts: metabase.GetLatestObjectLastSegment{ ObjectLocation: location, }, @@ -566,13 +567,13 @@ func TestGetLatestObjectLastSegment(t *testing.T) { ErrText: "metabase: object or segment missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Get last segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - createObject(ctx, t, db, obj, 2) + metabasetest.CreateObject(ctx, t, db, obj, 2) expectedSegmentSecond := metabase.Segment{ StreamID: obj.StreamID, @@ -588,21 +589,21 @@ func TestGetLatestObjectLastSegment(t *testing.T) { PlainSize: 512, PlainOffset: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } expectedSegmentFirst := expectedSegmentSecond expectedSegmentFirst.Position.Index = 0 expectedSegmentFirst.PlainOffset = 0 - GetLatestObjectLastSegment{ + metabasetest.GetLatestObjectLastSegment{ Opts: metabase.GetLatestObjectLastSegment{ ObjectLocation: location, }, Result: expectedSegmentSecond, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -614,7 +615,7 @@ func TestGetLatestObjectLastSegment(t *testing.T) { TotalEncryptedSize: 2048, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -627,16 +628,16 @@ func TestGetLatestObjectLastSegment(t *testing.T) { } func TestGetSegmentByOffset(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - GetSegmentByOffset{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{ ObjectLocation: test.ObjectLocation, }, @@ -644,14 +645,14 @@ func TestGetSegmentByOffset(t *testing.T) { ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("Invalid PlainOffset", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetSegmentByOffset{ + metabasetest.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{ ObjectLocation: location, PlainOffset: -1, @@ -660,13 +661,13 @@ func TestGetSegmentByOffset(t *testing.T) { ErrText: "Invalid PlainOffset: -1", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Object or segment missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetSegmentByOffset{ + metabasetest.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{ ObjectLocation: location, }, @@ -674,13 +675,13 @@ func TestGetSegmentByOffset(t *testing.T) { ErrText: "metabase: object or segment missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Get segment", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - CreateTestObject{}.Run(ctx, t, db, obj, 4) + metabasetest.CreateTestObject{}.Run(ctx, t, db, obj, 4) segments := make([]metabase.Segment, 4) for i := range segments { @@ -698,7 +699,7 @@ func TestGetSegmentByOffset(t *testing.T) { PlainSize: 512, PlainOffset: int64(i * 512), Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } } @@ -713,7 +714,7 @@ func TestGetSegmentByOffset(t *testing.T) { } for _, tc := range testCases { - GetSegmentByOffset{ + metabasetest.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{ ObjectLocation: location, PlainOffset: tc.Offset, @@ -722,7 +723,7 @@ func TestGetSegmentByOffset(t *testing.T) { }.Check(ctx, t, db) } - GetSegmentByOffset{ + metabasetest.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{ ObjectLocation: location, PlainOffset: 2048, @@ -731,7 +732,7 @@ func TestGetSegmentByOffset(t *testing.T) { ErrText: "metabase: object or segment missing", }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -743,7 +744,7 @@ func TestGetSegmentByOffset(t *testing.T) { TotalEncryptedSize: 4240, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -758,26 +759,26 @@ func TestGetSegmentByOffset(t *testing.T) { } func TestBucketEmpty(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() t.Run("ProjectID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BucketEmpty{ + metabasetest.BucketEmpty{ Opts: metabase.BucketEmpty{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "ProjectID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("BucketName missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BucketEmpty{ + metabasetest.BucketEmpty{ Opts: metabase.BucketEmpty{ ProjectID: obj.ProjectID, }, @@ -785,13 +786,13 @@ func TestBucketEmpty(t *testing.T) { ErrText: "BucketName missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("BucketEmpty true", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BucketEmpty{ + metabasetest.BucketEmpty{ Opts: metabase.BucketEmpty{ ProjectID: obj.ProjectID, BucketName: obj.BucketName, @@ -799,22 +800,22 @@ func TestBucketEmpty(t *testing.T) { Result: true, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("BucketEmpty false with pending object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - BucketEmpty{ + metabasetest.BucketEmpty{ Opts: metabase.BucketEmpty{ ProjectID: obj.ProjectID, BucketName: obj.BucketName, @@ -822,24 +823,24 @@ func TestBucketEmpty(t *testing.T) { Result: false, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, }.Check(ctx, t, db) }) t.Run("BucketEmpty false with committed object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 0) + object := metabasetest.CreateObject(ctx, t, db, obj, 0) - BucketEmpty{ + metabasetest.BucketEmpty{ Opts: metabase.BucketEmpty{ ProjectID: obj.ProjectID, BucketName: obj.BucketName, @@ -847,7 +848,7 @@ func TestBucketEmpty(t *testing.T) { Result: false, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ metabase.RawObject(object), }, diff --git a/satellite/metabase/iterator_test.go b/satellite/metabase/iterator_test.go index 12330bc6b..13a57c059 100644 --- a/satellite/metabase/iterator_test.go +++ b/satellite/metabase/iterator_test.go @@ -14,13 +14,14 @@ import ( "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestIterateObjects(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { t.Run("ProjectID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IterateObjects{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{}, BucketName: "sj://mybucket", @@ -28,12 +29,12 @@ func TestIterateObjects(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "ProjectID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("BucketName missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "", @@ -41,11 +42,11 @@ func TestIterateObjects(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "BucketName missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Limit is negative", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IterateObjects{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -54,13 +55,13 @@ func TestIterateObjects(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "BatchSize is negative", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("empty bucket", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) objects := createObjects(ctx, t, db, 2, uuid.UUID{1}, "mybucket") - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "myemptybucket", @@ -68,17 +69,17 @@ func TestIterateObjects(t *testing.T) { }, Result: nil, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("pending and committed", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - pending := randObjectStream() + pending := metabasetest.RandObjectStream() pending.ObjectKey = metabase.ObjectKey("firstObject") - committed := randObjectStream() + committed := metabasetest.RandObjectStream() committed.ProjectID = pending.ProjectID committed.BucketName = pending.BucketName committed.ObjectKey = metabase.ObjectKey("secondObject") @@ -86,10 +87,10 @@ func TestIterateObjects(t *testing.T) { projectID := pending.ProjectID bucketName := pending.BucketName - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -98,14 +99,14 @@ func TestIterateObjects(t *testing.T) { encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: committed, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -114,7 +115,7 @@ func TestIterateObjects(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -126,7 +127,7 @@ func TestIterateObjects(t *testing.T) { StreamID: pending.StreamID, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, { ObjectKey: committed.ObjectKey, @@ -134,7 +135,7 @@ func TestIterateObjects(t *testing.T) { StreamID: committed.StreamID, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadata: encryptedMetadata, EncryptedMetadataEncryptedKey: encryptedMetadataKey, @@ -144,7 +145,7 @@ func TestIterateObjects(t *testing.T) { }) t.Run("less objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjects := 3 limit := 10 expected := make([]metabase.ObjectEntry, numberOfObjects) @@ -152,7 +153,7 @@ func TestIterateObjects(t *testing.T) { for i, obj := range objects { expected[i] = objectEntryFromRaw(obj) } - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -160,11 +161,11 @@ func TestIterateObjects(t *testing.T) { }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("more objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjects := 10 limit := 3 expected := make([]metabase.ObjectEntry, numberOfObjects) @@ -172,7 +173,7 @@ func TestIterateObjects(t *testing.T) { for i, obj := range objects { expected[i] = objectEntryFromRaw(obj) } - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -180,11 +181,11 @@ func TestIterateObjects(t *testing.T) { }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("objects in one bucket in project with 2 buckets", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjectsPerBucket := 5 expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) objectsBucketA := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "bucket-a") @@ -192,18 +193,18 @@ func TestIterateObjects(t *testing.T) { for i, obj := range objectsBucketA { expected[i] = objectEntryFromRaw(obj) } - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "bucket-a", }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: append(objectsBucketA, objectsBucketB...)}.Check(ctx, t, db) + metabasetest.Verify{Objects: append(objectsBucketA, objectsBucketB...)}.Check(ctx, t, db) }) t.Run("objects in one bucket with same bucketName in another project", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjectsPerBucket := 5 expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) objectsProject1 := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "mybucket") @@ -211,18 +212,18 @@ func TestIterateObjects(t *testing.T) { for i, obj := range objectsProject1 { expected[i] = objectEntryFromRaw(obj) } - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: append(objectsProject1, objectsProject2...)}.Check(ctx, t, db) + metabasetest.Verify{Objects: append(objectsProject1, objectsProject2...)}.Check(ctx, t, db) }) t.Run("recursive", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ @@ -237,7 +238,7 @@ func TestIterateObjects(t *testing.T) { "g", }) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -255,7 +256,7 @@ func TestIterateObjects(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -274,7 +275,7 @@ func TestIterateObjects(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -293,7 +294,7 @@ func TestIterateObjects(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -307,7 +308,7 @@ func TestIterateObjects(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -322,7 +323,7 @@ func TestIterateObjects(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -336,7 +337,7 @@ func TestIterateObjects(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjects{ + metabasetest.IterateObjects{ Opts: metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -349,7 +350,7 @@ func TestIterateObjects(t *testing.T) { }) t.Run("boundaries", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" queries := []metabase.ObjectKey{""} @@ -370,7 +371,7 @@ func TestIterateObjects(t *testing.T) { for _, cursor := range queries { for _, prefix := range queries { - var collector IterateCollector + var collector metabasetest.IterateCollector err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -386,11 +387,11 @@ func TestIterateObjects(t *testing.T) { }) t.Run("verify-iterator-boundary", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" queries := []metabase.ObjectKey{"\x00\xFF"} createObjectsWithKeys(ctx, t, db, projectID, bucketName, queries) - var collector IterateCollector + var collector metabasetest.IterateCollector err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -404,7 +405,7 @@ func TestIterateObjects(t *testing.T) { }) t.Run("verify-cursor-continuation", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ @@ -413,7 +414,7 @@ func TestIterateObjects(t *testing.T) { "a/0", }) - var collector IterateCollector + var collector metabasetest.IterateCollector err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{ ProjectID: projectID, BucketName: bucketName, @@ -426,12 +427,12 @@ func TestIterateObjects(t *testing.T) { } func TestIterateObjectsWithStatus(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { t.Run("BucketName missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) t.Run("ProjectID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IterateObjectsWithStatus{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{}, BucketName: "sj://mybucket", @@ -441,9 +442,9 @@ func TestIterateObjectsWithStatus(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "ProjectID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "", @@ -453,11 +454,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "BucketName missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Limit is negative", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IterateObjectsWithStatus{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -468,11 +469,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "BatchSize is negative", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Status is invalid", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IterateObjectsWithStatus{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "test", @@ -482,13 +483,13 @@ func TestIterateObjectsWithStatus(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "Status 255 is not supported", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("empty bucket", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) objects := createObjects(ctx, t, db, 2, uuid.UUID{1}, "mybucket") - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "myemptybucket", @@ -498,26 +499,26 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, Result: nil, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("based on status", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - pending := randObjectStream() - committed := randObjectStream() + pending := metabasetest.RandObjectStream() + committed := metabasetest.RandObjectStream() committed.ProjectID = pending.ProjectID committed.BucketName = pending.BucketName projectID := pending.ProjectID bucketName := pending.BucketName - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -526,14 +527,14 @@ func TestIterateObjectsWithStatus(t *testing.T) { encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: committed, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -542,7 +543,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -555,14 +556,14 @@ func TestIterateObjectsWithStatus(t *testing.T) { StreamID: committed.StreamID, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadata: encryptedMetadata, EncryptedMetadataEncryptedKey: encryptedMetadataKey, }}, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -575,13 +576,13 @@ func TestIterateObjectsWithStatus(t *testing.T) { StreamID: pending.StreamID, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }}, }.Check(ctx, t, db) }) t.Run("less objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjects := 3 limit := 10 expected := make([]metabase.ObjectEntry, numberOfObjects) @@ -589,7 +590,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { for i, obj := range objects { expected[i] = objectEntryFromRaw(obj) } - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -599,11 +600,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("more objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjects := 10 limit := 3 expected := make([]metabase.ObjectEntry, numberOfObjects) @@ -611,7 +612,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { for i, obj := range objects { expected[i] = objectEntryFromRaw(obj) } - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -621,11 +622,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("objects in one bucket in project with 2 buckets", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjectsPerBucket := 5 expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) objectsBucketA := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "bucket-a") @@ -633,7 +634,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { for i, obj := range objectsBucketA { expected[i] = objectEntryFromRaw(obj) } - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "bucket-a", @@ -642,11 +643,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: append(objectsBucketA, objectsBucketB...)}.Check(ctx, t, db) + metabasetest.Verify{Objects: append(objectsBucketA, objectsBucketB...)}.Check(ctx, t, db) }) t.Run("objects in one bucket with same bucketName in another project", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjectsPerBucket := 5 expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) objectsProject1 := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "mybucket") @@ -654,7 +655,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { for i, obj := range objectsProject1 { expected[i] = objectEntryFromRaw(obj) } - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: uuid.UUID{1}, BucketName: "mybucket", @@ -663,11 +664,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, Result: expected, }.Check(ctx, t, db) - Verify{Objects: append(objectsProject1, objectsProject2...)}.Check(ctx, t, db) + metabasetest.Verify{Objects: append(objectsProject1, objectsProject2...)}.Check(ctx, t, db) }) t.Run("recursive", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ @@ -682,7 +683,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { "g", }) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -702,7 +703,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -723,7 +724,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -744,7 +745,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -760,7 +761,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -777,7 +778,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -793,7 +794,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -808,7 +809,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }) t.Run("non-recursive", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ @@ -823,7 +824,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { "g", }) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -838,7 +839,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -854,7 +855,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -870,7 +871,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -885,7 +886,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -901,7 +902,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -916,7 +917,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -928,7 +929,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { Result: nil, }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -944,7 +945,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { ), }.Check(ctx, t, db) - IterateObjectsWithStatus{ + metabasetest.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -959,7 +960,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }) t.Run("boundaries", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" queries := []metabase.ObjectKey{""} @@ -980,7 +981,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { for _, cursor := range queries { for _, prefix := range queries { - var collector IterateCollector + var collector metabasetest.IterateCollector err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -1010,11 +1011,11 @@ func TestIterateObjectsWithStatus(t *testing.T) { }) t.Run("verify-iterator-boundary", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" queries := []metabase.ObjectKey{"\x00\xFF"} createObjectsWithKeys(ctx, t, db, projectID, bucketName, queries) - var collector IterateCollector + var collector metabasetest.IterateCollector err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -1029,7 +1030,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { }) t.Run("verify-cursor-continuation", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ @@ -1038,7 +1039,7 @@ func TestIterateObjectsWithStatus(t *testing.T) { "a/0", }) - var collector IterateCollector + var collector metabasetest.IterateCollector err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{ ProjectID: projectID, BucketName: bucketName, @@ -1052,35 +1053,35 @@ func TestIterateObjectsWithStatus(t *testing.T) { } func TestIteratePendingObjectsWithObjectKey(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() location := obj.Location() now := time.Now() - for _, test := range invalidObjectLocations(location) { + for _, test := range metabasetest.InvalidObjectLocations(location) { test := test t.Run(test.Name, func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IteratePendingObjectsByKey{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: test.ObjectLocation, }, ErrClass: test.ErrClass, ErrText: test.ErrText, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) } t.Run("committed object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - obj := randObjectStream() + obj := metabasetest.RandObjectStream() - createObject(ctx, t, db, obj, 0) - IteratePendingObjectsByKey{ + metabasetest.CreateObject(ctx, t, db, obj, 0) + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: obj.Location(), BatchSize: 10, @@ -1089,19 +1090,19 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { }.Check(ctx, t, db) }) t.Run("non existing object", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - pending := randObjectStream() - createPendingObject(ctx, t, db, pending, 0) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + pending := metabasetest.RandObjectStream() + metabasetest.CreatePendingObject(ctx, t, db, pending, 0) object := metabase.RawObject{ ObjectStream: pending, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } - IteratePendingObjectsByKey{ + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: metabase.ObjectLocation{ ProjectID: pending.ProjectID, @@ -1113,13 +1114,13 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { Result: nil, }.Check(ctx, t, db) - Verify{Objects: []metabase.RawObject{object}}.Check(ctx, t, db) + metabasetest.Verify{Objects: []metabase.RawObject{object}}.Check(ctx, t, db) }) t.Run("less and more objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - pending := []metabase.ObjectStream{randObjectStream(), randObjectStream(), randObjectStream()} + pending := []metabase.ObjectStream{metabasetest.RandObjectStream(), metabasetest.RandObjectStream(), metabasetest.RandObjectStream()} location := pending[0].Location() objects := make([]metabase.RawObject, 3) @@ -1131,23 +1132,23 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { obj.ObjectKey = location.ObjectKey obj.Version = metabase.Version(i + 1) - createPendingObject(ctx, t, db, obj, 0) + metabasetest.CreatePendingObject(ctx, t, db, obj, 0) objects[i] = metabase.RawObject{ ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } expected[i] = objectEntryFromRaw(objects[i]) } sort.Slice(expected, func(i, j int) bool { - return less(expected[i].StreamID, expected[j].StreamID) + return expected[i].StreamID.Less(expected[j].StreamID) }) - IteratePendingObjectsByKey{ + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: location, BatchSize: 10, @@ -1155,7 +1156,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { Result: expected, }.Check(ctx, t, db) - IteratePendingObjectsByKey{ + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: location, BatchSize: 2, @@ -1163,37 +1164,37 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("prefixed object key", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - pending := randObjectStream() + pending := metabasetest.RandObjectStream() pending.ObjectKey = metabase.ObjectKey("a/prefixed/" + string(location.ObjectKey)) - createPendingObject(ctx, t, db, pending, 0) + metabasetest.CreatePendingObject(ctx, t, db, pending, 0) object := metabase.RawObject{ ObjectStream: pending, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } - IteratePendingObjectsByKey{ + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: pending.Location(), }, Result: []metabase.ObjectEntry{objectEntryFromRaw(object)}, }.Check(ctx, t, db) - Verify{Objects: []metabase.RawObject{object}}.Check(ctx, t, db) + metabasetest.Verify{Objects: []metabase.RawObject{object}}.Check(ctx, t, db) }) t.Run("using streamID cursor", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - pending := []metabase.ObjectStream{randObjectStream(), randObjectStream(), randObjectStream()} + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + pending := []metabase.ObjectStream{metabasetest.RandObjectStream(), metabasetest.RandObjectStream(), metabasetest.RandObjectStream()} location := pending[0].Location() objects := make([]metabase.RawObject, 3) @@ -1205,23 +1206,23 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { obj.ObjectKey = location.ObjectKey obj.Version = metabase.Version(i + 1) - createPendingObject(ctx, t, db, obj, 0) + metabasetest.CreatePendingObject(ctx, t, db, obj, 0) objects[i] = metabase.RawObject{ ObjectStream: obj, CreatedAt: now, Status: metabase.Pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } expected[i] = objectEntryFromRaw(objects[i]) } sort.Slice(expected, func(i, j int) bool { - return less(expected[i].StreamID, expected[j].StreamID) + return expected[i].StreamID.Less(expected[j].StreamID) }) - IteratePendingObjectsByKey{ + metabasetest.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{ ObjectLocation: location, BatchSize: 10, @@ -1232,7 +1233,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { Result: expected[1:], }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) }) } @@ -1240,18 +1241,18 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) { func createObjects(ctx *testcontext.Context, t *testing.T, db *metabase.DB, numberOfObjects int, projectID uuid.UUID, bucketName string) []metabase.RawObject { objects := make([]metabase.RawObject, numberOfObjects) for i := 0; i < numberOfObjects; i++ { - obj := randObjectStream() + obj := metabasetest.RandObjectStream() obj.ProjectID = projectID obj.BucketName = bucketName now := time.Now() - createObject(ctx, t, db, obj, 0) + metabasetest.CreateObject(ctx, t, db, obj, 0) objects[i] = metabase.RawObject{ ObjectStream: obj, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } } sort.SliceStable(objects, func(i, j int) bool { @@ -1263,13 +1264,13 @@ func createObjects(ctx *testcontext.Context, t *testing.T, db *metabase.DB, numb func createObjectsWithKeys(ctx *testcontext.Context, t *testing.T, db *metabase.DB, projectID uuid.UUID, bucketName string, keys []metabase.ObjectKey) map[metabase.ObjectKey]metabase.ObjectEntry { objects := make(map[metabase.ObjectKey]metabase.ObjectEntry, len(keys)) for _, key := range keys { - obj := randObjectStream() + obj := metabasetest.RandObjectStream() obj.ProjectID = projectID obj.BucketName = bucketName obj.ObjectKey = key now := time.Now() - createObject(ctx, t, db, obj, 0) + metabasetest.CreateObject(ctx, t, db, obj, 0) objects[key] = metabase.ObjectEntry{ ObjectKey: obj.ObjectKey, @@ -1277,7 +1278,7 @@ func createObjectsWithKeys(ctx *testcontext.Context, t *testing.T, db *metabase. StreamID: obj.StreamID, CreatedAt: now, Status: metabase.Committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, } } diff --git a/satellite/metabase/list_segments_test.go b/satellite/metabase/list_segments_test.go index 407e0fc70..b50b6228d 100644 --- a/satellite/metabase/list_segments_test.go +++ b/satellite/metabase/list_segments_test.go @@ -11,29 +11,30 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestListSegments(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() t.Run("StreamID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Invalid limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: -1, @@ -42,13 +43,13 @@ func TestListSegments(t *testing.T) { ErrText: "Invalid limit: -1", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("no segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 1, @@ -56,13 +57,13 @@ func TestListSegments(t *testing.T) { Result: metabase.ListSegmentsResult{}, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - expectedObject := createObject(ctx, t, db, obj, 10) + expectedObject := metabasetest.CreateObject(ctx, t, db, obj, 10) expectedSegment := metabase.Segment{ StreamID: obj.StreamID, @@ -77,7 +78,7 @@ func TestListSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } expectedRawSegments := make([]metabase.RawSegment, 10) @@ -89,7 +90,7 @@ func TestListSegments(t *testing.T) { expectedSegment.PlainOffset += int64(expectedSegment.PlainSize) } - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 10, @@ -99,7 +100,7 @@ func TestListSegments(t *testing.T) { }, }.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 1, @@ -110,7 +111,7 @@ func TestListSegments(t *testing.T) { }, }.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 2, @@ -124,7 +125,7 @@ func TestListSegments(t *testing.T) { }, }.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 2, @@ -137,7 +138,7 @@ func TestListSegments(t *testing.T) { }, }.Check(ctx, t, db) - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 2, @@ -151,7 +152,7 @@ func TestListSegments(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ metabase.RawObject(expectedObject), }, @@ -160,7 +161,7 @@ func TestListSegments(t *testing.T) { }) t.Run("unordered parts", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) var testCases = []struct { segments []metabase.SegmentPosition @@ -195,22 +196,22 @@ func TestListSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } for _, tc := range testCases { - obj := randObjectStream() + obj := metabasetest.RandObjectStream() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) for i, segmentPosition := range tc.segments { - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Position: segmentPosition, @@ -222,7 +223,7 @@ func TestListSegments(t *testing.T) { }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: segmentPosition, @@ -236,12 +237,12 @@ func TestListSegments(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) } - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, @@ -257,7 +258,7 @@ func TestListSegments(t *testing.T) { expectedOffset += int64(expectedSegment.PlainSize) } - ListSegments{ + metabasetest.ListSegments{ Opts: metabase.ListSegments{ StreamID: obj.StreamID, Limit: 0, @@ -272,26 +273,26 @@ func TestListSegments(t *testing.T) { } func TestListStreamPositions(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() t.Run("StreamID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("Invalid limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: -1, @@ -300,13 +301,13 @@ func TestListStreamPositions(t *testing.T) { ErrText: "Invalid limit: -1", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("no segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 1, @@ -314,13 +315,13 @@ func TestListStreamPositions(t *testing.T) { Result: metabase.ListStreamPositionsResult{}, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - expectedObject := createObject(ctx, t, db, obj, 10) + expectedObject := metabasetest.CreateObject(ctx, t, db, obj, 10) expectedSegment := metabase.Segment{ StreamID: obj.StreamID, @@ -335,7 +336,7 @@ func TestListStreamPositions(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } expectedRawSegments := make([]metabase.RawSegment, 10) @@ -355,7 +356,7 @@ func TestListStreamPositions(t *testing.T) { expectedSegment.PlainOffset += int64(expectedSegment.PlainSize) } - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 10, @@ -365,7 +366,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 1, @@ -376,7 +377,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 2, @@ -390,7 +391,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 2, @@ -403,7 +404,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 2, @@ -417,7 +418,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ metabase.RawObject(expectedObject), }, @@ -426,7 +427,7 @@ func TestListStreamPositions(t *testing.T) { }) t.Run("unordered parts", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) var testCases = []struct { segments []metabase.SegmentPosition @@ -460,22 +461,22 @@ func TestListStreamPositions(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } for _, tc := range testCases { - obj := randObjectStream() + obj := metabasetest.RandObjectStream() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) for i, segmentPosition := range tc.segments { - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Position: segmentPosition, @@ -487,7 +488,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: segmentPosition, @@ -501,12 +502,12 @@ func TestListStreamPositions(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) } - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, @@ -529,7 +530,7 @@ func TestListStreamPositions(t *testing.T) { expectedOffset += int64(expectedSegment.PlainSize) } - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: 0, @@ -542,7 +543,7 @@ func TestListStreamPositions(t *testing.T) { }) t.Run("range", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) const segmentCount = 10 const segmentSize = 512 @@ -556,15 +557,15 @@ func TestListStreamPositions(t *testing.T) { EncryptedSize: 1024, PlainSize: segmentSize, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } - obj := randObjectStream() + obj := metabasetest.RandObjectStream() - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: obj.Version, }.Check(ctx, t, db) @@ -575,7 +576,7 @@ func TestListStreamPositions(t *testing.T) { Index: uint32(i % 2), } - BeginSegment{ + metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ ObjectStream: obj, Position: segmentPosition, @@ -587,7 +588,7 @@ func TestListStreamPositions(t *testing.T) { }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: segmentPosition, @@ -601,12 +602,12 @@ func TestListStreamPositions(t *testing.T) { EncryptedSize: 1024, PlainSize: segmentSize, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) } - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: obj, }, @@ -631,7 +632,7 @@ func TestListStreamPositions(t *testing.T) { expectedOffset += int64(expectedSegment.PlainSize) } - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Range: &metabase.StreamRange{ @@ -669,7 +670,7 @@ func TestListStreamPositions(t *testing.T) { {limit: 2, plainStart: segmentSize, plainLimit: totalSize, results: expectedSegments[1:3], more: true}, } for _, test := range tests { - ListStreamPositions{ + metabasetest.ListStreamPositions{ Opts: metabase.ListStreamPositions{ StreamID: obj.StreamID, Limit: test.limit, diff --git a/satellite/metabase/loop_test.go b/satellite/metabase/loop_test.go index da26b36f9..54b6b9799 100644 --- a/satellite/metabase/loop_test.go +++ b/satellite/metabase/loop_test.go @@ -15,40 +15,41 @@ import ( "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestIterateLoopObjects(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { t.Run("Limit is negative", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) - IterateLoopObjects{ + defer metabasetest.DeleteAll{}.Check(ctx, t, db) + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: -1, }, ErrClass: &metabase.ErrInvalidRequest, ErrText: "BatchSize is negative", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("no data", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 0, }, Result: nil, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 10, }, Result: nil, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 10, AsOfSystemTime: time.Now(), @@ -56,21 +57,21 @@ func TestIterateLoopObjects(t *testing.T) { Result: nil, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("pending and committed", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - pending := randObjectStream() - committed := randObjectStream() + pending := metabasetest.RandObjectStream() + committed := metabasetest.RandObjectStream() committed.ProjectID = pending.ProjectID committed.BucketName = pending.BucketName + "z" - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: pending, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -79,15 +80,15 @@ func TestIterateLoopObjects(t *testing.T) { encryptedMetadataNonce := testrand.Nonce() encryptedMetadataKey := testrand.Bytes(265) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: committed, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) - CommitObject{ + metabasetest.CommitObject{ Opts: metabase.CommitObject{ ObjectStream: committed, EncryptedMetadataNonce: encryptedMetadataNonce[:], @@ -111,14 +112,14 @@ func TestIterateLoopObjects(t *testing.T) { }, } - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 1, }, Result: expected, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 1, AsOfSystemTime: time.Now(), @@ -128,7 +129,7 @@ func TestIterateLoopObjects(t *testing.T) { }) t.Run("less objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjects := 3 limit := 10 expected := make([]metabase.LoopObjectEntry, numberOfObjects) @@ -137,14 +138,14 @@ func TestIterateLoopObjects(t *testing.T) { expected[i] = loopObjectEntryFromRaw(obj) } - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: limit, }, Result: expected, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: limit, AsOfSystemTime: time.Now(), @@ -152,11 +153,11 @@ func TestIterateLoopObjects(t *testing.T) { Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("more objects than limit", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) numberOfObjects := 10 limit := 3 expected := make([]metabase.LoopObjectEntry, numberOfObjects) @@ -165,14 +166,14 @@ func TestIterateLoopObjects(t *testing.T) { expected[i] = loopObjectEntryFromRaw(obj) } - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: limit, }, Result: expected, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: limit, AsOfSystemTime: time.Now(), @@ -180,14 +181,14 @@ func TestIterateLoopObjects(t *testing.T) { Result: expected, }.Check(ctx, t, db) - Verify{Objects: objects}.Check(ctx, t, db) + metabasetest.Verify{Objects: objects}.Check(ctx, t, db) }) t.Run("recursive", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projectID, bucketName := uuid.UUID{1}, "bucky" - objects := createFullObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ + objects := metabasetest.CreateFullObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ "a", "b/1", "b/2", @@ -211,14 +212,14 @@ func TestIterateLoopObjects(t *testing.T) { objects["g"], } - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 3, }, Result: expected, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 3, AsOfSystemTime: time.Now(), @@ -228,7 +229,7 @@ func TestIterateLoopObjects(t *testing.T) { }) t.Run("multiple projects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projects := []uuid.UUID{} for i := 0; i < 10; i++ { @@ -248,14 +249,14 @@ func TestIterateLoopObjects(t *testing.T) { } } - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 3, }, Result: expected, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 3, AsOfSystemTime: time.Now(), @@ -265,7 +266,7 @@ func TestIterateLoopObjects(t *testing.T) { }) t.Run("multiple projects", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) projects := []uuid.UUID{} for i := 0; i < 10; i++ { @@ -278,25 +279,25 @@ func TestIterateLoopObjects(t *testing.T) { expected := make([]metabase.LoopObjectEntry, 0, len(projects)*len(bucketNames)) for _, projectID := range projects { for _, bucketName := range bucketNames { - obj := randObjectStream() + obj := metabasetest.RandObjectStream() obj.ProjectID = projectID obj.BucketName = bucketName for version := 1; version < 4; version++ { obj.Version = metabase.Version(version) - rawObject := createObject(ctx, t, db, obj, 0) + rawObject := metabasetest.CreateObject(ctx, t, db, obj, 0) expected = append(expected, loopObjectEntryFromRaw(metabase.RawObject(rawObject))) } } } - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 2, }, Result: expected, }.Check(ctx, t, db) - IterateLoopObjects{ + metabasetest.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{ BatchSize: 2, AsOfSystemTime: time.Now(), @@ -308,24 +309,24 @@ func TestIterateLoopObjects(t *testing.T) { } func TestIterateLoopStreams(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { t.Run("StreamIDs list is empty", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - IterateLoopStreams{ + metabasetest.IterateLoopStreams{ Opts: metabase.IterateLoopStreams{}, Result: map[uuid.UUID][]metabase.LoopSegmentEntry{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamIDs list is empty", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("StreamIDs list contains empty ID", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - IterateLoopStreams{ + metabasetest.IterateLoopStreams{ Opts: metabase.IterateLoopStreams{ StreamIDs: []uuid.UUID{{}}, }, @@ -334,18 +335,18 @@ func TestIterateLoopStreams(t *testing.T) { ErrText: "StreamID missing: index 0", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("List objects segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) now := time.Now() - expectedObject00 := createObject(ctx, t, db, randObjectStream(), 0) - expectedObject01 := createObject(ctx, t, db, randObjectStream(), 1) - expectedObject02 := createObject(ctx, t, db, randObjectStream(), 5) - expectedObject03 := createObject(ctx, t, db, randObjectStream(), 3) + expectedObject00 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 0) + expectedObject01 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 1) + expectedObject02 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 5) + expectedObject03 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 3) expectedRawSegments := []metabase.RawSegment{} @@ -375,7 +376,7 @@ func TestIterateLoopStreams(t *testing.T) { PlainSize: 512, PlainOffset: int64(i * 512), Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, } expectedSegments = append(expectedSegments, segment) @@ -397,7 +398,7 @@ func TestIterateLoopStreams(t *testing.T) { expectedMap[object.StreamID] = expectedSegments } - IterateLoopStreams{ + metabasetest.IterateLoopStreams{ Opts: metabase.IterateLoopStreams{ StreamIDs: []uuid.UUID{ expectedObject00.StreamID, @@ -411,7 +412,7 @@ func TestIterateLoopStreams(t *testing.T) { Result: expectedMap, }.Check(ctx, t, db) - IterateLoopStreams{ + metabasetest.IterateLoopStreams{ Opts: metabase.IterateLoopStreams{ StreamIDs: []uuid.UUID{ expectedObject00.StreamID, @@ -423,7 +424,7 @@ func TestIterateLoopStreams(t *testing.T) { Result: expectedMap, }.Check(ctx, t, db) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ metabase.RawObject(expectedObject00), metabase.RawObject(expectedObject01), @@ -436,26 +437,6 @@ func TestIterateLoopStreams(t *testing.T) { }) } -func createFullObjectsWithKeys(ctx *testcontext.Context, t *testing.T, db *metabase.DB, projectID uuid.UUID, bucketName string, keys []metabase.ObjectKey) map[metabase.ObjectKey]metabase.LoopObjectEntry { - objects := make(map[metabase.ObjectKey]metabase.LoopObjectEntry, len(keys)) - for _, key := range keys { - obj := randObjectStream() - obj.ProjectID = projectID - obj.BucketName = bucketName - obj.ObjectKey = key - - createObject(ctx, t, db, obj, 0) - - objects[key] = metabase.LoopObjectEntry{ - ObjectStream: obj, - Status: metabase.Committed, - CreatedAt: time.Now(), - } - } - - return objects -} - func loopObjectEntryFromRaw(m metabase.RawObject) metabase.LoopObjectEntry { return metabase.LoopObjectEntry{ ObjectStream: m.ObjectStream, diff --git a/satellite/metabase/metabasetest/common.go b/satellite/metabase/metabasetest/common.go new file mode 100644 index 000000000..3e5da84bd --- /dev/null +++ b/satellite/metabase/metabasetest/common.go @@ -0,0 +1,76 @@ +// Copyright (C) 2021 Storj Labs, Inc. +// See LICENSE for copying information. + +package metabasetest + +import ( + "bytes" + "sort" + "testing" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/stretchr/testify/require" + "github.com/zeebo/errs" + + "storj.io/common/testcontext" + "storj.io/storj/satellite/metabase" +) + +// DeleteAll deletes all data from metabase. +type DeleteAll struct{} + +// Check runs the test. +func (step DeleteAll) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { + err := db.TestingDeleteAll(ctx) + require.NoError(t, err) +} + +// Verify verifies whether metabase state matches the content. +type Verify metabase.RawState + +// Check runs the test. +func (step Verify) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { + state, err := db.TestingGetState(ctx) + require.NoError(t, err) + + sortRawObjects(state.Objects) + sortRawObjects(step.Objects) + sortRawSegments(state.Segments) + sortRawSegments(step.Segments) + + diff := cmp.Diff(metabase.RawState(step), *state, + cmpopts.EquateApproxTime(5*time.Second)) + require.Zero(t, diff) +} + +func sortObjects(objects []metabase.Object) { + sort.Slice(objects, func(i, j int) bool { + return bytes.Compare(objects[i].StreamID[:], objects[j].StreamID[:]) < 0 + }) +} + +func sortRawObjects(objects []metabase.RawObject) { + sort.Slice(objects, func(i, j int) bool { + return bytes.Compare(objects[i].StreamID[:], objects[j].StreamID[:]) < 0 + }) +} + +func sortRawSegments(segments []metabase.RawSegment) { + sort.Slice(segments, func(i, j int) bool { + return bytes.Compare(segments[i].StreamID[:], segments[j].StreamID[:]) < 0 + }) +} + +func checkError(t testing.TB, err error, errClass *errs.Class, errText string) { + if errClass != nil { + require.True(t, errClass.Has(err), "expected an error %v got %v", *errClass, err) + } + if errText != "" { + require.EqualError(t, err, errClass.New(errText).Error()) + } + if errClass == nil && errText == "" { + require.NoError(t, err) + } +} diff --git a/satellite/metabase/metabasetest/create.go b/satellite/metabase/metabasetest/create.go new file mode 100644 index 000000000..f7496fef0 --- /dev/null +++ b/satellite/metabase/metabasetest/create.go @@ -0,0 +1,255 @@ +// Copyright (C) 2021 Storj Labs, Inc. +// See LICENSE for copying information. + +package metabasetest + +import ( + "testing" + "time" + + "storj.io/common/storj" + "storj.io/common/testcontext" + "storj.io/common/testrand" + "storj.io/common/uuid" + "storj.io/storj/satellite/metabase" +) + +// RandObjectStream returns a random object stream. +func RandObjectStream() metabase.ObjectStream { + return metabase.ObjectStream{ + ProjectID: testrand.UUID(), + BucketName: testrand.BucketName(), + ObjectKey: metabase.ObjectKey(testrand.Bytes(16)), + Version: 1, + StreamID: testrand.UUID(), + } +} + +// CreatePendingObject creates a new pending object with the specified number of segments. +func CreatePendingObject(ctx *testcontext.Context, t *testing.T, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte) { + BeginObjectExactVersion{ + Opts: metabase.BeginObjectExactVersion{ + ObjectStream: obj, + Encryption: DefaultEncryption, + }, + Version: obj.Version, + }.Check(ctx, t, db) + + for i := byte(0); i < numberOfSegments; i++ { + BeginSegment{ + Opts: metabase.BeginSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{i + 1}, + Pieces: []metabase.Piece{{ + Number: 1, + StorageNode: testrand.NodeID(), + }}, + }, + }.Check(ctx, t, db) + + CommitSegment{ + Opts: metabase.CommitSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{1}, + Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, + + EncryptedKey: []byte{3}, + EncryptedKeyNonce: []byte{4}, + EncryptedETag: []byte{5}, + + EncryptedSize: 1024, + PlainSize: 512, + PlainOffset: 0, + Redundancy: DefaultRedundancy, + }, + }.Check(ctx, t, db) + } +} + +// CreateObject creates a new committed object with the specified number of segments. +func CreateObject(ctx *testcontext.Context, t *testing.T, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte) metabase.Object { + BeginObjectExactVersion{ + Opts: metabase.BeginObjectExactVersion{ + ObjectStream: obj, + Encryption: DefaultEncryption, + }, + Version: obj.Version, + }.Check(ctx, t, db) + + for i := byte(0); i < numberOfSegments; i++ { + BeginSegment{ + Opts: metabase.BeginSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{i + 1}, + Pieces: []metabase.Piece{{ + Number: 1, + StorageNode: testrand.NodeID(), + }}, + }, + }.Check(ctx, t, db) + + CommitSegment{ + Opts: metabase.CommitSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{1}, + Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, + + EncryptedKey: []byte{3}, + EncryptedKeyNonce: []byte{4}, + EncryptedETag: []byte{5}, + + EncryptedSize: 1024, + PlainSize: 512, + PlainOffset: 0, + Redundancy: DefaultRedundancy, + }, + }.Check(ctx, t, db) + } + + return CommitObject{ + Opts: metabase.CommitObject{ + ObjectStream: obj, + }, + }.Check(ctx, t, db) +} + +// CreateExpiredObject creates a new committed expired object with the specified number of segments. +func CreateExpiredObject(ctx *testcontext.Context, t *testing.T, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte, expiresAt time.Time) metabase.Object { + BeginObjectExactVersion{ + Opts: metabase.BeginObjectExactVersion{ + ObjectStream: obj, + Encryption: DefaultEncryption, + ExpiresAt: &expiresAt, + }, + Version: obj.Version, + }.Check(ctx, t, db) + + for i := byte(0); i < numberOfSegments; i++ { + BeginSegment{ + Opts: metabase.BeginSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{i + 1}, + Pieces: []metabase.Piece{{ + Number: 1, + StorageNode: testrand.NodeID(), + }}, + }, + }.Check(ctx, t, db) + + CommitSegment{ + Opts: metabase.CommitSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{1}, + Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, + + EncryptedKey: []byte{3}, + EncryptedKeyNonce: []byte{4}, + EncryptedETag: []byte{5}, + + EncryptedSize: 1024, + PlainSize: 512, + PlainOffset: 0, + Redundancy: DefaultRedundancy, + }, + }.Check(ctx, t, db) + } + + return CommitObject{ + Opts: metabase.CommitObject{ + ObjectStream: obj, + }, + }.Check(ctx, t, db) +} + +// CreateFullObjectsWithKeys creates multiple objects with the specified keys. +func CreateFullObjectsWithKeys(ctx *testcontext.Context, t *testing.T, db *metabase.DB, projectID uuid.UUID, bucketName string, keys []metabase.ObjectKey) map[metabase.ObjectKey]metabase.LoopObjectEntry { + objects := make(map[metabase.ObjectKey]metabase.LoopObjectEntry, len(keys)) + for _, key := range keys { + obj := RandObjectStream() + obj.ProjectID = projectID + obj.BucketName = bucketName + obj.ObjectKey = key + + CreateObject(ctx, t, db, obj, 0) + + objects[key] = metabase.LoopObjectEntry{ + ObjectStream: obj, + Status: metabase.Committed, + CreatedAt: time.Now(), + } + } + + return objects +} + +// CreateTestObject is for testing metabase.CreateTestObject. +type CreateTestObject struct { + BeginObjectExactVersion *metabase.BeginObjectExactVersion + CommitObject *metabase.CommitObject + // TODO add BeginSegment, CommitSegment +} + +// Run runs the test. +func (co CreateTestObject) Run(ctx *testcontext.Context, t testing.TB, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte) metabase.Object { + boeOpts := metabase.BeginObjectExactVersion{ + ObjectStream: obj, + Encryption: DefaultEncryption, + } + if co.BeginObjectExactVersion != nil { + boeOpts = *co.BeginObjectExactVersion + } + + BeginObjectExactVersion{ + Opts: boeOpts, + Version: obj.Version, + }.Check(ctx, t, db) + + for i := byte(0); i < numberOfSegments; i++ { + BeginSegment{ + Opts: metabase.BeginSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{i + 1}, + Pieces: []metabase.Piece{{ + Number: 1, + StorageNode: testrand.NodeID(), + }}, + }, + }.Check(ctx, t, db) + + CommitSegment{ + Opts: metabase.CommitSegment{ + ObjectStream: obj, + Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, + RootPieceID: storj.PieceID{1}, + Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, + + EncryptedKey: []byte{3}, + EncryptedKeyNonce: []byte{4}, + EncryptedETag: []byte{5}, + + EncryptedSize: 1060, + PlainSize: 512, + PlainOffset: int64(i) * 512, + Redundancy: DefaultRedundancy, + }, + }.Check(ctx, t, db) + } + + coOpts := metabase.CommitObject{ + ObjectStream: obj, + } + if co.CommitObject != nil { + coOpts = *co.CommitObject + } + + return CommitObject{ + Opts: coOpts, + }.Check(ctx, t, db) +} diff --git a/satellite/metabase/metabasetest/defaults.go b/satellite/metabase/metabasetest/defaults.go new file mode 100644 index 000000000..bc0387b88 --- /dev/null +++ b/satellite/metabase/metabasetest/defaults.go @@ -0,0 +1,22 @@ +// Copyright (C) 2021 Storj Labs, Inc. +// See LICENSE for copying information. + +package metabasetest + +import "storj.io/common/storj" + +// DefaultRedundancy contains default redundancy scheme. +var DefaultRedundancy = storj.RedundancyScheme{ + Algorithm: storj.ReedSolomon, + ShareSize: 2048, + RequiredShares: 1, + RepairShares: 1, + OptimalShares: 1, + TotalShares: 1, +} + +// DefaultEncryption contains default encryption parameters. +var DefaultEncryption = storj.EncryptionParameters{ + CipherSuite: storj.EncAESGCM, + BlockSize: 29 * 256, +} diff --git a/satellite/metabase/metabasetest/invalid.go b/satellite/metabase/metabasetest/invalid.go new file mode 100644 index 000000000..7904c34bb --- /dev/null +++ b/satellite/metabase/metabasetest/invalid.go @@ -0,0 +1,166 @@ +// Copyright (C) 2021 Storj Labs, Inc. +// See LICENSE for copying information. + +package metabasetest + +import ( + "github.com/zeebo/errs" + + "storj.io/common/uuid" + "storj.io/storj/satellite/metabase" +) + +// InvalidObjectStream contains info about an invalid stream. +type InvalidObjectStream struct { + Name string + ObjectStream metabase.ObjectStream + ErrClass *errs.Class + ErrText string +} + +// InvalidObjectStreams returns a list of invalid object streams. +func InvalidObjectStreams(base metabase.ObjectStream) []InvalidObjectStream { + var tests []InvalidObjectStream + { + stream := base + stream.ProjectID = uuid.UUID{} + tests = append(tests, InvalidObjectStream{ + Name: "ProjectID missing", + ObjectStream: stream, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "ProjectID missing", + }) + } + { + stream := base + stream.BucketName = "" + tests = append(tests, InvalidObjectStream{ + Name: "BucketName missing", + ObjectStream: stream, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "BucketName missing", + }) + } + { + stream := base + stream.ObjectKey = "" + tests = append(tests, InvalidObjectStream{ + Name: "ObjectKey missing", + ObjectStream: stream, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "ObjectKey missing", + }) + } + { + stream := base + stream.Version = -1 + tests = append(tests, InvalidObjectStream{ + Name: "Version invalid", + ObjectStream: stream, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "Version invalid: -1", + }) + } + { + stream := base + stream.StreamID = uuid.UUID{} + tests = append(tests, InvalidObjectStream{ + Name: "StreamID missing", + ObjectStream: stream, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "StreamID missing", + }) + } + + return tests +} + +// InvalidObjectLocation contains info about an invalid object location. +type InvalidObjectLocation struct { + Name string + ObjectLocation metabase.ObjectLocation + ErrClass *errs.Class + ErrText string +} + +// InvalidObjectLocations returns a list of invalid object locations. +func InvalidObjectLocations(base metabase.ObjectLocation) []InvalidObjectLocation { + var tests []InvalidObjectLocation + { + location := base + location.ProjectID = uuid.UUID{} + tests = append(tests, InvalidObjectLocation{ + Name: "ProjectID missing", + ObjectLocation: location, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "ProjectID missing", + }) + } + { + location := base + location.BucketName = "" + tests = append(tests, InvalidObjectLocation{ + Name: "BucketName missing", + ObjectLocation: location, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "BucketName missing", + }) + } + { + location := base + location.ObjectKey = "" + tests = append(tests, InvalidObjectLocation{ + Name: "ObjectKey missing", + ObjectLocation: location, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "ObjectKey missing", + }) + } + + return tests +} + +// InvalidSegmentLocation contains info about an invalid segment location. +type InvalidSegmentLocation struct { + Name string + SegmentLocation metabase.SegmentLocation + ErrClass *errs.Class + ErrText string +} + +// InvalidSegmentLocations returns a list of invalid segment locations. +func InvalidSegmentLocations(base metabase.SegmentLocation) []InvalidSegmentLocation { + var tests []InvalidSegmentLocation + { + location := base + location.ProjectID = uuid.UUID{} + tests = append(tests, InvalidSegmentLocation{ + Name: "ProjectID missing", + SegmentLocation: location, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "ProjectID missing", + }) + } + { + location := base + location.BucketName = "" + tests = append(tests, InvalidSegmentLocation{ + Name: "BucketName missing", + SegmentLocation: location, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "BucketName missing", + }) + } + { + location := base + location.ObjectKey = "" + tests = append(tests, InvalidSegmentLocation{ + Name: "ObjectKey missing", + SegmentLocation: location, + ErrClass: &metabase.ErrInvalidRequest, + ErrText: "ObjectKey missing", + }) + } + + return tests +} diff --git a/satellite/metabase/db_test.go b/satellite/metabase/metabasetest/run.go similarity index 59% rename from satellite/metabase/db_test.go rename to satellite/metabase/metabasetest/run.go index 3c27bde0d..19bef7a14 100644 --- a/satellite/metabase/db_test.go +++ b/satellite/metabase/metabasetest/run.go @@ -1,7 +1,7 @@ -// Copyright (C) 2020 Storj Labs, Inc. +// Copyright (C) 2021 Storj Labs, Inc. // See LICENSE for copying information. -package metabase_test +package metabasetest import ( "flag" @@ -9,50 +9,51 @@ import ( "strings" "testing" - "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" "storj.io/common/testcontext" - _ "storj.io/private/dbutil/cockroachutil" // register cockroach driver "storj.io/storj/satellite/metabase" "storj.io/storj/satellite/satellitedb/satellitedbtest" ) -var databases = flag.String("databases", os.Getenv("STORJ_TEST_DATABASES"), "databases to use for testing") +var databasesFlag = flag.String("databases", os.Getenv("STORJ_TEST_DATABASES"), "databases to use for testing") -type dbinfo struct { - name string - driver string - connstr string +// Database contains info about a test database connection. +type Database struct { + Name string + Driver string + ConnStr string } -func databaseInfos() []dbinfo { - infos := []dbinfo{ +// DatabaseEntries returns databases passed in with -databases or STORJ_TEST_DATABASES flag. +func DatabaseEntries() []Database { + infos := []Database{ {"pg", "pgx", "postgres://storj:storj-pass@localhost/metabase?sslmode=disable"}, {"crdb", "pgx", "cockroach://root@localhost:26257/metabase?sslmode=disable"}, } - if *databases != "" { + if *databasesFlag != "" { infos = nil - for _, db := range strings.Split(*databases, ";") { + for _, db := range strings.Split(*databasesFlag, ";") { toks := strings.Split(strings.TrimSpace(db), "|") - infos = append(infos, dbinfo{toks[0], toks[1], toks[2]}) + infos = append(infos, Database{toks[0], toks[1], toks[2]}) } } return infos } -func All(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) { - for _, info := range databaseInfos() { +// Run runs tests against all configured databases. +func Run(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) { + for _, info := range DatabaseEntries() { info := info - t.Run(info.name, func(t *testing.T) { + t.Run(info.Name, func(t *testing.T) { t.Parallel() ctx := testcontext.New(t) defer ctx.Cleanup() db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{ - Name: info.name, - URL: info.connstr, + Name: info.Name, + URL: info.ConnStr, Message: "", }) if err != nil { @@ -73,16 +74,17 @@ func All(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metab } } +// Bench runs benchmark for all configured databases. func Bench(b *testing.B, fn func(ctx *testcontext.Context, b *testing.B, db *metabase.DB)) { - for _, info := range databaseInfos() { + for _, info := range DatabaseEntries() { info := info - b.Run(info.name, func(b *testing.B) { + b.Run(info.Name, func(b *testing.B) { ctx := testcontext.New(b) defer ctx.Cleanup() db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, satellitedbtest.Database{ - Name: info.name, - URL: info.connstr, + Name: info.Name, + URL: info.ConnStr, Message: "", }) if err != nil { @@ -103,13 +105,3 @@ func Bench(b *testing.B, fn func(ctx *testcontext.Context, b *testing.B, db *met }) } } - -func TestSetup(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - err := db.Ping(ctx) - require.NoError(t, err) - - _, err = db.TestingGetState(ctx) - require.NoError(t, err) - }) -} diff --git a/satellite/metabase/metabasetest/run_test.go b/satellite/metabase/metabasetest/run_test.go new file mode 100644 index 000000000..7e4b9487d --- /dev/null +++ b/satellite/metabase/metabasetest/run_test.go @@ -0,0 +1,25 @@ +// Copyright (C) 2020 Storj Labs, Inc. +// See LICENSE for copying information. + +package metabasetest_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "storj.io/common/testcontext" + _ "storj.io/private/dbutil/cockroachutil" // register cockroach driver + "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" +) + +func TestSetup(t *testing.T) { + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + err := db.Ping(ctx) + require.NoError(t, err) + + _, err = db.TestingGetState(ctx) + require.NoError(t, err) + }) +} diff --git a/satellite/metabase/test_test.go b/satellite/metabase/metabasetest/test.go similarity index 73% rename from satellite/metabase/test_test.go rename to satellite/metabase/metabasetest/test.go index a3f7d3b32..5f03cbb03 100644 --- a/satellite/metabase/test_test.go +++ b/satellite/metabase/metabasetest/test.go @@ -1,10 +1,9 @@ // Copyright (C) 2020 Storj Labs, Inc. // See LICENSE for copying information. -package metabase_test +package metabasetest import ( - "bytes" "context" "sort" "testing" @@ -17,11 +16,11 @@ import ( "storj.io/common/storj" "storj.io/common/testcontext" - "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite/metabase" ) +// BeginObjectNextVersion is for testing metabase.BeginObjectNextVersion. type BeginObjectNextVersion struct { Opts metabase.BeginObjectNextVersion Version metabase.Version @@ -29,12 +28,14 @@ type BeginObjectNextVersion struct { ErrText string } +// Check runs the test. func (step BeginObjectNextVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { got, err := db.BeginObjectNextVersion(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) require.Equal(t, step.Version, got) } +// BeginObjectExactVersion is for testing metabase.BeginObjectExactVersion. type BeginObjectExactVersion struct { Opts metabase.BeginObjectExactVersion Version metabase.Version @@ -42,6 +43,7 @@ type BeginObjectExactVersion struct { ErrText string } +// Check runs the test. func (step BeginObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { got, err := db.BeginObjectExactVersion(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -55,12 +57,14 @@ func (step BeginObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB } } +// CommitObject is for testing metabase.CommitObject. type CommitObject struct { Opts metabase.CommitObject ErrClass *errs.Class ErrText string } +// Check runs the test. func (step CommitObject) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) metabase.Object { object, err := db.CommitObject(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -70,6 +74,7 @@ func (step CommitObject) Check(ctx *testcontext.Context, t testing.TB, db *metab return object } +// CommitObjectWithSegments is for testing metabase.CommitObjectWithSegments. type CommitObjectWithSegments struct { Opts metabase.CommitObjectWithSegments Deleted []metabase.DeletedSegmentInfo @@ -77,6 +82,7 @@ type CommitObjectWithSegments struct { ErrText string } +// Check runs the test. func (step CommitObjectWithSegments) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) metabase.Object { object, deleted, err := db.CommitObjectWithSegments(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -87,39 +93,46 @@ func (step CommitObjectWithSegments) Check(ctx *testcontext.Context, t testing.T return object } +// BeginSegment is for testing metabase.BeginSegment. type BeginSegment struct { Opts metabase.BeginSegment ErrClass *errs.Class ErrText string } +// Check runs the test. func (step BeginSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.BeginSegment(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// CommitSegment is for testing metabase.CommitSegment. type CommitSegment struct { Opts metabase.CommitSegment ErrClass *errs.Class ErrText string } +// Check runs the test. func (step CommitSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.CommitSegment(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// CommitInlineSegment is for testing metabase.CommitInlineSegment. type CommitInlineSegment struct { Opts metabase.CommitInlineSegment ErrClass *errs.Class ErrText string } +// Check runs the test. func (step CommitInlineSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.CommitInlineSegment(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// DeleteBucketObjects is for testing metabase.DeleteBucketObjects. type DeleteBucketObjects struct { Opts metabase.DeleteBucketObjects Deleted int64 @@ -127,34 +140,40 @@ type DeleteBucketObjects struct { ErrText string } +// Check runs the test. func (step DeleteBucketObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { deleted, err := db.DeleteBucketObjects(ctx, step.Opts) require.Equal(t, step.Deleted, deleted) checkError(t, err, step.ErrClass, step.ErrText) } +// UpdateObjectMetadata is for testing metabase.UpdateObjectMetadata. type UpdateObjectMetadata struct { Opts metabase.UpdateObjectMetadata ErrClass *errs.Class ErrText string } +// Check runs the test. func (step UpdateObjectMetadata) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.UpdateObjectMetadata(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// UpdateSegmentPieces is for testing metabase.UpdateSegmentPieces. type UpdateSegmentPieces struct { Opts metabase.UpdateSegmentPieces ErrClass *errs.Class ErrText string } +// Check runs the test. func (step UpdateSegmentPieces) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.UpdateSegmentPieces(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// GetObjectExactVersion is for testing metabase.GetObjectExactVersion. type GetObjectExactVersion struct { Opts metabase.GetObjectExactVersion Result metabase.Object @@ -162,6 +181,7 @@ type GetObjectExactVersion struct { ErrText string } +// Check runs the test. func (step GetObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetObjectExactVersion(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -170,6 +190,7 @@ func (step GetObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, require.Zero(t, diff) } +// GetObjectLatestVersion is for testing metabase.GetObjectLatestVersion. type GetObjectLatestVersion struct { Opts metabase.GetObjectLatestVersion Result metabase.Object @@ -177,6 +198,7 @@ type GetObjectLatestVersion struct { ErrText string } +// Check runs the test. func (step GetObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetObjectLatestVersion(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -185,6 +207,7 @@ func (step GetObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, require.Zero(t, diff) } +// GetSegmentByLocation is for testing metabase.GetSegmentByLocation. type GetSegmentByLocation struct { Opts metabase.GetSegmentByLocation Result metabase.Segment @@ -192,6 +215,7 @@ type GetSegmentByLocation struct { ErrText string } +// Check runs the test. func (step GetSegmentByLocation) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetSegmentByLocation(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -200,6 +224,7 @@ func (step GetSegmentByLocation) Check(ctx *testcontext.Context, t testing.TB, d require.Zero(t, diff) } +// GetSegmentByPosition is for testing metabase.GetSegmentByPosition. type GetSegmentByPosition struct { Opts metabase.GetSegmentByPosition Result metabase.Segment @@ -207,6 +232,7 @@ type GetSegmentByPosition struct { ErrText string } +// Check runs the test. func (step GetSegmentByPosition) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetSegmentByPosition(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -215,6 +241,7 @@ func (step GetSegmentByPosition) Check(ctx *testcontext.Context, t testing.TB, d require.Zero(t, diff) } +// GetLatestObjectLastSegment is for testing metabase.GetLatestObjectLastSegment. type GetLatestObjectLastSegment struct { Opts metabase.GetLatestObjectLastSegment Result metabase.Segment @@ -222,6 +249,7 @@ type GetLatestObjectLastSegment struct { ErrText string } +// Check runs the test. func (step GetLatestObjectLastSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetLatestObjectLastSegment(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -230,6 +258,7 @@ func (step GetLatestObjectLastSegment) Check(ctx *testcontext.Context, t testing require.Zero(t, diff) } +// GetSegmentByOffset is for testing metabase.GetSegmentByOffset. type GetSegmentByOffset struct { Opts metabase.GetSegmentByOffset Result metabase.Segment @@ -237,6 +266,7 @@ type GetSegmentByOffset struct { ErrText string } +// Check runs the test. func (step GetSegmentByOffset) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetSegmentByOffset(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -245,6 +275,7 @@ func (step GetSegmentByOffset) Check(ctx *testcontext.Context, t testing.TB, db require.Zero(t, diff) } +// BucketEmpty is for testing metabase.BucketEmpty. type BucketEmpty struct { Opts metabase.BucketEmpty Result bool @@ -252,6 +283,7 @@ type BucketEmpty struct { ErrText string } +// Check runs the test. func (step BucketEmpty) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.BucketEmpty(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -259,6 +291,7 @@ func (step BucketEmpty) Check(ctx *testcontext.Context, t testing.TB, db *metaba require.Equal(t, step.Result, result) } +// ListSegments is for testing metabase.ListSegments. type ListSegments struct { Opts metabase.ListSegments Result metabase.ListSegmentsResult @@ -266,6 +299,7 @@ type ListSegments struct { ErrText string } +// Check runs the test. func (step ListSegments) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.ListSegments(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -274,6 +308,7 @@ func (step ListSegments) Check(ctx *testcontext.Context, t testing.TB, db *metab require.Zero(t, diff) } +// ListStreamPositions is for testing metabase.ListStreamPositions. type ListStreamPositions struct { Opts metabase.ListStreamPositions Result metabase.ListStreamPositionsResult @@ -281,6 +316,7 @@ type ListStreamPositions struct { ErrText string } +// Check runs the test. func (step ListStreamPositions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.ListStreamPositions(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -289,6 +325,7 @@ func (step ListStreamPositions) Check(ctx *testcontext.Context, t testing.TB, db require.Zero(t, diff) } +// GetStreamPieceCountByNodeID is for testing metabase.GetStreamPieceCountByNodeID. type GetStreamPieceCountByNodeID struct { Opts metabase.GetStreamPieceCountByNodeID Result map[storj.NodeID]int64 @@ -296,6 +333,7 @@ type GetStreamPieceCountByNodeID struct { ErrText string } +// Check runs the test. func (step GetStreamPieceCountByNodeID) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.GetStreamPieceCountByNodeID(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -304,6 +342,7 @@ func (step GetStreamPieceCountByNodeID) Check(ctx *testcontext.Context, t testin require.Zero(t, diff) } +// IterateLoopStreams is for testing metabase.IterateLoopStreams. type IterateLoopStreams struct { Opts metabase.IterateLoopStreams Result map[uuid.UUID][]metabase.LoopSegmentEntry @@ -311,6 +350,7 @@ type IterateLoopStreams struct { ErrText string } +// Check runs the test. func (step IterateLoopStreams) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result := make(map[uuid.UUID][]metabase.LoopSegmentEntry) err := db.IterateLoopStreams(ctx, step.Opts, @@ -332,6 +372,7 @@ func (step IterateLoopStreams) Check(ctx *testcontext.Context, t testing.TB, db require.Zero(t, diff) } +// DeleteObjectExactVersion is for testing metabase.DeleteObjectExactVersion. type DeleteObjectExactVersion struct { Opts metabase.DeleteObjectExactVersion Result metabase.DeleteObjectResult @@ -339,6 +380,7 @@ type DeleteObjectExactVersion struct { ErrText string } +// Check runs the test. func (step DeleteObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.DeleteObjectExactVersion(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -347,6 +389,7 @@ func (step DeleteObjectExactVersion) Check(ctx *testcontext.Context, t testing.T require.Zero(t, diff) } +// DeletePendingObject is for testing metabase.DeletePendingObject. type DeletePendingObject struct { Opts metabase.DeletePendingObject Result metabase.DeleteObjectResult @@ -354,6 +397,7 @@ type DeletePendingObject struct { ErrText string } +// Check runs the test. func (step DeletePendingObject) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.DeletePendingObject(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -362,6 +406,7 @@ func (step DeletePendingObject) Check(ctx *testcontext.Context, t testing.TB, db require.Zero(t, diff) } +// DeleteObjectLatestVersion is for testing metabase.DeleteObjectLatestVersion. type DeleteObjectLatestVersion struct { Opts metabase.DeleteObjectLatestVersion Result metabase.DeleteObjectResult @@ -369,6 +414,7 @@ type DeleteObjectLatestVersion struct { ErrText string } +// Check runs the test. func (step DeleteObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.DeleteObjectLatestVersion(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -377,6 +423,7 @@ func (step DeleteObjectLatestVersion) Check(ctx *testcontext.Context, t testing. require.Zero(t, diff) } +// DeleteObjectAnyStatusAllVersions is for testing metabase.DeleteObjectAnyStatusAllVersions. type DeleteObjectAnyStatusAllVersions struct { Opts metabase.DeleteObjectAnyStatusAllVersions Result metabase.DeleteObjectResult @@ -384,6 +431,7 @@ type DeleteObjectAnyStatusAllVersions struct { ErrText string } +// Check runs the test. func (step DeleteObjectAnyStatusAllVersions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.DeleteObjectAnyStatusAllVersions(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -392,6 +440,7 @@ func (step DeleteObjectAnyStatusAllVersions) Check(ctx *testcontext.Context, t t require.Zero(t, diff) } +// DeleteObjectsAllVersions is for testing metabase.DeleteObjectsAllVersions. type DeleteObjectsAllVersions struct { Opts metabase.DeleteObjectsAllVersions Result metabase.DeleteObjectResult @@ -399,6 +448,7 @@ type DeleteObjectsAllVersions struct { ErrText string } +// Check runs the test. func (step DeleteObjectsAllVersions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { result, err := db.DeleteObjectsAllVersions(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) @@ -410,6 +460,7 @@ func (step DeleteObjectsAllVersions) Check(ctx *testcontext.Context, t testing.T require.Zero(t, diff) } +// DeleteExpiredObjects is for testing metabase.DeleteExpiredObjects. type DeleteExpiredObjects struct { Opts metabase.DeleteExpiredObjects @@ -417,13 +468,16 @@ type DeleteExpiredObjects struct { ErrText string } +// Check runs the test. func (step DeleteExpiredObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.DeleteExpiredObjects(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// IterateCollector is for testing metabase.IterateCollector. type IterateCollector []metabase.ObjectEntry +// Add adds object entries from iterator to the collection. func (coll *IterateCollector) Add(ctx context.Context, it metabase.ObjectsIterator) error { var item metabase.ObjectEntry @@ -433,8 +487,10 @@ func (coll *IterateCollector) Add(ctx context.Context, it metabase.ObjectsIterat return nil } +// LoopIterateCollector is for testing metabase.LoopIterateCollector. type LoopIterateCollector []metabase.LoopObjectEntry +// Add adds object entries from iterator to the collection. func (coll *LoopIterateCollector) Add(ctx context.Context, it metabase.LoopObjectsIterator) error { var item metabase.LoopObjectEntry @@ -444,6 +500,7 @@ func (coll *LoopIterateCollector) Add(ctx context.Context, it metabase.LoopObjec return nil } +// IterateObjects is for testing metabase.IterateObjects. type IterateObjects struct { Opts metabase.IterateObjects @@ -452,6 +509,7 @@ type IterateObjects struct { ErrText string } +// Check runs the test. func (step IterateObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { var collector IterateCollector @@ -466,6 +524,7 @@ func (step IterateObjects) Check(ctx *testcontext.Context, t testing.TB, db *met require.Zero(t, diff) } +// IteratePendingObjectsByKey is for testing metabase.IteratePendingObjectsByKey. type IteratePendingObjectsByKey struct { Opts metabase.IteratePendingObjectsByKey @@ -474,6 +533,7 @@ type IteratePendingObjectsByKey struct { ErrText string } +// Check runs the test. func (step IteratePendingObjectsByKey) Check(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { var collector IterateCollector @@ -486,6 +546,7 @@ func (step IteratePendingObjectsByKey) Check(ctx *testcontext.Context, t *testin require.Zero(t, diff) } +// IterateObjectsWithStatus is for testing metabase.IterateObjectsWithStatus. type IterateObjectsWithStatus struct { Opts metabase.IterateObjectsWithStatus @@ -494,6 +555,7 @@ type IterateObjectsWithStatus struct { ErrText string } +// Check runs the test. func (step IterateObjectsWithStatus) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { var result IterateCollector @@ -504,6 +566,7 @@ func (step IterateObjectsWithStatus) Check(ctx *testcontext.Context, t testing.T require.Zero(t, diff) } +// IterateLoopObjects is for testing metabase.IterateLoopObjects. type IterateLoopObjects struct { Opts metabase.IterateLoopObjects @@ -512,6 +575,7 @@ type IterateLoopObjects struct { ErrText string } +// Check runs the test. func (step IterateLoopObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { var result LoopIterateCollector @@ -522,6 +586,7 @@ func (step IterateLoopObjects) Check(ctx *testcontext.Context, t testing.TB, db require.Zero(t, diff) } +// EnsureNodeAliases is for testing metabase.EnsureNodeAliases. type EnsureNodeAliases struct { Opts metabase.EnsureNodeAliases @@ -529,233 +594,21 @@ type EnsureNodeAliases struct { ErrText string } +// Check runs the test. func (step EnsureNodeAliases) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { err := db.EnsureNodeAliases(ctx, step.Opts) checkError(t, err, step.ErrClass, step.ErrText) } +// ListNodeAliases is for testing metabase.ListNodeAliases. type ListNodeAliases struct { ErrClass *errs.Class ErrText string } +// Check runs the test. func (step ListNodeAliases) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) []metabase.NodeAliasEntry { result, err := db.ListNodeAliases(ctx) checkError(t, err, step.ErrClass, step.ErrText) return result } - -func checkError(t testing.TB, err error, errClass *errs.Class, errText string) { - if errClass != nil { - require.True(t, errClass.Has(err), "expected an error %v got %v", *errClass, err) - } - if errText != "" { - require.EqualError(t, err, errClass.New(errText).Error()) - } - if errClass == nil && errText == "" { - require.NoError(t, err) - } -} - -// less returns true if uuid 'id' is less than uuid 'other'. -func less(id uuid.UUID, other uuid.UUID) bool { - for k, v := range id { - if v < other[k] { - return true - } else if v > other[k] { - return false - } - } - return false -} - -func sortObjects(objects []metabase.Object) { - sort.Slice(objects, func(i, j int) bool { - return bytes.Compare(objects[i].StreamID[:], objects[j].StreamID[:]) < 0 - }) -} - -func sortRawObjects(objects []metabase.RawObject) { - sort.Slice(objects, func(i, j int) bool { - return bytes.Compare(objects[i].StreamID[:], objects[j].StreamID[:]) < 0 - }) -} - -func sortRawSegments(segments []metabase.RawSegment) { - sort.Slice(segments, func(i, j int) bool { - return bytes.Compare(segments[i].StreamID[:], segments[j].StreamID[:]) < 0 - }) -} - -type DeleteAll struct{} - -func (step DeleteAll) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { - err := db.TestingDeleteAll(ctx) - require.NoError(t, err) -} - -type Verify metabase.RawState - -func (step Verify) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { - state, err := db.TestingGetState(ctx) - require.NoError(t, err) - - sortRawObjects(state.Objects) - sortRawObjects(step.Objects) - sortRawSegments(state.Segments) - sortRawSegments(step.Segments) - - diff := cmp.Diff(metabase.RawState(step), *state, - cmpopts.EquateApproxTime(5*time.Second)) - require.Zero(t, diff) -} - -type CreateTestObject struct { - BeginObjectExactVersion *metabase.BeginObjectExactVersion - CommitObject *metabase.CommitObject - // TODO add BeginSegment, CommitSegment -} - -func (co CreateTestObject) Run(ctx *testcontext.Context, t testing.TB, db *metabase.DB, obj metabase.ObjectStream, numberOfSegments byte) metabase.Object { - boeOpts := metabase.BeginObjectExactVersion{ - ObjectStream: obj, - Encryption: defaultTestEncryption, - } - if co.BeginObjectExactVersion != nil { - boeOpts = *co.BeginObjectExactVersion - } - - BeginObjectExactVersion{ - Opts: boeOpts, - Version: obj.Version, - }.Check(ctx, t, db) - - for i := byte(0); i < numberOfSegments; i++ { - BeginSegment{ - Opts: metabase.BeginSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{i + 1}, - Pieces: []metabase.Piece{{ - Number: 1, - StorageNode: testrand.NodeID(), - }}, - }, - }.Check(ctx, t, db) - - CommitSegment{ - Opts: metabase.CommitSegment{ - ObjectStream: obj, - Position: metabase.SegmentPosition{Part: 0, Index: uint32(i)}, - RootPieceID: storj.PieceID{1}, - Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, - - EncryptedKey: []byte{3}, - EncryptedKeyNonce: []byte{4}, - EncryptedETag: []byte{5}, - - EncryptedSize: 1060, - PlainSize: 512, - PlainOffset: int64(i) * 512, - Redundancy: defaultTestRedundancy, - }, - }.Check(ctx, t, db) - } - - coOpts := metabase.CommitObject{ - ObjectStream: obj, - } - if co.CommitObject != nil { - coOpts = *co.CommitObject - } - - return CommitObject{ - Opts: coOpts, - }.Check(ctx, t, db) -} - -type invalidObjectLocation struct { - Name string - ObjectLocation metabase.ObjectLocation - ErrClass *errs.Class - ErrText string -} - -func invalidObjectLocations(base metabase.ObjectLocation) []invalidObjectLocation { - var tests []invalidObjectLocation - { - location := base - location.ProjectID = uuid.UUID{} - tests = append(tests, invalidObjectLocation{ - Name: "ProjectID missing", - ObjectLocation: location, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ProjectID missing", - }) - } - { - location := base - location.BucketName = "" - tests = append(tests, invalidObjectLocation{ - Name: "BucketName missing", - ObjectLocation: location, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "BucketName missing", - }) - } - { - location := base - location.ObjectKey = "" - tests = append(tests, invalidObjectLocation{ - Name: "ObjectKey missing", - ObjectLocation: location, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ObjectKey missing", - }) - } - - return tests -} - -type invalidSegmentLocation struct { - Name string - SegmentLocation metabase.SegmentLocation - ErrClass *errs.Class - ErrText string -} - -func invalidSegmentLocations(base metabase.SegmentLocation) []invalidSegmentLocation { - var tests []invalidSegmentLocation - { - location := base - location.ProjectID = uuid.UUID{} - tests = append(tests, invalidSegmentLocation{ - Name: "ProjectID missing", - SegmentLocation: location, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ProjectID missing", - }) - } - { - location := base - location.BucketName = "" - tests = append(tests, invalidSegmentLocation{ - Name: "BucketName missing", - SegmentLocation: location, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "BucketName missing", - }) - } - { - location := base - location.ObjectKey = "" - tests = append(tests, invalidSegmentLocation{ - Name: "ObjectKey missing", - SegmentLocation: location, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ObjectKey missing", - }) - } - - return tests -} diff --git a/satellite/metabase/streamstat_test.go b/satellite/metabase/streamstat_test.go index c5094861f..c43bac4e5 100644 --- a/satellite/metabase/streamstat_test.go +++ b/satellite/metabase/streamstat_test.go @@ -10,44 +10,45 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" ) func TestGetStreamPieceCountByNodeID(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() t.Run("StreamID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetStreamPieceCountByNodeID{ + metabasetest.GetStreamPieceCountByNodeID{ Opts: metabase.GetStreamPieceCountByNodeID{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamID missing", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("no segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - GetStreamPieceCountByNodeID{ + metabasetest.GetStreamPieceCountByNodeID{ Opts: metabase.GetStreamPieceCountByNodeID{ StreamID: obj.StreamID, }, Result: map[storj.NodeID]int64{}, }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("inline segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -55,7 +56,7 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) { encryptedKey := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32) - CommitInlineSegment{ + metabasetest.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -69,7 +70,7 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) { }, }.Check(ctx, t, db) - GetStreamPieceCountByNodeID{ + metabasetest.GetStreamPieceCountByNodeID{ Opts: metabase.GetStreamPieceCountByNodeID{ StreamID: obj.StreamID, }, @@ -78,12 +79,12 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) { }) t.Run("remote segments", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - BeginObjectExactVersion{ + metabasetest.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{ ObjectStream: obj, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, Version: 1, }.Check(ctx, t, db) @@ -95,7 +96,7 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) { n02 := testrand.NodeID() n03 := testrand.NodeID() - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 0, Index: 0}, @@ -112,11 +113,11 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - CommitSegment{ + metabasetest.CommitSegment{ Opts: metabase.CommitSegment{ ObjectStream: obj, Position: metabase.SegmentPosition{Part: 1, Index: 56}, @@ -134,11 +135,11 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) { EncryptedSize: 1024, PlainSize: 512, PlainOffset: 0, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, }, }.Check(ctx, t, db) - GetStreamPieceCountByNodeID{ + metabasetest.GetStreamPieceCountByNodeID{ Opts: metabase.GetStreamPieceCountByNodeID{ StreamID: obj.StreamID, }, diff --git a/satellite/metabase/update_test.go b/satellite/metabase/update_test.go index df5bbfa5d..5bd95de4a 100644 --- a/satellite/metabase/update_test.go +++ b/satellite/metabase/update_test.go @@ -15,12 +15,13 @@ import ( "storj.io/common/testcontext" "storj.io/common/testrand" "storj.io/storj/satellite/metabase" + "storj.io/storj/satellite/metabase/metabasetest" "storj.io/storj/storage" ) func TestUpdateSegmentPieces(t *testing.T) { - All(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := randObjectStream() + metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { + obj := metabasetest.RandObjectStream() now := time.Now() @@ -30,9 +31,9 @@ func TestUpdateSegmentPieces(t *testing.T) { }} t.Run("StreamID missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{}, ErrClass: &metabase.ErrInvalidRequest, ErrText: "StreamID missing", @@ -40,9 +41,9 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("OldPieces missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, }, @@ -52,9 +53,9 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("OldPieces: piece number 1 is missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: []metabase.Piece{{ @@ -65,13 +66,13 @@ func TestUpdateSegmentPieces(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "OldPieces: piece number 1 is missing storage node id", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("OldPieces: duplicated piece number 1", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: []metabase.Piece{ @@ -88,13 +89,13 @@ func TestUpdateSegmentPieces(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "OldPieces: duplicated piece number 1", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("OldPieces: pieces should be ordered", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: []metabase.Piece{ @@ -111,13 +112,13 @@ func TestUpdateSegmentPieces(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "OldPieces: pieces should be ordered", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("NewRedundancy zero", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: []metabase.Piece{{ @@ -131,16 +132,16 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("NewPieces vs NewRedundancy", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: []metabase.Piece{{ Number: 1, StorageNode: testrand.NodeID(), }}, - NewRedundancy: defaultTestRedundancy, + NewRedundancy: metabasetest.DefaultRedundancy, }, ErrClass: &metabase.ErrInvalidRequest, ErrText: "number of new pieces is less than new redundancy repair shares value", @@ -148,13 +149,13 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("NewPieces: piece number 1 is missing", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: validPieces, - NewRedundancy: defaultTestRedundancy, + NewRedundancy: metabasetest.DefaultRedundancy, NewPieces: []metabase.Piece{{ Number: 1, StorageNode: storj.NodeID{}, @@ -163,17 +164,17 @@ func TestUpdateSegmentPieces(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "NewPieces: piece number 1 is missing storage node id", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("NewPieces: duplicated piece number 1", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: validPieces, - NewRedundancy: defaultTestRedundancy, + NewRedundancy: metabasetest.DefaultRedundancy, NewPieces: []metabase.Piece{ { Number: 1, @@ -188,17 +189,17 @@ func TestUpdateSegmentPieces(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "NewPieces: duplicated piece number 1", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("NewPieces: pieces should be ordered", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, OldPieces: validPieces, - NewRedundancy: defaultTestRedundancy, + NewRedundancy: metabasetest.DefaultRedundancy, NewPieces: []metabase.Piece{ { Number: 2, @@ -213,18 +214,18 @@ func TestUpdateSegmentPieces(t *testing.T) { ErrClass: &metabase.ErrInvalidRequest, ErrText: "NewPieces: pieces should be ordered", }.Check(ctx, t, db) - Verify{}.Check(ctx, t, db) + metabasetest.Verify{}.Check(ctx, t, db) }) t.Run("segment not found", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, Position: metabase.SegmentPosition{Index: 1}, OldPieces: validPieces, - NewRedundancy: defaultTestRedundancy, + NewRedundancy: metabasetest.DefaultRedundancy, NewPieces: validPieces, }, ErrClass: &metabase.ErrSegmentNotFound, @@ -233,9 +234,9 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("segment pieces column was changed", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - obj := createObject(ctx, t, db, obj, 1) + obj := metabasetest.CreateObject(ctx, t, db, obj, 1) newRedundancy := storj.RedundancyScheme{ RequiredShares: 1, @@ -244,7 +245,7 @@ func TestUpdateSegmentPieces(t *testing.T) { TotalShares: 4, } - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, Position: metabase.SegmentPosition{Index: 0}, @@ -262,7 +263,7 @@ func TestUpdateSegmentPieces(t *testing.T) { }.Check(ctx, t, db) // verify that original pieces and redundancy did not change - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ metabase.RawObject(obj), }, @@ -278,7 +279,7 @@ func TestUpdateSegmentPieces(t *testing.T) { PlainOffset: 0, PlainSize: 512, - Redundancy: defaultTestRedundancy, + Redundancy: metabasetest.DefaultRedundancy, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, }, }, @@ -286,9 +287,9 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("update pieces", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 1) + object := metabasetest.CreateObject(ctx, t, db, obj, 1) segment, err := db.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{ StreamID: object.StreamID, @@ -307,19 +308,19 @@ func TestUpdateSegmentPieces(t *testing.T) { }, } - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, Position: metabase.SegmentPosition{Index: 0}, OldPieces: segment.Pieces, - NewRedundancy: defaultTestRedundancy, + NewRedundancy: metabasetest.DefaultRedundancy, NewPieces: expectedPieces, }, }.Check(ctx, t, db) expectedSegment := segment expectedSegment.Pieces = expectedPieces - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -331,7 +332,7 @@ func TestUpdateSegmentPieces(t *testing.T) { TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{ @@ -341,9 +342,9 @@ func TestUpdateSegmentPieces(t *testing.T) { }) t.Run("update pieces and repair at", func(t *testing.T) { - defer DeleteAll{}.Check(ctx, t, db) + defer metabasetest.DeleteAll{}.Check(ctx, t, db) - object := createObject(ctx, t, db, obj, 1) + object := metabasetest.CreateObject(ctx, t, db, obj, 1) segment, err := db.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{ StreamID: object.StreamID, @@ -363,7 +364,7 @@ func TestUpdateSegmentPieces(t *testing.T) { } repairedAt := now.Add(time.Hour) - UpdateSegmentPieces{ + metabasetest.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{ StreamID: obj.StreamID, Position: metabase.SegmentPosition{Index: 0}, @@ -406,7 +407,7 @@ func TestUpdateSegmentPieces(t *testing.T) { diff = cmp.Diff(expectedSegment, segment, cmpopts.EquateApproxTime(5*time.Second)) require.Zero(t, diff) - Verify{ + metabasetest.Verify{ Objects: []metabase.RawObject{ { ObjectStream: obj, @@ -418,7 +419,7 @@ func TestUpdateSegmentPieces(t *testing.T) { TotalEncryptedSize: 1024, FixedSegmentSize: 512, - Encryption: defaultTestEncryption, + Encryption: metabasetest.DefaultEncryption, }, }, Segments: []metabase.RawSegment{