chronograf/filestore/apps_test.go

380 lines
7.9 KiB
Go
Raw Normal View History

package filestore_test
import (
"context"
"errors"
"os"
"path"
2016-10-11 00:34:05 +00:00
"path/filepath"
"reflect"
2016-10-11 00:34:05 +00:00
"sort"
"strconv"
"testing"
"time"
2016-10-20 14:38:23 +00:00
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/filestore"
2016-11-07 16:27:35 +00:00
clog "github.com/influxdata/chronograf/log"
)
func TestAll(t *testing.T) {
t.Parallel()
var tests = []struct {
2016-10-20 14:38:23 +00:00
Existing []chronograf.Layout
Err error
}{
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{
2016-10-11 00:34:05 +00:00
{ID: "1",
Application: "howdy",
},
2016-10-11 00:34:05 +00:00
{ID: "2",
Application: "doody",
},
},
Err: nil,
},
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{},
Err: nil,
},
{
Existing: nil,
Err: errors.New("Error"),
},
}
for i, test := range tests {
apps, _ := MockApps(test.Existing, test.Err)
layouts, err := apps.All(context.Background())
if err != test.Err {
t.Errorf("Test %d: apps all error expected: %v; actual: %v", i, test.Err, err)
}
if !reflect.DeepEqual(layouts, test.Existing) {
t.Errorf("Test %d: Layouts should be equal; expected %v; actual %v", i, test.Existing, layouts)
}
}
}
func TestAdd(t *testing.T) {
t.Parallel()
var tests = []struct {
2016-10-20 14:38:23 +00:00
Existing []chronograf.Layout
Add chronograf.Layout
2016-10-11 00:34:05 +00:00
ExpectedID string
Err error
}{
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{
2016-10-11 00:34:05 +00:00
{ID: "1",
Application: "howdy",
},
2016-10-11 00:34:05 +00:00
{ID: "2",
Application: "doody",
},
},
2016-10-20 14:38:23 +00:00
Add: chronograf.Layout{
Application: "newbie",
},
2016-10-11 00:34:05 +00:00
ExpectedID: "3",
Err: nil,
},
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{},
Add: chronograf.Layout{
Application: "newbie",
},
2016-10-11 00:34:05 +00:00
ExpectedID: "1",
Err: nil,
},
{
Existing: nil,
2016-10-20 14:38:23 +00:00
Add: chronograf.Layout{
Application: "newbie",
},
2016-10-11 00:34:05 +00:00
ExpectedID: "",
Err: errors.New("Error"),
},
}
for i, test := range tests {
apps, _ := MockApps(test.Existing, test.Err)
layout, err := apps.Add(context.Background(), test.Add)
if err != test.Err {
t.Errorf("Test %d: apps add error expected: %v; actual: %v", i, test.Err, err)
}
if layout.ID != test.ExpectedID {
2016-10-25 15:20:06 +00:00
t.Errorf("Test %d: Layout ID should be equal; expected %s; actual %s", i, test.ExpectedID, layout.ID)
}
}
}
func TestDelete(t *testing.T) {
t.Parallel()
var tests = []struct {
2016-10-20 14:38:23 +00:00
Existing []chronograf.Layout
2016-10-11 00:34:05 +00:00
DeleteID string
2016-10-20 14:38:23 +00:00
Expected map[string]chronograf.Layout
Err error
}{
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{
2016-10-11 00:34:05 +00:00
{ID: "1",
Application: "howdy",
},
2016-10-11 00:34:05 +00:00
{ID: "2",
Application: "doody",
},
},
2016-10-11 00:34:05 +00:00
DeleteID: "1",
2016-10-20 14:38:23 +00:00
Expected: map[string]chronograf.Layout{
2016-10-11 00:34:05 +00:00
"dir/2.json": {ID: "2",
Application: "doody",
},
},
Err: nil,
},
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{},
2016-10-11 00:34:05 +00:00
DeleteID: "1",
2016-10-20 14:38:23 +00:00
Expected: map[string]chronograf.Layout{},
Err: chronograf.ErrLayoutNotFound,
},
{
Existing: nil,
2016-10-11 00:34:05 +00:00
DeleteID: "1",
2016-10-20 14:38:23 +00:00
Expected: map[string]chronograf.Layout{},
Err: errors.New("Error"),
},
}
for i, test := range tests {
apps, actual := MockApps(test.Existing, test.Err)
2016-10-20 14:38:23 +00:00
err := apps.Delete(context.Background(), chronograf.Layout{ID: test.DeleteID})
if err != test.Err {
t.Errorf("Test %d: apps delete error expected: %v; actual: %v", i, test.Err, err)
}
if !reflect.DeepEqual(*actual, test.Expected) {
t.Errorf("Test %d: Layouts should be equal; expected %v; actual %v", i, test.Expected, actual)
}
}
}
func TestGet(t *testing.T) {
t.Parallel()
var tests = []struct {
2016-10-20 14:38:23 +00:00
Existing []chronograf.Layout
2016-10-11 00:34:05 +00:00
ID string
2016-10-20 14:38:23 +00:00
Expected chronograf.Layout
Err error
}{
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{
2016-10-11 00:34:05 +00:00
{ID: "1",
Application: "howdy",
},
2016-10-11 00:34:05 +00:00
{ID: "2",
Application: "doody",
},
},
2016-10-11 00:34:05 +00:00
ID: "1",
2016-10-20 14:38:23 +00:00
Expected: chronograf.Layout{
2016-10-11 00:34:05 +00:00
ID: "1",
Application: "howdy",
},
Err: nil,
},
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{},
2016-10-11 00:34:05 +00:00
ID: "1",
2016-10-20 14:38:23 +00:00
Expected: chronograf.Layout{},
Err: chronograf.ErrLayoutNotFound,
},
{
Existing: nil,
2016-10-11 00:34:05 +00:00
ID: "1",
2016-10-20 14:38:23 +00:00
Expected: chronograf.Layout{},
Err: chronograf.ErrLayoutNotFound,
},
}
for i, test := range tests {
apps, _ := MockApps(test.Existing, test.Err)
layout, err := apps.Get(context.Background(), test.ID)
if err != test.Err {
t.Errorf("Test %d: Layouts get error expected: %v; actual: %v", i, test.Err, err)
}
if !reflect.DeepEqual(layout, test.Expected) {
t.Errorf("Test %d: Layouts should be equal; expected %v; actual %v", i, test.Expected, layout)
}
}
}
func TestUpdate(t *testing.T) {
t.Parallel()
var tests = []struct {
2016-10-20 14:38:23 +00:00
Existing []chronograf.Layout
Update chronograf.Layout
Expected map[string]chronograf.Layout
Err error
}{
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{
2016-10-11 00:34:05 +00:00
{ID: "1",
Application: "howdy",
},
2016-10-11 00:34:05 +00:00
{ID: "2",
Application: "doody",
},
},
2016-10-20 14:38:23 +00:00
Update: chronograf.Layout{
2016-10-11 00:34:05 +00:00
ID: "1",
Application: "hello",
Measurement: "measurement",
},
2016-10-20 14:38:23 +00:00
Expected: map[string]chronograf.Layout{
2016-10-11 00:34:05 +00:00
"dir/1.json": {ID: "1",
Application: "hello",
Measurement: "measurement",
},
2016-10-11 00:34:05 +00:00
"dir/2.json": {ID: "2",
Application: "doody",
},
},
Err: nil,
},
{
2016-10-20 14:38:23 +00:00
Existing: []chronograf.Layout{},
Update: chronograf.Layout{
2016-10-11 00:34:05 +00:00
ID: "1",
},
2016-10-20 14:38:23 +00:00
Expected: map[string]chronograf.Layout{},
Err: chronograf.ErrLayoutNotFound,
},
{
Existing: nil,
2016-10-20 14:38:23 +00:00
Update: chronograf.Layout{
2016-10-11 00:34:05 +00:00
ID: "1",
},
2016-10-20 14:38:23 +00:00
Expected: map[string]chronograf.Layout{},
Err: chronograf.ErrLayoutNotFound,
},
}
for i, test := range tests {
apps, actual := MockApps(test.Existing, test.Err)
err := apps.Update(context.Background(), test.Update)
if err != test.Err {
t.Errorf("Test %d: Layouts get error expected: %v; actual: %v", i, test.Err, err)
}
if !reflect.DeepEqual(*actual, test.Expected) {
t.Errorf("Test %d: Layouts should be equal; expected %v; actual %v", i, test.Expected, actual)
}
}
}
type MockFileInfo struct {
name string
}
func (m *MockFileInfo) Name() string {
return m.name
}
func (m *MockFileInfo) Size() int64 {
return 0
}
func (m *MockFileInfo) Mode() os.FileMode {
return 0666
}
func (m *MockFileInfo) ModTime() time.Time {
return time.Now()
}
func (m *MockFileInfo) IsDir() bool {
return false
}
func (m *MockFileInfo) Sys() interface{} {
return nil
}
2016-10-11 00:34:05 +00:00
type MockFileInfos []os.FileInfo
func (m MockFileInfos) Len() int { return len(m) }
func (m MockFileInfos) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m MockFileInfos) Less(i, j int) bool { return m[i].Name() < m[j].Name() }
type MockID struct {
id int
}
func (m *MockID) Generate() (string, error) {
m.id++
return strconv.Itoa(m.id), nil
}
func MockApps(existing []chronograf.Layout, expected error) (filestore.Apps, *map[string]chronograf.Layout) {
2016-10-20 14:38:23 +00:00
layouts := map[string]chronograf.Layout{}
fileName := func(dir string, layout chronograf.Layout) string {
2016-10-11 00:34:05 +00:00
return path.Join(dir, layout.ID+".json")
}
dir := "dir"
for _, l := range existing {
layouts[fileName(dir, l)] = l
}
2016-10-20 14:38:23 +00:00
load := func(file string) (chronograf.Layout, error) {
if expected != nil {
2016-10-20 14:38:23 +00:00
return chronograf.Layout{}, expected
}
l, ok := layouts[file]
if !ok {
2016-10-20 14:38:23 +00:00
return chronograf.Layout{}, chronograf.ErrLayoutNotFound
}
return l, nil
}
2016-10-20 14:38:23 +00:00
create := func(file string, layout chronograf.Layout) error {
if expected != nil {
return expected
}
layouts[file] = layout
return nil
}
readDir := func(dirname string) ([]os.FileInfo, error) {
if expected != nil {
return nil, expected
}
info := []os.FileInfo{}
for k := range layouts {
2016-10-11 00:34:05 +00:00
info = append(info, &MockFileInfo{filepath.Base(k)})
}
2016-10-11 00:34:05 +00:00
sort.Sort(MockFileInfos(info))
return info, nil
}
remove := func(name string) error {
if expected != nil {
return expected
}
if _, ok := layouts[name]; !ok {
2016-10-20 14:38:23 +00:00
return chronograf.ErrLayoutNotFound
}
delete(layouts, name)
return nil
}
return filestore.Apps{
Dir: dir,
Load: load,
Filename: fileName,
Create: create,
ReadDir: readDir,
Remove: remove,
2016-10-11 00:34:05 +00:00
IDs: &MockID{
id: len(existing),
},
2016-11-07 16:27:35 +00:00
Logger: clog.New(clog.ParseLevel("debug")),
}, &layouts
}