satellite/metabase/metabasetest: move test utils to a separate package

Change-Id: Iaa607996478497d5c97c6a6973d099b315b3abf0
This commit is contained in:
Egon Elbre 2021-04-26 12:35:44 +03:00
parent 307886ffe8
commit 948529a600
23 changed files with 1858 additions and 1702 deletions

View File

@ -13,18 +13,19 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestNodeAliases(t *testing.T) { 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) { t.Run("Zero", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
nodes := []storj.NodeID{ nodes := []storj.NodeID{
testrand.NodeID(), testrand.NodeID(),
{}, {},
} }
EnsureNodeAliases{ metabasetest.EnsureNodeAliases{
Opts: metabase.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{
Nodes: nodes, Nodes: nodes,
}, },
@ -34,14 +35,14 @@ func TestNodeAliases(t *testing.T) {
}) })
t.Run("Empty", func(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) require.Len(t, aliasesAfter, 0)
}) })
t.Run("Valid", func(t *testing.T) { t.Run("Valid", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
nodes := []storj.NodeID{ nodes := []storj.NodeID{
testrand.NodeID(), testrand.NodeID(),
@ -50,19 +51,19 @@ func TestNodeAliases(t *testing.T) {
} }
nodes = append(nodes, nodes...) // add duplicates to our slice nodes = append(nodes, nodes...) // add duplicates to our slice
EnsureNodeAliases{ metabasetest.EnsureNodeAliases{
Opts: metabase.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{
Nodes: nodes, Nodes: nodes,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
EnsureNodeAliases{ metabasetest.EnsureNodeAliases{
Opts: metabase.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{
Nodes: nodes, Nodes: nodes,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
aliases := ListNodeAliases{}.Check(ctx, t, db) aliases := metabasetest.ListNodeAliases{}.Check(ctx, t, db)
require.Len(t, aliases, 3) require.Len(t, aliases, 3)
for _, entry := range aliases { for _, entry := range aliases {
@ -70,18 +71,18 @@ func TestNodeAliases(t *testing.T) {
require.LessOrEqual(t, int(entry.Alias), 3) require.LessOrEqual(t, int(entry.Alias), 3)
} }
EnsureNodeAliases{ metabasetest.EnsureNodeAliases{
Opts: metabase.EnsureNodeAliases{ Opts: metabase.EnsureNodeAliases{
Nodes: []storj.NodeID{testrand.NodeID()}, Nodes: []storj.NodeID{testrand.NodeID()},
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
aliasesAfter := ListNodeAliases{}.Check(ctx, t, db) aliasesAfter := metabasetest.ListNodeAliases{}.Check(ctx, t, db)
require.Len(t, aliasesAfter, 4) require.Len(t, aliasesAfter, 4)
}) })
t.Run("Concurrent", func(t *testing.T) { 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) nodes := make([]storj.NodeID, 128)
for i := range nodes { for i := range nodes {
@ -99,7 +100,7 @@ func TestNodeAliases(t *testing.T) {
} }
require.NoError(t, group.Wait()) require.NoError(t, group.Wait())
aliases := ListNodeAliases{}.Check(ctx, t, db) aliases := metabasetest.ListNodeAliases{}.Check(ctx, t, db)
seen := map[metabase.NodeAlias]bool{} seen := map[metabase.NodeAlias]bool{}
require.Len(t, aliases, len(nodes)) require.Len(t, aliases, len(nodes))
for _, entry := range aliases { for _, entry := range aliases {
@ -112,7 +113,7 @@ func TestNodeAliases(t *testing.T) {
}) })
t.Run("Stress Concurrent", func(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) nodes := make([]storj.NodeID, 128)
for i := range nodes { for i := range nodes {

View File

@ -18,6 +18,7 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestNodeAliasCache(t *testing.T) { func TestNodeAliasCache(t *testing.T) {
@ -138,9 +139,9 @@ func TestNodeAliasCache(t *testing.T) {
} }
func TestNodeAliasCache_DB(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) { 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) cache := metabase.NewNodeAliasCache(db)
nodes, err := cache.Nodes(ctx, []metabase.NodeAlias{1, 2, 3}) 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) { 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) cache := metabase.NewNodeAliasCache(db)

View File

@ -14,6 +14,7 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
var letters = []rune("abcdefghijklmnopqrstuvwxyz") var letters = []rune("abcdefghijklmnopqrstuvwxyz")
@ -84,7 +85,7 @@ func (g *expirationDateGenerator) getDeadline() time.Time {
// Run runs the scenario as a subtest. // Run runs the scenario as a subtest.
func (s expiredScenario) Run(b *testing.B) { 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. // 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) { b.Run("Delete expired objects", func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
// wipe data so we can do the exact same test // wipe data so we can do the exact same test
DeleteAll{}.Check(ctx, b, db) metabasetest.DeleteAll{}.Check(ctx, b, db)
s.objectStream = nil s.objectStream = nil
var expiredGenerator expirationDateGenerator var expiredGenerator expirationDateGenerator

View File

@ -18,6 +18,7 @@ import (
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/common/uuid" "storj.io/common/uuid"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func Benchmark(b *testing.B) { func Benchmark(b *testing.B) {
@ -42,7 +43,7 @@ type scenario struct {
// Run runs the scenario as a subtest. // Run runs the scenario as a subtest.
func (s scenario) Run(b *testing.B) { 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. // 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++ { for i := 0; i < b.N; i++ {
// wipe data so we can do the exact same test // wipe data so we can do the exact same test
b.StopTimer() b.StopTimer()
DeleteAll{}.Check(ctx, b, db) metabasetest.DeleteAll{}.Check(ctx, b, db)
if err := db.EnsureNodeAliases(ctx, metabase.EnsureNodeAliases{Nodes: nodes}); err != nil { if err := db.EnsureNodeAliases(ctx, metabase.EnsureNodeAliases{Nodes: nodes}); err != nil {
require.NoError(b, err) require.NoError(b, err)
} }

View File

@ -11,35 +11,36 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestCommitObjectWithSegments(t *testing.T) { func TestCommitObjectWithSegments(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
for _, test := range invalidObjectStreams(obj) { for _, test := range metabasetest.InvalidObjectStreams(obj) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: test.ObjectStream, ObjectStream: test.ObjectStream,
}, },
ErrClass: test.ErrClass, ErrClass: test.ErrClass,
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
} }
t.Run("invalid order", func(t *testing.T) { 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} pos00 := metabase.SegmentPosition{Part: 0, Index: 0}
pos01 := metabase.SegmentPosition{Part: 0, Index: 1} pos01 := metabase.SegmentPosition{Part: 0, Index: 1}
pos10 := metabase.SegmentPosition{Part: 1, Index: 0} pos10 := metabase.SegmentPosition{Part: 1, Index: 0}
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -51,7 +52,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
ErrText: "segments not in ascending order, got {0 1} before {0 0}", ErrText: "segments not in ascending order, got {0 1} before {0 0}",
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -63,7 +64,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
ErrText: "segments not in ascending order, got {1 0} before {0 0}", ErrText: "segments not in ascending order, got {1 0} before {0 0}",
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -75,13 +76,13 @@ func TestCommitObjectWithSegments(t *testing.T) {
ErrText: "segments not in ascending order, got {0 0} before {0 0}", ErrText: "segments not in ascending order, got {0 0} before {0 0}",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("version without pending", func(t *testing.T) { 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{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: metabase.ObjectStream{ ObjectStream: metabase.ObjectStream{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
@ -94,13 +95,13 @@ func TestCommitObjectWithSegments(t *testing.T) {
ErrClass: &storj.ErrObjectNotFound, ErrClass: &storj.ErrObjectNotFound,
ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("version", func(t *testing.T) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: metabase.ObjectStream{ ObjectStream: metabase.ObjectStream{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
@ -109,7 +110,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
Version: 5, Version: 5,
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 5, Version: 5,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -119,7 +120,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
encryptedMetadataNonce := testrand.Nonce() encryptedMetadataNonce := testrand.Nonce()
encryptedMetadataKey := testrand.Bytes(265) encryptedMetadataKey := testrand.Bytes(265)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: metabase.ObjectStream{ ObjectStream: metabase.ObjectStream{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
@ -135,7 +136,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}.Check(ctx, t, db) }.Check(ctx, t, db)
// disallow for double commit // disallow for double commit
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: metabase.ObjectStream{ ObjectStream: metabase.ObjectStream{
ProjectID: obj.ProjectID, 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 ErrText: "metabase: object with specified version and pending status is missing", // TODO: this error message could be better
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: metabase.ObjectStream{ ObjectStream: metabase.ObjectStream{
@ -166,26 +167,26 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedMetadata: encryptedMetadata, EncryptedMetadata: encryptedMetadata,
EncryptedMetadataEncryptedKey: encryptedMetadataKey, EncryptedMetadataEncryptedKey: encryptedMetadataKey,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("segments missing in database", func(t *testing.T) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
now := time.Now() now := time.Now()
pos00 := metabase.SegmentPosition{Part: 0, Index: 0} pos00 := metabase.SegmentPosition{Part: 0, Index: 0}
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -196,26 +197,26 @@ func TestCommitObjectWithSegments(t *testing.T) {
ErrText: "segments and database does not match: {0 0}: segment not committed", ErrText: "segments and database does not match: {0 0}: segment not committed",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("delete segments that are not in proofs", func(t *testing.T) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -233,7 +234,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
encryptedKey01 := testrand.Bytes(32) encryptedKey01 := testrand.Bytes(32)
encryptedKeyNonce01 := testrand.Bytes(32) encryptedKeyNonce01 := testrand.Bytes(32)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -247,11 +248,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -265,11 +266,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -281,7 +282,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -293,7 +294,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: -1, FixedSegmentSize: -1,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -310,7 +311,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 0, PlainOffset: 0,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces01, 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) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -340,7 +341,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
encryptedKey01 := testrand.Bytes(32) encryptedKey01 := testrand.Bytes(32)
encryptedKeyNonce01 := testrand.Bytes(32) encryptedKeyNonce01 := testrand.Bytes(32)
CommitInlineSegment{ metabasetest.CommitInlineSegment{
Opts: metabase.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{
ObjectStream: obj, ObjectStream: obj,
Position: pos00, Position: pos00,
@ -355,7 +356,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitInlineSegment{ metabasetest.CommitInlineSegment{
Opts: metabase.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{
ObjectStream: obj, ObjectStream: obj,
Position: pos01, Position: pos01,
@ -370,7 +371,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -380,7 +381,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
Deleted: nil, Deleted: nil,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -392,7 +393,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: -1, FixedSegmentSize: -1,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -415,12 +416,12 @@ func TestCommitObjectWithSegments(t *testing.T) {
}) })
t.Run("updated plain offset", func(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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -438,7 +439,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
encryptedKey01 := testrand.Bytes(32) encryptedKey01 := testrand.Bytes(32)
encryptedKeyNonce01 := testrand.Bytes(32) encryptedKeyNonce01 := testrand.Bytes(32)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -452,11 +453,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -470,11 +471,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -484,7 +485,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -496,7 +497,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
TotalEncryptedSize: 2048, TotalEncryptedSize: 2048,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -513,7 +514,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 0, PlainOffset: 0,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces00, Pieces: pieces00,
}, },
@ -530,7 +531,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 512, PlainOffset: 512,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces01, Pieces: pieces01,
}, },
@ -539,12 +540,12 @@ func TestCommitObjectWithSegments(t *testing.T) {
}) })
t.Run("fixed segment size", func(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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -562,7 +563,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
encryptedKey10 := testrand.Bytes(32) encryptedKey10 := testrand.Bytes(32)
encryptedKeyNonce10 := testrand.Bytes(32) encryptedKeyNonce10 := testrand.Bytes(32)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -576,11 +577,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -594,11 +595,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -608,7 +609,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -620,7 +621,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
TotalEncryptedSize: 2048, TotalEncryptedSize: 2048,
FixedSegmentSize: -1, FixedSegmentSize: -1,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -637,7 +638,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 0, PlainOffset: 0,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces00, Pieces: pieces00,
}, },
@ -654,7 +655,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 512, PlainOffset: 512,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces10, Pieces: pieces10,
}, },
@ -663,12 +664,12 @@ func TestCommitObjectWithSegments(t *testing.T) {
}) })
t.Run("skipped fixed segment size", func(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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -686,7 +687,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
encryptedKey02 := testrand.Bytes(32) encryptedKey02 := testrand.Bytes(32)
encryptedKeyNonce02 := testrand.Bytes(32) encryptedKeyNonce02 := testrand.Bytes(32)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -700,11 +701,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
@ -718,11 +719,11 @@ func TestCommitObjectWithSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
Segments: []metabase.SegmentPosition{ Segments: []metabase.SegmentPosition{
@ -732,7 +733,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -744,7 +745,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
TotalEncryptedSize: 2048, TotalEncryptedSize: 2048,
FixedSegmentSize: -1, FixedSegmentSize: -1,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -761,7 +762,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 0, PlainOffset: 0,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces00, Pieces: pieces00,
}, },
@ -778,7 +779,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
PlainOffset: 512, PlainOffset: 512,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: pieces02, Pieces: pieces02,
}, },
@ -787,31 +788,31 @@ func TestCommitObjectWithSegments(t *testing.T) {
}) })
t.Run("no segments", func(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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
now := time.Now() now := time.Now()
CommitObjectWithSegments{ metabasetest.CommitObjectWithSegments{
Opts: metabase.CommitObjectWithSegments{ Opts: metabase.CommitObjectWithSegments{
ObjectStream: obj, ObjectStream: obj,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)

File diff suppressed because it is too large Load Diff

View File

@ -14,21 +14,22 @@ import (
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/common/uuid" "storj.io/common/uuid"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
"storj.io/storj/satellite/satellitedb/satellitedbtest" "storj.io/storj/satellite/satellitedb/satellitedbtest"
) )
func TestMigrateToAliases(t *testing.T) { func TestMigrateToAliases(t *testing.T) {
for _, info := range databaseInfos() { for _, info := range metabasetest.DatabaseEntries() {
info := info info := info
t.Run(info.name, func(t *testing.T) { t.Run(info.Name, func(t *testing.T) {
t.Parallel() t.Parallel()
ctx := testcontext.New(t) ctx := testcontext.New(t)
defer ctx.Cleanup() defer ctx.Cleanup()
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{ db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{
Name: info.name, Name: info.Name,
URL: info.connstr, URL: info.ConnStr,
Message: "", Message: "",
}) })
require.NoError(t, err) require.NoError(t, err)

View File

@ -13,15 +13,16 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/uuid" "storj.io/common/uuid"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestDeleteBucketObjects(t *testing.T) { func TestDeleteBucketObjects(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) {
obj1 := randObjectStream() obj1 := metabasetest.RandObjectStream()
obj2 := randObjectStream() obj2 := metabasetest.RandObjectStream()
obj3 := randObjectStream() obj3 := metabasetest.RandObjectStream()
objX := randObjectStream() objX := metabasetest.RandObjectStream()
objY := randObjectStream() objY := metabasetest.RandObjectStream()
obj2.ProjectID, obj2.BucketName = obj1.ProjectID, obj1.BucketName obj2.ProjectID, obj2.BucketName = obj1.ProjectID, obj1.BucketName
obj3.ProjectID, obj3.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 objY.BucketName = obj1.BucketName
t.Run("invalid options", func(t *testing.T) { 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{ Opts: metabase.DeleteBucketObjects{
Bucket: metabase.BucketLocation{ Bucket: metabase.BucketLocation{
ProjectID: uuid.UUID{}, ProjectID: uuid.UUID{},
@ -42,7 +43,7 @@ func TestDeleteBucketObjects(t *testing.T) {
ErrText: "ProjectID missing", ErrText: "ProjectID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
DeleteBucketObjects{ metabasetest.DeleteBucketObjects{
Opts: metabase.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{
Bucket: metabase.BucketLocation{ Bucket: metabase.BucketLocation{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
@ -53,13 +54,13 @@ func TestDeleteBucketObjects(t *testing.T) {
ErrText: "BucketName missing", ErrText: "BucketName missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("empty bucket", func(t *testing.T) { 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{ Opts: metabase.DeleteBucketObjects{
Bucket: obj1.Location().Bucket(), Bucket: obj1.Location().Bucket(),
DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error { DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error {
@ -69,15 +70,15 @@ func TestDeleteBucketObjects(t *testing.T) {
Deleted: 0, Deleted: 0,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("one object", func(t *testing.T) { 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{ Opts: metabase.DeleteBucketObjects{
Bucket: obj1.Location().Bucket(), Bucket: obj1.Location().Bucket(),
DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error { DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error {
@ -95,15 +96,15 @@ func TestDeleteBucketObjects(t *testing.T) {
Deleted: 1, Deleted: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("empty object", func(t *testing.T) { 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{ Opts: metabase.DeleteBucketObjects{
Bucket: obj1.Location().Bucket(), Bucket: obj1.Location().Bucket(),
DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error { DeletePieces: func(ctx context.Context, segments []metabase.DeletedSegmentInfo) error {
@ -115,17 +116,17 @@ func TestDeleteBucketObjects(t *testing.T) {
Deleted: 0, Deleted: 0,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("three objects", func(t *testing.T) { 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) metabasetest.CreateObject(ctx, t, db, obj1, 2)
createObject(ctx, t, db, obj2, 2) metabasetest.CreateObject(ctx, t, db, obj2, 2)
createObject(ctx, t, db, obj3, 2) metabasetest.CreateObject(ctx, t, db, obj3, 2)
DeleteBucketObjects{ metabasetest.DeleteBucketObjects{
Opts: metabase.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{
Bucket: obj1.Location().Bucket(), Bucket: obj1.Location().Bucket(),
BatchSize: 2, BatchSize: 2,
@ -144,25 +145,25 @@ func TestDeleteBucketObjects(t *testing.T) {
Deleted: 3, Deleted: 3,
}.Check(ctx, t, db) }.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) { 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) metabasetest.CreateObject(ctx, t, db, obj1, 1)
createObject(ctx, t, db, objX, 1) metabasetest.CreateObject(ctx, t, db, objX, 1)
createObject(ctx, t, db, objY, 1) metabasetest.CreateObject(ctx, t, db, objY, 1)
now := time.Now() now := time.Now()
DeleteBucketObjects{ metabasetest.DeleteBucketObjects{
Opts: metabase.DeleteBucketObjects{ Opts: metabase.DeleteBucketObjects{
Bucket: obj1.Location().Bucket(), Bucket: obj1.Location().Bucket(),
}, },
Deleted: 1, Deleted: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: objX, ObjectStream: objX,
@ -173,7 +174,7 @@ func TestDeleteBucketObjects(t *testing.T) {
TotalPlainSize: 512, TotalPlainSize: 512,
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
{ {
ObjectStream: objY, ObjectStream: objY,
@ -184,7 +185,7 @@ func TestDeleteBucketObjects(t *testing.T) {
TotalPlainSize: 512, TotalPlainSize: 512,
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -203,7 +204,7 @@ func TestDeleteBucketObjects(t *testing.T) {
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
{ {
StreamID: objY.StreamID, StreamID: objY.StreamID,
@ -220,7 +221,7 @@ func TestDeleteBucketObjects(t *testing.T) {
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)

View File

@ -10,75 +10,76 @@ import (
"storj.io/common/storj" "storj.io/common/storj"
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestDeleteExpiredObjects(t *testing.T) { func TestDeleteExpiredObjects(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) {
obj1 := randObjectStream() obj1 := metabasetest.RandObjectStream()
obj2 := randObjectStream() obj2 := metabasetest.RandObjectStream()
obj3 := randObjectStream() obj3 := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
pastTime := now.Add(-1 * time.Hour) pastTime := now.Add(-1 * time.Hour)
futureTime := now.Add(1 * time.Hour) futureTime := now.Add(1 * time.Hour)
t.Run("none", func(t *testing.T) { 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{ Opts: metabase.DeleteExpiredObjects{
ExpiredBefore: time.Now(), ExpiredBefore: time.Now(),
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("partial objects", func(t *testing.T) { 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 // pending object without expiration time
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj1, ObjectStream: obj1,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
// pending object with expiration time in the past // pending object with expiration time in the past
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj2, ObjectStream: obj2,
ExpiresAt: &pastTime, ExpiresAt: &pastTime,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
// pending object with expiration time in the future // pending object with expiration time in the future
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj3, ObjectStream: obj3,
ExpiresAt: &futureTime, ExpiresAt: &futureTime,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
DeleteExpiredObjects{ metabasetest.DeleteExpiredObjects{
Opts: metabase.DeleteExpiredObjects{ Opts: metabase.DeleteExpiredObjects{
ExpiredBefore: time.Now(), ExpiredBefore: time.Now(),
}, },
}.Check(ctx, t, db) }.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{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj1, ObjectStream: obj1,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
{ {
ObjectStream: obj3, ObjectStream: obj3,
@ -86,7 +87,7 @@ func TestDeleteExpiredObjects(t *testing.T) {
ExpiresAt: &futureTime, ExpiresAt: &futureTime,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -95,34 +96,34 @@ func TestDeleteExpiredObjects(t *testing.T) {
t.Run("batch size", func(t *testing.T) { t.Run("batch size", func(t *testing.T) {
expiresAt := time.Now().Add(-30 * 24 * time.Hour) expiresAt := time.Now().Add(-30 * 24 * time.Hour)
for i := 0; i < 32; i++ { 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{ Opts: metabase.DeleteExpiredObjects{
ExpiredBefore: time.Now().Add(time.Hour), ExpiredBefore: time.Now().Add(time.Hour),
BatchSize: 4, BatchSize: 4,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("committed objects", func(t *testing.T) { 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) object1 := metabasetest.CreateTestObject{}.Run(ctx, t, db, obj1, 1)
CreateTestObject{ metabasetest.CreateTestObject{
BeginObjectExactVersion: &metabase.BeginObjectExactVersion{ BeginObjectExactVersion: &metabase.BeginObjectExactVersion{
ObjectStream: obj2, ObjectStream: obj2,
ExpiresAt: &pastTime, ExpiresAt: &pastTime,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}.Run(ctx, t, db, obj2, 1) }.Run(ctx, t, db, obj2, 1)
object3 := CreateTestObject{ object3 := metabasetest.CreateTestObject{
BeginObjectExactVersion: &metabase.BeginObjectExactVersion{ BeginObjectExactVersion: &metabase.BeginObjectExactVersion{
ObjectStream: obj3, ObjectStream: obj3,
ExpiresAt: &futureTime, ExpiresAt: &futureTime,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}.Run(ctx, t, db, obj3, 1) }.Run(ctx, t, db, obj3, 1)
@ -136,19 +137,19 @@ func TestDeleteExpiredObjects(t *testing.T) {
EncryptedSize: 1060, EncryptedSize: 1060,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
expectedObj3Segment := expectedObj1Segment expectedObj3Segment := expectedObj1Segment
expectedObj3Segment.StreamID = obj3.StreamID expectedObj3Segment.StreamID = obj3.StreamID
DeleteExpiredObjects{ metabasetest.DeleteExpiredObjects{
Opts: metabase.DeleteExpiredObjects{ Opts: metabase.DeleteExpiredObjects{
ExpiredBefore: time.Now(), ExpiredBefore: time.Now(),
}, },
}.Check(ctx, t, db) }.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{ Objects: []metabase.RawObject{
metabase.RawObject(object1), metabase.RawObject(object1),
metabase.RawObject(object3), metabase.RawObject(object3),

File diff suppressed because it is too large Load Diff

View File

@ -11,21 +11,22 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestGetObjectExactVersion(t *testing.T) { func TestGetObjectExactVersion(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
location := obj.Location() location := obj.Location()
now := time.Now() now := time.Now()
for _, test := range invalidObjectLocations(location) { for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
GetObjectExactVersion{ metabasetest.GetObjectExactVersion{
Opts: metabase.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{
ObjectLocation: test.ObjectLocation, ObjectLocation: test.ObjectLocation,
}, },
@ -33,14 +34,14 @@ func TestGetObjectExactVersion(t *testing.T) {
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
} }
t.Run("Version invalid", func(t *testing.T) { 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{ Opts: metabase.GetObjectExactVersion{
ObjectLocation: location, ObjectLocation: location,
Version: 0, Version: 0,
@ -49,13 +50,13 @@ func TestGetObjectExactVersion(t *testing.T) {
ErrText: "Version invalid: 0", ErrText: "Version invalid: 0",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Object missing", func(t *testing.T) { 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{ Opts: metabase.GetObjectExactVersion{
ObjectLocation: location, ObjectLocation: location,
Version: 1, Version: 1,
@ -64,15 +65,15 @@ func TestGetObjectExactVersion(t *testing.T) {
ErrText: "metabase: sql: no rows in result set", ErrText: "metabase: sql: no rows in result set",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.GetObjectExactVersion{
ObjectLocation: location, ObjectLocation: location,
Version: 11, Version: 11,
@ -81,32 +82,32 @@ func TestGetObjectExactVersion(t *testing.T) {
ErrText: "metabase: sql: no rows in result set", ErrText: "metabase: sql: no rows in result set",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("Get pending object", func(t *testing.T) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
GetObjectExactVersion{ metabasetest.GetObjectExactVersion{
Opts: metabase.GetObjectExactVersion{ Opts: metabase.GetObjectExactVersion{
ObjectLocation: location, ObjectLocation: location,
Version: 1, Version: 1,
@ -115,25 +116,25 @@ func TestGetObjectExactVersion(t *testing.T) {
ErrText: "metabase: sql: no rows in result set", ErrText: "metabase: sql: no rows in result set",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("Get object", func(t *testing.T) { 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{ Opts: metabase.GetObjectExactVersion{
ObjectLocation: location, ObjectLocation: location,
Version: 1, Version: 1,
@ -143,17 +144,17 @@ func TestGetObjectExactVersion(t *testing.T) {
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{Objects: []metabase.RawObject{ metabasetest.Verify{Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}}.Check(ctx, t, db) }}.Check(ctx, t, db)
}) })
@ -161,18 +162,18 @@ func TestGetObjectExactVersion(t *testing.T) {
} }
func TestGetObjectLatestVersion(t *testing.T) { func TestGetObjectLatestVersion(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
location := obj.Location() location := obj.Location()
now := time.Now() now := time.Now()
for _, test := range invalidObjectLocations(location) { for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
GetObjectLatestVersion{ metabasetest.GetObjectLatestVersion{
Opts: metabase.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{
ObjectLocation: test.ObjectLocation, ObjectLocation: test.ObjectLocation,
}, },
@ -180,14 +181,14 @@ func TestGetObjectLatestVersion(t *testing.T) {
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
} }
t.Run("Object missing", func(t *testing.T) { 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{ Opts: metabase.GetObjectLatestVersion{
ObjectLocation: location, ObjectLocation: location,
}, },
@ -195,22 +196,22 @@ func TestGetObjectLatestVersion(t *testing.T) {
ErrText: "metabase: sql: no rows in result set", ErrText: "metabase: sql: no rows in result set",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Get pending object", func(t *testing.T) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
GetObjectLatestVersion{ metabasetest.GetObjectLatestVersion{
Opts: metabase.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{
ObjectLocation: location, ObjectLocation: location,
}, },
@ -218,27 +219,27 @@ func TestGetObjectLatestVersion(t *testing.T) {
ErrText: "metabase: sql: no rows in result set", ErrText: "metabase: sql: no rows in result set",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("Get object", func(t *testing.T) { 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) encryptedMetadata := testrand.Bytes(1024)
encryptedMetadataNonce := testrand.Nonce() encryptedMetadataNonce := testrand.Nonce()
encryptedMetadataKey := testrand.Bytes(265) encryptedMetadataKey := testrand.Bytes(265)
CreateTestObject{ metabasetest.CreateTestObject{
CommitObject: &metabase.CommitObject{ CommitObject: &metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
@ -247,7 +248,7 @@ func TestGetObjectLatestVersion(t *testing.T) {
}, },
}.Run(ctx, t, db, obj, 0) }.Run(ctx, t, db, obj, 0)
GetObjectLatestVersion{ metabasetest.GetObjectLatestVersion{
Opts: metabase.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{
ObjectLocation: location, ObjectLocation: location,
}, },
@ -256,7 +257,7 @@ func TestGetObjectLatestVersion(t *testing.T) {
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
EncryptedMetadata: encryptedMetadata, EncryptedMetadata: encryptedMetadata,
@ -264,13 +265,13 @@ func TestGetObjectLatestVersion(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{Objects: []metabase.RawObject{ metabasetest.Verify{Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
EncryptedMetadata: encryptedMetadata, EncryptedMetadata: encryptedMetadata,
@ -280,10 +281,10 @@ func TestGetObjectLatestVersion(t *testing.T) {
}) })
t.Run("Get latest object version from multiple", func(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 firstVersion := obj
createObject(ctx, t, db, firstVersion, 0) metabasetest.CreateObject(ctx, t, db, firstVersion, 0)
secondVersion := metabase.ObjectStream{ secondVersion := metabase.ObjectStream{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
BucketName: obj.BucketName, BucketName: obj.BucketName,
@ -291,9 +292,9 @@ func TestGetObjectLatestVersion(t *testing.T) {
Version: 2, Version: 2,
StreamID: obj.StreamID, StreamID: obj.StreamID,
} }
createObject(ctx, t, db, secondVersion, 0) metabasetest.CreateObject(ctx, t, db, secondVersion, 0)
GetObjectLatestVersion{ metabasetest.GetObjectLatestVersion{
Opts: metabase.GetObjectLatestVersion{ Opts: metabase.GetObjectLatestVersion{
ObjectLocation: location, ObjectLocation: location,
}, },
@ -302,24 +303,24 @@ func TestGetObjectLatestVersion(t *testing.T) {
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{Objects: []metabase.RawObject{ metabasetest.Verify{Objects: []metabase.RawObject{
{ {
ObjectStream: firstVersion, ObjectStream: firstVersion,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
{ {
ObjectStream: secondVersion, ObjectStream: secondVersion,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}}.Check(ctx, t, db) }}.Check(ctx, t, db)
}) })
@ -327,8 +328,8 @@ func TestGetObjectLatestVersion(t *testing.T) {
} }
func TestGetSegmentByLocation(t *testing.T) { func TestGetSegmentByLocation(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
@ -338,11 +339,11 @@ func TestGetSegmentByLocation(t *testing.T) {
ObjectKey: obj.ObjectKey, ObjectKey: obj.ObjectKey,
} }
for _, test := range invalidSegmentLocations(location) { for _, test := range metabasetest.InvalidSegmentLocations(location) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
GetSegmentByLocation{ metabasetest.GetSegmentByLocation{
Opts: metabase.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{
SegmentLocation: test.SegmentLocation, SegmentLocation: test.SegmentLocation,
}, },
@ -350,14 +351,14 @@ func TestGetSegmentByLocation(t *testing.T) {
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
} }
t.Run("Object missing", func(t *testing.T) { 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{ Opts: metabase.GetSegmentByLocation{
SegmentLocation: location, SegmentLocation: location,
}, },
@ -365,13 +366,13 @@ func TestGetSegmentByLocation(t *testing.T) {
ErrText: "metabase: object or segment missing", ErrText: "metabase: object or segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Get segment", func(t *testing.T) { 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{ expectedSegment := metabase.Segment{
StreamID: obj.StreamID, StreamID: obj.StreamID,
@ -386,10 +387,10 @@ func TestGetSegmentByLocation(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
GetSegmentByLocation{ metabasetest.GetSegmentByLocation{
Opts: metabase.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{
SegmentLocation: location, SegmentLocation: location,
}, },
@ -397,7 +398,7 @@ func TestGetSegmentByLocation(t *testing.T) {
}.Check(ctx, t, db) }.Check(ctx, t, db)
// check non existing segment in existing object // check non existing segment in existing object
GetSegmentByLocation{ metabasetest.GetSegmentByLocation{
Opts: metabase.GetSegmentByLocation{ Opts: metabase.GetSegmentByLocation{
SegmentLocation: metabase.SegmentLocation{ SegmentLocation: metabase.SegmentLocation{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
@ -412,7 +413,7 @@ func TestGetSegmentByLocation(t *testing.T) {
ErrText: "metabase: object or segment missing", ErrText: "metabase: object or segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -424,7 +425,7 @@ func TestGetSegmentByLocation(t *testing.T) {
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -436,27 +437,27 @@ func TestGetSegmentByLocation(t *testing.T) {
} }
func TestGetSegmentByPosition(t *testing.T) { func TestGetSegmentByPosition(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
t.Run("StreamID missing", func(t *testing.T) { 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{}, Opts: metabase.GetSegmentByPosition{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "StreamID missing", ErrText: "StreamID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Segment missing", func(t *testing.T) { 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{ Opts: metabase.GetSegmentByPosition{
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
@ -464,13 +465,13 @@ func TestGetSegmentByPosition(t *testing.T) {
ErrText: "segment missing", ErrText: "segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Get segment", func(t *testing.T) { 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{ expectedSegment := metabase.Segment{
StreamID: obj.StreamID, StreamID: obj.StreamID,
@ -485,10 +486,10 @@ func TestGetSegmentByPosition(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
GetSegmentByPosition{ metabasetest.GetSegmentByPosition{
Opts: metabase.GetSegmentByPosition{ Opts: metabase.GetSegmentByPosition{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Position: metabase.SegmentPosition{ Position: metabase.SegmentPosition{
@ -499,7 +500,7 @@ func TestGetSegmentByPosition(t *testing.T) {
}.Check(ctx, t, db) }.Check(ctx, t, db)
// check non existing segment in existing object // check non existing segment in existing object
GetSegmentByPosition{ metabasetest.GetSegmentByPosition{
Opts: metabase.GetSegmentByPosition{ Opts: metabase.GetSegmentByPosition{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Position: metabase.SegmentPosition{ Position: metabase.SegmentPosition{
@ -510,7 +511,7 @@ func TestGetSegmentByPosition(t *testing.T) {
ErrText: "segment missing", ErrText: "segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -522,7 +523,7 @@ func TestGetSegmentByPosition(t *testing.T) {
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -534,16 +535,16 @@ func TestGetSegmentByPosition(t *testing.T) {
} }
func TestGetLatestObjectLastSegment(t *testing.T) { func TestGetLatestObjectLastSegment(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
location := obj.Location() location := obj.Location()
now := time.Now() now := time.Now()
for _, test := range invalidObjectLocations(location) { for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
GetLatestObjectLastSegment{ metabasetest.GetLatestObjectLastSegment{
Opts: metabase.GetLatestObjectLastSegment{ Opts: metabase.GetLatestObjectLastSegment{
ObjectLocation: test.ObjectLocation, ObjectLocation: test.ObjectLocation,
}, },
@ -551,14 +552,14 @@ func TestGetLatestObjectLastSegment(t *testing.T) {
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.GetLatestObjectLastSegment{
ObjectLocation: location, ObjectLocation: location,
}, },
@ -566,13 +567,13 @@ func TestGetLatestObjectLastSegment(t *testing.T) {
ErrText: "metabase: object or segment missing", ErrText: "metabase: object or segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Get last segment", func(t *testing.T) { 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{ expectedSegmentSecond := metabase.Segment{
StreamID: obj.StreamID, StreamID: obj.StreamID,
@ -588,21 +589,21 @@ func TestGetLatestObjectLastSegment(t *testing.T) {
PlainSize: 512, PlainSize: 512,
PlainOffset: 512, PlainOffset: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
expectedSegmentFirst := expectedSegmentSecond expectedSegmentFirst := expectedSegmentSecond
expectedSegmentFirst.Position.Index = 0 expectedSegmentFirst.Position.Index = 0
expectedSegmentFirst.PlainOffset = 0 expectedSegmentFirst.PlainOffset = 0
GetLatestObjectLastSegment{ metabasetest.GetLatestObjectLastSegment{
Opts: metabase.GetLatestObjectLastSegment{ Opts: metabase.GetLatestObjectLastSegment{
ObjectLocation: location, ObjectLocation: location,
}, },
Result: expectedSegmentSecond, Result: expectedSegmentSecond,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -614,7 +615,7 @@ func TestGetLatestObjectLastSegment(t *testing.T) {
TotalEncryptedSize: 2048, TotalEncryptedSize: 2048,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -627,16 +628,16 @@ func TestGetLatestObjectLastSegment(t *testing.T) {
} }
func TestGetSegmentByOffset(t *testing.T) { func TestGetSegmentByOffset(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
location := obj.Location() location := obj.Location()
now := time.Now() now := time.Now()
for _, test := range invalidObjectLocations(location) { for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
GetSegmentByOffset{ metabasetest.GetSegmentByOffset{
Opts: metabase.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{
ObjectLocation: test.ObjectLocation, ObjectLocation: test.ObjectLocation,
}, },
@ -644,14 +645,14 @@ func TestGetSegmentByOffset(t *testing.T) {
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
} }
t.Run("Invalid PlainOffset", func(t *testing.T) { 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{ Opts: metabase.GetSegmentByOffset{
ObjectLocation: location, ObjectLocation: location,
PlainOffset: -1, PlainOffset: -1,
@ -660,13 +661,13 @@ func TestGetSegmentByOffset(t *testing.T) {
ErrText: "Invalid PlainOffset: -1", ErrText: "Invalid PlainOffset: -1",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.GetSegmentByOffset{
ObjectLocation: location, ObjectLocation: location,
}, },
@ -674,13 +675,13 @@ func TestGetSegmentByOffset(t *testing.T) {
ErrText: "metabase: object or segment missing", ErrText: "metabase: object or segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Get segment", func(t *testing.T) { 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) segments := make([]metabase.Segment, 4)
for i := range segments { for i := range segments {
@ -698,7 +699,7 @@ func TestGetSegmentByOffset(t *testing.T) {
PlainSize: 512, PlainSize: 512,
PlainOffset: int64(i * 512), PlainOffset: int64(i * 512),
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, 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 { for _, tc := range testCases {
GetSegmentByOffset{ metabasetest.GetSegmentByOffset{
Opts: metabase.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{
ObjectLocation: location, ObjectLocation: location,
PlainOffset: tc.Offset, PlainOffset: tc.Offset,
@ -722,7 +723,7 @@ func TestGetSegmentByOffset(t *testing.T) {
}.Check(ctx, t, db) }.Check(ctx, t, db)
} }
GetSegmentByOffset{ metabasetest.GetSegmentByOffset{
Opts: metabase.GetSegmentByOffset{ Opts: metabase.GetSegmentByOffset{
ObjectLocation: location, ObjectLocation: location,
PlainOffset: 2048, PlainOffset: 2048,
@ -731,7 +732,7 @@ func TestGetSegmentByOffset(t *testing.T) {
ErrText: "metabase: object or segment missing", ErrText: "metabase: object or segment missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -743,7 +744,7 @@ func TestGetSegmentByOffset(t *testing.T) {
TotalEncryptedSize: 4240, TotalEncryptedSize: 4240,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -758,26 +759,26 @@ func TestGetSegmentByOffset(t *testing.T) {
} }
func TestBucketEmpty(t *testing.T) { func TestBucketEmpty(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
t.Run("ProjectID missing", func(t *testing.T) { 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{}, Opts: metabase.BucketEmpty{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "ProjectID missing", ErrText: "ProjectID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("BucketName missing", func(t *testing.T) { 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{ Opts: metabase.BucketEmpty{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
}, },
@ -785,13 +786,13 @@ func TestBucketEmpty(t *testing.T) {
ErrText: "BucketName missing", ErrText: "BucketName missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("BucketEmpty true", func(t *testing.T) { 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{ Opts: metabase.BucketEmpty{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
BucketName: obj.BucketName, BucketName: obj.BucketName,
@ -799,22 +800,22 @@ func TestBucketEmpty(t *testing.T) {
Result: true, Result: true,
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
BucketEmpty{ metabasetest.BucketEmpty{
Opts: metabase.BucketEmpty{ Opts: metabase.BucketEmpty{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
BucketName: obj.BucketName, BucketName: obj.BucketName,
@ -822,24 +823,24 @@ func TestBucketEmpty(t *testing.T) {
Result: false, Result: false,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("BucketEmpty false with committed object", func(t *testing.T) { 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{ Opts: metabase.BucketEmpty{
ProjectID: obj.ProjectID, ProjectID: obj.ProjectID,
BucketName: obj.BucketName, BucketName: obj.BucketName,
@ -847,7 +848,7 @@ func TestBucketEmpty(t *testing.T) {
Result: false, Result: false,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
metabase.RawObject(object), metabase.RawObject(object),
}, },

View File

@ -14,13 +14,14 @@ import (
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/common/uuid" "storj.io/common/uuid"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestIterateObjects(t *testing.T) { 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) { t.Run("ProjectID missing", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{}, ProjectID: uuid.UUID{},
BucketName: "sj://mybucket", BucketName: "sj://mybucket",
@ -28,12 +29,12 @@ func TestIterateObjects(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "ProjectID missing", ErrText: "ProjectID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("BucketName missing", func(t *testing.T) { 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{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "", BucketName: "",
@ -41,11 +42,11 @@ func TestIterateObjects(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BucketName missing", ErrText: "BucketName missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Limit is negative", func(t *testing.T) { t.Run("Limit is negative", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -54,13 +55,13 @@ func TestIterateObjects(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BatchSize is negative", ErrText: "BatchSize is negative",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("empty bucket", func(t *testing.T) { 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") objects := createObjects(ctx, t, db, 2, uuid.UUID{1}, "mybucket")
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "myemptybucket", BucketName: "myemptybucket",
@ -68,17 +69,17 @@ func TestIterateObjects(t *testing.T) {
}, },
Result: nil, Result: nil,
}.Check(ctx, t, db) }.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) { 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() now := time.Now()
pending := randObjectStream() pending := metabasetest.RandObjectStream()
pending.ObjectKey = metabase.ObjectKey("firstObject") pending.ObjectKey = metabase.ObjectKey("firstObject")
committed := randObjectStream() committed := metabasetest.RandObjectStream()
committed.ProjectID = pending.ProjectID committed.ProjectID = pending.ProjectID
committed.BucketName = pending.BucketName committed.BucketName = pending.BucketName
committed.ObjectKey = metabase.ObjectKey("secondObject") committed.ObjectKey = metabase.ObjectKey("secondObject")
@ -86,10 +87,10 @@ func TestIterateObjects(t *testing.T) {
projectID := pending.ProjectID projectID := pending.ProjectID
bucketName := pending.BucketName bucketName := pending.BucketName
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: pending, ObjectStream: pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -98,14 +99,14 @@ func TestIterateObjects(t *testing.T) {
encryptedMetadataNonce := testrand.Nonce() encryptedMetadataNonce := testrand.Nonce()
encryptedMetadataKey := testrand.Bytes(265) encryptedMetadataKey := testrand.Bytes(265)
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: committed, ObjectStream: committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObject{ metabasetest.CommitObject{
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: committed, ObjectStream: committed,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
@ -114,7 +115,7 @@ func TestIterateObjects(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -126,7 +127,7 @@ func TestIterateObjects(t *testing.T) {
StreamID: pending.StreamID, StreamID: pending.StreamID,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
{ {
ObjectKey: committed.ObjectKey, ObjectKey: committed.ObjectKey,
@ -134,7 +135,7 @@ func TestIterateObjects(t *testing.T) {
StreamID: committed.StreamID, StreamID: committed.StreamID,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
EncryptedMetadata: encryptedMetadata, EncryptedMetadata: encryptedMetadata,
EncryptedMetadataEncryptedKey: encryptedMetadataKey, EncryptedMetadataEncryptedKey: encryptedMetadataKey,
@ -144,7 +145,7 @@ func TestIterateObjects(t *testing.T) {
}) })
t.Run("less objects than limit", func(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 numberOfObjects := 3
limit := 10 limit := 10
expected := make([]metabase.ObjectEntry, numberOfObjects) expected := make([]metabase.ObjectEntry, numberOfObjects)
@ -152,7 +153,7 @@ func TestIterateObjects(t *testing.T) {
for i, obj := range objects { for i, obj := range objects {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -160,11 +161,11 @@ func TestIterateObjects(t *testing.T) {
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjects := 10
limit := 3 limit := 3
expected := make([]metabase.ObjectEntry, numberOfObjects) expected := make([]metabase.ObjectEntry, numberOfObjects)
@ -172,7 +173,7 @@ func TestIterateObjects(t *testing.T) {
for i, obj := range objects { for i, obj := range objects {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -180,11 +181,11 @@ func TestIterateObjects(t *testing.T) {
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjectsPerBucket := 5
expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket)
objectsBucketA := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "bucket-a") 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 { for i, obj := range objectsBucketA {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "bucket-a", BucketName: "bucket-a",
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjectsPerBucket := 5
expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket)
objectsProject1 := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "mybucket") objectsProject1 := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "mybucket")
@ -211,18 +212,18 @@ func TestIterateObjects(t *testing.T) {
for i, obj := range objectsProject1 { for i, obj := range objectsProject1 {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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" projectID, bucketName := uuid.UUID{1}, "bucky"
objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{
@ -237,7 +238,7 @@ func TestIterateObjects(t *testing.T) {
"g", "g",
}) })
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -255,7 +256,7 @@ func TestIterateObjects(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -274,7 +275,7 @@ func TestIterateObjects(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -293,7 +294,7 @@ func TestIterateObjects(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -307,7 +308,7 @@ func TestIterateObjects(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -322,7 +323,7 @@ func TestIterateObjects(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -336,7 +337,7 @@ func TestIterateObjects(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjects{ metabasetest.IterateObjects{
Opts: metabase.IterateObjects{ Opts: metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -349,7 +350,7 @@ func TestIterateObjects(t *testing.T) {
}) })
t.Run("boundaries", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
queries := []metabase.ObjectKey{""} queries := []metabase.ObjectKey{""}
@ -370,7 +371,7 @@ func TestIterateObjects(t *testing.T) {
for _, cursor := range queries { for _, cursor := range queries {
for _, prefix := range queries { for _, prefix := range queries {
var collector IterateCollector var collector metabasetest.IterateCollector
err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{ err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -386,11 +387,11 @@ func TestIterateObjects(t *testing.T) {
}) })
t.Run("verify-iterator-boundary", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
queries := []metabase.ObjectKey{"\x00\xFF"} queries := []metabase.ObjectKey{"\x00\xFF"}
createObjectsWithKeys(ctx, t, db, projectID, bucketName, queries) createObjectsWithKeys(ctx, t, db, projectID, bucketName, queries)
var collector IterateCollector var collector metabasetest.IterateCollector
err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{ err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -404,7 +405,7 @@ func TestIterateObjects(t *testing.T) {
}) })
t.Run("verify-cursor-continuation", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{
@ -413,7 +414,7 @@ func TestIterateObjects(t *testing.T) {
"a/0", "a/0",
}) })
var collector IterateCollector var collector metabasetest.IterateCollector
err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{ err := db.IterateObjectsAllVersions(ctx, metabase.IterateObjects{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -426,12 +427,12 @@ func TestIterateObjects(t *testing.T) {
} }
func TestIterateObjectsWithStatus(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) { 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) { t.Run("ProjectID missing", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{}, ProjectID: uuid.UUID{},
BucketName: "sj://mybucket", BucketName: "sj://mybucket",
@ -441,9 +442,9 @@ func TestIterateObjectsWithStatus(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "ProjectID missing", ErrText: "ProjectID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "", BucketName: "",
@ -453,11 +454,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BucketName missing", ErrText: "BucketName missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Limit is negative", func(t *testing.T) { t.Run("Limit is negative", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -468,11 +469,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BatchSize is negative", ErrText: "BatchSize is negative",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Status is invalid", func(t *testing.T) { t.Run("Status is invalid", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "test", BucketName: "test",
@ -482,13 +483,13 @@ func TestIterateObjectsWithStatus(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "Status 255 is not supported", ErrText: "Status 255 is not supported",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("empty bucket", func(t *testing.T) { 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") objects := createObjects(ctx, t, db, 2, uuid.UUID{1}, "mybucket")
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "myemptybucket", BucketName: "myemptybucket",
@ -498,26 +499,26 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
Result: nil, Result: nil,
}.Check(ctx, t, db) }.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) { 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() now := time.Now()
pending := randObjectStream() pending := metabasetest.RandObjectStream()
committed := randObjectStream() committed := metabasetest.RandObjectStream()
committed.ProjectID = pending.ProjectID committed.ProjectID = pending.ProjectID
committed.BucketName = pending.BucketName committed.BucketName = pending.BucketName
projectID := pending.ProjectID projectID := pending.ProjectID
bucketName := pending.BucketName bucketName := pending.BucketName
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: pending, ObjectStream: pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -526,14 +527,14 @@ func TestIterateObjectsWithStatus(t *testing.T) {
encryptedMetadataNonce := testrand.Nonce() encryptedMetadataNonce := testrand.Nonce()
encryptedMetadataKey := testrand.Bytes(265) encryptedMetadataKey := testrand.Bytes(265)
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: committed, ObjectStream: committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObject{ metabasetest.CommitObject{
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: committed, ObjectStream: committed,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
@ -542,7 +543,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -555,14 +556,14 @@ func TestIterateObjectsWithStatus(t *testing.T) {
StreamID: committed.StreamID, StreamID: committed.StreamID,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
EncryptedMetadata: encryptedMetadata, EncryptedMetadata: encryptedMetadata,
EncryptedMetadataEncryptedKey: encryptedMetadataKey, EncryptedMetadataEncryptedKey: encryptedMetadataKey,
}}, }},
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -575,13 +576,13 @@ func TestIterateObjectsWithStatus(t *testing.T) {
StreamID: pending.StreamID, StreamID: pending.StreamID,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}}, }},
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("less objects than limit", func(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 numberOfObjects := 3
limit := 10 limit := 10
expected := make([]metabase.ObjectEntry, numberOfObjects) expected := make([]metabase.ObjectEntry, numberOfObjects)
@ -589,7 +590,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
for i, obj := range objects { for i, obj := range objects {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -599,11 +600,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjects := 10
limit := 3 limit := 3
expected := make([]metabase.ObjectEntry, numberOfObjects) expected := make([]metabase.ObjectEntry, numberOfObjects)
@ -611,7 +612,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
for i, obj := range objects { for i, obj := range objects {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -621,11 +622,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjectsPerBucket := 5
expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket)
objectsBucketA := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "bucket-a") 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 { for i, obj := range objectsBucketA {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "bucket-a", BucketName: "bucket-a",
@ -642,11 +643,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjectsPerBucket := 5
expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket) expected := make([]metabase.ObjectEntry, numberOfObjectsPerBucket)
objectsProject1 := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "mybucket") objectsProject1 := createObjects(ctx, t, db, numberOfObjectsPerBucket, uuid.UUID{1}, "mybucket")
@ -654,7 +655,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
for i, obj := range objectsProject1 { for i, obj := range objectsProject1 {
expected[i] = objectEntryFromRaw(obj) expected[i] = objectEntryFromRaw(obj)
} }
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: uuid.UUID{1}, ProjectID: uuid.UUID{1},
BucketName: "mybucket", BucketName: "mybucket",
@ -663,11 +664,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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" projectID, bucketName := uuid.UUID{1}, "bucky"
objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{
@ -682,7 +683,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
"g", "g",
}) })
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -702,7 +703,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -723,7 +724,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -744,7 +745,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -760,7 +761,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -777,7 +778,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -793,7 +794,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -808,7 +809,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}) })
t.Run("non-recursive", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ objects := createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{
@ -823,7 +824,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
"g", "g",
}) })
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -838,7 +839,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -854,7 +855,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -870,7 +871,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -885,7 +886,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -901,7 +902,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -916,7 +917,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -928,7 +929,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
Result: nil, Result: nil,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -944,7 +945,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
), ),
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateObjectsWithStatus{ metabasetest.IterateObjectsWithStatus{
Opts: metabase.IterateObjectsWithStatus{ Opts: metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -959,7 +960,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}) })
t.Run("boundaries", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
queries := []metabase.ObjectKey{""} queries := []metabase.ObjectKey{""}
@ -980,7 +981,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
for _, cursor := range queries { for _, cursor := range queries {
for _, prefix := range queries { for _, prefix := range queries {
var collector IterateCollector var collector metabasetest.IterateCollector
err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{ err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -1010,11 +1011,11 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}) })
t.Run("verify-iterator-boundary", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
queries := []metabase.ObjectKey{"\x00\xFF"} queries := []metabase.ObjectKey{"\x00\xFF"}
createObjectsWithKeys(ctx, t, db, projectID, bucketName, queries) createObjectsWithKeys(ctx, t, db, projectID, bucketName, queries)
var collector IterateCollector var collector metabasetest.IterateCollector
err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{ err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -1029,7 +1030,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
}) })
t.Run("verify-cursor-continuation", func(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" projectID, bucketName := uuid.UUID{1}, "bucky"
createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{ createObjectsWithKeys(ctx, t, db, projectID, bucketName, []metabase.ObjectKey{
@ -1038,7 +1039,7 @@ func TestIterateObjectsWithStatus(t *testing.T) {
"a/0", "a/0",
}) })
var collector IterateCollector var collector metabasetest.IterateCollector
err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{ err := db.IterateObjectsAllVersionsWithStatus(ctx, metabase.IterateObjectsWithStatus{
ProjectID: projectID, ProjectID: projectID,
BucketName: bucketName, BucketName: bucketName,
@ -1052,35 +1053,35 @@ func TestIterateObjectsWithStatus(t *testing.T) {
} }
func TestIteratePendingObjectsWithObjectKey(t *testing.T) { func TestIteratePendingObjectsWithObjectKey(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
location := obj.Location() location := obj.Location()
now := time.Now() now := time.Now()
for _, test := range invalidObjectLocations(location) { for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test test := test
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IteratePendingObjectsByKey{ metabasetest.IteratePendingObjectsByKey{
Opts: metabase.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: test.ObjectLocation, ObjectLocation: test.ObjectLocation,
}, },
ErrClass: test.ErrClass, ErrClass: test.ErrClass,
ErrText: test.ErrText, ErrText: test.ErrText,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
} }
t.Run("committed object", func(t *testing.T) { 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) metabasetest.CreateObject(ctx, t, db, obj, 0)
IteratePendingObjectsByKey{ metabasetest.IteratePendingObjectsByKey{
Opts: metabase.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: obj.Location(), ObjectLocation: obj.Location(),
BatchSize: 10, BatchSize: 10,
@ -1089,19 +1090,19 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
}.Check(ctx, t, db) }.Check(ctx, t, db)
}) })
t.Run("non existing object", func(t *testing.T) { t.Run("non existing object", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
pending := randObjectStream() pending := metabasetest.RandObjectStream()
createPendingObject(ctx, t, db, pending, 0) metabasetest.CreatePendingObject(ctx, t, db, pending, 0)
object := metabase.RawObject{ object := metabase.RawObject{
ObjectStream: pending, ObjectStream: pending,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
} }
IteratePendingObjectsByKey{ metabasetest.IteratePendingObjectsByKey{
Opts: metabase.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: metabase.ObjectLocation{ ObjectLocation: metabase.ObjectLocation{
ProjectID: pending.ProjectID, ProjectID: pending.ProjectID,
@ -1113,13 +1114,13 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Result: nil, Result: nil,
}.Check(ctx, t, db) }.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) { 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() location := pending[0].Location()
objects := make([]metabase.RawObject, 3) objects := make([]metabase.RawObject, 3)
@ -1131,23 +1132,23 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
obj.ObjectKey = location.ObjectKey obj.ObjectKey = location.ObjectKey
obj.Version = metabase.Version(i + 1) obj.Version = metabase.Version(i + 1)
createPendingObject(ctx, t, db, obj, 0) metabasetest.CreatePendingObject(ctx, t, db, obj, 0)
objects[i] = metabase.RawObject{ objects[i] = metabase.RawObject{
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
} }
expected[i] = objectEntryFromRaw(objects[i]) expected[i] = objectEntryFromRaw(objects[i])
} }
sort.Slice(expected, func(i, j int) bool { 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{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: location, ObjectLocation: location,
BatchSize: 10, BatchSize: 10,
@ -1155,7 +1156,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IteratePendingObjectsByKey{ metabasetest.IteratePendingObjectsByKey{
Opts: metabase.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: location, ObjectLocation: location,
BatchSize: 2, BatchSize: 2,
@ -1163,37 +1164,37 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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)) 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{ object := metabase.RawObject{
ObjectStream: pending, ObjectStream: pending,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
} }
IteratePendingObjectsByKey{ metabasetest.IteratePendingObjectsByKey{
Opts: metabase.IteratePendingObjectsByKey{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: pending.Location(), ObjectLocation: pending.Location(),
}, },
Result: []metabase.ObjectEntry{objectEntryFromRaw(object)}, Result: []metabase.ObjectEntry{objectEntryFromRaw(object)},
}.Check(ctx, t, db) }.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) { t.Run("using streamID cursor", 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() location := pending[0].Location()
objects := make([]metabase.RawObject, 3) objects := make([]metabase.RawObject, 3)
@ -1205,23 +1206,23 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
obj.ObjectKey = location.ObjectKey obj.ObjectKey = location.ObjectKey
obj.Version = metabase.Version(i + 1) obj.Version = metabase.Version(i + 1)
createPendingObject(ctx, t, db, obj, 0) metabasetest.CreatePendingObject(ctx, t, db, obj, 0)
objects[i] = metabase.RawObject{ objects[i] = metabase.RawObject{
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Pending, Status: metabase.Pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
} }
expected[i] = objectEntryFromRaw(objects[i]) expected[i] = objectEntryFromRaw(objects[i])
} }
sort.Slice(expected, func(i, j int) bool { 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{ Opts: metabase.IteratePendingObjectsByKey{
ObjectLocation: location, ObjectLocation: location,
BatchSize: 10, BatchSize: 10,
@ -1232,7 +1233,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Result: expected[1:], Result: expected[1:],
}.Check(ctx, t, db) }.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 { 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) objects := make([]metabase.RawObject, numberOfObjects)
for i := 0; i < numberOfObjects; i++ { for i := 0; i < numberOfObjects; i++ {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
obj.ProjectID = projectID obj.ProjectID = projectID
obj.BucketName = bucketName obj.BucketName = bucketName
now := time.Now() now := time.Now()
createObject(ctx, t, db, obj, 0) metabasetest.CreateObject(ctx, t, db, obj, 0)
objects[i] = metabase.RawObject{ objects[i] = metabase.RawObject{
ObjectStream: obj, ObjectStream: obj,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
} }
} }
sort.SliceStable(objects, func(i, j int) bool { 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 { 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)) objects := make(map[metabase.ObjectKey]metabase.ObjectEntry, len(keys))
for _, key := range keys { for _, key := range keys {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
obj.ProjectID = projectID obj.ProjectID = projectID
obj.BucketName = bucketName obj.BucketName = bucketName
obj.ObjectKey = key obj.ObjectKey = key
now := time.Now() now := time.Now()
createObject(ctx, t, db, obj, 0) metabasetest.CreateObject(ctx, t, db, obj, 0)
objects[key] = metabase.ObjectEntry{ objects[key] = metabase.ObjectEntry{
ObjectKey: obj.ObjectKey, ObjectKey: obj.ObjectKey,
@ -1277,7 +1278,7 @@ func createObjectsWithKeys(ctx *testcontext.Context, t *testing.T, db *metabase.
StreamID: obj.StreamID, StreamID: obj.StreamID,
CreatedAt: now, CreatedAt: now,
Status: metabase.Committed, Status: metabase.Committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
} }
} }

View File

@ -11,29 +11,30 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestListSegments(t *testing.T) { func TestListSegments(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
t.Run("StreamID missing", func(t *testing.T) { 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{}, Opts: metabase.ListSegments{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "StreamID missing", ErrText: "StreamID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Invalid limit", func(t *testing.T) { 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{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: -1, Limit: -1,
@ -42,13 +43,13 @@ func TestListSegments(t *testing.T) {
ErrText: "Invalid limit: -1", ErrText: "Invalid limit: -1",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("no segments", func(t *testing.T) { 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{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 1, Limit: 1,
@ -56,13 +57,13 @@ func TestListSegments(t *testing.T) {
Result: metabase.ListSegmentsResult{}, Result: metabase.ListSegmentsResult{},
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("segments", func(t *testing.T) { 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{ expectedSegment := metabase.Segment{
StreamID: obj.StreamID, StreamID: obj.StreamID,
@ -77,7 +78,7 @@ func TestListSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
expectedRawSegments := make([]metabase.RawSegment, 10) expectedRawSegments := make([]metabase.RawSegment, 10)
@ -89,7 +90,7 @@ func TestListSegments(t *testing.T) {
expectedSegment.PlainOffset += int64(expectedSegment.PlainSize) expectedSegment.PlainOffset += int64(expectedSegment.PlainSize)
} }
ListSegments{ metabasetest.ListSegments{
Opts: metabase.ListSegments{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 10, Limit: 10,
@ -99,7 +100,7 @@ func TestListSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListSegments{ metabasetest.ListSegments{
Opts: metabase.ListSegments{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 1, Limit: 1,
@ -110,7 +111,7 @@ func TestListSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListSegments{ metabasetest.ListSegments{
Opts: metabase.ListSegments{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 2, Limit: 2,
@ -124,7 +125,7 @@ func TestListSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListSegments{ metabasetest.ListSegments{
Opts: metabase.ListSegments{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 2, Limit: 2,
@ -137,7 +138,7 @@ func TestListSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListSegments{ metabasetest.ListSegments{
Opts: metabase.ListSegments{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 2, Limit: 2,
@ -151,7 +152,7 @@ func TestListSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
metabase.RawObject(expectedObject), metabase.RawObject(expectedObject),
}, },
@ -160,7 +161,7 @@ func TestListSegments(t *testing.T) {
}) })
t.Run("unordered parts", func(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 { var testCases = []struct {
segments []metabase.SegmentPosition segments []metabase.SegmentPosition
@ -195,22 +196,22 @@ func TestListSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
for _, tc := range testCases { for _, tc := range testCases {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: obj.Version, Version: obj.Version,
}.Check(ctx, t, db) }.Check(ctx, t, db)
for i, segmentPosition := range tc.segments { for i, segmentPosition := range tc.segments {
BeginSegment{ metabasetest.BeginSegment{
Opts: metabase.BeginSegment{ Opts: metabase.BeginSegment{
ObjectStream: obj, ObjectStream: obj,
Position: segmentPosition, Position: segmentPosition,
@ -222,7 +223,7 @@ func TestListSegments(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
Position: segmentPosition, Position: segmentPosition,
@ -236,12 +237,12 @@ func TestListSegments(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
} }
CommitObject{ metabasetest.CommitObject{
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
}, },
@ -257,7 +258,7 @@ func TestListSegments(t *testing.T) {
expectedOffset += int64(expectedSegment.PlainSize) expectedOffset += int64(expectedSegment.PlainSize)
} }
ListSegments{ metabasetest.ListSegments{
Opts: metabase.ListSegments{ Opts: metabase.ListSegments{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 0, Limit: 0,
@ -272,26 +273,26 @@ func TestListSegments(t *testing.T) {
} }
func TestListStreamPositions(t *testing.T) { func TestListStreamPositions(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
t.Run("StreamID missing", func(t *testing.T) { 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{}, Opts: metabase.ListStreamPositions{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "StreamID missing", ErrText: "StreamID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("Invalid limit", func(t *testing.T) { 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{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: -1, Limit: -1,
@ -300,13 +301,13 @@ func TestListStreamPositions(t *testing.T) {
ErrText: "Invalid limit: -1", ErrText: "Invalid limit: -1",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("no segments", func(t *testing.T) { 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{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 1, Limit: 1,
@ -314,13 +315,13 @@ func TestListStreamPositions(t *testing.T) {
Result: metabase.ListStreamPositionsResult{}, Result: metabase.ListStreamPositionsResult{},
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("segments", func(t *testing.T) { 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{ expectedSegment := metabase.Segment{
StreamID: obj.StreamID, StreamID: obj.StreamID,
@ -335,7 +336,7 @@ func TestListStreamPositions(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
expectedRawSegments := make([]metabase.RawSegment, 10) expectedRawSegments := make([]metabase.RawSegment, 10)
@ -355,7 +356,7 @@ func TestListStreamPositions(t *testing.T) {
expectedSegment.PlainOffset += int64(expectedSegment.PlainSize) expectedSegment.PlainOffset += int64(expectedSegment.PlainSize)
} }
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 10, Limit: 10,
@ -365,7 +366,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 1, Limit: 1,
@ -376,7 +377,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 2, Limit: 2,
@ -390,7 +391,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 2, Limit: 2,
@ -403,7 +404,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 2, Limit: 2,
@ -417,7 +418,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
metabase.RawObject(expectedObject), metabase.RawObject(expectedObject),
}, },
@ -426,7 +427,7 @@ func TestListStreamPositions(t *testing.T) {
}) })
t.Run("unordered parts", func(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 { var testCases = []struct {
segments []metabase.SegmentPosition segments []metabase.SegmentPosition
@ -460,22 +461,22 @@ func TestListStreamPositions(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
for _, tc := range testCases { for _, tc := range testCases {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: obj.Version, Version: obj.Version,
}.Check(ctx, t, db) }.Check(ctx, t, db)
for i, segmentPosition := range tc.segments { for i, segmentPosition := range tc.segments {
BeginSegment{ metabasetest.BeginSegment{
Opts: metabase.BeginSegment{ Opts: metabase.BeginSegment{
ObjectStream: obj, ObjectStream: obj,
Position: segmentPosition, Position: segmentPosition,
@ -487,7 +488,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
Position: segmentPosition, Position: segmentPosition,
@ -501,12 +502,12 @@ func TestListStreamPositions(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
} }
CommitObject{ metabasetest.CommitObject{
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
}, },
@ -529,7 +530,7 @@ func TestListStreamPositions(t *testing.T) {
expectedOffset += int64(expectedSegment.PlainSize) expectedOffset += int64(expectedSegment.PlainSize)
} }
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: 0, Limit: 0,
@ -542,7 +543,7 @@ func TestListStreamPositions(t *testing.T) {
}) })
t.Run("range", func(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 segmentCount = 10
const segmentSize = 512 const segmentSize = 512
@ -556,15 +557,15 @@ func TestListStreamPositions(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: segmentSize, PlainSize: segmentSize,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: obj.Version, Version: obj.Version,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -575,7 +576,7 @@ func TestListStreamPositions(t *testing.T) {
Index: uint32(i % 2), Index: uint32(i % 2),
} }
BeginSegment{ metabasetest.BeginSegment{
Opts: metabase.BeginSegment{ Opts: metabase.BeginSegment{
ObjectStream: obj, ObjectStream: obj,
Position: segmentPosition, Position: segmentPosition,
@ -587,7 +588,7 @@ func TestListStreamPositions(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
Position: segmentPosition, Position: segmentPosition,
@ -601,12 +602,12 @@ func TestListStreamPositions(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: segmentSize, PlainSize: segmentSize,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
} }
CommitObject{ metabasetest.CommitObject{
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
}, },
@ -631,7 +632,7 @@ func TestListStreamPositions(t *testing.T) {
expectedOffset += int64(expectedSegment.PlainSize) expectedOffset += int64(expectedSegment.PlainSize)
} }
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Range: &metabase.StreamRange{ Range: &metabase.StreamRange{
@ -669,7 +670,7 @@ func TestListStreamPositions(t *testing.T) {
{limit: 2, plainStart: segmentSize, plainLimit: totalSize, results: expectedSegments[1:3], more: true}, {limit: 2, plainStart: segmentSize, plainLimit: totalSize, results: expectedSegments[1:3], more: true},
} }
for _, test := range tests { for _, test := range tests {
ListStreamPositions{ metabasetest.ListStreamPositions{
Opts: metabase.ListStreamPositions{ Opts: metabase.ListStreamPositions{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Limit: test.limit, Limit: test.limit,

View File

@ -15,40 +15,41 @@ import (
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/common/uuid" "storj.io/common/uuid"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestIterateLoopObjects(t *testing.T) { 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) { t.Run("Limit is negative", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: -1, BatchSize: -1,
}, },
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "BatchSize is negative", ErrText: "BatchSize is negative",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("no data", func(t *testing.T) { 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{ Opts: metabase.IterateLoopObjects{
BatchSize: 0, BatchSize: 0,
}, },
Result: nil, Result: nil,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 10, BatchSize: 10,
}, },
Result: nil, Result: nil,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 10, BatchSize: 10,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -56,21 +57,21 @@ func TestIterateLoopObjects(t *testing.T) {
Result: nil, Result: nil,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("pending and committed", func(t *testing.T) { t.Run("pending and committed", func(t *testing.T) {
defer DeleteAll{}.Check(ctx, t, db) defer metabasetest.DeleteAll{}.Check(ctx, t, db)
pending := randObjectStream() pending := metabasetest.RandObjectStream()
committed := randObjectStream() committed := metabasetest.RandObjectStream()
committed.ProjectID = pending.ProjectID committed.ProjectID = pending.ProjectID
committed.BucketName = pending.BucketName + "z" committed.BucketName = pending.BucketName + "z"
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: pending, ObjectStream: pending,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -79,15 +80,15 @@ func TestIterateLoopObjects(t *testing.T) {
encryptedMetadataNonce := testrand.Nonce() encryptedMetadataNonce := testrand.Nonce()
encryptedMetadataKey := testrand.Bytes(265) encryptedMetadataKey := testrand.Bytes(265)
BeginObjectExactVersion{ metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: committed, ObjectStream: committed,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitObject{ metabasetest.CommitObject{
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: committed, ObjectStream: committed,
EncryptedMetadataNonce: encryptedMetadataNonce[:], EncryptedMetadataNonce: encryptedMetadataNonce[:],
@ -111,14 +112,14 @@ func TestIterateLoopObjects(t *testing.T) {
}, },
} }
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 1, BatchSize: 1,
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 1, BatchSize: 1,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -128,7 +129,7 @@ func TestIterateLoopObjects(t *testing.T) {
}) })
t.Run("less objects than limit", func(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 numberOfObjects := 3
limit := 10 limit := 10
expected := make([]metabase.LoopObjectEntry, numberOfObjects) expected := make([]metabase.LoopObjectEntry, numberOfObjects)
@ -137,14 +138,14 @@ func TestIterateLoopObjects(t *testing.T) {
expected[i] = loopObjectEntryFromRaw(obj) expected[i] = loopObjectEntryFromRaw(obj)
} }
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: limit, BatchSize: limit,
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: limit, BatchSize: limit,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -152,11 +153,11 @@ func TestIterateLoopObjects(t *testing.T) {
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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 numberOfObjects := 10
limit := 3 limit := 3
expected := make([]metabase.LoopObjectEntry, numberOfObjects) expected := make([]metabase.LoopObjectEntry, numberOfObjects)
@ -165,14 +166,14 @@ func TestIterateLoopObjects(t *testing.T) {
expected[i] = loopObjectEntryFromRaw(obj) expected[i] = loopObjectEntryFromRaw(obj)
} }
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: limit, BatchSize: limit,
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: limit, BatchSize: limit,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -180,14 +181,14 @@ func TestIterateLoopObjects(t *testing.T) {
Result: expected, Result: expected,
}.Check(ctx, t, db) }.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) { 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" 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", "a",
"b/1", "b/1",
"b/2", "b/2",
@ -211,14 +212,14 @@ func TestIterateLoopObjects(t *testing.T) {
objects["g"], objects["g"],
} }
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 3, BatchSize: 3,
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 3, BatchSize: 3,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -228,7 +229,7 @@ func TestIterateLoopObjects(t *testing.T) {
}) })
t.Run("multiple projects", func(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{} projects := []uuid.UUID{}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -248,14 +249,14 @@ func TestIterateLoopObjects(t *testing.T) {
} }
} }
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 3, BatchSize: 3,
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 3, BatchSize: 3,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -265,7 +266,7 @@ func TestIterateLoopObjects(t *testing.T) {
}) })
t.Run("multiple projects", func(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{} projects := []uuid.UUID{}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -278,25 +279,25 @@ func TestIterateLoopObjects(t *testing.T) {
expected := make([]metabase.LoopObjectEntry, 0, len(projects)*len(bucketNames)) expected := make([]metabase.LoopObjectEntry, 0, len(projects)*len(bucketNames))
for _, projectID := range projects { for _, projectID := range projects {
for _, bucketName := range bucketNames { for _, bucketName := range bucketNames {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
obj.ProjectID = projectID obj.ProjectID = projectID
obj.BucketName = bucketName obj.BucketName = bucketName
for version := 1; version < 4; version++ { for version := 1; version < 4; version++ {
obj.Version = metabase.Version(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))) expected = append(expected, loopObjectEntryFromRaw(metabase.RawObject(rawObject)))
} }
} }
} }
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 2, BatchSize: 2,
}, },
Result: expected, Result: expected,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopObjects{ metabasetest.IterateLoopObjects{
Opts: metabase.IterateLoopObjects{ Opts: metabase.IterateLoopObjects{
BatchSize: 2, BatchSize: 2,
AsOfSystemTime: time.Now(), AsOfSystemTime: time.Now(),
@ -308,24 +309,24 @@ func TestIterateLoopObjects(t *testing.T) {
} }
func TestIterateLoopStreams(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) { 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{}, Opts: metabase.IterateLoopStreams{},
Result: map[uuid.UUID][]metabase.LoopSegmentEntry{}, Result: map[uuid.UUID][]metabase.LoopSegmentEntry{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "StreamIDs list is empty", ErrText: "StreamIDs list is empty",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.IterateLoopStreams{
StreamIDs: []uuid.UUID{{}}, StreamIDs: []uuid.UUID{{}},
}, },
@ -334,18 +335,18 @@ func TestIterateLoopStreams(t *testing.T) {
ErrText: "StreamID missing: index 0", ErrText: "StreamID missing: index 0",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("List objects segments", func(t *testing.T) { 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() now := time.Now()
expectedObject00 := createObject(ctx, t, db, randObjectStream(), 0) expectedObject00 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 0)
expectedObject01 := createObject(ctx, t, db, randObjectStream(), 1) expectedObject01 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 1)
expectedObject02 := createObject(ctx, t, db, randObjectStream(), 5) expectedObject02 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 5)
expectedObject03 := createObject(ctx, t, db, randObjectStream(), 3) expectedObject03 := metabasetest.CreateObject(ctx, t, db, metabasetest.RandObjectStream(), 3)
expectedRawSegments := []metabase.RawSegment{} expectedRawSegments := []metabase.RawSegment{}
@ -375,7 +376,7 @@ func TestIterateLoopStreams(t *testing.T) {
PlainSize: 512, PlainSize: 512,
PlainOffset: int64(i * 512), PlainOffset: int64(i * 512),
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}},
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
} }
expectedSegments = append(expectedSegments, segment) expectedSegments = append(expectedSegments, segment)
@ -397,7 +398,7 @@ func TestIterateLoopStreams(t *testing.T) {
expectedMap[object.StreamID] = expectedSegments expectedMap[object.StreamID] = expectedSegments
} }
IterateLoopStreams{ metabasetest.IterateLoopStreams{
Opts: metabase.IterateLoopStreams{ Opts: metabase.IterateLoopStreams{
StreamIDs: []uuid.UUID{ StreamIDs: []uuid.UUID{
expectedObject00.StreamID, expectedObject00.StreamID,
@ -411,7 +412,7 @@ func TestIterateLoopStreams(t *testing.T) {
Result: expectedMap, Result: expectedMap,
}.Check(ctx, t, db) }.Check(ctx, t, db)
IterateLoopStreams{ metabasetest.IterateLoopStreams{
Opts: metabase.IterateLoopStreams{ Opts: metabase.IterateLoopStreams{
StreamIDs: []uuid.UUID{ StreamIDs: []uuid.UUID{
expectedObject00.StreamID, expectedObject00.StreamID,
@ -423,7 +424,7 @@ func TestIterateLoopStreams(t *testing.T) {
Result: expectedMap, Result: expectedMap,
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
metabase.RawObject(expectedObject00), metabase.RawObject(expectedObject00),
metabase.RawObject(expectedObject01), 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 { func loopObjectEntryFromRaw(m metabase.RawObject) metabase.LoopObjectEntry {
return metabase.LoopObjectEntry{ return metabase.LoopObjectEntry{
ObjectStream: m.ObjectStream, ObjectStream: m.ObjectStream,

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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,
}

View File

@ -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
}

View File

@ -1,7 +1,7 @@
// Copyright (C) 2020 Storj Labs, Inc. // Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information. // See LICENSE for copying information.
package metabase_test package metabasetest
import ( import (
"flag" "flag"
@ -9,50 +9,51 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
"storj.io/common/testcontext" "storj.io/common/testcontext"
_ "storj.io/private/dbutil/cockroachutil" // register cockroach driver
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/satellitedb/satellitedbtest" "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 { // Database contains info about a test database connection.
name string type Database struct {
driver string Name string
connstr string Driver string
ConnStr string
} }
func databaseInfos() []dbinfo { // DatabaseEntries returns databases passed in with -databases or STORJ_TEST_DATABASES flag.
infos := []dbinfo{ func DatabaseEntries() []Database {
infos := []Database{
{"pg", "pgx", "postgres://storj:storj-pass@localhost/metabase?sslmode=disable"}, {"pg", "pgx", "postgres://storj:storj-pass@localhost/metabase?sslmode=disable"},
{"crdb", "pgx", "cockroach://root@localhost:26257/metabase?sslmode=disable"}, {"crdb", "pgx", "cockroach://root@localhost:26257/metabase?sslmode=disable"},
} }
if *databases != "" { if *databasesFlag != "" {
infos = nil infos = nil
for _, db := range strings.Split(*databases, ";") { for _, db := range strings.Split(*databasesFlag, ";") {
toks := strings.Split(strings.TrimSpace(db), "|") 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 return infos
} }
func All(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) { // Run runs tests against all configured databases.
for _, info := range databaseInfos() { func Run(t *testing.T, fn func(ctx *testcontext.Context, t *testing.T, db *metabase.DB)) {
for _, info := range DatabaseEntries() {
info := info info := info
t.Run(info.name, func(t *testing.T) { t.Run(info.Name, func(t *testing.T) {
t.Parallel() t.Parallel()
ctx := testcontext.New(t) ctx := testcontext.New(t)
defer ctx.Cleanup() defer ctx.Cleanup()
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{ db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(t), t.Name(), "M", 0, satellitedbtest.Database{
Name: info.name, Name: info.Name,
URL: info.connstr, URL: info.ConnStr,
Message: "", Message: "",
}) })
if err != nil { 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)) { 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 info := info
b.Run(info.name, func(b *testing.B) { b.Run(info.Name, func(b *testing.B) {
ctx := testcontext.New(b) ctx := testcontext.New(b)
defer ctx.Cleanup() defer ctx.Cleanup()
db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, satellitedbtest.Database{ db, err := satellitedbtest.CreateMetabaseDB(ctx, zaptest.NewLogger(b), b.Name(), "M", 0, satellitedbtest.Database{
Name: info.name, Name: info.Name,
URL: info.connstr, URL: info.ConnStr,
Message: "", Message: "",
}) })
if err != nil { 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)
})
}

View File

@ -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)
})
}

View File

@ -1,10 +1,9 @@
// Copyright (C) 2020 Storj Labs, Inc. // Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information. // See LICENSE for copying information.
package metabase_test package metabasetest
import ( import (
"bytes"
"context" "context"
"sort" "sort"
"testing" "testing"
@ -17,11 +16,11 @@ import (
"storj.io/common/storj" "storj.io/common/storj"
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand"
"storj.io/common/uuid" "storj.io/common/uuid"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
) )
// BeginObjectNextVersion is for testing metabase.BeginObjectNextVersion.
type BeginObjectNextVersion struct { type BeginObjectNextVersion struct {
Opts metabase.BeginObjectNextVersion Opts metabase.BeginObjectNextVersion
Version metabase.Version Version metabase.Version
@ -29,12 +28,14 @@ type BeginObjectNextVersion struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step BeginObjectNextVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step BeginObjectNextVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
got, err := db.BeginObjectNextVersion(ctx, step.Opts) got, err := db.BeginObjectNextVersion(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
require.Equal(t, step.Version, got) require.Equal(t, step.Version, got)
} }
// BeginObjectExactVersion is for testing metabase.BeginObjectExactVersion.
type BeginObjectExactVersion struct { type BeginObjectExactVersion struct {
Opts metabase.BeginObjectExactVersion Opts metabase.BeginObjectExactVersion
Version metabase.Version Version metabase.Version
@ -42,6 +43,7 @@ type BeginObjectExactVersion struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step BeginObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step BeginObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
got, err := db.BeginObjectExactVersion(ctx, step.Opts) got, err := db.BeginObjectExactVersion(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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 { type CommitObject struct {
Opts metabase.CommitObject Opts metabase.CommitObject
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step CommitObject) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) metabase.Object { func (step CommitObject) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) metabase.Object {
object, err := db.CommitObject(ctx, step.Opts) object, err := db.CommitObject(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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 return object
} }
// CommitObjectWithSegments is for testing metabase.CommitObjectWithSegments.
type CommitObjectWithSegments struct { type CommitObjectWithSegments struct {
Opts metabase.CommitObjectWithSegments Opts metabase.CommitObjectWithSegments
Deleted []metabase.DeletedSegmentInfo Deleted []metabase.DeletedSegmentInfo
@ -77,6 +82,7 @@ type CommitObjectWithSegments struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step CommitObjectWithSegments) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) metabase.Object { func (step CommitObjectWithSegments) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) metabase.Object {
object, deleted, err := db.CommitObjectWithSegments(ctx, step.Opts) object, deleted, err := db.CommitObjectWithSegments(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
@ -87,39 +93,46 @@ func (step CommitObjectWithSegments) Check(ctx *testcontext.Context, t testing.T
return object return object
} }
// BeginSegment is for testing metabase.BeginSegment.
type BeginSegment struct { type BeginSegment struct {
Opts metabase.BeginSegment Opts metabase.BeginSegment
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step BeginSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step BeginSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.BeginSegment(ctx, step.Opts) err := db.BeginSegment(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// CommitSegment is for testing metabase.CommitSegment.
type CommitSegment struct { type CommitSegment struct {
Opts metabase.CommitSegment Opts metabase.CommitSegment
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step CommitSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step CommitSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.CommitSegment(ctx, step.Opts) err := db.CommitSegment(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// CommitInlineSegment is for testing metabase.CommitInlineSegment.
type CommitInlineSegment struct { type CommitInlineSegment struct {
Opts metabase.CommitInlineSegment Opts metabase.CommitInlineSegment
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step CommitInlineSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step CommitInlineSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.CommitInlineSegment(ctx, step.Opts) err := db.CommitInlineSegment(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// DeleteBucketObjects is for testing metabase.DeleteBucketObjects.
type DeleteBucketObjects struct { type DeleteBucketObjects struct {
Opts metabase.DeleteBucketObjects Opts metabase.DeleteBucketObjects
Deleted int64 Deleted int64
@ -127,34 +140,40 @@ type DeleteBucketObjects struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeleteBucketObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeleteBucketObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
deleted, err := db.DeleteBucketObjects(ctx, step.Opts) deleted, err := db.DeleteBucketObjects(ctx, step.Opts)
require.Equal(t, step.Deleted, deleted) require.Equal(t, step.Deleted, deleted)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// UpdateObjectMetadata is for testing metabase.UpdateObjectMetadata.
type UpdateObjectMetadata struct { type UpdateObjectMetadata struct {
Opts metabase.UpdateObjectMetadata Opts metabase.UpdateObjectMetadata
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step UpdateObjectMetadata) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step UpdateObjectMetadata) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.UpdateObjectMetadata(ctx, step.Opts) err := db.UpdateObjectMetadata(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// UpdateSegmentPieces is for testing metabase.UpdateSegmentPieces.
type UpdateSegmentPieces struct { type UpdateSegmentPieces struct {
Opts metabase.UpdateSegmentPieces Opts metabase.UpdateSegmentPieces
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step UpdateSegmentPieces) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step UpdateSegmentPieces) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.UpdateSegmentPieces(ctx, step.Opts) err := db.UpdateSegmentPieces(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// GetObjectExactVersion is for testing metabase.GetObjectExactVersion.
type GetObjectExactVersion struct { type GetObjectExactVersion struct {
Opts metabase.GetObjectExactVersion Opts metabase.GetObjectExactVersion
Result metabase.Object Result metabase.Object
@ -162,6 +181,7 @@ type GetObjectExactVersion struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetObjectExactVersion(ctx, step.Opts) result, err := db.GetObjectExactVersion(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// GetObjectLatestVersion is for testing metabase.GetObjectLatestVersion.
type GetObjectLatestVersion struct { type GetObjectLatestVersion struct {
Opts metabase.GetObjectLatestVersion Opts metabase.GetObjectLatestVersion
Result metabase.Object Result metabase.Object
@ -177,6 +198,7 @@ type GetObjectLatestVersion struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetObjectLatestVersion(ctx, step.Opts) result, err := db.GetObjectLatestVersion(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// GetSegmentByLocation is for testing metabase.GetSegmentByLocation.
type GetSegmentByLocation struct { type GetSegmentByLocation struct {
Opts metabase.GetSegmentByLocation Opts metabase.GetSegmentByLocation
Result metabase.Segment Result metabase.Segment
@ -192,6 +215,7 @@ type GetSegmentByLocation struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetSegmentByLocation) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetSegmentByLocation) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetSegmentByLocation(ctx, step.Opts) result, err := db.GetSegmentByLocation(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// GetSegmentByPosition is for testing metabase.GetSegmentByPosition.
type GetSegmentByPosition struct { type GetSegmentByPosition struct {
Opts metabase.GetSegmentByPosition Opts metabase.GetSegmentByPosition
Result metabase.Segment Result metabase.Segment
@ -207,6 +232,7 @@ type GetSegmentByPosition struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetSegmentByPosition) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetSegmentByPosition) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetSegmentByPosition(ctx, step.Opts) result, err := db.GetSegmentByPosition(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// GetLatestObjectLastSegment is for testing metabase.GetLatestObjectLastSegment.
type GetLatestObjectLastSegment struct { type GetLatestObjectLastSegment struct {
Opts metabase.GetLatestObjectLastSegment Opts metabase.GetLatestObjectLastSegment
Result metabase.Segment Result metabase.Segment
@ -222,6 +249,7 @@ type GetLatestObjectLastSegment struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetLatestObjectLastSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetLatestObjectLastSegment) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetLatestObjectLastSegment(ctx, step.Opts) result, err := db.GetLatestObjectLastSegment(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
@ -230,6 +258,7 @@ func (step GetLatestObjectLastSegment) Check(ctx *testcontext.Context, t testing
require.Zero(t, diff) require.Zero(t, diff)
} }
// GetSegmentByOffset is for testing metabase.GetSegmentByOffset.
type GetSegmentByOffset struct { type GetSegmentByOffset struct {
Opts metabase.GetSegmentByOffset Opts metabase.GetSegmentByOffset
Result metabase.Segment Result metabase.Segment
@ -237,6 +266,7 @@ type GetSegmentByOffset struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetSegmentByOffset) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetSegmentByOffset) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetSegmentByOffset(ctx, step.Opts) result, err := db.GetSegmentByOffset(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// BucketEmpty is for testing metabase.BucketEmpty.
type BucketEmpty struct { type BucketEmpty struct {
Opts metabase.BucketEmpty Opts metabase.BucketEmpty
Result bool Result bool
@ -252,6 +283,7 @@ type BucketEmpty struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step BucketEmpty) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step BucketEmpty) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.BucketEmpty(ctx, step.Opts) result, err := db.BucketEmpty(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Equal(t, step.Result, result)
} }
// ListSegments is for testing metabase.ListSegments.
type ListSegments struct { type ListSegments struct {
Opts metabase.ListSegments Opts metabase.ListSegments
Result metabase.ListSegmentsResult Result metabase.ListSegmentsResult
@ -266,6 +299,7 @@ type ListSegments struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step ListSegments) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step ListSegments) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.ListSegments(ctx, step.Opts) result, err := db.ListSegments(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// ListStreamPositions is for testing metabase.ListStreamPositions.
type ListStreamPositions struct { type ListStreamPositions struct {
Opts metabase.ListStreamPositions Opts metabase.ListStreamPositions
Result metabase.ListStreamPositionsResult Result metabase.ListStreamPositionsResult
@ -281,6 +316,7 @@ type ListStreamPositions struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step ListStreamPositions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step ListStreamPositions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.ListStreamPositions(ctx, step.Opts) result, err := db.ListStreamPositions(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// GetStreamPieceCountByNodeID is for testing metabase.GetStreamPieceCountByNodeID.
type GetStreamPieceCountByNodeID struct { type GetStreamPieceCountByNodeID struct {
Opts metabase.GetStreamPieceCountByNodeID Opts metabase.GetStreamPieceCountByNodeID
Result map[storj.NodeID]int64 Result map[storj.NodeID]int64
@ -296,6 +333,7 @@ type GetStreamPieceCountByNodeID struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step GetStreamPieceCountByNodeID) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step GetStreamPieceCountByNodeID) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.GetStreamPieceCountByNodeID(ctx, step.Opts) result, err := db.GetStreamPieceCountByNodeID(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
@ -304,6 +342,7 @@ func (step GetStreamPieceCountByNodeID) Check(ctx *testcontext.Context, t testin
require.Zero(t, diff) require.Zero(t, diff)
} }
// IterateLoopStreams is for testing metabase.IterateLoopStreams.
type IterateLoopStreams struct { type IterateLoopStreams struct {
Opts metabase.IterateLoopStreams Opts metabase.IterateLoopStreams
Result map[uuid.UUID][]metabase.LoopSegmentEntry Result map[uuid.UUID][]metabase.LoopSegmentEntry
@ -311,6 +350,7 @@ type IterateLoopStreams struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step IterateLoopStreams) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step IterateLoopStreams) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result := make(map[uuid.UUID][]metabase.LoopSegmentEntry) result := make(map[uuid.UUID][]metabase.LoopSegmentEntry)
err := db.IterateLoopStreams(ctx, step.Opts, 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) require.Zero(t, diff)
} }
// DeleteObjectExactVersion is for testing metabase.DeleteObjectExactVersion.
type DeleteObjectExactVersion struct { type DeleteObjectExactVersion struct {
Opts metabase.DeleteObjectExactVersion Opts metabase.DeleteObjectExactVersion
Result metabase.DeleteObjectResult Result metabase.DeleteObjectResult
@ -339,6 +380,7 @@ type DeleteObjectExactVersion struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeleteObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeleteObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.DeleteObjectExactVersion(ctx, step.Opts) result, err := db.DeleteObjectExactVersion(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// DeletePendingObject is for testing metabase.DeletePendingObject.
type DeletePendingObject struct { type DeletePendingObject struct {
Opts metabase.DeletePendingObject Opts metabase.DeletePendingObject
Result metabase.DeleteObjectResult Result metabase.DeleteObjectResult
@ -354,6 +397,7 @@ type DeletePendingObject struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeletePendingObject) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeletePendingObject) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.DeletePendingObject(ctx, step.Opts) result, err := db.DeletePendingObject(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// DeleteObjectLatestVersion is for testing metabase.DeleteObjectLatestVersion.
type DeleteObjectLatestVersion struct { type DeleteObjectLatestVersion struct {
Opts metabase.DeleteObjectLatestVersion Opts metabase.DeleteObjectLatestVersion
Result metabase.DeleteObjectResult Result metabase.DeleteObjectResult
@ -369,6 +414,7 @@ type DeleteObjectLatestVersion struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeleteObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeleteObjectLatestVersion) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.DeleteObjectLatestVersion(ctx, step.Opts) result, err := db.DeleteObjectLatestVersion(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
@ -377,6 +423,7 @@ func (step DeleteObjectLatestVersion) Check(ctx *testcontext.Context, t testing.
require.Zero(t, diff) require.Zero(t, diff)
} }
// DeleteObjectAnyStatusAllVersions is for testing metabase.DeleteObjectAnyStatusAllVersions.
type DeleteObjectAnyStatusAllVersions struct { type DeleteObjectAnyStatusAllVersions struct {
Opts metabase.DeleteObjectAnyStatusAllVersions Opts metabase.DeleteObjectAnyStatusAllVersions
Result metabase.DeleteObjectResult Result metabase.DeleteObjectResult
@ -384,6 +431,7 @@ type DeleteObjectAnyStatusAllVersions struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeleteObjectAnyStatusAllVersions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeleteObjectAnyStatusAllVersions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.DeleteObjectAnyStatusAllVersions(ctx, step.Opts) result, err := db.DeleteObjectAnyStatusAllVersions(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
@ -392,6 +440,7 @@ func (step DeleteObjectAnyStatusAllVersions) Check(ctx *testcontext.Context, t t
require.Zero(t, diff) require.Zero(t, diff)
} }
// DeleteObjectsAllVersions is for testing metabase.DeleteObjectsAllVersions.
type DeleteObjectsAllVersions struct { type DeleteObjectsAllVersions struct {
Opts metabase.DeleteObjectsAllVersions Opts metabase.DeleteObjectsAllVersions
Result metabase.DeleteObjectResult Result metabase.DeleteObjectResult
@ -399,6 +448,7 @@ type DeleteObjectsAllVersions struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeleteObjectsAllVersions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeleteObjectsAllVersions) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
result, err := db.DeleteObjectsAllVersions(ctx, step.Opts) result, err := db.DeleteObjectsAllVersions(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) 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) require.Zero(t, diff)
} }
// DeleteExpiredObjects is for testing metabase.DeleteExpiredObjects.
type DeleteExpiredObjects struct { type DeleteExpiredObjects struct {
Opts metabase.DeleteExpiredObjects Opts metabase.DeleteExpiredObjects
@ -417,13 +468,16 @@ type DeleteExpiredObjects struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step DeleteExpiredObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step DeleteExpiredObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.DeleteExpiredObjects(ctx, step.Opts) err := db.DeleteExpiredObjects(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// IterateCollector is for testing metabase.IterateCollector.
type IterateCollector []metabase.ObjectEntry type IterateCollector []metabase.ObjectEntry
// Add adds object entries from iterator to the collection.
func (coll *IterateCollector) Add(ctx context.Context, it metabase.ObjectsIterator) error { func (coll *IterateCollector) Add(ctx context.Context, it metabase.ObjectsIterator) error {
var item metabase.ObjectEntry var item metabase.ObjectEntry
@ -433,8 +487,10 @@ func (coll *IterateCollector) Add(ctx context.Context, it metabase.ObjectsIterat
return nil return nil
} }
// LoopIterateCollector is for testing metabase.LoopIterateCollector.
type LoopIterateCollector []metabase.LoopObjectEntry type LoopIterateCollector []metabase.LoopObjectEntry
// Add adds object entries from iterator to the collection.
func (coll *LoopIterateCollector) Add(ctx context.Context, it metabase.LoopObjectsIterator) error { func (coll *LoopIterateCollector) Add(ctx context.Context, it metabase.LoopObjectsIterator) error {
var item metabase.LoopObjectEntry var item metabase.LoopObjectEntry
@ -444,6 +500,7 @@ func (coll *LoopIterateCollector) Add(ctx context.Context, it metabase.LoopObjec
return nil return nil
} }
// IterateObjects is for testing metabase.IterateObjects.
type IterateObjects struct { type IterateObjects struct {
Opts metabase.IterateObjects Opts metabase.IterateObjects
@ -452,6 +509,7 @@ type IterateObjects struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step IterateObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step IterateObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
var collector IterateCollector var collector IterateCollector
@ -466,6 +524,7 @@ func (step IterateObjects) Check(ctx *testcontext.Context, t testing.TB, db *met
require.Zero(t, diff) require.Zero(t, diff)
} }
// IteratePendingObjectsByKey is for testing metabase.IteratePendingObjectsByKey.
type IteratePendingObjectsByKey struct { type IteratePendingObjectsByKey struct {
Opts metabase.IteratePendingObjectsByKey Opts metabase.IteratePendingObjectsByKey
@ -474,6 +533,7 @@ type IteratePendingObjectsByKey struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step IteratePendingObjectsByKey) Check(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { func (step IteratePendingObjectsByKey) Check(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
var collector IterateCollector var collector IterateCollector
@ -486,6 +546,7 @@ func (step IteratePendingObjectsByKey) Check(ctx *testcontext.Context, t *testin
require.Zero(t, diff) require.Zero(t, diff)
} }
// IterateObjectsWithStatus is for testing metabase.IterateObjectsWithStatus.
type IterateObjectsWithStatus struct { type IterateObjectsWithStatus struct {
Opts metabase.IterateObjectsWithStatus Opts metabase.IterateObjectsWithStatus
@ -494,6 +555,7 @@ type IterateObjectsWithStatus struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step IterateObjectsWithStatus) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step IterateObjectsWithStatus) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
var result IterateCollector var result IterateCollector
@ -504,6 +566,7 @@ func (step IterateObjectsWithStatus) Check(ctx *testcontext.Context, t testing.T
require.Zero(t, diff) require.Zero(t, diff)
} }
// IterateLoopObjects is for testing metabase.IterateLoopObjects.
type IterateLoopObjects struct { type IterateLoopObjects struct {
Opts metabase.IterateLoopObjects Opts metabase.IterateLoopObjects
@ -512,6 +575,7 @@ type IterateLoopObjects struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step IterateLoopObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step IterateLoopObjects) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
var result LoopIterateCollector var result LoopIterateCollector
@ -522,6 +586,7 @@ func (step IterateLoopObjects) Check(ctx *testcontext.Context, t testing.TB, db
require.Zero(t, diff) require.Zero(t, diff)
} }
// EnsureNodeAliases is for testing metabase.EnsureNodeAliases.
type EnsureNodeAliases struct { type EnsureNodeAliases struct {
Opts metabase.EnsureNodeAliases Opts metabase.EnsureNodeAliases
@ -529,233 +594,21 @@ type EnsureNodeAliases struct {
ErrText string ErrText string
} }
// Check runs the test.
func (step EnsureNodeAliases) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { func (step EnsureNodeAliases) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) {
err := db.EnsureNodeAliases(ctx, step.Opts) err := db.EnsureNodeAliases(ctx, step.Opts)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
} }
// ListNodeAliases is for testing metabase.ListNodeAliases.
type ListNodeAliases struct { type ListNodeAliases struct {
ErrClass *errs.Class ErrClass *errs.Class
ErrText string ErrText string
} }
// Check runs the test.
func (step ListNodeAliases) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) []metabase.NodeAliasEntry { func (step ListNodeAliases) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) []metabase.NodeAliasEntry {
result, err := db.ListNodeAliases(ctx) result, err := db.ListNodeAliases(ctx)
checkError(t, err, step.ErrClass, step.ErrText) checkError(t, err, step.ErrClass, step.ErrText)
return result 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
}

View File

@ -10,44 +10,45 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
) )
func TestGetStreamPieceCountByNodeID(t *testing.T) { func TestGetStreamPieceCountByNodeID(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
t.Run("StreamID missing", func(t *testing.T) { 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{}, Opts: metabase.GetStreamPieceCountByNodeID{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "StreamID missing", ErrText: "StreamID missing",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("no segments", func(t *testing.T) { 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{ Opts: metabase.GetStreamPieceCountByNodeID{
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
Result: map[storj.NodeID]int64{}, Result: map[storj.NodeID]int64{},
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("inline segments", func(t *testing.T) { 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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -55,7 +56,7 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) {
encryptedKey := testrand.Bytes(32) encryptedKey := testrand.Bytes(32)
encryptedKeyNonce := testrand.Bytes(32) encryptedKeyNonce := testrand.Bytes(32)
CommitInlineSegment{ metabasetest.CommitInlineSegment{
Opts: metabase.CommitInlineSegment{ Opts: metabase.CommitInlineSegment{
ObjectStream: obj, ObjectStream: obj,
Position: metabase.SegmentPosition{Part: 0, Index: 0}, Position: metabase.SegmentPosition{Part: 0, Index: 0},
@ -69,7 +70,7 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) {
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
GetStreamPieceCountByNodeID{ metabasetest.GetStreamPieceCountByNodeID{
Opts: metabase.GetStreamPieceCountByNodeID{ Opts: metabase.GetStreamPieceCountByNodeID{
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
@ -78,12 +79,12 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) {
}) })
t.Run("remote segments", func(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{ Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj, ObjectStream: obj,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
Version: 1, Version: 1,
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -95,7 +96,7 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) {
n02 := testrand.NodeID() n02 := testrand.NodeID()
n03 := testrand.NodeID() n03 := testrand.NodeID()
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
Position: metabase.SegmentPosition{Part: 0, Index: 0}, Position: metabase.SegmentPosition{Part: 0, Index: 0},
@ -112,11 +113,11 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
CommitSegment{ metabasetest.CommitSegment{
Opts: metabase.CommitSegment{ Opts: metabase.CommitSegment{
ObjectStream: obj, ObjectStream: obj,
Position: metabase.SegmentPosition{Part: 1, Index: 56}, Position: metabase.SegmentPosition{Part: 1, Index: 56},
@ -134,11 +135,11 @@ func TestGetStreamPieceCountByNodeID(t *testing.T) {
EncryptedSize: 1024, EncryptedSize: 1024,
PlainSize: 512, PlainSize: 512,
PlainOffset: 0, PlainOffset: 0,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
GetStreamPieceCountByNodeID{ metabasetest.GetStreamPieceCountByNodeID{
Opts: metabase.GetStreamPieceCountByNodeID{ Opts: metabase.GetStreamPieceCountByNodeID{
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },

View File

@ -15,12 +15,13 @@ import (
"storj.io/common/testcontext" "storj.io/common/testcontext"
"storj.io/common/testrand" "storj.io/common/testrand"
"storj.io/storj/satellite/metabase" "storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/metabasetest"
"storj.io/storj/storage" "storj.io/storj/storage"
) )
func TestUpdateSegmentPieces(t *testing.T) { func TestUpdateSegmentPieces(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) {
obj := randObjectStream() obj := metabasetest.RandObjectStream()
now := time.Now() now := time.Now()
@ -30,9 +31,9 @@ func TestUpdateSegmentPieces(t *testing.T) {
}} }}
t.Run("StreamID missing", func(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{}, Opts: metabase.UpdateSegmentPieces{},
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "StreamID missing", ErrText: "StreamID missing",
@ -40,9 +41,9 @@ func TestUpdateSegmentPieces(t *testing.T) {
}) })
t.Run("OldPieces missing", func(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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
}, },
@ -52,9 +53,9 @@ func TestUpdateSegmentPieces(t *testing.T) {
}) })
t.Run("OldPieces: piece number 1 is missing", func(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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: []metabase.Piece{{ OldPieces: []metabase.Piece{{
@ -65,13 +66,13 @@ func TestUpdateSegmentPieces(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "OldPieces: piece number 1 is missing storage node id", ErrText: "OldPieces: piece number 1 is missing storage node id",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: []metabase.Piece{ OldPieces: []metabase.Piece{
@ -88,13 +89,13 @@ func TestUpdateSegmentPieces(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "OldPieces: duplicated piece number 1", ErrText: "OldPieces: duplicated piece number 1",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: []metabase.Piece{ OldPieces: []metabase.Piece{
@ -111,13 +112,13 @@ func TestUpdateSegmentPieces(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "OldPieces: pieces should be ordered", ErrText: "OldPieces: pieces should be ordered",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("NewRedundancy zero", func(t *testing.T) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: []metabase.Piece{{ OldPieces: []metabase.Piece{{
@ -131,16 +132,16 @@ func TestUpdateSegmentPieces(t *testing.T) {
}) })
t.Run("NewPieces vs NewRedundancy", func(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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: []metabase.Piece{{ OldPieces: []metabase.Piece{{
Number: 1, Number: 1,
StorageNode: testrand.NodeID(), StorageNode: testrand.NodeID(),
}}, }},
NewRedundancy: defaultTestRedundancy, NewRedundancy: metabasetest.DefaultRedundancy,
}, },
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "number of new pieces is less than new redundancy repair shares value", 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) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: validPieces, OldPieces: validPieces,
NewRedundancy: defaultTestRedundancy, NewRedundancy: metabasetest.DefaultRedundancy,
NewPieces: []metabase.Piece{{ NewPieces: []metabase.Piece{{
Number: 1, Number: 1,
StorageNode: storj.NodeID{}, StorageNode: storj.NodeID{},
@ -163,17 +164,17 @@ func TestUpdateSegmentPieces(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "NewPieces: piece number 1 is missing storage node id", ErrText: "NewPieces: piece number 1 is missing storage node id",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: validPieces, OldPieces: validPieces,
NewRedundancy: defaultTestRedundancy, NewRedundancy: metabasetest.DefaultRedundancy,
NewPieces: []metabase.Piece{ NewPieces: []metabase.Piece{
{ {
Number: 1, Number: 1,
@ -188,17 +189,17 @@ func TestUpdateSegmentPieces(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "NewPieces: duplicated piece number 1", ErrText: "NewPieces: duplicated piece number 1",
}.Check(ctx, t, db) }.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) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
OldPieces: validPieces, OldPieces: validPieces,
NewRedundancy: defaultTestRedundancy, NewRedundancy: metabasetest.DefaultRedundancy,
NewPieces: []metabase.Piece{ NewPieces: []metabase.Piece{
{ {
Number: 2, Number: 2,
@ -213,18 +214,18 @@ func TestUpdateSegmentPieces(t *testing.T) {
ErrClass: &metabase.ErrInvalidRequest, ErrClass: &metabase.ErrInvalidRequest,
ErrText: "NewPieces: pieces should be ordered", ErrText: "NewPieces: pieces should be ordered",
}.Check(ctx, t, db) }.Check(ctx, t, db)
Verify{}.Check(ctx, t, db) metabasetest.Verify{}.Check(ctx, t, db)
}) })
t.Run("segment not found", func(t *testing.T) { 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{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Position: metabase.SegmentPosition{Index: 1}, Position: metabase.SegmentPosition{Index: 1},
OldPieces: validPieces, OldPieces: validPieces,
NewRedundancy: defaultTestRedundancy, NewRedundancy: metabasetest.DefaultRedundancy,
NewPieces: validPieces, NewPieces: validPieces,
}, },
ErrClass: &metabase.ErrSegmentNotFound, ErrClass: &metabase.ErrSegmentNotFound,
@ -233,9 +234,9 @@ func TestUpdateSegmentPieces(t *testing.T) {
}) })
t.Run("segment pieces column was changed", func(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{ newRedundancy := storj.RedundancyScheme{
RequiredShares: 1, RequiredShares: 1,
@ -244,7 +245,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
TotalShares: 4, TotalShares: 4,
} }
UpdateSegmentPieces{ metabasetest.UpdateSegmentPieces{
Opts: metabase.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Position: metabase.SegmentPosition{Index: 0}, Position: metabase.SegmentPosition{Index: 0},
@ -262,7 +263,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
}.Check(ctx, t, db) }.Check(ctx, t, db)
// verify that original pieces and redundancy did not change // verify that original pieces and redundancy did not change
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
metabase.RawObject(obj), metabase.RawObject(obj),
}, },
@ -278,7 +279,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
PlainOffset: 0, PlainOffset: 0,
PlainSize: 512, PlainSize: 512,
Redundancy: defaultTestRedundancy, Redundancy: metabasetest.DefaultRedundancy,
Pieces: metabase.Pieces{{Number: 0, StorageNode: storj.NodeID{2}}}, 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) { 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{ segment, err := db.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{
StreamID: object.StreamID, StreamID: object.StreamID,
@ -307,19 +308,19 @@ func TestUpdateSegmentPieces(t *testing.T) {
}, },
} }
UpdateSegmentPieces{ metabasetest.UpdateSegmentPieces{
Opts: metabase.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Position: metabase.SegmentPosition{Index: 0}, Position: metabase.SegmentPosition{Index: 0},
OldPieces: segment.Pieces, OldPieces: segment.Pieces,
NewRedundancy: defaultTestRedundancy, NewRedundancy: metabasetest.DefaultRedundancy,
NewPieces: expectedPieces, NewPieces: expectedPieces,
}, },
}.Check(ctx, t, db) }.Check(ctx, t, db)
expectedSegment := segment expectedSegment := segment
expectedSegment.Pieces = expectedPieces expectedSegment.Pieces = expectedPieces
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -331,7 +332,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{
@ -341,9 +342,9 @@ func TestUpdateSegmentPieces(t *testing.T) {
}) })
t.Run("update pieces and repair at", func(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{ segment, err := db.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{
StreamID: object.StreamID, StreamID: object.StreamID,
@ -363,7 +364,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
} }
repairedAt := now.Add(time.Hour) repairedAt := now.Add(time.Hour)
UpdateSegmentPieces{ metabasetest.UpdateSegmentPieces{
Opts: metabase.UpdateSegmentPieces{ Opts: metabase.UpdateSegmentPieces{
StreamID: obj.StreamID, StreamID: obj.StreamID,
Position: metabase.SegmentPosition{Index: 0}, Position: metabase.SegmentPosition{Index: 0},
@ -406,7 +407,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
diff = cmp.Diff(expectedSegment, segment, cmpopts.EquateApproxTime(5*time.Second)) diff = cmp.Diff(expectedSegment, segment, cmpopts.EquateApproxTime(5*time.Second))
require.Zero(t, diff) require.Zero(t, diff)
Verify{ metabasetest.Verify{
Objects: []metabase.RawObject{ Objects: []metabase.RawObject{
{ {
ObjectStream: obj, ObjectStream: obj,
@ -418,7 +419,7 @@ func TestUpdateSegmentPieces(t *testing.T) {
TotalEncryptedSize: 1024, TotalEncryptedSize: 1024,
FixedSegmentSize: 512, FixedSegmentSize: 512,
Encryption: defaultTestEncryption, Encryption: metabasetest.DefaultEncryption,
}, },
}, },
Segments: []metabase.RawSegment{ Segments: []metabase.RawSegment{