2016-09-14 15:21:41 +00:00
|
|
|
package influx_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2016-09-15 18:01:53 +00:00
|
|
|
"time"
|
2016-09-14 15:21:41 +00:00
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
"github.com/influxdata/chronograf/influx"
|
|
|
|
"github.com/influxdata/chronograf/log"
|
2016-09-14 15:21:41 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-09-15 18:01:53 +00:00
|
|
|
func Test_Influx_MakesRequestsToQueryEndpoint(t *testing.T) {
|
2016-09-14 15:21:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
called := false
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
rw.Write([]byte(`{}`))
|
|
|
|
called = true
|
|
|
|
if path := r.URL.Path; path != "/query" {
|
|
|
|
t.Error("Expected the path to contain `/query` but was", path)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
var series chronograf.TimeSeries
|
2016-10-24 17:08:36 +00:00
|
|
|
series, err := influx.NewClient(ts.URL, log.New(log.DebugLevel))
|
2016-09-14 15:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected error initializing client: err:", err)
|
|
|
|
}
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
query := chronograf.Query{
|
2016-09-21 16:51:17 +00:00
|
|
|
Command: "show databases",
|
|
|
|
}
|
|
|
|
_, err = series.Query(context.Background(), query)
|
2016-09-14 15:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Expected no error but was", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if called == false {
|
|
|
|
t.Error("Expected http request to Influx but there was none")
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 18:01:53 +00:00
|
|
|
|
|
|
|
func Test_Influx_CancelsInFlightRequests(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2016-09-22 21:15:46 +00:00
|
|
|
started := make(chan bool, 1)
|
|
|
|
finished := make(chan bool, 1)
|
2016-09-15 18:01:53 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2016-09-22 21:15:46 +00:00
|
|
|
started <- true
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
finished <- true
|
2016-09-15 18:01:53 +00:00
|
|
|
}))
|
|
|
|
defer func() {
|
|
|
|
ts.CloseClientConnections()
|
|
|
|
ts.Close()
|
|
|
|
}()
|
|
|
|
|
2016-10-24 17:08:36 +00:00
|
|
|
series, _ := influx.NewClient(ts.URL, log.New(log.DebugLevel))
|
2016-09-15 18:01:53 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2016-09-15 19:08:25 +00:00
|
|
|
errs := make(chan (error))
|
2016-09-15 18:01:53 +00:00
|
|
|
go func() {
|
2016-10-20 14:38:23 +00:00
|
|
|
query := chronograf.Query{
|
2016-09-21 16:51:17 +00:00
|
|
|
Command: "show databases",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := series.Query(ctx, query)
|
2016-09-15 19:08:25 +00:00
|
|
|
errs <- err
|
2016-09-15 18:01:53 +00:00
|
|
|
}()
|
|
|
|
|
2016-09-22 21:15:46 +00:00
|
|
|
timer := time.NewTimer(10 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s := <-started:
|
|
|
|
if !s {
|
|
|
|
t.Errorf("Expected cancellation during request processing. Started: %t", s)
|
|
|
|
}
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("Expected server to finish")
|
|
|
|
}
|
|
|
|
|
2016-09-15 18:01:53 +00:00
|
|
|
cancel()
|
|
|
|
|
2016-09-22 21:15:46 +00:00
|
|
|
select {
|
|
|
|
case f := <-finished:
|
|
|
|
if !f {
|
|
|
|
t.Errorf("Expected cancellation during request processing. Finished: %t", f)
|
|
|
|
}
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatalf("Expected server to finish")
|
2016-09-15 18:01:53 +00:00
|
|
|
}
|
2016-09-15 19:08:25 +00:00
|
|
|
|
|
|
|
err := <-errs
|
2016-10-20 14:38:23 +00:00
|
|
|
if err != chronograf.ErrUpstreamTimeout {
|
2016-09-19 14:09:53 +00:00
|
|
|
t.Error("Expected timeout error but wasn't. err was", err)
|
2016-09-15 19:08:25 +00:00
|
|
|
}
|
2016-09-15 18:01:53 +00:00
|
|
|
}
|
2016-09-26 16:12:13 +00:00
|
|
|
|
|
|
|
func Test_Influx_RejectsInvalidHosts(t *testing.T) {
|
2016-10-24 17:08:36 +00:00
|
|
|
_, err := influx.NewClient(":", log.New(log.DebugLevel))
|
2016-09-26 16:12:13 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected err but was nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Influx_ReportsInfluxErrs(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
2016-10-24 17:08:36 +00:00
|
|
|
cl, err := influx.NewClient(ts.URL, log.New(log.DebugLevel))
|
2016-09-26 16:12:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Encountered unexpected error while initializing influx client: err:", err)
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
_, err = cl.Query(context.Background(), chronograf.Query{
|
|
|
|
Command: "show shards",
|
|
|
|
DB: "_internal",
|
|
|
|
RP: "autogen",
|
|
|
|
})
|
2016-09-26 16:12:13 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error but received none")
|
|
|
|
}
|
|
|
|
}
|