2021-04-26 10:35:44 +01:00
|
|
|
// 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)
|
2022-02-17 23:26:03 +00:00
|
|
|
sortRawCopies(state.Copies)
|
|
|
|
sortRawCopies(step.Copies)
|
2021-04-26 10:35:44 +01:00
|
|
|
|
|
|
|
diff := cmp.Diff(metabase.RawState(step), *state,
|
satellite/metabase/test: extend default time diff for comparison
Tests are intermittently fail with similar error:
```
--- FAIL: TestDeletePendingObject/Cockroach (15.85s)
test.go:380:
Error Trace: test.go:380
delete_test.go:221
Error: Should be zero, but was metabase.DeleteObjectResult{
Objects: []metabase.Object{
{
ObjectStream: {ProjectID: {0x0f, 0x40, 0x70, 0x41, ...}, BucketName: "txxywyg4", ObjectKey: "\xbb+$\x17\x80\xc6\xcaC\xa3\xdb\xc3z*\xa8\xbe\xaf", Version: 1, ...},
- CreatedAt: s"2022-05-20 14:40:15.995376773 +0200 CEST",
+ CreatedAt: s"2022-05-20 14:40:21.04949 +0200 CEST",
ExpiresAt: nil,
Status: 1,
... // 9 identical fields
},
},
Segments: {{RootPieceID: {0x01, 0x00, 0x00, 0x00, ...}, Pieces: {{...}}}, {RootPieceID: {0x01, 0x00, 0x00, 0x00, ...}, Pieces: {{...}}}},
}
Test: TestDeletePendingObject/Cockroach/with_segments
--- FAIL: TestDeletePendingObject/Cockroach/with_segments (0.68s)
```
Looks like we shouldn't have an assumption that all tests can be finished in 5 seconds, especially not in highly parallel environment.
These tests use `time.Now` at the beginning and compare the time saved in the database (usually filled by the database).
The difference shouldn't be higher than 20 seconds (before this commit: 5 seconds) which assumes that the records are saved in this timeframe...
Change-Id: Ia6f52897d13f88c6857c05d728bf8e72ab863c9b
2022-05-20 13:57:34 +01:00
|
|
|
DefaultTimeDiff(),
|
2022-03-22 12:03:02 +00:00
|
|
|
cmpopts.EquateEmpty())
|
2021-04-26 10:35:44 +01:00
|
|
|
require.Zero(t, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortObjects(objects []metabase.Object) {
|
|
|
|
sort.Slice(objects, func(i, j int) bool {
|
2022-02-18 00:14:50 +00:00
|
|
|
return objects[i].StreamID.Less(objects[j].StreamID)
|
2021-04-26 10:35:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-05 11:22:23 +01:00
|
|
|
func sortBucketTallies(tallies []metabase.BucketTally) {
|
|
|
|
sort.Slice(tallies, func(i, j int) bool {
|
|
|
|
if tallies[i].ProjectID == tallies[j].ProjectID {
|
|
|
|
return tallies[i].BucketName < tallies[j].BucketName
|
|
|
|
}
|
|
|
|
return tallies[i].ProjectID.Less(tallies[j].ProjectID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:35:44 +01:00
|
|
|
func sortRawObjects(objects []metabase.RawObject) {
|
|
|
|
sort.Slice(objects, func(i, j int) bool {
|
2022-02-18 00:14:50 +00:00
|
|
|
return objects[i].StreamID.Less(objects[j].StreamID)
|
2021-04-26 10:35:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortRawSegments(segments []metabase.RawSegment) {
|
|
|
|
sort.Slice(segments, func(i, j int) bool {
|
2022-02-18 00:14:50 +00:00
|
|
|
if segments[i].StreamID == segments[j].StreamID {
|
|
|
|
return segments[i].Position.Less(segments[j].Position)
|
|
|
|
}
|
|
|
|
return segments[i].StreamID.Less(segments[j].StreamID)
|
2021-04-26 10:35:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-17 23:26:03 +00:00
|
|
|
func sortRawCopies(copies []metabase.RawCopy) {
|
|
|
|
sort.Slice(copies, func(i, j int) bool {
|
2022-02-18 00:14:50 +00:00
|
|
|
return copies[i].StreamID.Less(copies[j].StreamID)
|
2022-02-17 23:26:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-07 10:36:02 +01:00
|
|
|
func sortDeletedSegments(segments []metabase.DeletedSegmentInfo) {
|
|
|
|
sort.Slice(segments, func(i, j int) bool {
|
|
|
|
return bytes.Compare(segments[i].RootPieceID[:], segments[j].RootPieceID[:]) < 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-01 10:50:46 +01:00
|
|
|
func checkError(t require.TestingT, err error, errClass *errs.Class, errText string) {
|
2021-04-26 10:35:44 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
satellite/metabase/test: extend default time diff for comparison
Tests are intermittently fail with similar error:
```
--- FAIL: TestDeletePendingObject/Cockroach (15.85s)
test.go:380:
Error Trace: test.go:380
delete_test.go:221
Error: Should be zero, but was metabase.DeleteObjectResult{
Objects: []metabase.Object{
{
ObjectStream: {ProjectID: {0x0f, 0x40, 0x70, 0x41, ...}, BucketName: "txxywyg4", ObjectKey: "\xbb+$\x17\x80\xc6\xcaC\xa3\xdb\xc3z*\xa8\xbe\xaf", Version: 1, ...},
- CreatedAt: s"2022-05-20 14:40:15.995376773 +0200 CEST",
+ CreatedAt: s"2022-05-20 14:40:21.04949 +0200 CEST",
ExpiresAt: nil,
Status: 1,
... // 9 identical fields
},
},
Segments: {{RootPieceID: {0x01, 0x00, 0x00, 0x00, ...}, Pieces: {{...}}}, {RootPieceID: {0x01, 0x00, 0x00, 0x00, ...}, Pieces: {{...}}}},
}
Test: TestDeletePendingObject/Cockroach/with_segments
--- FAIL: TestDeletePendingObject/Cockroach/with_segments (0.68s)
```
Looks like we shouldn't have an assumption that all tests can be finished in 5 seconds, especially not in highly parallel environment.
These tests use `time.Now` at the beginning and compare the time saved in the database (usually filled by the database).
The difference shouldn't be higher than 20 seconds (before this commit: 5 seconds) which assumes that the records are saved in this timeframe...
Change-Id: Ia6f52897d13f88c6857c05d728bf8e72ab863c9b
2022-05-20 13:57:34 +01:00
|
|
|
|
|
|
|
// DefaultTimeDiff is the central place to adjust test sql "timeout" (accepted diff between start and end of the test).
|
|
|
|
func DefaultTimeDiff() cmp.Option {
|
|
|
|
return cmpopts.EquateApproxTime(20 * time.Second)
|
|
|
|
}
|