2019-01-09 13:35:25 +00:00
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2021-03-11 19:51:03 +00:00
|
|
|
"time"
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
"github.com/dustin/go-humanize"
|
2020-07-30 01:08:04 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/inmem"
|
2021-09-13 19:12:35 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
2020-07-01 11:08:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kv/migration/all"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/storage"
|
2020-07-30 01:08:04 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/storage/mocks"
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/tenant"
|
2021-03-11 19:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
2019-12-28 00:58:57 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-01-09 13:35:25 +00:00
|
|
|
)
|
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
func TestBucketService_CreateBucket(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
rp time.Duration
|
|
|
|
inSg time.Duration
|
|
|
|
outSg time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "infinite RP, derived SGD",
|
|
|
|
rp: influxdb.InfiniteRetention,
|
|
|
|
inSg: 0,
|
|
|
|
outSg: humanize.Week,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "infinite RP, pinned SGD",
|
|
|
|
rp: influxdb.InfiniteRetention,
|
|
|
|
inSg: time.Hour,
|
|
|
|
outSg: time.Hour,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "large RP, derived SGD",
|
|
|
|
rp: humanize.Week,
|
|
|
|
inSg: 0,
|
|
|
|
outSg: humanize.Day,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "large RP, pinned SGD",
|
|
|
|
rp: humanize.Week,
|
|
|
|
inSg: 5 * time.Hour,
|
|
|
|
outSg: 5 * time.Hour,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "small RP, derived SGD",
|
|
|
|
rp: humanize.Day,
|
|
|
|
inSg: 0,
|
|
|
|
outSg: time.Hour,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "small RP, pinned SGD",
|
|
|
|
rp: humanize.Day,
|
|
|
|
inSg: 5 * time.Hour,
|
|
|
|
outSg: 5 * time.Hour,
|
|
|
|
},
|
|
|
|
}
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
engine := mocks.NewMockEngineSchema(ctrl)
|
|
|
|
logger := zaptest.NewLogger(t)
|
|
|
|
inmemService := newTenantService(t, logger)
|
|
|
|
service := storage.NewBucketService(logger, inmemService, engine)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
org := &influxdb.Organization{Name: "org1"}
|
|
|
|
require.NoError(t, inmemService.CreateOrganization(ctx, org))
|
|
|
|
|
|
|
|
bucket := &influxdb.Bucket{OrgID: org.ID, RetentionPeriod: tc.rp, ShardGroupDuration: tc.inSg}
|
|
|
|
|
|
|
|
// Test creating a bucket calls into the creator.
|
|
|
|
engine.EXPECT().CreateBucket(gomock.Any(), bucket)
|
|
|
|
require.NoError(t, service.CreateBucket(ctx, bucket))
|
|
|
|
|
|
|
|
// Test that a shard-group duration was created for the bucket
|
|
|
|
require.Equal(t, tc.outSg, bucket.ShardGroupDuration)
|
|
|
|
|
|
|
|
// Test that the shard-group duration was recorded in the KV store.
|
|
|
|
kvBucket, err := inmemService.FindBucketByID(ctx, bucket.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.outSg, kvBucket.ShardGroupDuration)
|
|
|
|
})
|
2019-01-09 13:35:25 +00:00
|
|
|
}
|
2021-03-11 19:51:03 +00:00
|
|
|
}
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
func TestBucketService_DeleteBucket(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
engine := mocks.NewMockEngineSchema(ctrl)
|
2020-11-02 22:51:52 +00:00
|
|
|
logger := zaptest.NewLogger(t)
|
2021-03-11 19:51:03 +00:00
|
|
|
inmemService := newTenantService(t, logger)
|
2020-11-02 22:51:52 +00:00
|
|
|
service := storage.NewBucketService(logger, inmemService, engine)
|
2021-03-11 19:51:03 +00:00
|
|
|
ctx := context.Background()
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
org := &influxdb.Organization{Name: "org1"}
|
2021-03-11 19:51:03 +00:00
|
|
|
require.NoError(t, inmemService.CreateOrganization(ctx, org))
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
bucket := &influxdb.Bucket{OrgID: org.ID}
|
2021-03-11 19:51:03 +00:00
|
|
|
require.NoError(t, inmemService.CreateBucket(ctx, bucket))
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
// Test deleting a bucket calls into the deleter.
|
2020-07-30 01:08:04 +00:00
|
|
|
engine.EXPECT().DeleteBucket(gomock.Any(), org.ID, bucket.ID)
|
2021-03-11 19:51:03 +00:00
|
|
|
require.NoError(t, service.DeleteBucket(ctx, bucket.ID))
|
|
|
|
}
|
2020-07-30 01:08:04 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
func TestBucketService_DeleteNonexistentBucket(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2019-01-09 13:35:25 +00:00
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
i, err := platform.IDFromString("2222222222222222")
|
2021-03-11 19:51:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
engine := mocks.NewMockEngineSchema(ctrl)
|
|
|
|
logger := zaptest.NewLogger(t)
|
|
|
|
inmemService := newTenantService(t, logger)
|
|
|
|
service := storage.NewBucketService(logger, inmemService, engine)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
require.Error(t, service.DeleteBucket(ctx, *i))
|
2019-01-09 13:35:25 +00:00
|
|
|
}
|
2019-12-28 00:58:57 +00:00
|
|
|
|
2021-03-11 19:51:03 +00:00
|
|
|
func newTenantService(t *testing.T, logger *zap.Logger) *tenant.Service {
|
2019-12-28 00:58:57 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2020-07-01 11:08:20 +00:00
|
|
|
store := inmem.NewKVStore()
|
|
|
|
if err := all.Up(context.Background(), logger, store); err != nil {
|
2019-12-28 00:58:57 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-01 11:08:20 +00:00
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
return tenant.NewService(tenant.NewStore(store))
|
2019-12-28 00:58:57 +00:00
|
|
|
}
|