Skip to content

Commit

Permalink
Extend config validation to the cloud and archive subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 12, 2019
1 parent 6d19e61 commit 9404af6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
4 changes: 4 additions & 0 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ An archive is a fully self-contained test run, and can be executed identically e
return err
}

if cerr := validateConfig(conf); cerr != nil {
return ExitCode{cerr, invalidConfigErrorCode}
}

err = r.SetOptions(conf.Options)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud
return err
}

if cerr := validateConfig(conf); cerr != nil {
return ExitCode{cerr, invalidConfigErrorCode}
}

err = r.SetOptions(conf.Options)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ package cmd

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"errors"

"github.com/kelseyhightower/envconfig"
"github.com/loadimpact/k6/lib"
Expand Down Expand Up @@ -300,3 +304,20 @@ func getConsolidatedConfig(fs afero.Fs, cliConf Config, runner lib.Runner) (conf

return buildExecutionConfig(conf)
}

func validateConfig(conf Config) error {
errList := conf.Validate()
if len(errList) == 0 {
return nil
}

errMsgParts := []string{"There were problems with the specified script configuration:"}
for _, err := range errList {
errMsgParts = append(errMsgParts, fmt.Sprintf("\t- %s", err.Error()))
}
errMsg := errors.New(strings.Join(errMsgParts, "\n"))

//TODO: actually return the error here instead of warning, so k6 aborts on config validation errors
log.Warn(errMsg)
return nil
}
17 changes: 6 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const (
teardownTimeoutErrorCode = 101
genericTimeoutErrorCode = 102
genericEngineErrorCode = 103
//invalidOptionsErrorCode = 104
invalidConfigErrorCode = 104
)

var (
Expand Down Expand Up @@ -164,21 +164,16 @@ a commandline interface for interacting with it.`,
)
}

//TODO: move a bunch of the logic above to a config "constructor" and to the Validate() method

// If duration is explicitly set to 0, it means run forever.
//TODO: just... handle this differently, e.g. as a part of the manual executor
if conf.Duration.Valid && conf.Duration.Duration == 0 {
conf.Duration = types.NullDuration{}
}

//TODO: move a bunch of the logic above to a config "constructor" and to the Validate() method
if errList := conf.Validate(); len(errList) != 0 {

errMsg := []string{"There were problems with the specified script configuration:"}
for _, err := range errList {
errMsg = append(errMsg, fmt.Sprintf("\t- %s", err.Error()))
}
//TODO: re-enable exiting with validation errors
log.Warn(errors.New(strings.Join(errMsg, "\n")))
//return ExitCode{errors.New(strings.Join(errMsg, "\n")), invalidOptionsErrorCode}
if cerr := validateConfig(conf); cerr != nil {
return ExitCode{cerr, invalidConfigErrorCode}
}

// If summary trend stats are defined, update the UI to reflect them
Expand Down

0 comments on commit 9404af6

Please sign in to comment.