2019-02-19 23:47:19 +00:00
|
|
|
package kv_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb"
|
2019-09-30 16:48:19 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
2019-02-19 23:47:19 +00:00
|
|
|
"github.com/influxdata/influxdb/kv"
|
|
|
|
"github.com/influxdata/influxdb/mock"
|
|
|
|
influxdbtesting "github.com/influxdata/influxdb/testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-09-30 16:48:19 +00:00
|
|
|
existingBucketID = platform.ID(mock.FirstMockID + 3)
|
|
|
|
firstMockID = platform.ID(mock.FirstMockID)
|
|
|
|
nonexistantID = platform.ID(10001)
|
2019-02-19 23:47:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StoreFn func() (kv.Store, func(), error)
|
|
|
|
|
|
|
|
func TestLookupService_Name_WithBolt(t *testing.T) {
|
|
|
|
testLookupName(NewTestBoltStore, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupService_Name_WithInMem(t *testing.T) {
|
|
|
|
testLookupName(NewTestInmemStore, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLookupName(newStore StoreFn, t *testing.T) {
|
|
|
|
type initFn func(context.Context, *kv.Service) error
|
|
|
|
type args struct {
|
|
|
|
resource influxdb.Resource
|
|
|
|
init initFn
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "error if id is invalid",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.DashboardsResourceType,
|
|
|
|
ID: influxdbtesting.IDPtr(influxdb.InvalidID()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error if resource is invalid",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.ResourceType("invalid"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "authorization resource without a name returns empty string",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.AuthorizationsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "task resource without a name returns empty string",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.TasksResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket with existing id returns name",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.BucketsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &existingBucketID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
init: func(ctx context.Context, s *kv.Service) error {
|
2019-09-07 23:00:26 +00:00
|
|
|
o1 := &influxdb.Organization{
|
2019-02-19 23:47:19 +00:00
|
|
|
Name: "o1",
|
2019-09-07 23:00:26 +00:00
|
|
|
}
|
|
|
|
_ = s.CreateOrganization(ctx, o1)
|
|
|
|
t.Log(o1)
|
2019-02-19 23:47:19 +00:00
|
|
|
return s.CreateBucket(ctx, &influxdb.Bucket{
|
2019-04-10 21:16:35 +00:00
|
|
|
Name: "b1",
|
2019-09-30 16:48:19 +00:00
|
|
|
OrgID: o1.ID,
|
2019-02-19 23:47:19 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "b1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket with non-existent id returns error",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.BucketsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &nonexistantID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dashboard with existing id returns name",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.DashboardsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
init: func(ctx context.Context, s *kv.Service) error {
|
|
|
|
return s.CreateDashboard(ctx, &influxdb.Dashboard{
|
|
|
|
Name: "dashboard1",
|
|
|
|
OrganizationID: 1,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "dashboard1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dashboard with non-existent id returns error",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.DashboardsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &nonexistantID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org with existing id returns name",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.OrgsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
init: func(ctx context.Context, s *kv.Service) error {
|
|
|
|
return s.CreateOrganization(ctx, &influxdb.Organization{
|
|
|
|
Name: "org1",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "org1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "org with non-existent id returns error",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.OrgsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &nonexistantID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "source with existing id returns name",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.SourcesResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
init: func(ctx context.Context, s *kv.Service) error {
|
|
|
|
return s.CreateSource(ctx, &influxdb.Source{
|
|
|
|
Name: "source1",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "source1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "source with non-existent id returns error",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.SourcesResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &nonexistantID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "telegraf with existing id returns name",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.TelegrafsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
init: func(ctx context.Context, s *kv.Service) error {
|
|
|
|
return s.CreateTelegrafConfig(ctx, &influxdb.TelegrafConfig{
|
2019-04-15 19:25:48 +00:00
|
|
|
OrgID: influxdbtesting.MustIDBase16("0000000000000009"),
|
|
|
|
Name: "telegraf1",
|
2019-09-30 16:48:19 +00:00
|
|
|
}, existingBucketID)
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "telegraf1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "telegraf with non-existent id returns error",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.TelegrafsResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &nonexistantID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user with existing id returns name",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.UsersResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &firstMockID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
init: func(ctx context.Context, s *kv.Service) error {
|
|
|
|
return s.CreateUser(ctx, &influxdb.User{
|
|
|
|
Name: "user1",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "user1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user with non-existent id returns error",
|
|
|
|
args: args{
|
|
|
|
resource: influxdb.Resource{
|
|
|
|
Type: influxdb.UsersResourceType,
|
2019-09-30 16:48:19 +00:00
|
|
|
ID: &nonexistantID,
|
2019-02-19 23:47:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
store, done, err := newStore()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create bolt test client: %v", err)
|
|
|
|
}
|
|
|
|
svc := kv.NewService(store)
|
|
|
|
defer done()
|
|
|
|
|
2019-09-30 16:48:19 +00:00
|
|
|
svc.IDGenerator = mock.NewMockIDGenerator()
|
|
|
|
svc.OrgBucketIDs = mock.NewMockIDGenerator()
|
2019-09-07 23:00:26 +00:00
|
|
|
svc.WithSpecialOrgBucketIDs(svc.IDGenerator)
|
2019-02-19 23:47:19 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
if tt.args.init != nil {
|
|
|
|
if err := tt.args.init(ctx, svc); err != nil {
|
|
|
|
t.Errorf("Service.Name() unable to initialize service: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id := influxdb.InvalidID()
|
|
|
|
if tt.args.resource.ID != nil {
|
|
|
|
id = *tt.args.resource.ID
|
|
|
|
}
|
|
|
|
got, err := svc.Name(ctx, tt.args.resource.Type, id)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Service.Name() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("Service.Name() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|