diff --git a/lib/options.go b/lib/options.go index 4f013e50652..e4f3a2d6929 100644 --- a/lib/options.go +++ b/lib/options.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "encoding/json" "encoding/pem" + "errors" "fmt" "net" "reflect" @@ -519,7 +520,7 @@ func (o Options) Apply(opts Options) Options { func (o Options) Validate() []error { // TODO: validate all of the other options... that we should have already been validating... // TODO: maybe integrate an external validation lib: https://github.com/avelino/awesome-go#validation - var errors []error + var validationErrors []error if o.ExecutionSegmentSequence != nil { var segmentFound bool for _, segment := range *o.ExecutionSegmentSequence { @@ -529,12 +530,18 @@ func (o Options) Validate() []error { } } if !segmentFound { - errors = append(errors, + validationErrors = append(validationErrors, fmt.Errorf("provided segment %s can't be found in sequence %s", o.ExecutionSegment, o.ExecutionSegmentSequence)) } } - return append(errors, o.Scenarios.Validate()...) + validationErrors = append(validationErrors, o.Scenarios.Validate()...) + + // Duration + if o.SetupTimeout.Valid && o.SetupTimeout.Duration <= 0 { + validationErrors = append(validationErrors, errors.New("setupTimeout must be positive")) + } + return validationErrors } // ForEachSpecified enumerates all struct fields and calls the supplied function with each diff --git a/lib/options_test.go b/lib/options_test.go index 586cfc52324..c7132c4e983 100644 --- a/lib/options_test.go +++ b/lib/options_test.go @@ -789,3 +789,44 @@ func TestHost(t *testing.T) { }) } } + +func TestValidate(t *testing.T) { + t.Parallel() + t.Run("setupTimeout", func(t *testing.T) { + t.Parallel() + testData := []struct { + input string + expectFailure bool + expectOutput types.Duration + }{ + { + input: "1s", + expectFailure: false, + expectOutput: types.Duration(1 * time.Second), + }, + { + input: "0s", + expectFailure: true, + }, + { + input: "-1s", + expectFailure: true, + }, + } + for _, data := range testData { + data := data + t.Run(data.input, func(t *testing.T) { + t.Parallel() + sec, _ := time.ParseDuration(data.input) + opts := Options{}.Apply(Options{SetupTimeout: types.NewNullDuration(sec, true)}) + errorsSlice := opts.Validate() + if data.expectFailure { + assert.Len(t, errorsSlice, 1) + } else { + assert.Len(t, errorsSlice, 0) + assert.Equal(t, time.Duration(opts.SetupTimeout.Duration), sec) + } + }) + } + }) +}