Skip to content

Commit

Permalink
add: setuptimeout option test
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukasaI committed Aug 31, 2024
1 parent afd45b6 commit 8d31633
Showing 1 changed file with 85 additions and 34 deletions.
119 changes: 85 additions & 34 deletions lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,39 @@ func TestOptions(t *testing.T) {
t.Parallel()
t.Run("Paused", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Paused: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{Paused: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.Paused.Valid)
assert.True(t, opts.Paused.Bool)
})
t.Run("VUs", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{VUs: null.IntFrom(12345)})
opts, err := Options{}.Apply(Options{VUs: null.IntFrom(12345)})
assert.Nil(t, err)
assert.True(t, opts.VUs.Valid)
assert.Equal(t, int64(12345), opts.VUs.Int64)
})
t.Run("Duration", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Duration: types.NullDurationFrom(2 * time.Minute)})
opts, err := Options{}.Apply(Options{Duration: types.NullDurationFrom(2 * time.Minute)})
assert.Nil(t, err)
assert.True(t, opts.Duration.Valid)
assert.Equal(t, "2m0s", opts.Duration.String())
})
t.Run("Iterations", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Iterations: null.IntFrom(1234)})
opts, err := Options{}.Apply(Options{Iterations: null.IntFrom(1234)})
assert.Nil(t, err)
assert.True(t, opts.Iterations.Valid)
assert.Equal(t, int64(1234), opts.Iterations.Int64)
})
t.Run("Stages", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Stages: []Stage{
opts, err := Options{}.Apply(Options{Stages: []Stage{
{Duration: types.NullDurationFrom(1 * time.Second), Target: null.IntFrom(10)},
{Duration: types.NullDurationFrom(2 * time.Second), Target: null.IntFrom(20)},
}})
assert.Nil(t, err)
assert.NotNil(t, opts.Stages)
assert.Len(t, opts.Stages, 2)
assert.Equal(t, 1*time.Second, opts.Stages[0].Duration.TimeDuration())
Expand All @@ -57,7 +62,8 @@ func TestOptions(t *testing.T) {
assert.Equal(t, int64(20), opts.Stages[1].Target.Int64)

emptyStages := []Stage{}
emptyStagesOpts, _ := Options{}.Apply(Options{Stages: []Stage{{}}})
emptyStagesOpts, err := Options{}.Apply(Options{Stages: []Stage{{}}})
assert.Nil(t, err)
assert.Equal(t, emptyStages, emptyStagesOpts.Stages)
emptyStagesOpts, _ = Options{}.Apply(Options{Stages: []Stage{}})
assert.Equal(t, emptyStages, emptyStagesOpts.Stages)
Expand All @@ -66,58 +72,68 @@ func TestOptions(t *testing.T) {
emptyStagesOpts, _ = opts.Apply(Options{Stages: []Stage{{}}})
assert.Equal(t, emptyStages, emptyStagesOpts.Stages)

applyiedOpt, _ := opts.Apply(opts)
applyiedOpt, err := opts.Apply(opts)
assert.Nil(t, err)
assert.Equal(t, opts.Stages, applyiedOpt.Stages)

oneStage := []Stage{{Duration: types.NullDurationFrom(5 * time.Second), Target: null.IntFrom(50)}}
oneStageAppliedOpt, _ := opts.Apply(Options{Stages: oneStage})
oneStageAppliedOpt, err := opts.Apply(Options{Stages: oneStage})
assert.Nil(t, err)
assert.Equal(t, oneStage, oneStageAppliedOpt.Stages)

pipedAppliedOpt, _ := Options{}.Apply(opts)
pipedAppliedOpt, err := Options{}.Apply(opts)
assert.Nil(t, err)
pipedAppliedOpt, _ = pipedAppliedOpt.Apply(Options{Stages: oneStage})
pipedAppliedOpt, _ = pipedAppliedOpt.Apply(Options{Stages: oneStage})
assert.Equal(t, oneStage, pipedAppliedOpt.Stages)
})
// Execution overwriting is tested by the config consolidation test in cmd
t.Run("RPS", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{RPS: null.IntFrom(12345)})
opts, err := Options{}.Apply(Options{RPS: null.IntFrom(12345)})
assert.Nil(t, err)
assert.True(t, opts.RPS.Valid)
assert.Equal(t, int64(12345), opts.RPS.Int64)
})
t.Run("MaxRedirects", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{MaxRedirects: null.IntFrom(12345)})
opts, err := Options{}.Apply(Options{MaxRedirects: null.IntFrom(12345)})
assert.Nil(t, err)
assert.True(t, opts.MaxRedirects.Valid)
assert.Equal(t, int64(12345), opts.MaxRedirects.Int64)
})
t.Run("UserAgent", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{UserAgent: null.StringFrom("foo")})
opts, err := Options{}.Apply(Options{UserAgent: null.StringFrom("foo")})
assert.Nil(t, err)
assert.True(t, opts.UserAgent.Valid)
assert.Equal(t, "foo", opts.UserAgent.String)
})
t.Run("Batch", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Batch: null.IntFrom(12345)})
opts, err := Options{}.Apply(Options{Batch: null.IntFrom(12345)})
assert.Nil(t, err)
assert.True(t, opts.Batch.Valid)
assert.Equal(t, int64(12345), opts.Batch.Int64)
})
t.Run("BatchPerHost", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{BatchPerHost: null.IntFrom(12345)})
opts, err := Options{}.Apply(Options{BatchPerHost: null.IntFrom(12345)})
assert.Nil(t, err)
assert.True(t, opts.BatchPerHost.Valid)
assert.Equal(t, int64(12345), opts.BatchPerHost.Int64)
})
t.Run("HTTPDebug", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{HTTPDebug: null.StringFrom("foo")})
opts, err := Options{}.Apply(Options{HTTPDebug: null.StringFrom("foo")})
assert.Nil(t, err)
assert.True(t, opts.HTTPDebug.Valid)
assert.Equal(t, "foo", opts.HTTPDebug.String)
})
t.Run("InsecureSkipTLSVerify", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{InsecureSkipTLSVerify: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{InsecureSkipTLSVerify: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.InsecureSkipTLSVerify.Valid)
assert.True(t, opts.InsecureSkipTLSVerify.Bool)
})
Expand All @@ -127,7 +143,8 @@ func TestOptions(t *testing.T) {
suiteName, suiteID := suiteName, suiteID
t.Run(suiteName, func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{TLSCipherSuites: &TLSCipherSuites{suiteID}})
opts, err := Options{}.Apply(Options{TLSCipherSuites: &TLSCipherSuites{suiteID}})
assert.Nil(t, err)

assert.NotNil(t, opts.TLSCipherSuites)
assert.Len(t, *(opts.TLSCipherSuites), 1)
Expand Down Expand Up @@ -171,7 +188,8 @@ func TestOptions(t *testing.T) {
t.Run("TLSVersion", func(t *testing.T) {
t.Parallel()
versions := TLSVersions{Min: tls.VersionSSL30, Max: tls.VersionTLS12}
opts, _ := Options{}.Apply(Options{TLSVersion: &versions})
opts, err := Options{}.Apply(Options{TLSVersion: &versions})
assert.Nil(t, err)

assert.NotNil(t, opts.TLSVersion)
assert.Equal(t, opts.TLSVersion.Min, TLSVersion(tls.VersionSSL30))
Expand Down Expand Up @@ -275,7 +293,8 @@ func TestOptions(t *testing.T) {
"-----END EC PRIVATE KEY-----",
}, nil},
}
opts, _ := Options{}.Apply(Options{TLSAuth: tlsAuth})
opts, err := Options{}.Apply(Options{TLSAuth: tlsAuth})
assert.Nil(t, err)
assert.Equal(t, tlsAuth, opts.TLSAuth)

t.Run("Roundtrip", func(t *testing.T) {
Expand Down Expand Up @@ -390,7 +409,8 @@ func TestOptions(t *testing.T) {
Password: null.StringFrom(tc.password),
}, nil},
}
opts, _ := Options{}.Apply(Options{TLSAuth: tlsAuth})
opts, err := Options{}.Apply(Options{TLSAuth: tlsAuth})
assert.Nil(t, err)
assert.Equal(t, tlsAuth, opts.TLSAuth)

t.Run("Roundtrip", func(t *testing.T) {
Expand All @@ -411,32 +431,36 @@ func TestOptions(t *testing.T) {
})
t.Run("NoConnectionReuse", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{NoConnectionReuse: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{NoConnectionReuse: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.NoConnectionReuse.Valid)
assert.True(t, opts.NoConnectionReuse.Bool)
})
t.Run("NoVUConnectionReuse", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{NoVUConnectionReuse: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{NoVUConnectionReuse: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.NoVUConnectionReuse.Valid)
assert.True(t, opts.NoVUConnectionReuse.Bool)
})
t.Run("NoCookiesReset", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{NoCookiesReset: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{NoCookiesReset: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.NoCookiesReset.Valid)
assert.True(t, opts.NoCookiesReset.Bool)
})
t.Run("BlacklistIPs", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{
opts, err := Options{}.Apply(Options{
BlacklistIPs: []*IPNet{{
IPNet: net.IPNet{
IP: net.IPv4bcast,
Mask: net.CIDRMask(31, 32),
},
}},
})
assert.Nil(t, err)
assert.NotNil(t, opts.BlacklistIPs)
assert.NotEmpty(t, opts.BlacklistIPs)
assert.Equal(t, net.IPv4bcast, opts.BlacklistIPs[0].IP)
Expand All @@ -459,7 +483,8 @@ func TestOptions(t *testing.T) {
t.Parallel()
blockedHostnames, err := types.NewNullHostnameTrie([]string{"test.k6.io", "*valid.pattern"})
require.NoError(t, err)
opts, _ := Options{}.Apply(Options{BlockedHostnames: blockedHostnames})
opts, err := Options{}.Apply(Options{BlockedHostnames: blockedHostnames})
assert.Nil(t, err)
assert.NotNil(t, opts.BlockedHostnames)
assert.Equal(t, blockedHostnames, opts.BlockedHostnames)
})
Expand All @@ -473,7 +498,8 @@ func TestOptions(t *testing.T) {
"test.loadimpact.com": *host,
})
assert.NoError(t, err)
opts, _ := Options{}.Apply(Options{Hosts: hosts})
opts, err := Options{}.Apply(Options{Hosts: hosts})
assert.Nil(t, err)
assert.NotNil(t, opts.Hosts)
assert.NotEmpty(t, opts.Hosts)

Expand All @@ -482,25 +508,28 @@ func TestOptions(t *testing.T) {

t.Run("Throws", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Throw: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{Throw: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.Throw.Valid)
assert.Equal(t, true, opts.Throw.Bool)
})

t.Run("Thresholds", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{Thresholds: map[string]metrics.Thresholds{
opts, err := Options{}.Apply(Options{Thresholds: map[string]metrics.Thresholds{
"metric": {
Thresholds: []*metrics.Threshold{{}},
},
}})
assert.Nil(t, err)
assert.NotNil(t, opts.Thresholds)
assert.NotEmpty(t, opts.Thresholds)
})
t.Run("External", func(t *testing.T) {
t.Parallel()
ext := map[string]json.RawMessage{"a": json.RawMessage("1")}
opts, _ := Options{}.Apply(Options{External: ext})
opts, err := Options{}.Apply(Options{External: ext})
assert.Nil(t, err)
assert.Equal(t, ext, opts.External)
})

Expand All @@ -514,7 +543,8 @@ func TestOptions(t *testing.T) {
})
t.Run("SystemTags", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{SystemTags: metrics.NewSystemTagSet(metrics.TagProto)})
opts, err := Options{}.Apply(Options{SystemTags: metrics.NewSystemTagSet(metrics.TagProto)})
assert.Nil(t, err)
assert.NotNil(t, opts.SystemTags)
assert.NotEmpty(t, opts.SystemTags)
assert.True(t, opts.SystemTags.Has(metrics.TagProto))
Expand Down Expand Up @@ -550,18 +580,21 @@ func TestOptions(t *testing.T) {
t.Run("SummaryTrendStats", func(t *testing.T) {
t.Parallel()
stats := []string{"myStat1", "myStat2"}
opts, _ := Options{}.Apply(Options{SummaryTrendStats: stats})
opts, err := Options{}.Apply(Options{SummaryTrendStats: stats})
assert.Nil(t, err)
assert.Equal(t, stats, opts.SummaryTrendStats)
})
t.Run("RunTags", func(t *testing.T) {
t.Parallel()
tags := map[string]string{"myTag": "hello"}
opts, _ := Options{}.Apply(Options{RunTags: tags})
opts, err := Options{}.Apply(Options{RunTags: tags})
assert.Nil(t, err)
assert.Equal(t, tags, opts.RunTags)
})
t.Run("DiscardResponseBodies", func(t *testing.T) {
t.Parallel()
opts, _ := Options{}.Apply(Options{DiscardResponseBodies: null.BoolFrom(true)})
opts, err := Options{}.Apply(Options{DiscardResponseBodies: null.BoolFrom(true)})
assert.Nil(t, err)
assert.True(t, opts.DiscardResponseBodies.Valid)
assert.True(t, opts.DiscardResponseBodies.Bool)
})
Expand All @@ -570,9 +603,27 @@ func TestOptions(t *testing.T) {
clientIPRanges := types.NullIPPool{}
err := clientIPRanges.UnmarshalText([]byte("129.112.232.12,123.12.0.0/32"))
require.NoError(t, err)
opts, _ := Options{}.Apply(Options{LocalIPs: clientIPRanges})
opts, err := Options{}.Apply(Options{LocalIPs: clientIPRanges})
assert.Nil(t, err)
assert.NotNil(t, opts.LocalIPs)
})
t.Run("SetupTimeout", func(t *testing.T) {
t.Parallel()
t.Run("Positive Duration", func(t *testing.T) {
opts, err := Options{}.Apply(Options{SetupTimeout: types.NewNullDuration(1*time.Second, true)})
assert.Nil(t, err)
assert.True(t, opts.SetupTimeout.Valid)
assert.Equal(t, types.Duration(1*time.Second), opts.SetupTimeout.Duration)
})
t.Run("0 Duration", func(t *testing.T) {
_, err := Options{}.Apply(Options{SetupTimeout: types.NewNullDuration(0*time.Second, true)})
assert.NotNil(t, err)
})
t.Run("Negative Duration", func(t *testing.T) {
_, err := Options{}.Apply(Options{SetupTimeout: types.NewNullDuration(-1*time.Second, true)})
assert.NotNil(t, err)
})
})
}

func TestOptionsEnv(t *testing.T) {
Expand Down

0 comments on commit 8d31633

Please sign in to comment.