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

k6/execution:Don't panic on accessing test.options #3708

Merged
merged 2 commits into from
Apr 25, 2024
Merged
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
19 changes: 16 additions & 3 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ func (mi *ModuleInstance) newScenarioInfo() (*goja.Object, error) {
return newInfoObj(rt, si)
}

//nolint:lll,gochecknoglobals
var instanceInfoInitContextErr = common.NewInitContextError("getting instance information in the init context is not supported")

// newInstanceInfo returns a goja.Object with property accessors to retrieve
// information about the local instance stats.
func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) {
es := lib.GetExecutionState(mi.vu.Context())
if es == nil {
return nil, errors.New("getting instance information in the init context is not supported")
return nil, instanceInfoInitContextErr
}
rt := mi.vu.Runtime()

Expand All @@ -157,6 +160,9 @@ func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) {
return newInfoObj(rt, ti)
}

//nolint:gochecknoglobals
var testInfoInitContextErr = common.NewInitContextError("getting test options in the init context is not supported")

// newTestInfo returns a goja.Object with property accessors to retrieve
// information and control execution of the overall test run.
func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) {
Expand All @@ -176,8 +182,12 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) {
}
},
"options": func() interface{} {
vuState := mi.vu.State()
if vuState == nil {
common.Throw(rt, testInfoInitContextErr)
}
if optionsObject == nil {
opts, err := optionsAsObject(rt, mi.vu.State().Options)
opts, err := optionsAsObject(rt, vuState.Options)
if err != nil {
common.Throw(rt, err)
}
Expand All @@ -190,12 +200,15 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) {
return newInfoObj(rt, ti)
}

//nolint:gochecknoglobals
var vuInfoInitContextErr = common.NewInitContextError("getting VU information in the init context is not supported")

// newVUInfo returns a goja.Object with property accessors to retrieve
// information about the currently executing VU.
func (mi *ModuleInstance) newVUInfo() (*goja.Object, error) {
vuState := mi.vu.State()
if vuState == nil {
return nil, errors.New("getting VU information in the init context is not supported")
return nil, vuInfoInitContextErr
}
rt := mi.vu.Runtime()

Expand Down
17 changes: 17 additions & 0 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,23 @@ func TestScenarioNoAvailableInInitContext(t *testing.T) {
}
}

func TestOptionsNoAvailableInInitContext(t *testing.T) {
t.Parallel()

rt := goja.New()
m, ok := New().NewModuleInstance(
&modulestest.VU{
RuntimeField: rt,
CtxField: context.Background(),
},
).(*ModuleInstance)
require.True(t, ok)
require.NoError(t, rt.Set("exec", m.Exports().Default))

_, err := rt.RunString("exec.test.options")
require.ErrorContains(t, err, "getting test options in the init context is not supported")
}

func TestVUDefaultDetails(t *testing.T) {
t.Parallel()

Expand Down