Skip to content

Commit

Permalink
update: validate setTimeout value
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukasaI committed Aug 31, 2024
1 parent 600661b commit afd45b6
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 67 deletions.
4 changes: 3 additions & 1 deletion api/v1/group_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func getTestPreInitState(tb testing.TB) *lib.TestPreInitState {
}

func getTestRunState(tb testing.TB, options lib.Options, runner lib.Runner) *lib.TestRunState {
require.NoError(tb, runner.SetOptions(runner.GetOptions().Apply(options)))
opt, _ := runner.GetOptions().Apply(options)
require.NoError(tb, runner.SetOptions(opt))

piState := getTestPreInitState(tb)
return &lib.TestRunState{
TestPreInitState: piState,
Expand Down
30 changes: 24 additions & 6 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ func (c Config) Validate() []error {
}

// Apply the provided config on top of the current one, returning a new one. The provided config has priority.
func (c Config) Apply(cfg Config) Config {
c.Options = c.Options.Apply(cfg.Options)
func (c Config) Apply(cfg Config) (Config, error) {
options, err := c.Options.Apply(cfg.Options)
if err != nil {
return c, err
}
c.Options = options
if len(cfg.Out) > 0 {
c.Out = cfg.Out
}
Expand All @@ -74,7 +78,7 @@ func (c Config) Apply(cfg Config) Config {
if len(cfg.Collectors) > 0 {
c.Collectors = cfg.Collectors
}
return c
return c, nil
}

// Returns a Config but only parses the Options inside.
Expand Down Expand Up @@ -177,11 +181,25 @@ func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib
return conf, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}

conf = cliConf.Apply(fileConf)
conf, err = cliConf.Apply(fileConf)
if err != nil {
return conf, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}

conf = conf.Apply(Config{Options: runnerOpts})
conf, err = conf.Apply(Config{Options: runnerOpts})
if err != nil {
return conf, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}

conf = conf.Apply(envConf).Apply(cliConf)
conf, err = conf.Apply(envConf)
if err != nil {
return conf, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}

conf, err = conf.Apply(cliConf)
if err != nil {
return conf, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig)
}
conf = applyDefault(conf)

// TODO(imiric): Move this validation where it makes sense in the configuration
Expand Down
8 changes: 4 additions & 4 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,20 @@ func TestConfigApply(t *testing.T) {
t.Parallel()
t.Run("Linger", func(t *testing.T) {
t.Parallel()
conf := Config{}.Apply(Config{Linger: null.BoolFrom(true)})
conf, _ := Config{}.Apply(Config{Linger: null.BoolFrom(true)})
assert.Equal(t, null.BoolFrom(true), conf.Linger)
})
t.Run("NoUsageReport", func(t *testing.T) {
t.Parallel()
conf := Config{}.Apply(Config{NoUsageReport: null.BoolFrom(true)})
conf, _ := Config{}.Apply(Config{NoUsageReport: null.BoolFrom(true)})
assert.Equal(t, null.BoolFrom(true), conf.NoUsageReport)
})
t.Run("Out", func(t *testing.T) {
t.Parallel()
conf := Config{}.Apply(Config{Out: []string{"influxdb"}})
conf, _ := Config{}.Apply(Config{Out: []string{"influxdb"}})
assert.Equal(t, []string{"influxdb"}, conf.Out)

conf = Config{}.Apply(Config{Out: []string{"influxdb", "json"}})
conf, _ = Config{}.Apply(Config{Out: []string{"influxdb", "json"}})
assert.Equal(t, []string{"influxdb", "json"}, conf.Out)
})
}
Expand Down
19 changes: 13 additions & 6 deletions execution/scheduler_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ func newTestScheduler(
runner = &minirunner.MiniRunner{}
}
ctx, cancel = context.WithCancel(context.Background())
newOpts, err := executor.DeriveScenariosFromShortcuts(lib.Options{

bufferSizeOpt, _ := lib.Options{
MetricSamplesBufferSize: null.NewInt(200, false),
}.Apply(runner.GetOptions()).Apply(opts), nil)
}.Apply(runner.GetOptions())
appliedOpt, _ := bufferSizeOpt.Apply(opts)
newOpts, err := executor.DeriveScenariosFromShortcuts(appliedOpt, nil)
require.NoError(t, err)

testRunState := getTestRunState(t, getTestPreInitState(t), newOpts, runner)
Expand Down Expand Up @@ -328,9 +331,11 @@ func TestSchedulerSystemTags(t *testing.T) {
}, nil)
require.NoError(t, err)

require.NoError(t, runner.SetOptions(runner.GetOptions().Apply(lib.Options{
opt, _ := runner.GetOptions().Apply(lib.Options{
SystemTags: &metrics.DefaultSystemTagSet,
})))
})

require.NoError(t, runner.SetOptions(opt))

testRunState := getTestRunState(t, piState, runner.GetOptions(), runner)
execScheduler, err := execution.NewScheduler(testRunState, local.NewController())
Expand Down Expand Up @@ -1191,13 +1196,15 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
runner, err := js.New(piState, &loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: script}, nil)
require.NoError(t, err)

