Skip to content

Commit

Permalink
Add executor test to (de)serialize archive and check scenario options
Browse files Browse the repository at this point in the history
I'm not adding it to the lib package, since it could possibly result in
an import loop (lib -> lib/executor -> lib).
  • Loading branch information
Ivan Mirić committed May 3, 2023
1 parent 5236fa6 commit 07762a2
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/executor/executors_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package executor

import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"testing"
"time"

Expand All @@ -11,7 +13,10 @@ import (
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
)

type exp struct {
Expand Down Expand Up @@ -444,3 +449,68 @@ func TestConfigMapParsingAndValidation(t *testing.T) {
})
}
}

func TestArchiveRoundTripScenarioOptions(t *testing.T) {
t.Parallel()
arc1 := &lib.Archive{
Type: "js",
K6Version: consts.Version,
Options: lib.Options{
VUs: null.IntFrom(12345),
SystemTags: &metrics.DefaultSystemTagSet,
Scenarios: map[string]lib.ExecutorConfig{
"const-vus": ConstantVUsConfig{
BaseConfig: BaseConfig{
Name: "const-vus",
Type: "constant-vus",
StartTime: types.NullDurationFrom(10 * time.Second),
GracefulStop: types.NullDurationFrom(30 * time.Second),
Env: map[string]string{
"FOO": "bar",
},
Exec: null.StringFrom("default"),
Tags: map[string]string{
"tagkey": "tagvalue",
},
Options: lib.ScenarioOptions{
Browser: map[string]any{
"someOption": "someValue",
},
},
},
VUs: null.IntFrom(50),
Duration: types.NullDurationFrom(10 * time.Minute),
},
},
},
FilenameURL: &url.URL{Scheme: "file", Path: "/path/to/a.js"},
Data: []byte(`// a contents`),
PwdURL: &url.URL{Scheme: "file", Path: "/path/to"},
Filesystems: map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"/path/to/a.js": []byte(`// a contents`),
}),
},
}

buf := bytes.NewBuffer(nil)
require.NoError(t, arc1.Write(buf))

arc2, err := lib.ReadArchive(buf)
require.NoError(t, err)

scenario, ok := arc2.Options.Scenarios["const-vus"]
require.True(t, ok, "scenario const-vus not found in archive options")
scOpts := scenario.GetScenarioOptions()
require.NotNil(t, scOpts, "options for scenario const-vus must not be nil")
assert.Equal(t, "someValue", scOpts.Browser["someOption"])
}

// copied from lib/archive_test.go
func makeMemMapFs(t *testing.T, input map[string][]byte) fsext.Fs {
fs := fsext.NewMemMapFs()
for path, data := range input {
require.NoError(t, fsext.WriteFile(fs, path, data, 0o644))
}
return fs
}

0 comments on commit 07762a2

Please sign in to comment.