2016-09-30 20:39:27 +00:00
|
|
|
|
package bolt_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
|
"github.com/influxdata/chronograf"
|
2016-09-30 20:39:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
2016-09-30 23:46:28 +00:00
|
|
|
|
// Ensure an SourceStore can store, retrieve, update, and delete sources.
|
2016-09-30 20:39:27 +00:00
|
|
|
|
func TestSourceStore(t *testing.T) {
|
|
|
|
|
c, err := NewTestClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := c.Open(); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer c.Close()
|
|
|
|
|
s := c.SourcesStore
|
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
|
srcs := []chronograf.Source{
|
|
|
|
|
chronograf.Source{
|
2016-09-30 20:39:27 +00:00
|
|
|
|
Name: "Of Truth",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "marty",
|
|
|
|
|
Password: "I❤️ jennifer parker",
|
|
|
|
|
URL: []string{"toyota-hilux.lyon-estates.local", "lake.hilldale.local"},
|
|
|
|
|
Default: true,
|
|
|
|
|
},
|
2016-10-20 14:38:23 +00:00
|
|
|
|
chronograf.Source{
|
2016-09-30 20:39:27 +00:00
|
|
|
|
Name: "HipToBeSquare",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "calvinklein",
|
|
|
|
|
Password: "chuck b3rry",
|
|
|
|
|
URL: []string{"toyota-hilux.lyon-estates.local", "lake.hilldale.local"},
|
|
|
|
|
Default: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add new srcs.
|
|
|
|
|
for i, src := range srcs {
|
|
|
|
|
if srcs[i], err = s.Add(nil, src); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
// Confirm first src in the store is the same as the original.
|
|
|
|
|
if actual, err := s.Get(nil, srcs[i].ID); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if !reflect.DeepEqual(actual, srcs[i]) {
|
|
|
|
|
t.Fatal("source loaded is different then source saved; actual: %v, expected %v", actual, srcs[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update source.
|
|
|
|
|
srcs[0].Username = "calvinklein"
|
|
|
|
|
srcs[1].Name = "Enchantment Under the Sea Dance"
|
|
|
|
|
if err := s.Update(nil, srcs[0]); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if err := s.Update(nil, srcs[1]); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Confirm sources have updated.
|
|
|
|
|
if src, err := s.Get(nil, srcs[0].ID); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if src.Username != "calvinklein" {
|
|
|
|
|
t.Fatalf("source 0 update error: got %v, expected %v", src.Username, "calvinklein")
|
|
|
|
|
}
|
|
|
|
|
if src, err := s.Get(nil, srcs[1].ID); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if src.Name != "Enchantment Under the Sea Dance" {
|
|
|
|
|
t.Fatalf("source 1 update error: got %v, expected %v", src.Name, "Enchantment Under the Sea Dance")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete an source.
|
|
|
|
|
if err := s.Delete(nil, srcs[0]); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Confirm source has been deleted.
|
2016-10-20 14:38:23 +00:00
|
|
|
|
if _, err := s.Get(nil, srcs[0].ID); err != chronograf.ErrSourceNotFound {
|
|
|
|
|
t.Fatalf("source delete error: got %v, expected %v", err, chronograf.ErrSourceNotFound)
|
2016-09-30 20:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if bsrcs, err := s.All(nil); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if len(bsrcs) != 1 {
|
|
|
|
|
t.Fatalf("After delete All returned incorrect number of srcs; got %d, expected %d", len(bsrcs), 1)
|
|
|
|
|
} else if !reflect.DeepEqual(bsrcs[0], srcs[1]) {
|
|
|
|
|
t.Fatalf("After delete All returned incorrect source; got %v, expected %v", bsrcs[0], srcs[1])
|
|
|
|
|
}
|
|
|
|
|
}
|