Skip to content

Commit

Permalink
Merge pull request #106 from grafana/feat-env-params
Browse files Browse the repository at this point in the history
Accept parameters from environment variables
  • Loading branch information
szkiba authored Dec 4, 2023
2 parents 00e473f + 38312c5 commit 24d784b
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dashboard/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
103 changes: 90 additions & 13 deletions dashboard/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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, ",")...)
}

Expand Down Expand Up @@ -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"
)
53 changes: 47 additions & 6 deletions dashboard/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -70,15 +111,15 @@ 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))

assert.Equal(t, time.Second, opts.period(points*time.Second))
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))
Expand Down
2 changes: 1 addition & 1 deletion magefiles/magexk6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 24d784b

Please sign in to comment.