Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion internal/cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func TestExecutionTestOptionsDefaultValues(t *testing.T) {
loglines := ts.LoggerHook.Drain()
require.Len(t, loglines, 1)

expected := `{"paused":null,"executionSegment":null,"executionSegmentSequence":null,"noSetup":null,"setupTimeout":null,"noTeardown":null,"teardownTimeout":null,"rps":null,"dns":{"ttl":null,"select":null,"policy":null},"maxRedirects":null,"userAgent":null,"batch":null,"batchPerHost":null,"httpDebug":null,"insecureSkipTLSVerify":null,"tlsCipherSuites":null,"tlsVersion":null,"tlsAuth":null,"throw":null,"thresholds":null,"blacklistIPs":null,"blockHostnames":null,"hosts":null,"noConnectionReuse":null,"noVUConnectionReuse":null,"minIterationDuration":null,"ext":null,"summaryTrendStats":["avg", "min", "med", "max", "p(90)", "p(95)"],"summaryTimeUnit":null,"systemTags":["check","error","error_code","expected_response","group","method","name","proto","scenario","service","status","subproto","tls_version","url"],"tags":null,"metricSamplesBufferSize":null,"noCookiesReset":null,"discardResponseBodies":null,"consoleOutput":null,"scenarios":{"default":{"vus":null,"iterations":1,"executor":"shared-iterations","maxDuration":null,"startTime":null,"env":null,"tags":null,"gracefulStop":null,"exec":null}},"localIPs":null}`
expected := `{"paused":null,"executionSegment":null,"executionSegmentSequence":null,"noSetup":null,"setupTimeout":null,"noTeardown":null,"teardownTimeout":null,"rps":null,"dns":{"ttl":null,"select":null,"policy":null},"maxRedirects":null,"userAgent":null,"batch":null,"batchPerHost":null,"httpDebug":null,"insecureSkipTLSVerify":null,"tlsCipherSuites":null,"tlsVersion":null,"tlsAuth":null,"throw":null,"thresholds":null,"blacklistIPs":null,"blockHostnames":null,"hosts":null,"noConnectionReuse":null,"noVUConnectionReuse":null,"minIterationDuration":null,"ext":null,"summaryTrendStats":["avg", "min", "med", "max", "p(90)", "p(95)"],"summaryTimeUnit":null,"systemTags":["check","error","error_code","expected_response","group","method","name","proto","scenario","service","status","subproto","tls_version","url"],"tags":null,"metricSamplesBufferSize":null,"noCookiesReset":null,"discardResponseBodies":null,"consoleOutput":null,"scenarios":{"default":{"vus":null,"iterations":1,"executor":"shared-iterations","maxDuration":null,"startTime":null,"env":null,"tags":null,"gracefulStop":null,"exec":null, "userInit":null}},"localIPs":null}`
assert.JSONEq(t, expected, loglines[0].Message)
}

