Skip to content

Commit

Permalink
build(flux): update flux to v0.180.1 (#23666)
Browse files Browse the repository at this point in the history
* chore: make tests robust to Flux formatter changes
  • Loading branch information
Christopher M. Wolff committed Aug 24, 2022
1 parent ee8ca45 commit aa9c49e
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 21 deletions.
6 changes: 6 additions & 0 deletions checks/service_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/influxdata/influxdb/v2/notification"
"github.com/influxdata/influxdb/v2/notification/check"
"github.com/influxdata/influxdb/v2/task/taskmodel"
itesting "github.com/influxdata/influxdb/v2/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -173,6 +174,11 @@ var taskCmpOptions = cmp.Options{
})
return out
}),
cmp.Transformer("FormatFlux", func(in taskmodel.Task) taskmodel.Task {
newTask := in
newTask.Flux = itesting.FormatFluxString(&testing.T{}, newTask.Flux)
return newTask
}),
}

// CheckFields will include the IDGenerator, and checks
Expand Down
31 changes: 30 additions & 1 deletion etc/test-flux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,36 @@ remove_sort_selector
remove_sort_filter_range
remove_sort_aggregate_window
remove_sort_join
vec_conditional_time
vec_conditional_time_repeat
vec_conditional_int
vec_conditional_int_repeat
vec_conditional_float
vec_conditional_float_repeat
vec_conditional_uint
vec_conditional_string
vec_conditional_string_repeat
vec_const_with_const
vec_const_with_const_add_const
vec_const_add_member_const
vec_const_with_const_add_const_add_member
vec_const_with_const_add_member_add_const
vec_const_with_member_add_const_add_const
vec_const_kitchen_sink_column_types
vec_equality_time
vec_equality_time_repeat
vec_equality_int
vec_equality_int_repeat
vec_equality_float
vec_equality_float_repeat
vec_equality_uint
vec_equality_string
vec_equality_string_repeat
vec_equality_bool
vec_equality_casts
vec_with_float
vec_with_float_const
# Other skipped tests
align_time
buckets
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/google/go-jsonnet v0.17.0
github.com/hashicorp/vault/api v1.0.2
github.com/influxdata/cron v0.0.0-20201006132531-4bb0a200dcbe
github.com/influxdata/flux v0.179.0
github.com/influxdata/flux v0.180.1
github.com/influxdata/httprouter v1.3.1-0.20191122104820-ee83e2772f69
github.com/influxdata/influx-cli/v2 v2.2.1-0.20220318222112-88ba3464cd07
github.com/influxdata/influxql v1.1.1-0.20211004132434-7e7d61973256
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/cron v0.0.0-20201006132531-4bb0a200dcbe h1:7j4SdN/BvQwN6WoUq7mv0kg5U9NhnFBxPGMafYRKym0=
github.com/influxdata/cron v0.0.0-20201006132531-4bb0a200dcbe/go.mod h1:XabtPPW2qsCg0tl+kjaPU+cFS+CjQXEXbT1VJvHT4og=
github.com/influxdata/flux v0.179.0 h1:Mk85dwwN+KcHAj3lT0nwITGY++x6q6aKrXx/M7789fo=
github.com/influxdata/flux v0.179.0/go.mod h1:FY4XAM6a/04xK/pyKyGKQJ3Ovo98h87I5cbT1758b0M=
github.com/influxdata/flux v0.180.1 h1:zM+U3LgV+qWuAeixGeSm0+AVYSlnZAT71H/JSwLAsSc=
github.com/influxdata/flux v0.180.1/go.mod h1:FY4XAM6a/04xK/pyKyGKQJ3Ovo98h87I5cbT1758b0M=
github.com/influxdata/gosnowflake v1.6.9 h1:BhE39Mmh8bC+Rvd4QQsP2gHypfeYIH1wqW1AjGWxxrE=
github.com/influxdata/gosnowflake v1.6.9/go.mod h1:9W/BvCXOKx2gJtQ+jdi1Vudev9t9/UDOEHnlJZ/y1nU=
github.com/influxdata/httprouter v1.3.1-0.20191122104820-ee83e2772f69 h1:WQsmW0fXO4ZE/lFGIE84G6rIV5SJN3P3sjIXAP1a8eU=
Expand Down
44 changes: 42 additions & 2 deletions http/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,31 @@ func TestService_handleGetCheckQuery(t *testing.T) {
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: "{\"flux\":\"import \\\"influxdata/influxdb/monitor\\\"\\nimport \\\"influxdata/influxdb/v1\\\"\\n\\ndata =\\n from(bucket: \\\"foo\\\")\\n |\\u003e range(start: -1h)\\n |\\u003e filter(fn: (r) =\\u003e r._field == \\\"usage_idle\\\")\\n |\\u003e aggregateWindow(every: 1h, fn: mean, createEmpty: false)\\n\\noption task = {name: \\\"hello\\\", every: 1h}\\n\\ncheck = {_check_id: \\\"020f755c3c082000\\\", _check_name: \\\"hello\\\", _type: \\\"threshold\\\", tags: {aaa: \\\"vaaa\\\", bbb: \\\"vbbb\\\"}}\\nok = (r) =\\u003e r[\\\"usage_idle\\\"] \\u003e 10.0\\ninfo = (r) =\\u003e r[\\\"usage_idle\\\"] \\u003c 40.0\\nwarn = (r) =\\u003e r[\\\"usage_idle\\\"] \\u003c 40.0 and r[\\\"usage_idle\\\"] \\u003e 10.0\\ncrit = (r) =\\u003e r[\\\"usage_idle\\\"] \\u003c 40.0 and r[\\\"usage_idle\\\"] \\u003e 10.0\\nmessageFn = (r) =\\u003e \\\"whoa! {check.yeah}\\\"\\n\\ndata\\n |\\u003e v1[\\\"fieldsAsCols\\\"]()\\n |\\u003e monitor[\\\"check\\\"](\\n data: check,\\n messageFn: messageFn,\\n ok: ok,\\n info: info,\\n warn: warn,\\n crit: crit,\\n )\\n\"}\n",
body: "{\"flux\":" + formatFluxJson(t, `import "influxdata/influxdb/monitor"
import "influxdata/influxdb/v1"
data =
from(bucket: "foo")
|> range(start: -1h)
|> filter(fn: (r) => r._field == "usage_idle")
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
option task = {name: "hello", every: 1h}
check = {_check_id: "020f755c3c082000", _check_name: "hello", _type: "threshold", tags: {aaa: "vaaa", bbb: "vbbb"}}
ok = (r) => r["usage_idle"] > 10.0
info = (r) => r["usage_idle"] < 40.0
warn = (r) => r["usage_idle"] < 40.0 and r["usage_idle"] > 10.0
crit = (r) => r["usage_idle"] < 40.0 and r["usage_idle"] > 10.0
messageFn = (r) => "whoa! {check.yeah}"
data
|> v1["fieldsAsCols"]()
|> monitor["check"](
data: check,
messageFn: messageFn,
ok: ok,
info: info,
warn: warn,
crit: crit,
)
`) + "}\n",
},
},
}
Expand All @@ -445,14 +469,30 @@ func TestService_handleGetCheckQuery(t *testing.T) {
}).
ExpectBody(func(body *bytes.Buffer) {
if eq, diff, err := jsonEqual(body.String(), tt.wants.body); err != nil || tt.wants.body != "" && !eq {
fmt.Printf("%q\n", body.String())
if err != nil {
t.Errorf("jsonEqual error: %v", err)
}
t.Errorf("%q. handleGetChecks() = ***%v***", tt.name, diff)
}
})
})
}
}

