Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate setupTimeout option #3898

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 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 @@ -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 errorsSlice []error
olegbespalov marked this conversation as resolved.
Show resolved Hide resolved
if o.ExecutionSegmentSequence != nil {
var segmentFound bool
for _, segment := range *o.ExecutionSegmentSequence {
Expand All @@ -529,12 +530,18 @@ func (o Options) Validate() []error {
}
}
if !segmentFound {
errors = append(errors,
errorsSlice = append(errorsSlice,
fmt.Errorf("provided segment %s can't be found in sequence %s",
o.ExecutionSegment, o.ExecutionSegmentSequence))
}
}
return append(errors, o.Scenarios.Validate()...)
errorsSlice = append(errorsSlice, o.Scenarios.Validate()...)

// Duration
if o.SetupTimeout.Valid && o.SetupTimeout.Duration <= 0 {
errorsSlice = append(errorsSlice, errors.New("setupTimeout must be positive"))
}
return errorsSlice
}

// ForEachSpecified enumerates all struct fields and calls the supplied function with each
Expand Down
41 changes: 41 additions & 0 deletions lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
})
}