2017-10-30 18:31:19 +00:00
|
|
|
package organizations_test
|
2017-10-27 22:14:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-10-30 21:03:53 +00:00
|
|
|
"fmt"
|
2017-10-27 22:14:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
"github.com/influxdata/chronograf"
|
2017-10-30 21:03:53 +00:00
|
|
|
"github.com/influxdata/chronograf/mocks"
|
2017-10-30 18:31:19 +00:00
|
|
|
"github.com/influxdata/chronograf/organizations"
|
2017-10-27 22:14:46 +00:00
|
|
|
)
|
|
|
|
|
2017-11-01 16:30:42 +00:00
|
|
|
// IgnoreFields is used because ID cannot be predicted reliably
|
2017-10-27 22:14:46 +00:00
|
|
|
// EquateEmpty is used because we want nil slices, arrays, and maps to be equal to the empty map
|
|
|
|
var dashboardCmpOptions = cmp.Options{
|
|
|
|
cmpopts.EquateEmpty(),
|
2017-10-30 14:34:18 +00:00
|
|
|
cmpopts.IgnoreFields(chronograf.Dashboard{}, "ID"),
|
2017-10-27 22:14:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 18:31:19 +00:00
|
|
|
func TestDashboards_All(t *testing.T) {
|
2017-10-30 21:03:53 +00:00
|
|
|
type fields struct {
|
|
|
|
DashboardsStore chronograf.DashboardsStore
|
|
|
|
}
|
2017-10-27 22:14:46 +00:00
|
|
|
type args struct {
|
|
|
|
organization string
|
2017-10-30 14:34:18 +00:00
|
|
|
ctx context.Context
|
2017-10-27 22:14:46 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2017-10-30 21:03:53 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
fields fields
|
|
|
|
want []chronograf.Dashboard
|
|
|
|
wantRaw []chronograf.Dashboard
|
|
|
|
wantErr bool
|
2017-10-27 22:14:46 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-10-30 21:03:53 +00:00
|
|
|
name: "No Dashboards",
|
|
|
|
fields: fields{
|
|
|
|
DashboardsStore: &mocks.DashboardsStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Dashboard, error) {
|
|
|
|
return nil, fmt.Errorf("No Dashboards")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
wantErr: true,
|
2017-10-27 22:14:46 +00:00
|
|
|
},
|
|
|
|
{
|
2017-10-30 15:08:39 +00:00
|
|
|
name: "All Dashboards",
|
2017-10-30 21:03:53 +00:00
|
|
|
fields: fields{
|
|
|
|
DashboardsStore: &mocks.DashboardsStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Dashboard, error) {
|
|
|
|
return []chronograf.Dashboard{
|
|
|
|
{
|
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "doody",
|
|
|
|
Organization: "1338",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
args: args{
|
2017-10-27 22:14:46 +00:00
|
|
|
organization: "1337",
|
2017-10-30 14:34:18 +00:00
|
|
|
ctx: context.Background(),
|
2017-10-27 22:14:46 +00:00
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
want: []chronograf.Dashboard{
|
2017-10-27 22:14:46 +00:00
|
|
|
{
|
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2017-10-31 21:40:58 +00:00
|
|
|
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
|
2017-11-01 13:49:02 +00:00
|
|
|
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
|
2017-10-30 14:34:18 +00:00
|
|
|
gots, err := s.All(tt.args.ctx)
|
2017-10-27 22:14:46 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.All() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
2017-10-27 22:14:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, got := range gots {
|
2017-10-30 14:34:18 +00:00
|
|
|
if diff := cmp.Diff(got, tt.want[i], dashboardCmpOptions...); diff != "" {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.All():\n-got/+want\ndiff %s", tt.name, diff)
|
2017-10-27 22:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 14:34:18 +00:00
|
|
|
|
2017-10-30 18:31:19 +00:00
|
|
|
func TestDashboards_Add(t *testing.T) {
|
2017-10-30 21:03:53 +00:00
|
|
|
type fields struct {
|
|
|
|
DashboardsStore chronograf.DashboardsStore
|
|
|
|
}
|
2017-10-30 14:34:18 +00:00
|
|
|
type args struct {
|
|
|
|
organization string
|
|
|
|
ctx context.Context
|
|
|
|
dashboard chronograf.Dashboard
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2017-10-30 21:03:53 +00:00
|
|
|
fields fields
|
2017-10-30 14:34:18 +00:00
|
|
|
want chronograf.Dashboard
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2017-10-30 21:03:53 +00:00
|
|
|
name: "Add Dashboard",
|
|
|
|
fields: fields{
|
|
|
|
DashboardsStore: &mocks.DashboardsStore{
|
|
|
|
AddF: func(ctx context.Context, s chronograf.Dashboard) (chronograf.Dashboard, error) {
|
|
|
|
return s, nil
|
|
|
|
},
|
|
|
|
GetF: func(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) {
|
|
|
|
return chronograf.Dashboard{
|
|
|
|
ID: 1229,
|
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
args: args{
|
|
|
|
organization: "1337",
|
|
|
|
ctx: context.Background(),
|
|
|
|
dashboard: chronograf.Dashboard{
|
2017-10-30 21:03:53 +00:00
|
|
|
ID: 1229,
|
2017-10-30 14:34:18 +00:00
|
|
|
Name: "howdy",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: chronograf.Dashboard{
|
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2017-10-31 21:40:58 +00:00
|
|
|
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
|
2017-11-01 13:49:02 +00:00
|
|
|
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
|
2017-10-30 14:34:18 +00:00
|
|
|
d, err := s.Add(tt.args.ctx, tt.args.dashboard)
|
|
|
|
if (err != nil) != tt.wantErr {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.Add() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
2017-10-30 14:34:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
got, err := s.Get(tt.args.ctx, d.ID)
|
|
|
|
if diff := cmp.Diff(got, tt.want, dashboardCmpOptions...); diff != "" {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.Add():\n-got/+want\ndiff %s", tt.name, diff)
|
2017-10-30 14:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:31:19 +00:00
|
|
|
func TestDashboards_Delete(t *testing.T) {
|
2017-10-30 21:03:53 +00:00
|
|
|
type fields struct {
|
|
|
|
DashboardsStore chronograf.DashboardsStore
|
|
|
|
}
|
2017-10-30 14:34:18 +00:00
|
|
|
type args struct {
|
|
|
|
organization string
|
|
|
|
ctx context.Context
|
|
|
|
dashboard chronograf.Dashboard
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2017-10-30 21:03:53 +00:00
|
|
|
fields fields
|
2017-10-30 14:34:18 +00:00
|
|
|
args args
|
|
|
|
want []chronograf.Dashboard
|
|
|
|
addFirst bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Delete dashboard",
|
2017-10-30 21:03:53 +00:00
|
|
|
fields: fields{
|
|
|
|
DashboardsStore: &mocks.DashboardsStore{
|
|
|
|
DeleteF: func(ctx context.Context, s chronograf.Dashboard) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetF: func(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) {
|
|
|
|
return chronograf.Dashboard{
|
|
|
|
ID: 1229,
|
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
args: args{
|
|
|
|
organization: "1337",
|
|
|
|
ctx: context.Background(),
|
|
|
|
dashboard: chronograf.Dashboard{
|
2017-10-30 21:03:53 +00:00
|
|
|
ID: 1229,
|
2017-10-30 14:34:18 +00:00
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
addFirst: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2017-10-31 21:40:58 +00:00
|
|
|
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
|
2017-11-01 13:49:02 +00:00
|
|
|
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
|
2017-10-30 21:03:53 +00:00
|
|
|
err := s.Delete(tt.args.ctx, tt.args.dashboard)
|
2017-10-30 14:34:18 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.All() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
2017-10-30 14:34:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:31:19 +00:00
|
|
|
func TestDashboards_Get(t *testing.T) {
|
2017-10-30 21:03:53 +00:00
|
|
|
type fields struct {
|
|
|
|
DashboardsStore chronograf.DashboardsStore
|
|
|
|
}
|
2017-10-30 14:34:18 +00:00
|
|
|
type args struct {
|
|
|
|
organization string
|
|
|
|
ctx context.Context
|
|
|
|
dashboard chronograf.Dashboard
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2017-10-30 21:03:53 +00:00
|
|
|
fields fields
|
2017-10-30 14:34:18 +00:00
|
|
|
args args
|
|
|
|
want chronograf.Dashboard
|
|
|
|
addFirst bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2017-10-30 21:03:53 +00:00
|
|
|
name: "Get Dashboard",
|
|
|
|
fields: fields{
|
|
|
|
DashboardsStore: &mocks.DashboardsStore{
|
|
|
|
GetF: func(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) {
|
|
|
|
return chronograf.Dashboard{
|
|
|
|
ID: 1229,
|
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
args: args{
|
|
|
|
organization: "1337",
|
|
|
|
ctx: context.Background(),
|
|
|
|
dashboard: chronograf.Dashboard{
|
2017-10-30 21:03:53 +00:00
|
|
|
ID: 1229,
|
2017-10-30 14:34:18 +00:00
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: chronograf.Dashboard{
|
2017-10-30 21:03:53 +00:00
|
|
|
ID: 1229,
|
2017-10-30 14:34:18 +00:00
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2017-10-31 21:40:58 +00:00
|
|
|
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
|
2017-11-01 13:49:02 +00:00
|
|
|
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
|
2017-10-30 21:03:53 +00:00
|
|
|
got, err := s.Get(tt.args.ctx, tt.args.dashboard.ID)
|
2017-10-30 14:34:18 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2017-10-30 21:03:53 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.Get() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
2017-10-30 14:34:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(got, tt.want, dashboardCmpOptions...); diff != "" {
|
2017-10-30 21:03:53 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.Get():\n-got/+want\ndiff %s", tt.name, diff)
|
2017-10-30 14:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:31:19 +00:00
|
|
|
func TestDashboards_Update(t *testing.T) {
|
2017-10-30 21:03:53 +00:00
|
|
|
type fields struct {
|
|
|
|
DashboardsStore chronograf.DashboardsStore
|
|
|
|
}
|
2017-10-30 14:34:18 +00:00
|
|
|
type args struct {
|
|
|
|
organization string
|
|
|
|
ctx context.Context
|
|
|
|
dashboard chronograf.Dashboard
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2017-10-30 21:03:53 +00:00
|
|
|
fields fields
|
2017-10-30 14:34:18 +00:00
|
|
|
args args
|
|
|
|
want chronograf.Dashboard
|
|
|
|
addFirst bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2017-10-30 21:03:53 +00:00
|
|
|
name: "Update Dashboard Name",
|
|
|
|
fields: fields{
|
|
|
|
DashboardsStore: &mocks.DashboardsStore{
|
|
|
|
UpdateF: func(ctx context.Context, s chronograf.Dashboard) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetF: func(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) {
|
|
|
|
return chronograf.Dashboard{
|
|
|
|
ID: 1229,
|
|
|
|
Name: "doody",
|
|
|
|
Organization: "1337",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-30 14:34:18 +00:00
|
|
|
args: args{
|
|
|
|
organization: "1337",
|
|
|
|
ctx: context.Background(),
|
|
|
|
dashboard: chronograf.Dashboard{
|
2017-10-30 21:03:53 +00:00
|
|
|
ID: 1229,
|
2017-10-30 14:34:18 +00:00
|
|
|
Name: "howdy",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
name: "doody",
|
|
|
|
},
|
|
|
|
want: chronograf.Dashboard{
|
|
|
|
Name: "doody",
|
|
|
|
Organization: "1337",
|
|
|
|
},
|
|
|
|
addFirst: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if tt.args.name != "" {
|
|
|
|
tt.args.dashboard.Name = tt.args.name
|
|
|
|
}
|
2017-10-31 21:40:58 +00:00
|
|
|
s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization)
|
2017-11-01 13:49:02 +00:00
|
|
|
tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization)
|
2017-10-30 21:03:53 +00:00
|
|
|
err := s.Update(tt.args.ctx, tt.args.dashboard)
|
2017-10-30 14:34:18 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.Update() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
2017-10-30 14:34:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
got, err := s.Get(tt.args.ctx, tt.args.dashboard.ID)
|
|
|
|
if diff := cmp.Diff(got, tt.want, dashboardCmpOptions...); diff != "" {
|
2017-10-30 18:31:19 +00:00
|
|
|
t.Errorf("%q. DashboardsStore.Update():\n-got/+want\ndiff %s", tt.name, diff)
|
2017-10-30 14:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|