func formatFluxJson(t *testing.T, script string) string {
formatted := influxTesting.FormatFluxString(t, script)

enc, err := json.Marshal(formatted)
if err != nil {
t.Fatalf("error marshalling flux: %v", err)
}

var bb bytes.Buffer
json.HTMLEscape(&bb, enc)
std := bb.String()
return std
}

func TestService_handleGetCheck(t *testing.T) {
type fields struct {
CheckService influxdb.CheckService
Expand Down
3 changes: 2 additions & 1 deletion notification/check/deadman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/influxdata/influxdb/v2/notification"
"github.com/influxdata/influxdb/v2/notification/check"
"github.com/influxdata/influxdb/v2/query/fluxlang"
itesting "github.com/influxdata/influxdb/v2/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -195,7 +196,7 @@ data
t.Run(tt.name, func(t *testing.T) {
s, err := tt.args.deadman.GenerateFlux(fluxlang.DefaultService)
require.NoError(t, err)
assert.Equal(t, tt.wants.script, s)
assert.Equal(t, itesting.FormatFluxString(t, tt.wants.script), s)
})
}

Expand Down
3 changes: 2 additions & 1 deletion notification/check/threshold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/influxdata/influxdb/v2/notification"
"github.com/influxdata/influxdb/v2/notification/check"
"github.com/influxdata/influxdb/v2/query/fluxlang"
itesting "github.com/influxdata/influxdb/v2/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -357,7 +358,7 @@ data
t.Run(tt.name, func(t *testing.T) {
s, err := tt.args.threshold.GenerateFlux(fluxlang.DefaultService)
require.NoError(t, err)
assert.Equal(t, tt.wants.script, s)
assert.Equal(t, itesting.FormatFluxString(t, tt.wants.script), s)
})
}

