2016-11-18 19:13:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-07-15 01:02:13 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2016-11-18 19:13:32 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
2017-07-15 01:02:13 +00:00
|
|
|
"github.com/influxdata/chronograf/mocks"
|
2016-11-18 19:13:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_newSourceResponse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
src chronograf.Source
|
|
|
|
want sourceResponse
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test empty telegraf",
|
|
|
|
src: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Telegraf: "",
|
|
|
|
},
|
|
|
|
want: sourceResponse{
|
|
|
|
Source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Telegraf: "telegraf",
|
|
|
|
},
|
|
|
|
Links: sourceLinks{
|
2017-02-19 20:37:45 +00:00
|
|
|
Self: "/chronograf/v1/sources/1",
|
|
|
|
Proxy: "/chronograf/v1/sources/1/proxy",
|
2017-04-07 21:57:06 +00:00
|
|
|
Queries: "/chronograf/v1/sources/1/queries",
|
2017-05-03 05:06:40 +00:00
|
|
|
Write: "/chronograf/v1/sources/1/write",
|
2017-02-19 20:37:45 +00:00
|
|
|
Kapacitors: "/chronograf/v1/sources/1/kapacitors",
|
|
|
|
Users: "/chronograf/v1/sources/1/users",
|
|
|
|
Permissions: "/chronograf/v1/sources/1/permissions",
|
2017-03-23 21:06:52 +00:00
|
|
|
Databases: "/chronograf/v1/sources/1/dbs",
|
2016-11-18 19:13:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test non-default telegraf",
|
|
|
|
src: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Telegraf: "howdy",
|
|
|
|
},
|
|
|
|
want: sourceResponse{
|
|
|
|
Source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Telegraf: "howdy",
|
|
|
|
},
|
|
|
|
Links: sourceLinks{
|
2017-02-19 20:37:45 +00:00
|
|
|
Self: "/chronograf/v1/sources/1",
|
|
|
|
Proxy: "/chronograf/v1/sources/1/proxy",
|
2017-04-07 21:57:06 +00:00
|
|
|
Queries: "/chronograf/v1/sources/1/queries",
|
2017-05-03 05:06:40 +00:00
|
|
|
Write: "/chronograf/v1/sources/1/write",
|
2017-02-19 20:37:45 +00:00
|
|
|
Kapacitors: "/chronograf/v1/sources/1/kapacitors",
|
|
|
|
Users: "/chronograf/v1/sources/1/users",
|
|
|
|
Permissions: "/chronograf/v1/sources/1/permissions",
|
2017-03-23 21:06:52 +00:00
|
|
|
Databases: "/chronograf/v1/sources/1/dbs",
|
2016-11-18 19:13:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if got := newSourceResponse(tt.src); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("%q. newSourceResponse() = %v, want %v", tt.name, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-15 01:02:13 +00:00
|
|
|
|
|
|
|
func TestService_newSourceKapacitor(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
SourcesStore chronograf.SourcesStore
|
|
|
|
ServersStore chronograf.ServersStore
|
|
|
|
Logger chronograf.Logger
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
src chronograf.Source
|
|
|
|
kapa chronograf.Server
|
|
|
|
}
|
|
|
|
srcCount := 0
|
|
|
|
srvCount := 0
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantSrc int
|
|
|
|
wantSrv int
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Add when no existing sources",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Source, error) {
|
|
|
|
return []chronograf.Source{}, nil
|
|
|
|
},
|
|
|
|
AddF: func(ctx context.Context, src chronograf.Source) (chronograf.Source, error) {
|
|
|
|
srcCount++
|
|
|
|
src.ID = srcCount
|
|
|
|
return src, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServersStore: &mocks.ServersStore{
|
|
|
|
AddF: func(ctx context.Context, srv chronograf.Server) (chronograf.Server, error) {
|
|
|
|
srvCount++
|
|
|
|
return srv, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
src: chronograf.Source{
|
|
|
|
Name: "Influx 1",
|
|
|
|
},
|
|
|
|
kapa: chronograf.Server{
|
|
|
|
Name: "Kapa 1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantSrc: 1,
|
|
|
|
wantSrv: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should not add if existing source",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Source, error) {
|
|
|
|
return []chronograf.Source{
|
|
|
|
{
|
|
|
|
Name: "Influx 1",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
AddF: func(ctx context.Context, src chronograf.Source) (chronograf.Source, error) {
|
|
|
|
srcCount++
|
|
|
|
src.ID = srcCount
|
|
|
|
return src, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServersStore: &mocks.ServersStore{
|
|
|
|
AddF: func(ctx context.Context, srv chronograf.Server) (chronograf.Server, error) {
|
|
|
|
srvCount++
|
|
|
|
return srv, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: &mocks.TestLogger{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
src: chronograf.Source{
|
|
|
|
Name: "Influx 1",
|
|
|
|
},
|
|
|
|
kapa: chronograf.Server{
|
|
|
|
Name: "Kapa 1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantSrc: 0,
|
|
|
|
wantSrv: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error if All returns error",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Source, error) {
|
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: &mocks.TestLogger{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error if Add returns error",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Source, error) {
|
|
|
|
return []chronograf.Source{}, nil
|
|
|
|
},
|
|
|
|
AddF: func(ctx context.Context, src chronograf.Source) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{}, fmt.Errorf("error")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: &mocks.TestLogger{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error if kapa add is error",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
AllF: func(ctx context.Context) ([]chronograf.Source, error) {
|
|
|
|
return []chronograf.Source{}, nil
|
|
|
|
},
|
|
|
|
AddF: func(ctx context.Context, src chronograf.Source) (chronograf.Source, error) {
|
|
|
|
srcCount++
|
|
|
|
src.ID = srcCount
|
|
|
|
return src, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServersStore: &mocks.ServersStore{
|
|
|
|
AddF: func(ctx context.Context, srv chronograf.Server) (chronograf.Server, error) {
|
|
|
|
srvCount++
|
|
|
|
return chronograf.Server{}, fmt.Errorf("error")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Logger: &mocks.TestLogger{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
src: chronograf.Source{
|
|
|
|
Name: "Influx 1",
|
|
|
|
},
|
|
|
|
kapa: chronograf.Server{
|
|
|
|
Name: "Kapa 1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantSrc: 1,
|
|
|
|
wantSrv: 1,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
srcCount = 0
|
|
|
|
srvCount = 0
|
|
|
|
h := &Service{
|
|
|
|
SourcesStore: tt.fields.SourcesStore,
|
|
|
|
ServersStore: tt.fields.ServersStore,
|
|
|
|
Logger: tt.fields.Logger,
|
|
|
|
}
|
|
|
|
if err := h.newSourceKapacitor(tt.args.ctx, tt.args.src, tt.args.kapa); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Service.newSourceKapacitor() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if tt.wantSrc != srcCount {
|
|
|
|
t.Errorf("Service.newSourceKapacitor() count = %d, wantSrc %d", srcCount, tt.wantSrc)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|