2016-09-30 20:39:27 +00:00
|
|
|
|
package bolt_test
|
|
|
|
|
|
|
|
|
|
import (
|
2016-11-14 17:04:32 +00:00
|
|
|
|
"context"
|
2016-09-30 20:39:27 +00:00
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
|
"github.com/influxdata/chronograf"
|
2016-11-04 20:52:15 +00:00
|
|
|
|
"github.com/influxdata/chronograf/bolt"
|
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)
|
|
|
|
|
}
|
|
|
|
|
defer c.Close()
|
2017-12-17 22:25:18 +00:00
|
|
|
|
|
2016-09-30 20:39:27 +00:00
|
|
|
|
s := c.SourcesStore
|
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
|
srcs := []chronograf.Source{
|
|
|
|
|
chronograf.Source{
|
2017-10-25 15:51:15 +00:00
|
|
|
|
Name: "Of Truth",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "marty",
|
|
|
|
|
Password: "I❤️ jennifer parker",
|
|
|
|
|
URL: "toyota-hilux.lyon-estates.local",
|
|
|
|
|
Default: true,
|
|
|
|
|
Organization: "1337",
|
2016-09-30 20:39:27 +00:00
|
|
|
|
},
|
2016-10-20 14:38:23 +00:00
|
|
|
|
chronograf.Source{
|
2017-10-25 15:51:15 +00:00
|
|
|
|
Name: "HipToBeSquare",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "calvinklein",
|
|
|
|
|
Password: "chuck b3rry",
|
|
|
|
|
URL: "toyota-hilux.lyon-estates.local",
|
|
|
|
|
Default: true,
|
|
|
|
|
Organization: "1337",
|
2016-09-30 20:39:27 +00:00
|
|
|
|
},
|
2017-01-05 01:35:07 +00:00
|
|
|
|
chronograf.Source{
|
|
|
|
|
Name: "HipToBeSquare",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "calvinklein",
|
|
|
|
|
Password: "chuck b3rry",
|
|
|
|
|
URL: "https://toyota-hilux.lyon-estates.local",
|
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
|
Default: false,
|
2017-10-25 15:51:15 +00:00
|
|
|
|
Organization: "1337",
|
2017-01-05 01:35:07 +00:00
|
|
|
|
},
|
2016-09-30 20:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-14 17:04:32 +00:00
|
|
|
|
ctx := context.Background()
|
2016-09-30 20:39:27 +00:00
|
|
|
|
// Add new srcs.
|
|
|
|
|
for i, src := range srcs {
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if srcs[i], err = s.Add(ctx, src); err != nil {
|
2016-09-30 20:39:27 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
// Confirm first src in the store is the same as the original.
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if actual, err := s.Get(ctx, srcs[i].ID); err != nil {
|
2016-09-30 20:39:27 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if !reflect.DeepEqual(actual, srcs[i]) {
|
2016-10-25 15:20:06 +00:00
|
|
|
|
t.Fatalf("source loaded is different then source saved; actual: %v, expected %v", actual, srcs[i])
|
2016-09-30 20:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update source.
|
|
|
|
|
srcs[0].Username = "calvinklein"
|
|
|
|
|
srcs[1].Name = "Enchantment Under the Sea Dance"
|
2016-11-04 20:52:15 +00:00
|
|
|
|
mustUpdateSource(t, s, srcs[0])
|
|
|
|
|
mustUpdateSource(t, s, srcs[1])
|
2016-09-30 20:39:27 +00:00
|
|
|
|
|
|
|
|
|
// Confirm sources have updated.
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if src, err := s.Get(ctx, srcs[0].ID); err != nil {
|
2016-09-30 20:39:27 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if src.Username != "calvinklein" {
|
|
|
|
|
t.Fatalf("source 0 update error: got %v, expected %v", src.Username, "calvinklein")
|
|
|
|
|
}
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if src, err := s.Get(ctx, srcs[1].ID); err != nil {
|
2016-09-30 20:39:27 +00:00
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 20:52:15 +00:00
|
|
|
|
// Attempt to make two default sources
|
|
|
|
|
srcs[0].Default = true
|
|
|
|
|
srcs[1].Default = true
|
|
|
|
|
mustUpdateSource(t, s, srcs[0])
|
|
|
|
|
mustUpdateSource(t, s, srcs[1])
|
|
|
|
|
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if actual, err := s.Get(ctx, srcs[0].ID); err != nil {
|
2016-11-04 20:52:15 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if actual.Default == true {
|
|
|
|
|
t.Fatal("Able to set two default sources when only one should be permitted")
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-07 16:58:14 +00:00
|
|
|
|
// Attempt to add a new default source
|
|
|
|
|
srcs = append(srcs, chronograf.Source{
|
2017-10-25 15:51:15 +00:00
|
|
|
|
Name: "Biff Tannen",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "HELLO",
|
|
|
|
|
Password: "MCFLY",
|
|
|
|
|
URL: "anybody.in.there.local",
|
|
|
|
|
Default: true,
|
|
|
|
|
Organization: "1892",
|
2016-11-07 16:58:14 +00:00
|
|
|
|
})
|
|
|
|
|
|
2017-01-05 01:35:07 +00:00
|
|
|
|
srcs[3] = mustAddSource(t, s, srcs[3])
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if srcs, err := s.All(ctx); err != nil {
|
2016-11-07 16:58:14 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else {
|
|
|
|
|
defaults := 0
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
|
if src.Default {
|
|
|
|
|
defaults++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if defaults != 1 {
|
|
|
|
|
t.Fatal("Able to add more than one default source")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 20:39:27 +00:00
|
|
|
|
// Delete an source.
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if err := s.Delete(ctx, srcs[0]); err != nil {
|
2016-09-30 20:39:27 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Confirm source has been deleted.
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if _, err := s.Get(ctx, srcs[0].ID); err != chronograf.ErrSourceNotFound {
|
2016-10-20 14:38:23 +00:00
|
|
|
|
t.Fatalf("source delete error: got %v, expected %v", err, chronograf.ErrSourceNotFound)
|
2016-09-30 20:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-07 16:58:14 +00:00
|
|
|
|
// Delete the other source we created
|
2017-01-05 01:35:07 +00:00
|
|
|
|
if err := s.Delete(ctx, srcs[3]); err != nil {
|
2016-11-07 16:58:14 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if bsrcs, err := s.All(ctx); err != nil {
|
2016-09-30 20:39:27 +00:00
|
|
|
|
t.Fatal(err)
|
2017-01-05 01:35:07 +00:00
|
|
|
|
} else if len(bsrcs) != 2 {
|
|
|
|
|
t.Fatalf("After delete All returned incorrect number of srcs; got %d, expected %d", len(bsrcs), 2)
|
2016-09-30 20:39:27 +00:00
|
|
|
|
} else if !reflect.DeepEqual(bsrcs[0], srcs[1]) {
|
|
|
|
|
t.Fatalf("After delete All returned incorrect source; got %v, expected %v", bsrcs[0], srcs[1])
|
|
|
|
|
}
|
2016-11-07 23:13:02 +00:00
|
|
|
|
|
2017-01-05 01:35:07 +00:00
|
|
|
|
// Delete the final sources
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if err := s.Delete(ctx, srcs[1]); err != nil {
|
2016-11-07 23:13:02 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2017-01-05 01:35:07 +00:00
|
|
|
|
if err := s.Delete(ctx, srcs[2]); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2016-11-07 23:13:02 +00:00
|
|
|
|
|
|
|
|
|
// Try to add one source as a non-default and ensure that it becomes a
|
|
|
|
|
// default
|
|
|
|
|
src := mustAddSource(t, s, chronograf.Source{
|
2017-10-25 15:51:15 +00:00
|
|
|
|
Name: "Biff Tannen",
|
|
|
|
|
Type: "influx",
|
|
|
|
|
Username: "HELLO",
|
|
|
|
|
Password: "MCFLY",
|
|
|
|
|
URL: "anybody.in.there.local",
|
|
|
|
|
Default: false,
|
|
|
|
|
Organization: "1234",
|
2016-11-07 23:13:02 +00:00
|
|
|
|
})
|
|
|
|
|
|
2016-11-14 17:04:32 +00:00
|
|
|
|
if actual, err := s.Get(ctx, src.ID); err != nil {
|
2016-11-07 23:13:02 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
} else if !actual.Default {
|
|
|
|
|
t.Fatal("Expected first source added to be default but wasn't")
|
|
|
|
|
}
|
2016-09-30 20:39:27 +00:00
|
|
|
|
}
|
2016-11-04 20:52:15 +00:00
|
|
|
|
|
|
|
|
|
func mustUpdateSource(t *testing.T, s *bolt.SourcesStore, src chronograf.Source) {
|
2016-11-14 17:04:32 +00:00
|
|
|
|
ctx := context.Background()
|
|
|
|
|
if err := s.Update(ctx, src); err != nil {
|
2016-11-04 20:52:15 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-07 16:58:14 +00:00
|
|
|
|
|
|
|
|
|
func mustAddSource(t *testing.T, s *bolt.SourcesStore, src chronograf.Source) chronograf.Source {
|
2016-11-14 17:04:32 +00:00
|
|
|
|
ctx := context.Background()
|
|
|
|
|
if src, err := s.Add(ctx, src); err != nil {
|
2016-11-07 16:58:14 +00:00
|
|
|
|
t.Fatal(err)
|
|
|
|
|
return src
|
|
|
|
|
} else {
|
|
|
|
|
return src
|
|
|
|
|
}
|
|
|
|
|
}
|