Expand Down
17 changes: 9 additions & 8 deletions notification/rule/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"github.com/influxdata/influxdb/v2/notification"
"github.com/influxdata/influxdb/v2/notification/endpoint"
"github.com/influxdata/influxdb/v2/notification/rule"
itesting "github.com/influxdata/influxdb/v2/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHTTP_GenerateFlux(t *testing.T) {
want := `import "influxdata/influxdb/monitor"
want := itesting.FormatFluxString(t, `import "influxdata/influxdb/monitor"
import "http"
import "json"
import "experimental"
Expand Down Expand Up @@ -45,7 +46,7 @@ all_statuses
},
),
)
`
`)

s := &rule.HTTP{
Base: rule.Base{
Expand Down Expand Up @@ -83,7 +84,7 @@ all_statuses
}

func TestHTTP_GenerateFlux_basicAuth(t *testing.T) {
want := `import "influxdata/influxdb/monitor"
want := itesting.FormatFluxString(t, `import "influxdata/influxdb/monitor"
import "http"
import "json"
import "experimental"
Expand Down Expand Up @@ -122,7 +123,7 @@ all_statuses
},
),
)
`
`)
s := &rule.HTTP{
Base: rule.Base{
ID: 1,
Expand Down Expand Up @@ -166,7 +167,7 @@ all_statuses
}

func TestHTTP_GenerateFlux_bearer(t *testing.T) {
want := `import "influxdata/influxdb/monitor"
want := itesting.FormatFluxString(t, `import "influxdata/influxdb/monitor"
import "http"
import "json"
import "experimental"
Expand Down Expand Up @@ -201,7 +202,7 @@ all_statuses
},
),
)
`
`)

s := &rule.HTTP{
Base: rule.Base{
Expand Down Expand Up @@ -243,7 +244,7 @@ all_statuses
}

func TestHTTP_GenerateFlux_bearer_every_second(t *testing.T) {
want := `import "influxdata/influxdb/monitor"
want := itesting.FormatFluxString(t, `import "influxdata/influxdb/monitor"
import "http"
import "json"
import "experimental"
Expand Down Expand Up @@ -278,7 +279,7 @@ all_statuses
},
),
)
`
`)

