diff --git a/chronograf_test.go b/chronograf_test.go new file mode 100644 index 0000000000..bed0c4f40d --- /dev/null +++ b/chronograf_test.go @@ -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") + } +} diff --git a/server/server_test.go b/mocks/logger.go similarity index 98% rename from server/server_test.go rename to mocks/logger.go index 22330e851e..46c7bae9f6 100644 --- a/server/server_test.go +++ b/mocks/logger.go @@ -1,4 +1,4 @@ -package server_test +package mocks import ( "fmt" diff --git a/mocks/servers.go b/mocks/servers.go new file mode 100644 index 0000000000..837c0a3b1a --- /dev/null +++ b/mocks/servers.go @@ -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) +} diff --git a/server/url_prefixer_test.go b/server/url_prefixer_test.go index 619b304d2c..d1cf66678b 100644 --- a/server/url_prefixer_test.go +++ b/server/url_prefixer_test.go @@ -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,