Expand Down
4 changes: 0 additions & 4 deletions internal/js/modules/k6/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ const asyncFunctionNotSupportedMsg = "SharedArray constructor does not support a
func (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {
rt := d.vu.Runtime()

if d.vu.State() != nil {
common.Throw(rt, errors.New("new SharedArray must be called in the init context"))
}

name := call.Argument(0).String()
if name == "" {
common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))
Expand Down
6 changes: 4 additions & 2 deletions internal/js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestFailTest(t *testing.T) {
func TestOptionsTestFull(t *testing.T) {
t.Parallel()

expected := `{"paused":true,"scenarios":{"const-vus":{"executor":"constant-vus","options":{"browser":{"someOption":true}},"startTime":"10s","gracefulStop":"30s","env":{"FOO":"bar"},"exec":"default","tags":{"tagkey":"tagvalue"},"vus":50,"duration":"10m0s"}},"executionSegment":"0:1/4","executionSegmentSequence":"0,1/4,1/2,1","noSetup":true,"setupTimeout":"1m0s","noTeardown":true,"teardownTimeout":"5m0s","rps":100,"dns":{"ttl":"1m","select":"roundRobin","policy":"any"},"maxRedirects":3,"userAgent":"k6-user-agent","batch":15,"batchPerHost":5,"httpDebug":"full","insecureSkipTLSVerify":true,"tlsCipherSuites":["TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"],"tlsVersion":{"min":"tls1.2","max":"tls1.3"},"tlsAuth":[{"domains":["example.com"],"cert":"mycert.pem","key":"mycert-key.pem","password":"mypwd"}],"throw":true,"thresholds":{"http_req_duration":[{"threshold":"rate>0.01","abortOnFail":true,"delayAbortEval":"10s"}]},"blacklistIPs":["192.0.2.0/24"],"blockHostnames":["test.k6.io","*.example.com"],"hosts":{"test.k6.io":"1.2.3.4:8443"},"noConnectionReuse":true,"noVUConnectionReuse":true,"minIterationDuration":"10s","ext":{"ext-one":{"rawkey":"rawvalue"}},"summaryTrendStats":["avg","min","max"],"summaryTimeUnit":"ms","systemTags":["iter","vu"],"tags":null,"metricSamplesBufferSize":8,"noCookiesReset":true,"discardResponseBodies":true,"consoleOutput":"loadtest.log","tags":{"runtag-key":"runtag-value"},"localIPs":"192.168.20.12-192.168.20.15,192.168.10.0/27"}`
expected := `{"paused":true,"scenarios":{"const-vus":{"executor":"constant-vus","options":{"browser":{"someOption":true}},"startTime":"10s","gracefulStop":"30s","env":{"FOO":"bar"},"exec":"default","userInit":"userInit","tags":{"tagkey":"tagvalue"},"vus":50,"duration":"10m0s"}},"executionSegment":"0:1/4","executionSegmentSequence":"0,1/4,1/2,1","noSetup":true,"setupTimeout":"1m0s","noTeardown":true,"teardownTimeout":"5m0s","rps":100,"dns":{"ttl":"1m","select":"roundRobin","policy":"any"},"maxRedirects":3,"userAgent":"k6-user-agent","batch":15,"batchPerHost":5,"httpDebug":"full","insecureSkipTLSVerify":true,"tlsCipherSuites":["TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"],"tlsVersion":{"min":"tls1.2","max":"tls1.3"},"tlsAuth":[{"domains":["example.com"],"cert":"mycert.pem","key":"mycert-key.pem","password":"mypwd"}],"throw":true,"thresholds":{"http_req_duration":[{"threshold":"rate>0.01","abortOnFail":true,"delayAbortEval":"10s"}]},"blacklistIPs":["192.0.2.0/24"],"blockHostnames":["test.k6.io","*.example.com"],"hosts":{"test.k6.io":"1.2.3.4:8443"},"noConnectionReuse":true,"noVUConnectionReuse":true,"minIterationDuration":"10s","ext":{"ext-one":{"rawkey":"rawvalue"}},"summaryTrendStats":["avg","min","max"],"summaryTimeUnit":"ms","systemTags":["iter","vu"],"tags":null,"metricSamplesBufferSize":8,"noCookiesReset":true,"discardResponseBodies":true,"consoleOutput":"loadtest.log","tags":{"runtag-key":"runtag-value"},"localIPs":"192.168.20.12-192.168.20.15,192.168.10.0/27"}`

var (
rt = sobek.New()
Expand All @@ -304,7 +304,8 @@ func TestOptionsTestFull(t *testing.T) {
Env: map[string]string{
"FOO": "bar",
},
Exec: null.StringFrom("default"),
Exec: null.StringFrom("default"),
UserInit: null.StringFrom("userInit"),
Tags: map[string]string{
"tagkey": "tagvalue",
},
Expand Down Expand Up @@ -448,6 +449,7 @@ func TestOptionsTestFull(t *testing.T) {
).(*ModuleInstance)
require.True(t, ok)
require.NoError(t, rt.Set("exec", m.Exports().Default))
require.NoError(t, rt.Set("userInit", "userInit"))

opts, err := rt.RunString(`JSON.stringify(exec.test.options)`)
require.NoError(t, err)
Expand Down
16 changes: 15 additions & 1 deletion internal/js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
if params.Exec == "" {
params.Exec = consts.DefaultFn
}
// This case actually never happens in normal operation but only in tests.
if params.UserInit == "" {
params.UserInit = consts.UserInitFn
}

// Override the preset global env with any custom env vars
env := make(map[string]string, len(u.env)+len(params.Env))
Expand Down Expand Up @@ -817,7 +821,17 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
params.DeactivateCallback(u)
}
})

userInit := avu.getCallableExport(params.UserInit)
if userInit != nil {
_, err := userInit(sobek.Undefined()) // Actually run the JS script
if err != nil {
u.state.Logger.Errorf("error running userInit: %s", err)
} else {
u.state.Logger.Debug("userInit ran successfully")
}
} else {
u.state.Logger.Debug("no userInit function found")
}
return avu
}

Expand Down
Loading