2020-03-17 19:23:00 +00:00
|
|
|
package tenant_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
|
|
|
"github.com/influxdata/influxdb/v2/tenant"
|
|
|
|
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
|
2020-03-17 19:23:00 +00:00
|
|
|
)
|
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
func TestInmemBucketService(t *testing.T) {
|
|
|
|
influxdbtesting.BucketService(initInmemBucketService, t)
|
2020-03-17 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
func initInmemBucketService(f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
|
|
|
return initBucketService(s, f, t)
|
2020-03-17 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 11:08:20 +00:00
|
|
|
func initBucketService(s kv.SchemaStore, f influxdbtesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
|
|
|
|
storage := tenant.NewStore(s)
|
2020-08-11 14:56:42 +00:00
|
|
|
if f.IDGenerator != nil {
|
|
|
|
storage.IDGen = f.IDGenerator
|
|
|
|
}
|
2020-03-17 19:23:00 +00:00
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
if f.OrgIDs != nil {
|
|
|
|
storage.OrgIDGen = f.OrgIDs
|
2020-03-17 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
if f.BucketIDs != nil {
|
|
|
|
storage.BucketIDGen = f.BucketIDs
|
2020-03-17 19:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
// go direct to storage for test data
|
|
|
|
if err := s.Update(context.Background(), func(tx kv.Tx) error {
|
2020-03-17 19:23:00 +00:00
|
|
|
for _, o := range f.Organizations {
|
2020-08-11 14:56:42 +00:00
|
|
|
if err := storage.CreateOrg(tx.Context(), tx, o); err != nil {
|
|
|
|
return err
|
2020-03-17 19:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 14:56:42 +00:00
|
|
|
|
|
|
|
for _, b := range f.Buckets {
|
|
|
|
if err := storage.CreateBucket(tx.Context(), tx, b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failed to populate organizations: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tenant.NewService(storage), "tenant/", func() {
|
|
|
|
if err := s.Update(context.Background(), func(tx kv.Tx) error {
|
|
|
|
for _, b := range f.Buckets {
|
|
|
|
if err := storage.DeleteBucket(tx.Context(), tx, b.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range f.Organizations {
|
|
|
|
if err := storage.DeleteOrg(tx.Context(), tx, o.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Logf("failed to cleanup organizations: %s", err)
|
|
|
|
}
|
2020-03-17 19:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 15:30:14 +00:00
|
|
|
|
|
|
|
func TestBucketFind(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-05-22 15:30:14 +00:00
|
|
|
|
2020-07-01 11:08:20 +00:00
|
|
|
storage := tenant.NewStore(s)
|
2020-05-22 15:30:14 +00:00
|
|
|
svc := tenant.NewService(storage)
|
|
|
|
o := &influxdb.Organization{
|
|
|
|
Name: "theorg",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := svc.CreateOrganization(context.Background(), o); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
name := "thebucket"
|
2021-08-31 20:43:45 +00:00
|
|
|
_, _, err := svc.FindBuckets(context.Background(), influxdb.BucketFilter{
|
2020-05-22 15:30:14 +00:00
|
|
|
Name: &name,
|
|
|
|
Org: &o.Name,
|
|
|
|
})
|
|
|
|
if err.Error() != `bucket "thebucket" not found` {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 21:36:01 +00:00
|
|
|
|
|
|
|
func TestSystemBucketsInNameFind(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-05-26 21:36:01 +00:00
|
|
|
|
2020-07-01 11:08:20 +00:00
|
|
|
storage := tenant.NewStore(s)
|
2020-05-26 21:36:01 +00:00
|
|
|
svc := tenant.NewService(storage)
|
|
|
|
o := &influxdb.Organization{
|
|
|
|
Name: "theorg",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := svc.CreateOrganization(context.Background(), o); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
b := &influxdb.Bucket{
|
|
|
|
OrgID: o.ID,
|
|
|
|
Name: "thebucket",
|
|
|
|
}
|
|
|
|
if err := svc.CreateBucket(context.Background(), b); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
name := "thebucket"
|
|
|
|
buckets, _, _ := svc.FindBuckets(context.Background(), influxdb.BucketFilter{
|
|
|
|
Name: &name,
|
|
|
|
Org: &o.Name,
|
|
|
|
})
|
|
|
|
if len(buckets) != 1 {
|
|
|
|
t.Fatal("failed to return a single bucket when doing a bucket lookup by name")
|
|
|
|
}
|
|
|
|
}
|