Skip to content

Commit

Permalink
Merge 8307029 into 32f80d1
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwoodward authored Oct 5, 2021
2 parents 32f80d1 + 8307029 commit 2f396e2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
15 changes: 12 additions & 3 deletions output/csv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"gopkg.in/guregu/null.v3"

"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"go.k6.io/k6/lib/types"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func (c Config) Apply(cfg Config) Config {
}

// ParseArg takes an arg string and converts it to a config
func ParseArg(arg string) (Config, error) {
func ParseArg(arg string, logger *logrus.Logger) (Config, error) {
c := Config{}

if !strings.Contains(arg, "=") {
Expand All @@ -76,11 +77,17 @@ func ParseArg(arg string) (Config, error) {
}
switch r[0] {
case "save_interval":
logger.Warnf("CSV output argument '%s' is deprecated, please use 'saveInterval' instead.", r[0])
fallthrough
case "saveInterval":
err := c.SaveInterval.UnmarshalText([]byte(r[1]))
if err != nil {
return c, err
}
case "file_name":
logger.Warnf("CSV output argument '%s' is deprecated, please use 'fileName' instead.", r[0])
fallthrough
case "fileName":
c.FileName = null.StringFrom(r[1])
default:
return c, fmt.Errorf("unknown key %q as argument for csv output", r[0])
Expand All @@ -92,7 +99,9 @@ func ParseArg(arg string) (Config, error) {

// GetConsolidatedConfig combines {default config values + JSON config +
// environment vars + arg config values}, and returns the final result.
func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, arg string) (Config, error) {
func GetConsolidatedConfig(
jsonRawConf json.RawMessage, env map[string]string, arg string, logger *logrus.Logger,
) (Config, error) {
result := NewConfig()
if jsonRawConf != nil {
jsonConf := Config{}
Expand All @@ -110,7 +119,7 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, a
result = result.Apply(envConfig)

if arg != "" {
urlConf, err := ParseArg(arg)
urlConf, err := ParseArg(arg, logger)
if err != nil {
return result, err
}
Expand Down
42 changes: 39 additions & 3 deletions output/csv/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import (
"testing"
"time"

"github.com/sirupsen/logrus"

"gopkg.in/guregu/null.v3"

"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"

"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/types"
)

Expand Down Expand Up @@ -77,8 +81,9 @@ func TestApply(t *testing.T) {

func TestParseArg(t *testing.T) {
cases := map[string]struct {
config Config
expectedErr bool
config Config
expectedLogEntries []string
expectedErr bool
}{
"test_file.csv": {
config: Config{
Expand All @@ -90,12 +95,33 @@ func TestParseArg(t *testing.T) {
config: Config{
SaveInterval: types.NullDurationFrom(5 * time.Second),
},
expectedLogEntries: []string{
"CSV output argument 'save_interval' is deprecated, please use 'saveInterval' instead.",
},
},
"saveInterval=5s": {
config: Config{
SaveInterval: types.NullDurationFrom(5 * time.Second),
},
},
"file_name=test.csv,save_interval=5s": {
config: Config{
FileName: null.StringFrom("test.csv"),
SaveInterval: types.NullDurationFrom(5 * time.Second),
},
expectedLogEntries: []string{
"CSV output argument 'file_name' is deprecated, please use 'fileName' instead.",
"CSV output argument 'save_interval' is deprecated, please use 'saveInterval' instead.",
},
},
"fileName=test.csv,save_interval=5s": {
config: Config{
FileName: null.StringFrom("test.csv"),
SaveInterval: types.NullDurationFrom(5 * time.Second),
},
expectedLogEntries: []string{
"CSV output argument 'save_interval' is deprecated, please use 'saveInterval' instead.",
},
},
"filename=test.csv,save_interval=5s": {
expectedErr: true,
Expand All @@ -106,8 +132,11 @@ func TestParseArg(t *testing.T) {
arg := arg
testCase := testCase

testLogger, hook := test.NewNullLogger()
testLogger.SetOutput(testutils.NewTestOutput(t))

t.Run(arg, func(t *testing.T) {
config, err := ParseArg(arg)
config, err := ParseArg(arg, testLogger)

if testCase.expectedErr {
assert.Error(t, err)
Expand All @@ -116,6 +145,13 @@ func TestParseArg(t *testing.T) {
}
assert.Equal(t, testCase.config.FileName.String, config.FileName.String)
assert.Equal(t, testCase.config.SaveInterval.String(), config.SaveInterval.String())

var entries []string
for _, v := range hook.AllEntries() {
assert.Equal(t, v.Level, logrus.WarnLevel)
entries = append(entries, v.Message)
}
assert.ElementsMatch(t, entries, testCase.expectedLogEntries)
})
}
}
10 changes: 5 additions & 5 deletions output/csv/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ func newOutput(params output.Params) (*Output, error) {
sort.Strings(resTags)
sort.Strings(ignoredTags)

config, err := GetConsolidatedConfig(params.JSONConfig, params.Environment, params.ConfigArgument)
logger := params.Logger.WithFields(logrus.Fields{
"output": "csv",
"filename": params.ConfigArgument,
})
config, err := GetConsolidatedConfig(params.JSONConfig, params.Environment, params.ConfigArgument, logger.Logger)
if err != nil {
return nil, err
}

saveInterval := time.Duration(config.SaveInterval.Duration)
fname := config.FileName.String

logger := params.Logger.WithFields(logrus.Fields{
"output": "csv",
"filename": params.ConfigArgument,
})
if fname == "" || fname == "-" {
stdoutWriter := csv.NewWriter(os.Stdout)
return &Output{
Expand Down

0 comments on commit 2f396e2

Please sign in to comment.