Skip to content

Commit

Permalink
Merge pull request #1428 from loadimpact/feat/1007-1300-env-tags-exec…
Browse files Browse the repository at this point in the history
…-options

Add support for exec/env/tags executor options
  • Loading branch information
Ivan Mirić committed May 18, 2020
2 parents e09562d + b2b8648 commit 270fd91
Show file tree
Hide file tree
Showing 38 changed files with 979 additions and 361 deletions.
5 changes: 3 additions & 2 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ package cmd
import (
"os"

"github.com/loadimpact/k6/loader"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/loadimpact/k6/loader"
)

var archiveOut = "archive.tar"
Expand Down Expand Up @@ -77,7 +78,7 @@ An archive is a fully self-contained test run, and can be executed identically e
return err
}

if _, cerr := deriveAndValidateConfig(conf); cerr != nil {
if _, cerr := deriveAndValidateConfig(conf, r.IsExecutable); cerr != nil {
return ExitCode{error: cerr, Code: invalidConfigErrorCode}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud
return err
}

derivedConf, cerr := deriveAndValidateConfig(conf)
derivedConf, cerr := deriveAndValidateConfig(conf, r.IsExecutable)
if cerr != nil {
return ExitCode{error: cerr, Code: invalidConfigErrorCode}
}
Expand Down
21 changes: 18 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,24 @@ func applyDefault(conf Config) Config {
return conf
}

func deriveAndValidateConfig(conf Config) (result Config, err error) {
func deriveAndValidateConfig(conf Config, isExecutable func(string) bool) (result Config, err error) {
result = conf
result.Options, err = executor.DeriveExecutionFromShortcuts(conf.Options)
if err != nil {
return result, err
}
return result, validateConfig(result)
return result, validateConfig(result, isExecutable)
}

func validateConfig(conf Config) error {
func validateConfig(conf Config, isExecutable func(string) bool) error {
errList := conf.Validate()

for _, ec := range conf.Execution {
if err := validateExecutorConfig(ec, isExecutable); err != nil {
errList = append(errList, err)
}
}

if len(errList) == 0 {
return nil
}
Expand All @@ -284,3 +291,11 @@ func validateConfig(conf Config) error {

return errors.New(strings.Join(errMsgParts, "\n"))
}

func validateExecutorConfig(conf lib.ExecutorConfig, isExecutable func(string) bool) error {
execFn := conf.GetExec()
if !isExecutable(execFn) {
return fmt.Errorf("executor %s: function '%s' not found in exports", conf.GetName(), execFn)
}
return nil
}
52 changes: 51 additions & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ package cmd
import (
"fmt"
"testing"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/loadimpact/k6/lib/testutils"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/executor"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/types"
)

type testCmdData struct {
Expand Down Expand Up @@ -134,3 +139,48 @@ func TestConfigApply(t *testing.T) {
assert.Equal(t, []string{"influxdb", "json"}, conf.Out)
})
}

func TestDeriveAndValidateConfig(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
conf Config
isExec bool
err string
}{
{"defaultOK", Config{}, true, ""},
{"defaultErr", Config{}, false,
"executor default: function 'default' not found in exports"},
{"nonDefaultOK", Config{Options: lib.Options{Execution: lib.ExecutorConfigMap{
"per_vu_iters": executor.PerVUIterationsConfig{BaseConfig: executor.BaseConfig{
Name: "per_vu_iters", Type: "per-vu-iterations", Exec: null.StringFrom("nonDefault")},
VUs: null.IntFrom(1),
Iterations: null.IntFrom(1),
MaxDuration: types.NullDurationFrom(time.Second),
}}}}, true, "",
},
{"nonDefaultErr", Config{Options: lib.Options{Execution: lib.ExecutorConfigMap{
"per_vu_iters": executor.PerVUIterationsConfig{BaseConfig: executor.BaseConfig{
Name: "per_vu_iters", Type: "per-vu-iterations", Exec: null.StringFrom("nonDefaultErr")},
VUs: null.IntFrom(1),
Iterations: null.IntFrom(1),
MaxDuration: types.NullDurationFrom(time.Second),
}}}}, false,
"executor per_vu_iters: function 'nonDefaultErr' not found in exports",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
_, err := deriveAndValidateConfig(tc.conf,
func(_ string) bool { return tc.isExec })
if tc.err != "" {
assert.Contains(t, err.Error(), tc.err)
} else {
assert.NoError(t, err)
}
})
}
}
6 changes: 3 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ a commandline interface for interacting with it.`,
return err
}

conf, cerr := deriveAndValidateConfig(conf)
conf, cerr := deriveAndValidateConfig(conf, r.IsExecutable)
if cerr != nil {
return ExitCode{error: cerr, Code: invalidConfigErrorCode}
}
Expand Down Expand Up @@ -382,9 +382,9 @@ func getExitCodeFromEngine(err error) ExitCode {
switch e := errors.Cause(err).(type) {
case lib.TimeoutError:
switch e.Place() {
case "setup":
case consts.SetupFn:
return ExitCode{error: err, Code: setupTimeoutErrorCode, Hint: e.Hint()}
case "teardown":
case consts.TeardownFn:
return ExitCode{error: err, Code: teardownTimeoutErrorCode, Hint: e.Hint()}
default:
return ExitCode{error: err, Code: genericTimeoutErrorCode}
Expand Down

0 comments on commit 270fd91

Please sign in to comment.