2019-04-01 21:14:58 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-04-23 09:47:16 +01:00
|
|
|
package orders_test
|
2019-04-01 21:14:58 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/satellite"
|
2019-04-04 15:42:01 +01:00
|
|
|
"storj.io/storj/satellite/orders"
|
2019-04-01 21:14:58 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSerialNumbers(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
ordersDB := db.Orders()
|
2019-04-01 21:14:58 +01:00
|
|
|
|
|
|
|
expectedBucket := []byte("bucketID")
|
2019-04-09 20:12:58 +01:00
|
|
|
err := ordersDB.CreateSerialInfo(ctx, storj.SerialNumber{1}, expectedBucket, time.Now().UTC())
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
bucketID, err := ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedBucket, bucketID)
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
// try to use used serial number
|
|
|
|
_, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.Error(t, err)
|
2019-04-04 15:42:01 +01:00
|
|
|
require.True(t, orders.ErrUsingSerialNumber.Has(err))
|
2019-04-01 21:14:58 +01:00
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
err = ordersDB.UnuseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
bucketID, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedBucket, bucketID)
|
2019-04-04 15:42:01 +01:00
|
|
|
|
|
|
|
// not existing serial number
|
|
|
|
bucketID, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{99}, storj.NodeID{1})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, orders.ErrUsingSerialNumber.Has(err))
|
|
|
|
require.Empty(t, bucketID)
|
2019-08-27 18:12:38 +01:00
|
|
|
|
|
|
|
deleted, err := ordersDB.DeleteExpiredSerials(ctx, time.Now().UTC())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, deleted, 1)
|
|
|
|
|
|
|
|
// check serial number has been deleted from serial_numbers and used_serials
|
|
|
|
_, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
|
|
|
require.EqualError(t, err, "serial number: serial number not found")
|
2019-04-01 21:14:58 +01:00
|
|
|
})
|
|
|
|
}
|