Skip to content

Commit

Permalink
feat(http): add query plan validation
Browse files Browse the repository at this point in the history
  • Loading branch information
goller committed Nov 2, 2018
1 parent 7ca5187 commit 0d7b77d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
22 changes: 13 additions & 9 deletions http/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ type postPlanRequest struct {
Spec *flux.Spec `json:"spec,omityempty"`
}

// Valid check if the plan request has a query or spec defined, but not both.
func (p *postPlanRequest) Valid() error {
if p.Query == "" && p.Spec == nil {
return errors.MalformedDataf("query or spec required")
}

if p.Query != "" && p.Spec != nil {
return errors.MalformedDataf("cannot request both query and spec")
}
return nil
}

// postFluxPlan returns a flux plan for provided flux string
func (h *FluxHandler) postFluxPlan(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down Expand Up @@ -252,15 +264,7 @@ func decodePostPlanRequest(ctx context.Context, r *http.Request) (*postPlanReque
return nil, errors.MalformedDataf("invalid json: %v", err)
}

if req.Query == "" && req.Spec == nil {
return nil, errors.MalformedDataf("query or spec required")
}

if req.Query != "" && req.Spec != nil {
return nil, errors.MalformedDataf("cannot request both query and spec")
}

return req, nil
return req, req.Valid()
}

// fluxParams contain flux funciton parameters as defined by the semantic graph
Expand Down
52 changes: 52 additions & 0 deletions http/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

"github.com/influxdata/flux"
"github.com/influxdata/flux/csv"
"github.com/influxdata/flux/lang"
"github.com/influxdata/platform"
Expand Down Expand Up @@ -341,3 +342,54 @@ var crlfPattern = regexp.MustCompile(`\r?\n`)
func toCRLF(data string) string {
return crlfPattern.ReplaceAllString(data, "\r\n")
}

func Test_postPlanRequest_Valid(t *testing.T) {
type fields struct {
Query string
Spec *flux.Spec
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "no query nor spec is an error",
fields: fields{},
wantErr: true,
},
{
name: "both query and spec is an error",
fields: fields{
Query: "from()|>last()",
Spec: &flux.Spec{},
},
wantErr: true,
},
{

name: "request with query is valid",
fields: fields{
Query: `from(bucket:"telegraf")|> range(start: -5000h)|> filter(fn: (r) => r._measurement == "mem" AND r._field == "used_percent")|> group(by:["host"])|> mean()`,
},
},
{

name: "request with spec is valid",
fields: fields{
Spec: &flux.Spec{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &postPlanRequest{
Query: tt.fields.Query,
Spec: tt.fields.Spec,
}
if err := p.Valid(); (err != nil) != tt.wantErr {
t.Errorf("postPlanRequest.Valid() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
1 change: 0 additions & 1 deletion write/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ func TestBatcher_Write(t *testing.T) {
}

func TestBatcher_WriteTimeout(t *testing.T) {

// mocking the write service here to either return an error
// or get back all the bytes from the reader.
var got string
Expand Down

0 comments on commit 0d7b77d

Please sign in to comment.