s := &rule.HTTP{
Base: rule.Base{
Expand Down
3 changes: 2 additions & 1 deletion notification/rule/pagerduty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/influxdata/influxdb/v2/notification"
"github.com/influxdata/influxdb/v2/notification/endpoint"
"github.com/influxdata/influxdb/v2/notification/rule"
itesting "github.com/influxdata/influxdb/v2/testing"
)

func TestPagerDuty_GenerateFlux(t *testing.T) {
Expand Down Expand Up @@ -284,7 +285,7 @@ all_statuses
panic(err)
}

if got, want := script, tt.script; got != want {
if got, want := script, itesting.FormatFluxString(t, tt.script); got != want {
t.Errorf("\n\nStrings do not match:\n\n%s", diff.LineDiff(got, want))

}
Expand Down
5 changes: 3 additions & 2 deletions notification/rule/service/service_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/influxdata/influxdb/v2/notification/rule"
"github.com/influxdata/influxdb/v2/pkg/pointer"
"github.com/influxdata/influxdb/v2/task/taskmodel"
itesting "github.com/influxdata/influxdb/v2/testing"
)

const (
Expand Down Expand Up @@ -295,7 +296,7 @@ func CreateNotificationRule(
OwnerID: MustIDBase16("020f755c3c082005"),
Name: "name2",
Status: "active",
Flux: `import "influxdata/influxdb/monitor"
Flux: itesting.FormatFluxString(t, `import "influxdata/influxdb/monitor"
import "slack"
import "influxdata/influxdb/secrets"
import "experimental"
Expand Down Expand Up @@ -333,7 +334,7 @@ all_statuses
}),
),
)
`,
`),
Every: "1h",
},
},
Expand Down
3 changes: 2 additions & 1 deletion notification/rule/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/influxdata/influxdb/v2/notification"
"github.com/influxdata/influxdb/v2/notification/endpoint"
"github.com/influxdata/influxdb/v2/notification/rule"
itesting "github.com/influxdata/influxdb/v2/testing"
)

func mustDuration(d string) *notification.Duration {
Expand Down Expand Up @@ -393,7 +394,7 @@ all_statuses
t.Fatal(err)
}

if f != tt.want {
if f != itesting.FormatFluxString(t, tt.want) {
t.Errorf("scripts did not match. want:\n%v\n\ngot:\n%v", tt.want, f)
}
})
Expand Down
2 changes: 1 addition & 1 deletion notification/rule/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ all_statuses
return
}

if got, want := script, tt.script; got != want {
if got, want := script, influxTesting.FormatFluxString(t, tt.script); got != want {
t.Errorf("\n\nStrings do not match:\n\n%s", diff.LineDiff(got, want))
}
})
Expand Down
8 changes: 8 additions & 0 deletions query/fluxlang/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"

"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/ast/astutil"
"github.com/influxdata/flux/complete"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/parser"
Expand All @@ -26,6 +27,9 @@ type FluxLanguageService interface {
// but it may be null if parsing didn't even occur.
Parse(source string) (*ast.Package, error)

// Format will produce a string for the given *ast.File.
Format(f *ast.File) (string, error)

// EvalAST will evaluate and run an AST.
EvalAST(ctx context.Context, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error)

Expand All @@ -46,6 +50,10 @@ func (d defaultService) Parse(source string) (pkg *ast.Package, err error) {
return pkg, err
}

func (d defaultService) Format(f *ast.File) (string, error) {
return astutil.Format(f)
}

func (d defaultService) EvalAST(ctx context.Context, astPkg *ast.Package) ([]interpreter.SideEffect, values.Scope, error) {
return runtime.EvalAST(ctx, astPkg)
}
Expand Down
11 changes: 11 additions & 0 deletions testing/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/influxdata/influxdb/v2/kit/platform/errors"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/kv/migration/all"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
Expand Down Expand Up @@ -155,3 +156,13 @@ func influxErrsEqual(t *testing.T, expected *errors.Error, actual error) {
assert.Equal(t, expected.Code, iErr.Code)
assert.Truef(t, strings.HasPrefix(iErr.Error(), expected.Error()), "expected: %s got err: %s", expected.Error(), actual.Error())
}

func FormatFluxString(t *testing.T, script string) string {
svc := fluxlang.DefaultService

astPkg, err := svc.Parse(script)
require.NoError(t, err)
formatted, err := svc.Format(astPkg.Files[0])
require.NoError(t, err)
return formatted
}

0 comments on commit aa9c49e

Please sign in to comment.