Skip to content

Commit

Permalink
satellite/metabasetest: include copies in raw state.
Browse files Browse the repository at this point in the history
It can be useful to compare object copies created during unit tests.
They appear in the segment_copies table as couple (stream_id, ancestor_stream_id).

Change-Id: Id335c3ff7084fe30346456d27e670aff329154ea
  • Loading branch information
Fadila82 committed Feb 18, 2022
1 parent 9688c57 commit 5c7c4fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions satellite/metabase/copy_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ func TestFinishCopyObject(t *testing.T) {
metabase.RawObject(copyObj),
},
Segments: expectedSegments,
Copies: []metabase.RawCopy{{
StreamID: copyStream.StreamID,
AncestorStreamID: originalObj.StreamID,
}},
}.Check(ctx, t, db)
})
})
Expand Down
8 changes: 8 additions & 0 deletions satellite/metabase/metabasetest/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (step Verify) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB
sortRawObjects(step.Objects)
sortRawSegments(state.Segments)
sortRawSegments(step.Segments)
sortRawCopies(state.Copies)
sortRawCopies(step.Copies)

diff := cmp.Diff(metabase.RawState(step), *state,
cmpopts.EquateApproxTime(5*time.Second))
Expand All @@ -63,6 +65,12 @@ func sortRawSegments(segments []metabase.RawSegment) {
})
}

func sortRawCopies(copies []metabase.RawCopy) {
sort.Slice(copies, func(i, j int) bool {
return bytes.Compare(copies[i].StreamID[:], copies[j].StreamID[:]) < 0
})
}

func sortDeletedSegments(segments []metabase.DeletedSegmentInfo) {
sort.Slice(segments, func(i, j int) bool {
return bytes.Compare(segments[i].RootPieceID[:], segments[j].RootPieceID[:]) < 0
Expand Down
47 changes: 47 additions & 0 deletions satellite/metabase/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ type RawSegment struct {
Placement storj.PlacementConstraint
}

// RawCopy contains a copy that is stored in the database.
type RawCopy struct {
StreamID uuid.UUID
AncestorStreamID uuid.UUID
}

// RawState contains full state of a table.
type RawState struct {
Objects []RawObject
Segments []RawSegment
Copies []RawCopy
}

// TestingGetState returns the state of the database.
Expand All @@ -89,6 +96,11 @@ func (db *DB) TestingGetState(ctx context.Context) (_ *RawState, err error) {
return nil, Error.New("GetState: %w", err)
}

state.Copies, err = db.testingGetAllCopies(ctx)
if err != nil {
return nil, Error.New("GetState: %w", err)
}

return state, nil
}

Expand Down Expand Up @@ -234,3 +246,38 @@ func (db *DB) testingGetAllSegments(ctx context.Context) (_ []RawSegment, err er
}
return segs, nil
}

// testingGetAllCopies returns the state of the database.
func (db *DB) testingGetAllCopies(ctx context.Context) (_ []RawCopy, err error) {
copies := []RawCopy{}

rows, err := db.db.QueryContext(ctx, `
SELECT
stream_id, ancestor_stream_id
FROM segment_copies
ORDER BY stream_id ASC, ancestor_stream_id ASC
`)
if err != nil {
return nil, Error.New("testingGetAllCopies query: %w", err)
}
defer func() { err = errs.Combine(err, rows.Close()) }()
for rows.Next() {
var copy RawCopy
err := rows.Scan(
&copy.StreamID,
&copy.AncestorStreamID,
)
if err != nil {
return nil, Error.New("testingGetAllCopies scan failed: %w", err)
}
copies = append(copies, copy)
}
if err := rows.Err(); err != nil {
return nil, Error.New("testingGetAllCopies scan failed: %w", err)
}

if len(copies) == 0 {
return nil, nil
}
return copies, nil
}

0 comments on commit 5c7c4fe

Please sign in to comment.