Add test for creating NewSources via server flag

Move TestLogger to mocks

Signed-off-by: Tim Raymond <tim@timraymond.com>
pull/1695/head
Jared Scheib 2017-07-07 12:56:12 -07:00 committed by Tim Raymond
parent 965d72ea3f
commit 0855fc434b
4 changed files with 129 additions and 2 deletions

88
chronograf_test.go Normal file
View File

@ -0,0 +1,88 @@
package chronograf_test
import (
"context"
"reflect"
"testing"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/mocks"
)
func Test_NewSources(t *testing.T) {
t.Parallel()
srcsKaps := []chronograf.SourceAndKapacitor{
{
Source: chronograf.Source{
Default: true,
InsecureSkipVerify: false,
MetaURL: "http://metaurl.com",
Name: "Influx 1",
Password: "pass1",
Telegraf: "telegraf",
URL: "http://localhost:8086",
Username: "user1",
},
Kapacitor: chronograf.Server{
Active: true,
Name: "Kapa 1",
URL: "http://localhost:9092",
},
},
}
saboteurSrcsKaps := []chronograf.SourceAndKapacitor{
{
Source: chronograf.Source{
Name: "Influx 1",
},
Kapacitor: chronograf.Server{
Name: "Kapa Aspiring Saboteur",
},
},
}
ctx := context.Background()
srcs := []chronograf.Source{}
srcsStore := mocks.SourcesStore{
AllF: func(ctx context.Context) ([]chronograf.Source, error) {
return srcs, nil
},
AddF: func(ctx context.Context, src chronograf.Source) (chronograf.Source, error) {
srcs = append(srcs, src)
return src, nil
},
}
srvs := []chronograf.Server{}
srvsStore := mocks.ServersStore{
AddF: func(ctx context.Context, srv chronograf.Server) (chronograf.Server, error) {
srvs = append(srvs, srv)
return srv, nil
},
}
err := chronograf.NewSources(ctx, &srcsStore, &srvsStore, srcsKaps, &mocks.TestLogger{})
if err != nil {
t.Fatal("Expected no error when creating New Sources. Error:", err)
}
if len(srcs) != 1 {
t.Error("Expected one source in sourcesStore")
}
if len(srvs) != 1 {
t.Error("Expected one source in serversStore")
}
err = chronograf.NewSources(ctx, &srcsStore, &srvsStore, saboteurSrcsKaps, &mocks.TestLogger{})
if err != nil {
t.Fatal("Expected no error when creating New Sources. Error:", err)
}
if len(srcs) != 1 {
t.Error("Expected one source in sourcesStore")
}
if len(srvs) != 1 {
t.Error("Expected one source in serversStore")
}
if !reflect.DeepEqual(srcs[0], srcsKaps[0].Source) {
t.Error("Expected source in sourceStore to remain unchanged")
}
}

View File

@ -1,4 +1,4 @@
package server_test
package mocks
import (
"fmt"

38
mocks/servers.go Normal file
View File

@ -0,0 +1,38 @@
package mocks
import (
"context"
"github.com/influxdata/chronograf"
)
var _ chronograf.ServersStore = &ServersStore{}
// ServersStore mock allows all functions to be set for testing
type ServersStore struct {
AllF func(context.Context) ([]chronograf.Server, error)
AddF func(context.Context, chronograf.Server) (chronograf.Server, error)
DeleteF func(context.Context, chronograf.Server) error
GetF func(ctx context.Context, ID int) (chronograf.Server, error)
UpdateF func(context.Context, chronograf.Server) error
}
func (s *ServersStore) All(ctx context.Context) ([]chronograf.Server, error) {
return s.AllF(ctx)
}
func (s *ServersStore) Add(ctx context.Context, srv chronograf.Server) (chronograf.Server, error) {
return s.AddF(ctx, srv)
}
func (s *ServersStore) Delete(ctx context.Context, srv chronograf.Server) error {
return s.DeleteF(ctx, srv)
}
func (s *ServersStore) Get(ctx context.Context, id int) (chronograf.Server, error) {
return s.GetF(ctx, id)
}
func (s *ServersStore) Update(ctx context.Context, srv chronograf.Server) error {
return s.UpdateF(ctx, srv)
}

View File

@ -7,6 +7,7 @@ import (
"net/http/httptest"
"testing"
"github.com/influxdata/chronograf/mocks"
"github.com/influxdata/chronograf/server"
)
@ -138,7 +139,7 @@ func Test_Server_Prefixer_NoPrefixingWithoutFlusther(t *testing.T) {
})
}
tl := &TestLogger{}
tl := &mocks.TestLogger{}
pfx := &server.URLPrefixer{
Prefix: "/hill",
Next: backend,