2019-07-08 23:32:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
package buckets_test
|
2019-07-08 23:32:18 +01:00
|
|
|
|
|
|
|
import (
|
2022-10-05 11:53:02 +01:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2019-07-08 23:32:18 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/macaroon"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2021-11-12 20:47:41 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-07-08 23:32:18 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2022-10-05 11:53:02 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2019-07-08 23:32:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func newTestBucket(name string, projectID uuid.UUID) storj.Bucket {
|
|
|
|
return storj.Bucket{
|
2020-04-02 15:18:08 +01:00
|
|
|
ID: testrand.UUID(),
|
2019-07-08 23:32:18 +01:00
|
|
|
Name: name,
|
2020-04-02 15:18:08 +01:00
|
|
|
ProjectID: projectID,
|
2019-07-08 23:32:18 +01:00
|
|
|
PathCipher: storj.EncAESGCM,
|
|
|
|
DefaultSegmentsSize: 65536,
|
|
|
|
DefaultRedundancyScheme: storj.RedundancyScheme{
|
|
|
|
Algorithm: storj.ReedSolomon,
|
|
|
|
ShareSize: 9,
|
|
|
|
RequiredShares: 10,
|
|
|
|
RepairShares: 11,
|
|
|
|
OptimalShares: 12,
|
|
|
|
TotalShares: 13,
|
|
|
|
},
|
|
|
|
DefaultEncryptionParameters: storj.EncryptionParameters{
|
|
|
|
CipherSuite: storj.EncAESGCM,
|
2019-07-15 17:30:26 +01:00
|
|
|
BlockSize: 9 * 10,
|
2019-07-08 23:32:18 +01:00
|
|
|
},
|
2021-10-27 09:50:27 +01:00
|
|
|
Placement: storj.EU,
|
2019-07-08 23:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasicBucketOperations(t *testing.T) {
|
2021-11-12 20:47:41 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
db := sat.DB
|
2019-07-08 23:32:18 +01:00
|
|
|
consoleDB := db.Console()
|
2021-11-12 20:47:41 +00:00
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
project, err := consoleDB.Projects().Insert(ctx, &console.Project{Name: "testproject1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
bucketsDB := sat.API.Buckets.Service
|
2019-07-08 23:32:18 +01:00
|
|
|
expectedBucket := newTestBucket("testbucket", project.ID)
|
|
|
|
|
2020-06-30 22:49:29 +01:00
|
|
|
count, err := bucketsDB.CountBuckets(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
// CreateBucket
|
|
|
|
_, err = bucketsDB.CreateBucket(ctx, expectedBucket)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// GetBucket
|
|
|
|
bucket, err := bucketsDB.GetBucket(ctx, []byte("testbucket"), project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedBucket.ID, bucket.ID)
|
|
|
|
require.Equal(t, expectedBucket.Name, bucket.Name)
|
|
|
|
require.Equal(t, expectedBucket.ProjectID, bucket.ProjectID)
|
|
|
|
require.Equal(t, expectedBucket.PathCipher, bucket.PathCipher)
|
|
|
|
require.Equal(t, expectedBucket.DefaultSegmentsSize, bucket.DefaultSegmentsSize)
|
|
|
|
require.Equal(t, expectedBucket.DefaultRedundancyScheme, bucket.DefaultRedundancyScheme)
|
|
|
|
require.Equal(t, expectedBucket.DefaultEncryptionParameters, bucket.DefaultEncryptionParameters)
|
2021-10-27 09:50:27 +01:00
|
|
|
require.Equal(t, expectedBucket.Placement, bucket.Placement)
|
2019-07-08 23:32:18 +01:00
|
|
|
|
2021-10-18 09:54:59 +01:00
|
|
|
// GetMinimalBucket
|
|
|
|
minimalBucket, err := bucketsDB.GetMinimalBucket(ctx, []byte("testbucket"), project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("testbucket"), minimalBucket.Name)
|
|
|
|
require.False(t, minimalBucket.CreatedAt.IsZero())
|
|
|
|
|
|
|
|
_, err = bucketsDB.GetMinimalBucket(ctx, []byte("not-existing-bucket"), project.ID)
|
2021-10-27 09:50:27 +01:00
|
|
|
require.True(t, storj.ErrBucketNotFound.Has(err), err)
|
|
|
|
|
|
|
|
// GetBucketPlacement
|
|
|
|
placement, err := bucketsDB.GetBucketPlacement(ctx, []byte("testbucket"), project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedBucket.Placement, placement)
|
|
|
|
|
|
|
|
_, err = bucketsDB.GetBucketPlacement(ctx, []byte("not-existing-bucket"), project.ID)
|
|
|
|
require.True(t, storj.ErrBucketNotFound.Has(err), err)
|
2021-10-18 09:54:59 +01:00
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
// CountBuckets
|
2020-06-30 22:49:29 +01:00
|
|
|
count, err = bucketsDB.CountBuckets(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, count)
|
|
|
|
_, err = bucketsDB.CreateBucket(ctx, newTestBucket("testbucket2", project.ID))
|
|
|
|
require.NoError(t, err)
|
|
|
|
count, err = bucketsDB.CountBuckets(ctx, project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, count)
|
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
// DeleteBucket
|
|
|
|
err = bucketsDB.DeleteBucket(ctx, []byte("testbucket"), project.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListBucketsAllAllowed(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
cursor string
|
|
|
|
limit int
|
|
|
|
expectedItems int
|
|
|
|
expectedMore bool
|
|
|
|
}{
|
|
|
|
{"empty string cursor", "", 10, 10, false},
|
|
|
|
{"last bucket cursor", "zzz", 2, 1, false},
|
|
|
|
{"non matching cursor", "ccc", 10, 5, false},
|
|
|
|
{"first bucket cursor", "0test", 10, 10, false},
|
|
|
|
{"empty string cursor, more", "", 5, 5, true},
|
|
|
|
{"non matching cursor, more", "ccc", 3, 3, true},
|
|
|
|
{"first bucket cursor, more", "0test", 5, 5, true},
|
|
|
|
}
|
2021-11-12 20:47:41 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
db := sat.DB
|
2019-07-08 23:32:18 +01:00
|
|
|
consoleDB := db.Console()
|
2021-11-12 20:47:41 +00:00
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
project, err := consoleDB.Projects().Insert(ctx, &console.Project{Name: "testproject1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
bucketsDB := sat.API.Buckets.Service
|
2019-07-08 23:32:18 +01:00
|
|
|
|
2019-07-12 13:57:02 +01:00
|
|
|
allowedBuckets := macaroon.AllowedBuckets{
|
|
|
|
Buckets: map[string]struct{}{},
|
|
|
|
}
|
2019-07-08 23:32:18 +01:00
|
|
|
{ // setup some test buckets
|
|
|
|
var testBucketNames = []string{"aaa", "bbb", "mmm", "qqq", "zzz",
|
|
|
|
"test.bucket", "123", "0test", "999", "test-bucket.thing",
|
|
|
|
}
|
|
|
|
for _, bucket := range testBucketNames {
|
|
|
|
testBucket := newTestBucket(bucket, project.ID)
|
|
|
|
_, err := bucketsDB.CreateBucket(ctx, testBucket)
|
2019-07-12 13:57:02 +01:00
|
|
|
allowedBuckets.Buckets[bucket] = struct{}{}
|
2019-07-08 23:32:18 +01:00
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt // avoid scopelint error
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-07-12 13:57:02 +01:00
|
|
|
|
|
|
|
listOpts := storj.BucketListOptions{
|
2019-07-08 23:32:18 +01:00
|
|
|
Cursor: tt.cursor,
|
|
|
|
Direction: storj.Forward,
|
|
|
|
Limit: tt.limit,
|
2019-07-12 13:57:02 +01:00
|
|
|
}
|
|
|
|
bucketList, err := bucketsDB.ListBuckets(ctx, project.ID, listOpts, allowedBuckets)
|
2019-07-08 23:32:18 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.expectedItems, len(bucketList.Items))
|
|
|
|
require.Equal(t, tt.expectedMore, bucketList.More)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListBucketsNotAllowed(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2019-07-12 13:57:02 +01:00
|
|
|
name string
|
|
|
|
cursor string
|
|
|
|
limit int
|
|
|
|
expectedItems int
|
|
|
|
expectedMore bool
|
|
|
|
allowAll bool
|
|
|
|
allowedBuckets map[string]struct{}
|
|
|
|
expectedNames []string
|
2019-07-08 23:32:18 +01:00
|
|
|
}{
|
2019-07-12 13:57:02 +01:00
|
|
|
{"empty string cursor, 2 allowed", "", 10, 1, false, false, map[string]struct{}{"aaa": {}, "ddd": {}}, []string{"aaa"}},
|
|
|
|
{"empty string cursor, more", "", 2, 2, true, false, map[string]struct{}{"aaa": {}, "bbb": {}, "zzz": {}}, []string{"aaa", "bbb"}},
|
|
|
|
{"empty string cursor, 3 allowed", "", 4, 3, false, false, map[string]struct{}{"aaa": {}, "bbb": {}, "zzz": {}}, []string{"aaa", "bbb", "zzz"}},
|
|
|
|
{"last bucket cursor", "zzz", 2, 1, false, false, map[string]struct{}{"zzz": {}}, []string{"zzz"}},
|
|
|
|
{"last bucket cursor, allow all", "zzz", 2, 1, false, true, map[string]struct{}{"zzz": {}}, []string{"zzz"}},
|
|
|
|
{"empty string cursor, allow all, more", "", 5, 5, true, true, map[string]struct{}{"": {}}, []string{"123", "0test", "999", "aaa", "bbb"}},
|
2019-07-08 23:32:18 +01:00
|
|
|
}
|
2021-11-12 20:47:41 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
db := sat.DB
|
2019-07-08 23:32:18 +01:00
|
|
|
consoleDB := db.Console()
|
2021-11-12 20:47:41 +00:00
|
|
|
|
2019-07-08 23:32:18 +01:00
|
|
|
project, err := consoleDB.Projects().Insert(ctx, &console.Project{Name: "testproject1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-12 20:47:41 +00:00
|
|
|
bucketsDB := sat.API.Buckets.Service
|
2019-07-08 23:32:18 +01:00
|
|
|
|
|
|
|
{ // setup some test buckets
|
|
|
|
var testBucketNames = []string{"aaa", "bbb", "mmm", "qqq", "zzz",
|
|
|
|
"test.bucket", "123", "0test", "999", "test-bucket.thing",
|
|
|
|
}
|
|
|
|
for _, bucket := range testBucketNames {
|
|
|
|
testBucket := newTestBucket(bucket, project.ID)
|
|
|
|
_, err := bucketsDB.CreateBucket(ctx, testBucket)
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testCases {
|
|
|
|
tt := tt // avoid scopelint error
|
|
|
|
listOpts := storj.BucketListOptions{
|
|
|
|
Cursor: tt.cursor,
|
|
|
|
Direction: storj.Forward,
|
|
|
|
Limit: tt.limit,
|
|
|
|
}
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-07-12 13:57:02 +01:00
|
|
|
allowed := macaroon.AllowedBuckets{
|
|
|
|
Buckets: tt.allowedBuckets,
|
|
|
|
All: tt.allowAll,
|
|
|
|
}
|
|
|
|
bucketList, err := bucketsDB.ListBuckets(ctx,
|
|
|
|
project.ID,
|
2019-07-08 23:32:18 +01:00
|
|
|
listOpts,
|
2019-07-12 13:57:02 +01:00
|
|
|
allowed,
|
2019-07-08 23:32:18 +01:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.expectedItems, len(bucketList.Items))
|
|
|
|
require.Equal(t, tt.expectedMore, bucketList.More)
|
|
|
|
for _, actualItem := range bucketList.Items {
|
|
|
|
require.Contains(t, tt.expectedNames, actualItem.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-10-05 11:53:02 +01:00
|
|
|
|
|
|
|
func TestBatchBuckets(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{SatelliteCount: 1}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
sat := planet.Satellites[0]
|
|
|
|
db := sat.DB
|
|
|
|
consoleDB := db.Console()
|
|
|
|
|
|
|
|
var testBucketNames = []string{"aaa", "bbb", "mmm", "qqq", "zzz",
|
|
|
|
"test.bucket", "123", "0test", "999", "test-bucket.thing",
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketsService := sat.API.Buckets.Service
|
|
|
|
var expectedBucketLocations []metabase.BucketLocation
|
|
|
|
|
|
|
|
for i := 1; i < 4; i++ {
|
|
|
|
project, err := consoleDB.Projects().Insert(ctx, &console.Project{Name: "testproject" + strconv.Itoa(i)})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, bucket := range testBucketNames {
|
|
|
|
testBucket := newTestBucket(bucket, project.ID)
|
|
|
|
_, err := bucketsService.CreateBucket(ctx, testBucket)
|
|
|
|
require.NoError(t, err)
|
|
|
|
expectedBucketLocations = append(expectedBucketLocations, metabase.BucketLocation{
|
|
|
|
ProjectID: project.ID,
|
|
|
|
BucketName: bucket,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortBucketLocations(expectedBucketLocations)
|
|
|
|
|
|
|
|
testLimits := []int{1, 3, 30, 1000}
|
|
|
|
|
|
|
|
for _, testLimit := range testLimits {
|
|
|
|
err := db.Buckets().IterateBucketLocations(ctx, uuid.UUID{}, "", testLimit, func(bucketLocations []metabase.BucketLocation) (err error) {
|
|
|
|
if testLimit > len(expectedBucketLocations) {
|
|
|
|
testLimit = len(expectedBucketLocations)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := expectedBucketLocations[:testLimit]
|
|
|
|
require.Equal(t, expectedResult, bucketLocations)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortBucketLocations(locations []metabase.BucketLocation) {
|
|
|
|
sort.Slice(locations, func(i, j int) bool {
|
|
|
|
if locations[i].ProjectID == locations[j].ProjectID {
|
|
|
|
return locations[i].BucketName < locations[j].BucketName
|
|
|
|
}
|
|
|
|
return locations[i].ProjectID.Less(locations[j].ProjectID)
|
|
|
|
})
|
|
|
|
}
|