2019-01-09 13:35:25 +00:00
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/inmem"
|
2019-12-28 00:58:57 +00:00
|
|
|
"github.com/influxdata/influxdb/kv"
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/storage"
|
2019-12-28 00:58:57 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-01-09 13:35:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBucketService(t *testing.T) {
|
|
|
|
service := storage.NewBucketService(nil, nil)
|
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
i, err := influxdb.IDFromString("2222222222222222")
|
2019-01-09 13:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := service.DeleteBucket(context.TODO(), *i); err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
inmemService := newInMemKVSVC(t)
|
2019-04-09 18:24:40 +00:00
|
|
|
service = storage.NewBucketService(inmemService, nil)
|
2019-01-09 13:35:25 +00:00
|
|
|
|
|
|
|
if err := service.DeleteBucket(context.TODO(), *i); err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
org := &influxdb.Organization{Name: "org1"}
|
2019-04-09 18:24:40 +00:00
|
|
|
if err := inmemService.CreateOrganization(context.TODO(), org); err != nil {
|
2019-01-09 13:35:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
bucket := &influxdb.Bucket{OrgID: org.ID}
|
2019-04-09 18:24:40 +00:00
|
|
|
if err := inmemService.CreateBucket(context.TODO(), bucket); err != nil {
|
2019-01-09 13:35:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test deleting a bucket calls into the deleter.
|
|
|
|
deleter := &MockDeleter{}
|
2019-04-09 18:24:40 +00:00
|
|
|
service = storage.NewBucketService(inmemService, deleter)
|
2019-01-09 13:35:25 +00:00
|
|
|
|
|
|
|
if err := service.DeleteBucket(context.TODO(), bucket.ID); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleter.orgID != org.ID {
|
|
|
|
t.Errorf("got org ID: %s, expected %s", deleter.orgID, org.ID)
|
|
|
|
} else if deleter.bucketID != bucket.ID {
|
|
|
|
t.Errorf("got bucket ID: %s, expected %s", deleter.bucketID, bucket.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockDeleter struct {
|
2020-03-12 18:32:52 +00:00
|
|
|
orgID, bucketID influxdb.ID
|
2019-01-09 13:35:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 18:32:52 +00:00
|
|
|
func (m *MockDeleter) DeleteBucket(_ context.Context, orgID, bucketID influxdb.ID) error {
|
2019-01-09 13:35:25 +00:00
|
|
|
m.orgID, m.bucketID = orgID, bucketID
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-28 00:58:57 +00:00
|
|
|
|
|
|
|
func newInMemKVSVC(t *testing.T) *kv.Service {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
svc := kv.NewService(zaptest.NewLogger(t), inmem.NewKVStore())
|
|
|
|
if err := svc.Initialize(context.Background()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return svc
|
|
|
|
}
|