2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pieces_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-04-08 17:46:38 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-03-18 10:55:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-04-08 19:15:19 +01:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-07-19 18:40:27 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/pkg/signing"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/storagenode"
|
2019-07-19 18:40:27 +01:00
|
|
|
"storj.io/storj/storagenode/orders"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
)
|
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
func TestV0PieceInfo(t *testing.T) {
|
2019-03-18 10:55:06 +00:00
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceinfos := db.V0PieceInfo().(pieces.V0PieceInfoDBForTest)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
satellite0 := testidentity.MustPregeneratedSignedIdentity(0, storj.LatestIDVersion())
|
|
|
|
satellite1 := testidentity.MustPregeneratedSignedIdentity(1, storj.LatestIDVersion())
|
|
|
|
satellite2 := testidentity.MustPregeneratedSignedIdentity(2, storj.LatestIDVersion())
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
uplink0 := testidentity.MustPregeneratedSignedIdentity(3, storj.LatestIDVersion())
|
|
|
|
uplink1 := testidentity.MustPregeneratedSignedIdentity(4, storj.LatestIDVersion())
|
|
|
|
uplink2 := testidentity.MustPregeneratedSignedIdentity(5, storj.LatestIDVersion())
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
pieceid0 := storj.NewPieceID()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2019-06-05 14:47:01 +01:00
|
|
|
piecehash0, err := signing.SignPieceHash(ctx,
|
2019-03-18 10:55:06 +00:00
|
|
|
signing.SignerFromFullIdentity(uplink0),
|
|
|
|
&pb.PieceHash{
|
|
|
|
PieceId: pieceid0,
|
|
|
|
Hash: []byte{1, 2, 3, 4, 5},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info0 := &pieces.Info{
|
|
|
|
SatelliteID: satellite0.ID,
|
|
|
|
|
|
|
|
PieceID: pieceid0,
|
|
|
|
PieceSize: 123,
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceCreation: now,
|
|
|
|
PieceExpiration: now,
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
OrderLimit: &pb.OrderLimit{},
|
2019-03-18 10:55:06 +00:00
|
|
|
UplinkPieceHash: piecehash0,
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:47:01 +01:00
|
|
|
piecehash1, err := signing.SignPieceHash(ctx,
|
2019-03-18 10:55:06 +00:00
|
|
|
signing.SignerFromFullIdentity(uplink1),
|
|
|
|
&pb.PieceHash{
|
|
|
|
PieceId: pieceid0,
|
|
|
|
Hash: []byte{1, 2, 3, 4, 5},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info1 := &pieces.Info{
|
|
|
|
SatelliteID: satellite1.ID,
|
|
|
|
|
|
|
|
PieceID: pieceid0,
|
|
|
|
PieceSize: 123,
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceCreation: now,
|
|
|
|
PieceExpiration: now,
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
OrderLimit: &pb.OrderLimit{},
|
2019-03-18 10:55:06 +00:00
|
|
|
UplinkPieceHash: piecehash1,
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:47:01 +01:00
|
|
|
piecehash2, err := signing.SignPieceHash(ctx,
|
2019-04-08 17:46:38 +01:00
|
|
|
signing.SignerFromFullIdentity(uplink2),
|
|
|
|
&pb.PieceHash{
|
|
|
|
PieceId: pieceid0,
|
|
|
|
Hash: []byte{1, 2, 3, 4, 5},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-10 15:48:00 +01:00
|
|
|
// use different timezones
|
|
|
|
location := time.FixedZone("XYZ", int((8 * time.Hour).Seconds()))
|
|
|
|
now2 := now.In(location)
|
|
|
|
|
2019-04-08 17:46:38 +01:00
|
|
|
info2 := &pieces.Info{
|
|
|
|
SatelliteID: satellite2.ID,
|
|
|
|
|
|
|
|
PieceID: pieceid0,
|
|
|
|
PieceSize: 123,
|
2019-07-10 15:48:00 +01:00
|
|
|
PieceCreation: now2,
|
|
|
|
PieceExpiration: now2,
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
OrderLimit: &pb.OrderLimit{},
|
2019-04-08 17:46:38 +01:00
|
|
|
UplinkPieceHash: piecehash2,
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
_, err = pieceinfos.Get(ctx, info0.SatelliteID, info0.PieceID)
|
|
|
|
require.Error(t, err, "getting element that doesn't exist")
|
|
|
|
|
|
|
|
// adding stuff
|
|
|
|
err = pieceinfos.Add(ctx, info0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = pieceinfos.Add(ctx, info1)
|
|
|
|
require.NoError(t, err, "adding different satellite, but same pieceid")
|
|
|
|
|
2019-04-08 17:46:38 +01:00
|
|
|
err = pieceinfos.Add(ctx, info2)
|
|
|
|
require.NoError(t, err, "adding different satellite, but same pieceid")
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
err = pieceinfos.Add(ctx, info0)
|
|
|
|
require.Error(t, err, "adding duplicate")
|
|
|
|
|
|
|
|
// getting the added information
|
|
|
|
info0loaded, err := pieceinfos.Get(ctx, info0.SatelliteID, info0.PieceID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, cmp.Diff(info0, info0loaded, cmp.Comparer(pb.Equal)))
|
|
|
|
|
|
|
|
info1loaded, err := pieceinfos.Get(ctx, info1.SatelliteID, info1.PieceID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, cmp.Diff(info1, info1loaded, cmp.Comparer(pb.Equal)))
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
// getting no expired pieces
|
|
|
|
expired, err := pieceinfos.GetExpired(ctx, now.Add(-10*time.Hour), 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, expired, 0)
|
|
|
|
|
2019-04-08 17:46:38 +01:00
|
|
|
// getting expired pieces
|
2019-05-08 12:11:59 +01:00
|
|
|
exp := now.Add(8 * 24 * time.Hour)
|
|
|
|
expired, err = pieceinfos.GetExpired(ctx, exp, 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, expired, 3)
|
|
|
|
|
|
|
|
// mark info0 deletion as a failure
|
|
|
|
err = pieceinfos.DeleteFailed(ctx, info0.SatelliteID, info0.PieceID, exp)
|
2019-04-08 17:46:38 +01:00
|
|
|
assert.NoError(t, err)
|
2019-05-08 12:11:59 +01:00
|
|
|
|
|
|
|
// this shouldn't return info0
|
|
|
|
expired, err = pieceinfos.GetExpired(ctx, exp, 10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, expired, 2)
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// deleting
|
|
|
|
err = pieceinfos.Delete(ctx, info0.SatelliteID, info0.PieceID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pieceinfos.Delete(ctx, info1.SatelliteID, info1.PieceID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-08 17:46:38 +01:00
|
|
|
// deleting expired pieces
|
2019-05-08 12:11:59 +01:00
|
|
|
err = pieceinfos.Delete(ctx, info2.SatelliteID, info2.PieceID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// duplicate deletion
|
|
|
|
err = pieceinfos.Delete(ctx, info2.SatelliteID, info2.PieceID)
|
2019-04-08 17:46:38 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// getting after delete
|
|
|
|
_, err = pieceinfos.Get(ctx, info0.SatelliteID, info0.PieceID)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, err = pieceinfos.Get(ctx, info1.SatelliteID, info1.PieceID)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2019-07-19 18:40:27 +01:00
|
|
|
|
|
|
|
func TestPieceInfo_Trivial(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
satelliteID, serial := testrand.NodeID(), testrand.SerialNumber()
|
|
|
|
|
|
|
|
{ // Ensure Enqueue works at all
|
|
|
|
err := db.Orders().Enqueue(ctx, &orders.Info{
|
|
|
|
Order: &pb.Order{},
|
|
|
|
Limit: &pb.OrderLimit{
|
|
|
|
SatelliteId: satelliteID,
|
|
|
|
SerialNumber: serial,
|
|
|
|
OrderExpiration: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Ensure ListUnsent works at all
|
|
|
|
_, err := db.Orders().ListUnsent(ctx, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Ensure ListUnsentBySatellite works at all
|
|
|
|
_, err := db.Orders().ListUnsentBySatellite(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Ensure Archive works at all
|
2019-07-31 17:40:08 +01:00
|
|
|
err := db.Orders().Archive(ctx, orders.ArchiveRequest{
|
|
|
|
Satellite: satelliteID,
|
|
|
|
Serial: serial,
|
|
|
|
Status: orders.StatusAccepted,
|
|
|
|
})
|
2019-07-19 18:40:27 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Ensure ListArchived works at all
|
|
|
|
_, err := db.Orders().ListArchived(ctx, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|