options, err := executor.DeriveScenariosFromShortcuts(runner.GetOptions().Apply(lib.Options{
appliedOpt, _ := runner.GetOptions().Apply(lib.Options{
Iterations: null.IntFrom(2),
VUs: null.IntFrom(1),
SystemTags: &metrics.DefaultSystemTagSet,
SetupTimeout: types.NullDurationFrom(4 * time.Second),
TeardownTimeout: types.NullDurationFrom(4 * time.Second),
}), nil)
})

options, err := executor.DeriveScenariosFromShortcuts(appliedOpt, nil)
require.NoError(t, err)

testRunState := getTestRunState(t, piState, options, runner)
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/http/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestTLS13Support(t *testing.T) {

// We don't expect any failed requests
state.Options.Throw = null.BoolFrom(true)
state.Options.Apply(lib.Options{TLSVersion: &lib.TLSVersions{Max: tls.VersionTLS13}})
_, _ = state.Options.Apply(lib.Options{TLSVersion: &lib.TLSVersions{Max: tls.VersionTLS13}})

_, err := ts.runtime.VU.Runtime().RunString(ts.tb.Replacer.Replace(`
var resp = http.get("HTTPSBIN_URL/tls-version");
Expand Down
15 changes: 9 additions & 6 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@ func TestVURunContext(t *testing.T) {
exports.default = function() { fn(); }
`)
require.NoError(t, err)
require.NoError(t, r1.SetOptions(r1.GetOptions().Apply(lib.Options{Throw: null.BoolFrom(true)})))
opt, _ := r1.GetOptions().Apply(lib.Options{Throw: null.BoolFrom(true)})
require.NoError(t, r1.SetOptions(opt))

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down Expand Up @@ -975,7 +976,8 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
exports.default = function() { http.get("https://mybadssl.localhost/"); }
`)
require.NoError(t, err)
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)))
opt, _ := lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)
require.NoError(t, r1.SetOptions(opt))

r1.Bundle.Options.Hosts, err = types.NewNullHosts(map[string]types.Host{
"mybadssl.localhost": *mybadsslHostname,
Expand Down Expand Up @@ -1312,8 +1314,8 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
`)
require.NoError(t, err)

opts := lib.Options{Throw: null.BoolFrom(true)}
require.NoError(t, r1.SetOptions(opts.Apply(data.opts)))
opts, _ := lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)
require.NoError(t, r1.SetOptions(opts))

r1.Bundle.Options.Hosts, err = types.NewNullHosts(map[string]types.Host{
"sha256-badssl.localhost": *mybadsslHostname,
Expand Down Expand Up @@ -2199,12 +2201,13 @@ func TestSystemTags(t *testing.T) {
exports.noop = function() {};
`), lib.RuntimeOptions{CompatibilityMode: null.StringFrom("base")})
require.NoError(t, err)
require.NoError(t, r.SetOptions(r.GetOptions().Apply(lib.Options{
opt, _ := r.GetOptions().Apply(lib.Options{
Throw: null.BoolFrom(false),
TLSVersion: &lib.TLSVersions{Max: tls.VersionTLS13},
SystemTags: metrics.ToSystemTagSet([]string{tc.tag}),
InsecureSkipTLSVerify: null.BoolFrom(true),
})))
})
require.NoError(t, r.SetOptions(opt))

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
6 changes: 4 additions & 2 deletions lib/executor/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ func setupExecutorTest(
et, err := lib.NewExecutionTuple(segment, &sequence)
require.NoError(t, err)

options := lib.Options{
segmentOpt, _ := lib.Options{
ExecutionSegment: segment,
ExecutionSegmentSequence: &sequence,
}.Apply(runner.GetOptions()).Apply(extraOptions)
}.Apply(runner.GetOptions())

options, _ := segmentOpt.Apply(extraOptions)

testRunState := getTestRunState(t, options, runner)

Expand Down
9 changes: 6 additions & 3 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net"
"reflect"
Expand Down Expand Up @@ -353,7 +354,7 @@ type Options struct {
// a.Apply(b) // Options{VUs: null.IntFrom(5)}
//
//nolint:funlen,gocognit,cyclop
func (o Options) Apply(opts Options) Options {
func (o Options) Apply(opts Options) (Options, error) {
if opts.Paused.Valid {
o.Paused = opts.Paused
}
Expand Down Expand Up @@ -406,7 +407,9 @@ func (o Options) Apply(opts Options) Options {
if opts.NoSetup.Valid {
o.NoSetup = opts.NoSetup
}
if opts.SetupTimeout.Valid {
if opts.SetupTimeout.Valid && opts.SetupTimeout.Duration <= 0 {
return o, errors.New("setupTimeout must be positive")
} else if opts.SetupTimeout.Valid {
o.SetupTimeout = opts.SetupTimeout
}
if opts.NoTeardown.Valid {
Expand Down Expand Up @@ -512,7 +515,7 @@ func (o Options) Apply(opts Options) Options {
o.DNS.Policy = opts.DNS.Policy
}

return o
return o, nil
}

// Validate checks if all of the specified options make sense
Expand Down
Loading

0 comments on commit afd45b6

Please sign in to comment.