2021-02-08 09:33:45 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase_test
|
|
|
|
|
|
|
|
import (
|
2023-10-16 12:20:51 +01:00
|
|
|
"strconv"
|
2021-02-08 09:33:45 +00:00
|
|
|
"testing"
|
2021-05-07 10:05:51 +01:00
|
|
|
"time"
|
2021-02-08 09:33:45 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/testcontext"
|
2023-10-16 12:20:51 +01:00
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/private/dbutil/pgutil/pgerrcode"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2021-04-26 10:35:44 +01:00
|
|
|
"storj.io/storj/satellite/metabase/metabasetest"
|
2021-02-08 09:33:45 +00:00
|
|
|
)
|
|
|
|
|
2021-05-07 10:05:51 +01:00
|
|
|
func TestNow(t *testing.T) {
|
|
|
|
metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
|
|
|
|
sysnow := time.Now()
|
|
|
|
now, err := db.Now(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.WithinDuration(t, sysnow, now, 5*time.Second)
|
|
|
|
})
|
|
|
|
}
|
2023-10-16 12:20:51 +01:00
|
|
|
|
|
|
|
func TestDisallowDoubleUnversioned(t *testing.T) {
|
|
|
|
metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
|
|
|
|
// This checks that TestingUniqueUnversioned=true indeed works as needed.
|
|
|
|
objStream := metabasetest.RandObjectStream()
|
|
|
|
obj := metabasetest.CreateObject(ctx, t, db, objStream, 0)
|
|
|
|
|
|
|
|
internaldb := db.UnderlyingTagSQL()
|
|
|
|
_, err := internaldb.Exec(ctx, `
|
|
|
|
INSERT INTO objects (
|
|
|
|
project_id, bucket_name, object_key, version, stream_id,
|
|
|
|
status
|
|
|
|
) VALUES (
|
|
|
|
$1, $2, $3, $4, $5,
|
|
|
|
`+strconv.Itoa(int(metabase.CommittedUnversioned))+`
|
|
|
|
)
|
|
|
|
`, obj.ProjectID, []byte(obj.BucketName), obj.ObjectKey, obj.Version+1, testrand.UUID(),
|
|
|
|
)
|
|
|
|
require.True(t, pgerrcode.IsConstraintViolation(err))
|
|
|
|
require.ErrorContains(t, err, "objects_one_unversioned_per_location")
|
|
|
|
|
|
|
|
_, err = internaldb.Exec(ctx, `
|
|
|
|
INSERT INTO objects (
|
|
|
|
project_id, bucket_name, object_key, version, stream_id,
|
|
|
|
status
|
|
|
|
) VALUES (
|
|
|
|
$1, $2, $3, $4, $5,
|
|
|
|
`+strconv.Itoa(int(metabase.DeleteMarkerUnversioned))+`
|
|
|
|
)
|
|
|
|
`, obj.ProjectID, []byte(obj.BucketName), obj.ObjectKey, obj.Version+1, testrand.UUID(),
|
|
|
|
)
|
|
|
|
require.True(t, pgerrcode.IsConstraintViolation(err))
|
|
|
|
require.ErrorContains(t, err, "objects_one_unversioned_per_location")
|
|
|
|
|
|
|
|
metabasetest.Verify{
|
|
|
|
Objects: []metabase.RawObject{
|
|
|
|
metabase.RawObject(obj),
|
|
|
|
},
|
|
|
|
}.Check(ctx, t, db)
|
|
|
|
})
|
|
|
|
}
|