Skip to content

Commit

Permalink
Add GetScenarioOptions method to ExecutorConfig interface
Browse files Browse the repository at this point in the history
This simplifies retrieval of ScenarioOptions, since it avoids type
assertions of all possible ExecutorConfig implementations.

See grafana/xk6-browser#858 (comment)
  • Loading branch information
Ivan Mirić committed May 3, 2023
1 parent 698e894 commit 5236fa6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestOptionsTestFull(t *testing.T) {
Tags: map[string]string{
"tagkey": "tagvalue",
},
Options: executor.ScenarioOptions{
Options: lib.ScenarioOptions{
Browser: map[string]any{
"someOption": true,
},
Expand Down
28 changes: 14 additions & 14 deletions lib/executor/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/types"
)
Expand All @@ -20,22 +21,16 @@ var DefaultGracefulStopValue = 30 * time.Second //nolint:gochecknoglobals
var executorNameWhitelist = regexp.MustCompile(`^[0-9a-zA-Z_-]+$`) //nolint:gochecknoglobals
const executorNameErr = "the executor name should contain only numbers, latin letters, underscores, and dashes"

// ScenarioOptions are options specific to a scenario. These include k6 browser
// options, which are validated by the browser module, and not by k6 core.
type ScenarioOptions struct {
Browser map[string]any `json:"browser"`
}

// BaseConfig contains the common config fields for all executors
type BaseConfig struct {
Name string `json:"-"` // set via the JS object key
Type string `json:"executor"`
StartTime types.NullDuration `json:"startTime"`
GracefulStop types.NullDuration `json:"gracefulStop"`
Env map[string]string `json:"env"`
Exec null.String `json:"exec"` // function name, externally validated
Tags map[string]string `json:"tags"`
Options ScenarioOptions `json:"options"`
Name string `json:"-"` // set via the JS object key
Type string `json:"executor"`
StartTime types.NullDuration `json:"startTime"`
GracefulStop types.NullDuration `json:"gracefulStop"`
Env map[string]string `json:"env"`
Exec null.String `json:"exec"` // function name, externally validated
Tags map[string]string `json:"tags"`
Options lib.ScenarioOptions `json:"options"`

// TODO: future extensions like distribution, others?
}
Expand Down Expand Up @@ -116,6 +111,11 @@ func (bc BaseConfig) GetExec() string {
return exec
}

// GetScenarioOptions returns the options specific to a scenario.
func (bc BaseConfig) GetScenarioOptions() *lib.ScenarioOptions {
return &bc.Options
}

// GetTags returns any custom tags configured for the executor.
func (bc BaseConfig) GetTags() map[string]string {
return bc.Tags
Expand Down
7 changes: 7 additions & 0 deletions lib/executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type ExecutorConfig interface {
// execution, including any extensions caused by waiting for iterations to
// finish with graceful stops or ramp-downs.
GetExecutionRequirements(*ExecutionTuple) []ExecutionStep
GetScenarioOptions() *ScenarioOptions

// Return a human-readable description of the executor
GetDescription(*ExecutionTuple) string
Expand All @@ -90,6 +91,12 @@ type ExecutorConfig interface {
HasWork(*ExecutionTuple) bool
}

// ScenarioOptions are options specific to a scenario. These include k6 browser
// options, which are validated by the browser module, and not by k6 core.
type ScenarioOptions struct {
Browser map[string]any `json:"browser"`
}

// ScenarioState holds runtime scenario information returned by the k6/execution
// JS module.
type ScenarioState struct {
Expand Down

0 comments on commit 5236fa6

Please sign in to comment.