diff --git a/dashboard/extension.go b/dashboard/extension.go index e2793b4..397f52a 100644 --- a/dashboard/extension.go +++ b/dashboard/extension.go @@ -54,7 +54,7 @@ func New(params output.Params) (output.Output, error) { } func newWithAssets(params output.Params, assets *assets) (*extension, error) { - opts, err := getopts(params.ConfigArgument) + opts, err := getopts(params.ConfigArgument, params.Environment) if err != nil { return nil, err } diff --git a/dashboard/options.go b/dashboard/options.go index 2cedc92..4bdd19f 100644 --- a/dashboard/options.go +++ b/dashboard/options.go @@ -34,12 +34,12 @@ type options struct { Open bool Report string Record string - Tags []string `schema:"tag"` - TagsS string `schema:"tags"` + Tags []string + TagsS string } -func getopts(query string) (opts *options, err error) { //nolint:nonamedreturns - opts = &options{ +func envopts(env map[string]string) (*options, error) { + opts := &options{ Port: defaultPort, Host: defaultHost, Period: defaultPeriod, @@ -50,6 +50,57 @@ func getopts(query string) (opts *options, err error) { //nolint:nonamedreturns TagsS: "", } + if len(env) == 0 { + return opts, nil + } + + if v, ok := env[envPort]; ok { + i, e := strconv.Atoi(v) + if e != nil { + return nil, e + } + + opts.Port = i + } + + if v, ok := env[envHost]; ok { + opts.Host = v + } + + if v, ok := env[envReport]; ok { + opts.Report = v + } + + if v, ok := env[envRecord]; ok { + opts.Record = v + } + + if v, ok := env[envPeriod]; ok { + d, e := time.ParseDuration(v) + if e != nil { + return nil, errInvalidDuration + } + + opts.Period = d + } + + if v, ok := env[envOpen]; ok && v == "true" { + opts.Open = true + } + + if v, ok := env[envTags]; ok { + opts.Tags = strings.Split(v, ",") + } + + return opts, nil +} + +func getopts(query string, env map[string]string) (*options, error) { + opts, err := envopts(env) + if err != nil { + return nil, err + } + if query == "" { return opts, nil } @@ -59,28 +110,28 @@ func getopts(query string) (opts *options, err error) { //nolint:nonamedreturns return nil, err } - if v := value.Get("port"); len(v) != 0 { + if v := value.Get(paramPort); len(v) != 0 { i, e := strconv.Atoi(v) if e != nil { - return nil, err + return nil, e } opts.Port = i } - if v := value.Get("host"); len(v) != 0 { + if v := value.Get(paramHost); len(v) != 0 { opts.Host = v } - if v := value.Get("report"); len(v) != 0 { + if v := value.Get(paramReport); len(v) != 0 { opts.Report = v } - if v := value.Get("record"); len(v) != 0 { + if v := value.Get(paramRecord); len(v) != 0 { opts.Record = v } - if v := value.Get("period"); len(v) != 0 { + if v := value.Get(paramPeriod); len(v) != 0 { d, e := time.ParseDuration(v) if e != nil { return nil, errInvalidDuration @@ -89,15 +140,15 @@ func getopts(query string) (opts *options, err error) { //nolint:nonamedreturns opts.Period = d } - if v := value["tag"]; len(v) != 0 { + if v := value[paramTag]; len(v) != 0 { opts.Tags = v } - if value.Has("open") && len(value.Get("open")) == 0 { + if value.Has(paramOpen) && (len(value.Get(paramOpen)) == 0 || value.Get(paramOpen) == "true") { opts.Open = true } - if v := value.Get("tags"); len(v) != 0 { + if v := value.Get(paramTags); len(v) != 0 { opts.Tags = append(opts.Tags, strings.Split(v, ",")...) } @@ -142,3 +193,29 @@ approx. 1MB max report size, 8 hours test run with 10sec event period. const points = 2880 var errInvalidDuration = errors.New("invalid duration") + +const ( + envPrefix = "K6_WEB_DASHBOARD_" + + paramPort = "port" + envPort = envPrefix + "PORT" + + paramHost = "host" + envHost = envPrefix + "HOST" + + paramPeriod = "period" + envPeriod = envPrefix + "PERIOD" + + paramOpen = "open" + envOpen = envPrefix + "OPEN" + + paramReport = "report" + envReport = envPrefix + "REPORT" + + paramRecord = "record" + envRecord = envPrefix + "RECORD" + + paramTag = "tag" + paramTags = "tags" + envTags = envPrefix + "TAGS" +) diff --git a/dashboard/options_test.go b/dashboard/options_test.go index 3011fc3..573eb6d 100644 --- a/dashboard/options_test.go +++ b/dashboard/options_test.go @@ -19,7 +19,7 @@ import ( func Test_getopts_defaults(t *testing.T) { t.Parallel() - opts, err := getopts("") + opts, err := getopts("", nil) assert.NoError(t, err) assert.NotNil(t, opts) @@ -41,15 +41,56 @@ func Test_getopts_defaults(t *testing.T) { func Test_getopts_error(t *testing.T) { t.Parallel() - _, err := getopts("period=s") + _, err := getopts("period=s", nil) assert.Error(t, err) } +func Test_getopts_env(t *testing.T) { + t.Parallel() + + env := map[string]string{ + envPort: "1", + envHost: "example.com", + envPeriod: "1h", + envRecord: "results.data", + envReport: "report.html", + envTags: "foo,bar", + envOpen: "true", + } + + opts, err := getopts("", env) + + assert.NoError(t, err) + assert.NotNil(t, opts) + + assert.Equal(t, "example.com", opts.Host) + assert.Equal(t, 1, opts.Port) + assert.Equal(t, time.Hour, opts.Period) + assert.Equal(t, true, opts.Open) + assert.Equal(t, "report.html", opts.Report) + assert.Equal(t, []string{"foo", "bar"}, opts.Tags) + + assert.Equal(t, "http://example.com:1", opts.url()) +} + func Test_getopts(t *testing.T) { t.Parallel() - opts, err := getopts("period=1s&port=1&host=localhost&open&report=report.html&tag=foo&tag=bar") + env := map[string]string{ + envPort: "6666", + envHost: "example.net", + envPeriod: "2h", + envRecord: "results.data", + envReport: "final.html", + envTags: "foo,bar", + envOpen: "true", + } + + opts, err := getopts( + "period=1s&port=1&host=localhost&open&report=report.html&tag=foo&tag=bar", + env, + ) assert.NoError(t, err) assert.Equal(t, time.Second, opts.Period) @@ -61,7 +102,7 @@ func Test_getopts(t *testing.T) { assert.Equal(t, "localhost:1", opts.addr()) assert.Equal(t, []string{"foo", "bar"}, opts.Tags) - opts, err = getopts("tag=foo&tag=bar&tags=apple,orange") + opts, err = getopts("tag=foo&tag=bar&tags=apple,orange", nil) assert.NoError(t, err) assert.Equal(t, []string{"foo", "bar", "apple", "orange"}, opts.Tags) @@ -70,7 +111,7 @@ func Test_getopts(t *testing.T) { func Test_options_pack_calc(t *testing.T) { t.Parallel() - opts, _ := getopts("period=1s") + opts, _ := getopts("period=1s", nil) assert.Equal(t, time.Second, opts.period(0)) @@ -78,7 +119,7 @@ func Test_options_pack_calc(t *testing.T) { assert.Equal(t, 2*time.Second, opts.period(2*points*time.Second)) assert.Equal(t, 3*time.Second, opts.period(3*points*time.Second)) - opts, _ = getopts("") + opts, _ = getopts("", nil) assert.Equal(t, 10*time.Second, opts.period(time.Second)) assert.Equal(t, 10*time.Second, opts.period(4*time.Hour)) diff --git a/magefiles/magexk6.go b/magefiles/magexk6.go index 12accf9..8612086 100644 --- a/magefiles/magexk6.go +++ b/magefiles/magexk6.go @@ -156,7 +156,7 @@ func tools() error { func xk6build() error { mg.Deps(tools) - return sh.Run("xk6", "build") + return sh.Run("xk6", "build", "--with", module+"=.") } func xk6run(args ...string) error {