2018-09-13 16:56:49 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-09-13 20:26:36 +00:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
2018-09-13 16:56:49 +00:00
|
|
|
"testing"
|
2018-09-13 20:26:36 +00:00
|
|
|
|
2019-03-07 15:32:13 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/influxdata/flux"
|
2018-09-13 20:26:36 +00:00
|
|
|
"github.com/influxdata/flux/csv"
|
|
|
|
"github.com/influxdata/flux/lang"
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
2019-03-26 03:05:44 +00:00
|
|
|
"github.com/influxdata/influxdb/kit/check"
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/query"
|
2018-09-13 16:56:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFluxService_Query(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
token string
|
|
|
|
ctx context.Context
|
|
|
|
r *query.ProxyRequest
|
|
|
|
status int
|
2019-03-07 15:32:13 +00:00
|
|
|
want flux.Statistics
|
2018-09-13 16:56:49 +00:00
|
|
|
wantW string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "query",
|
|
|
|
ctx: context.Background(),
|
|
|
|
token: "mytoken",
|
|
|
|
r: &query.ProxyRequest{
|
|
|
|
Request: query.Request{
|
|
|
|
Compiler: lang.FluxCompiler{
|
|
|
|
Query: "from()",
|
|
|
|
},
|
|
|
|
},
|
2018-09-13 18:00:27 +00:00
|
|
|
Dialect: csv.DefaultDialect(),
|
2018-09-13 16:56:49 +00:00
|
|
|
},
|
|
|
|
status: http.StatusOK,
|
2019-03-07 15:32:13 +00:00
|
|
|
want: flux.Statistics{},
|
2018-09-13 16:56:49 +00:00
|
|
|
wantW: "howdy\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error status",
|
|
|
|
token: "mytoken",
|
|
|
|
ctx: context.Background(),
|
|
|
|
r: &query.ProxyRequest{
|
|
|
|
Request: query.Request{
|
|
|
|
Compiler: lang.FluxCompiler{
|
|
|
|
Query: "from()",
|
|
|
|
},
|
|
|
|
},
|
2018-09-13 18:00:27 +00:00
|
|
|
Dialect: csv.DefaultDialect(),
|
2018-09-13 16:56:49 +00:00
|
|
|
},
|
|
|
|
status: http.StatusUnauthorized,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(tt.status)
|
|
|
|
fmt.Fprintln(w, "howdy")
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
s := &FluxService{
|
2018-10-04 19:11:45 +00:00
|
|
|
Addr: ts.URL,
|
2018-09-13 16:56:49 +00:00
|
|
|
Token: tt.token,
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &bytes.Buffer{}
|
|
|
|
got, err := s.Query(tt.ctx, w, tt.r)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("FluxService.Query() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2019-03-07 15:32:13 +00:00
|
|
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
|
|
|
t.Errorf("FluxService.Query() = -want/+got: %v", diff)
|
2018-09-13 16:56:49 +00:00
|
|
|
}
|
|
|
|
if gotW := w.String(); gotW != tt.wantW {
|
|
|
|
t.Errorf("FluxService.Query() = %v, want %v", gotW, tt.wantW)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 20:26:36 +00:00
|
|
|
|
|
|
|
func TestFluxQueryService_Query(t *testing.T) {
|
2018-09-26 16:36:39 +00:00
|
|
|
var orgID platform.ID
|
|
|
|
orgID.DecodeFromString("aaaaaaaaaaaaaaaa")
|
2018-09-13 20:26:36 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
token string
|
|
|
|
ctx context.Context
|
|
|
|
r *query.Request
|
|
|
|
csv string
|
|
|
|
status int
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "error status",
|
|
|
|
token: "mytoken",
|
|
|
|
ctx: context.Background(),
|
|
|
|
r: &query.Request{
|
2018-09-26 16:36:39 +00:00
|
|
|
OrganizationID: orgID,
|
2018-09-13 20:26:36 +00:00
|
|
|
Compiler: lang.FluxCompiler{
|
|
|
|
Query: "from()",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
status: http.StatusUnauthorized,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "returns csv",
|
|
|
|
token: "mytoken",
|
|
|
|
ctx: context.Background(),
|
|
|
|
r: &query.Request{
|
2018-09-26 16:36:39 +00:00
|
|
|
OrganizationID: orgID,
|
2018-09-13 20:26:36 +00:00
|
|
|
Compiler: lang.FluxCompiler{
|
|
|
|
Query: "from()",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
status: http.StatusOK,
|
|
|
|
csv: `#datatype,string,long,dateTime:RFC3339,double,long,string,boolean,string,string,string
|
|
|
|
#group,false,false,false,false,false,false,false,true,true,true
|
2019-03-05 23:43:18 +00:00
|
|
|
#default,_result,,,,,,,,,
|
2018-09-13 20:26:36 +00:00
|
|
|
,result,table,_time,usage_user,test,mystr,this,cpu,host,_measurement
|
|
|
|
,,0,2018-08-29T13:08:47Z,10.2,10,yay,true,cpu-total,a,cpui
|
|
|
|
`,
|
2019-03-05 23:43:18 +00:00
|
|
|
want: toCRLF(`,_result,0,2018-08-29T13:08:47Z,10.2,10,yay,true,cpu-total,a,cpui
|
2018-09-13 20:26:36 +00:00
|
|
|
|
|
|
|
`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-09-26 16:36:39 +00:00
|
|
|
var orgIDStr string
|
2018-09-13 20:26:36 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-09-26 16:36:39 +00:00
|
|
|
orgIDStr = r.URL.Query().Get(OrgID)
|
2018-09-13 20:26:36 +00:00
|
|
|
w.WriteHeader(tt.status)
|
|
|
|
fmt.Fprintln(w, tt.csv)
|
|
|
|
}))
|
|
|
|
s := &FluxQueryService{
|
2018-10-04 19:11:45 +00:00
|
|
|
Addr: ts.URL,
|
2018-09-13 20:26:36 +00:00
|
|
|
Token: tt.token,
|
|
|
|
}
|
|
|
|
res, err := s.Query(tt.ctx, tt.r)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("FluxQueryService.Query() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if res != nil && res.Err() != nil {
|
|
|
|
t.Errorf("FluxQueryService.Query() result error = %v", res.Err())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tt.wantErr {
|
|
|
|
return
|
|
|
|
}
|
2018-11-08 20:49:35 +00:00
|
|
|
defer res.Release()
|
2018-09-13 20:26:36 +00:00
|
|
|
|
|
|
|
enc := csv.NewMultiResultEncoder(csv.ResultEncoderConfig{
|
|
|
|
NoHeader: true,
|
|
|
|
Delimiter: ',',
|
|
|
|
})
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
n, err := enc.Encode(&b, res)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("FluxQueryService.Query() encode error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != int64(len(tt.want)) {
|
|
|
|
t.Errorf("FluxQueryService.Query() encode result = %d, want %d", n, len(tt.want))
|
|
|
|
}
|
2018-09-26 16:36:39 +00:00
|
|
|
if orgIDStr == "" {
|
|
|
|
t.Error("FluxQueryService.Query() encoded orgID is empty")
|
|
|
|
}
|
|
|
|
if got, want := orgIDStr, tt.r.OrganizationID.String(); got != want {
|
|
|
|
t.Errorf("FluxQueryService.Query() encoded orgID = %s, want %s", got, want)
|
|
|
|
}
|
2018-09-13 20:26:36 +00:00
|
|
|
|
|
|
|
got := b.String()
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("FluxQueryService.Query() =\n%s\n%s", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:21:53 +00:00
|
|
|
func TestFluxHandler_postFluxAST(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
w *httptest.ResponseRecorder
|
|
|
|
r *http.Request
|
|
|
|
want string
|
|
|
|
status int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "get ast from()",
|
|
|
|
w: httptest.NewRecorder(),
|
2018-11-02 00:09:01 +00:00
|
|
|
r: httptest.NewRequest("POST", "/api/v2/query/ast", bytes.NewBufferString(`{"query": "from()"}`)),
|
2019-01-04 18:08:07 +00:00
|
|
|
want: `{"ast":{"type":"Package","package":"main","files":[{"type":"File","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":7},"source":"from()"},"package":null,"imports":null,"body":[{"type":"ExpressionStatement","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":7},"source":"from()"},"expression":{"type":"CallExpression","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":7},"source":"from()"},"callee":{"type":"Identifier","location":{"start":{"line":1,"column":1},"end":{"line":1,"column":5},"source":"from"},"name":"from"}}}]}]}}
|
2018-10-04 18:21:53 +00:00
|
|
|
`,
|
|
|
|
status: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error from bad json",
|
|
|
|
w: httptest.NewRecorder(),
|
2018-11-02 00:09:01 +00:00
|
|
|
r: httptest.NewRequest("POST", "/api/v2/query/ast", bytes.NewBufferString(`error!`)),
|
2019-01-24 00:15:42 +00:00
|
|
|
want: `{"code":"invalid","message":"invalid json","error":"invalid character 'e' looking for beginning of value"}`,
|
2018-10-04 18:21:53 +00:00
|
|
|
status: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
h := &FluxHandler{}
|
|
|
|
h.postFluxAST(tt.w, tt.r)
|
|
|
|
if got := tt.w.Body.String(); got != tt.want {
|
|
|
|
t.Errorf("http.postFluxAST = got\n%vwant\n%v", got, tt.want)
|
|
|
|
}
|
|
|
|
if got := tt.w.Code; got != tt.status {
|
|
|
|
t.Errorf("http.postFluxAST = got %d\nwant %d", got, tt.status)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 03:05:44 +00:00
|
|
|
func TestFluxService_Check(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(HealthHandler))
|
|
|
|
defer ts.Close()
|
|
|
|
s := &FluxService{
|
|
|
|
Addr: ts.URL,
|
|
|
|
}
|
|
|
|
got := s.Check(context.Background())
|
|
|
|
want := check.Response{
|
|
|
|
Name: "influxdb",
|
|
|
|
Status: "pass",
|
|
|
|
Message: "ready for queries and writes",
|
|
|
|
Checks: check.Responses{},
|
|
|
|
}
|
|
|
|
if !cmp.Equal(want, got) {
|
|
|
|
t.Errorf("unexpected response -want/+got: " + cmp.Diff(want, got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFluxQueryService_Check(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(HealthHandler))
|
|
|
|
defer ts.Close()
|
|
|
|
s := &FluxQueryService{
|
|
|
|
Addr: ts.URL,
|
|
|
|
}
|
|
|
|
got := s.Check(context.Background())
|
|
|
|
want := check.Response{
|
|
|
|
Name: "influxdb",
|
|
|
|
Status: "pass",
|
|
|
|
Message: "ready for queries and writes",
|
|
|
|
Checks: check.Responses{},
|
|
|
|
}
|
|
|
|
if !cmp.Equal(want, got) {
|
|
|
|
t.Errorf("unexpected response -want/+got: " + cmp.Diff(want, got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 20:26:36 +00:00
|
|
|
var crlfPattern = regexp.MustCompile(`\r?\n`)
|
|
|
|
|
|
|
|
func toCRLF(data string) string {
|
|
|
|
return crlfPattern.ReplaceAllString(data, "\r\n")
|
|
|
|
}
|