diff --git a/cmd/archive_test.go b/cmd/archive_test.go index dee80b726660..f12c22e1e671 100644 --- a/cmd/archive_test.go +++ b/cmd/archive_test.go @@ -128,6 +128,110 @@ func TestArchiveContainsEnv(t *testing.T) { require.Equal(t, "ipsum", metadata.Env["ENV2"]) } +func TestArchiveContainsLegacyCloudSettings(t *testing.T) { + t.Parallel() + + // given a script with the cloud options + fileName := "script.js" + testScript := []byte(` + export let options = { + ext: { + loadimpact: { + distribution: { + one: { loadZone: 'amazon:us:ashburn', percent: 30 }, + two: { loadZone: 'amazon:ie:dublin', percent: 70 }, + }, + }, + }, + }; + export default function () {} + `) + ts := tests.NewGlobalTestState(t) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644)) + + // when we do archiving + ts.CmdArgs = []string{"k6", "archive", fileName} + + newRootCommand(ts.GlobalState).execute() + require.NoError(t, untar(t, ts.FS, "archive.tar", "tmp/")) + + data, err := fsext.ReadFile(ts.FS, "tmp/metadata.json") + require.NoError(t, err) + + // we just need some basic struct + metadata := struct { + Options struct { + Ext struct { + LoadImpact struct { + Distribution map[string]struct { + LoadZone string `json:"loadZone"` + Percent float64 `json:"percent"` + } `json:"distribution"` + } `json:"loadimpact"` + } `json:"ext"` + } `json:"options"` + }{} + + // then unpacked metadata should contain a ext.loadimpact struct the proper values + require.NoError(t, json.Unmarshal(data, &metadata)) + require.Len(t, metadata.Options.Ext.LoadImpact.Distribution, 2) + + require.Equal(t, metadata.Options.Ext.LoadImpact.Distribution["one"].LoadZone, "amazon:us:ashburn") + require.Equal(t, metadata.Options.Ext.LoadImpact.Distribution["one"].Percent, 30.) + require.Equal(t, metadata.Options.Ext.LoadImpact.Distribution["two"].LoadZone, "amazon:ie:dublin") + require.Equal(t, metadata.Options.Ext.LoadImpact.Distribution["two"].Percent, 70.) +} + +func TestArchiveContainsCloudSettings(t *testing.T) { + t.Parallel() + + // given a script with the cloud options + fileName := "script.js" + testScript := []byte(` + export let options = { + cloud: { + distribution: { + one: { loadZone: 'amazon:us:ashburn', percent: 30 }, + two: { loadZone: 'amazon:ie:dublin', percent: 70 }, + }, + }, + }; + export default function () {} + `) + ts := tests.NewGlobalTestState(t) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644)) + + // when we do archiving + ts.CmdArgs = []string{"k6", "archive", fileName} + + newRootCommand(ts.GlobalState).execute() + require.NoError(t, untar(t, ts.FS, "archive.tar", "tmp/")) + + data, err := fsext.ReadFile(ts.FS, "tmp/metadata.json") + require.NoError(t, err) + + // we just need some basic struct + metadata := struct { + Options struct { + Cloud struct { + Distribution map[string]struct { + LoadZone string `json:"loadZone"` + Percent float64 `json:"percent"` + } `json:"distribution"` + } `json:"cloud"` + } `json:"options"` + }{} + + // then unpacked metadata should contain options.cloud + require.NoError(t, json.Unmarshal(data, &metadata)) + require.Len(t, metadata.Options.Cloud.Distribution, 2) + + require.Equal(t, metadata.Options.Cloud.Distribution["one"].LoadZone, "amazon:us:ashburn") + require.Equal(t, metadata.Options.Cloud.Distribution["one"].Percent, 30.) + require.Equal(t, metadata.Options.Cloud.Distribution["two"].LoadZone, "amazon:ie:dublin") + require.Equal(t, metadata.Options.Cloud.Distribution["two"].Percent, 70.) +} + func TestArchiveNotContainsEnv(t *testing.T) { t.Parallel() diff --git a/lib/options.go b/lib/options.go index 06721cc001fa..f6704962f56a 100644 --- a/lib/options.go +++ b/lib/options.go @@ -301,7 +301,7 @@ type Options struct { // Cloud is the config for the cloud // formally known as ext.loadimpact - Cloud json.RawMessage `json:"cloud,omitempty" ignored:"true"` + Cloud json.RawMessage `json:"cloud,omitempty"` // These values are for third party collectors' benefit. // Can't be set through env vars. @@ -462,6 +462,9 @@ func (o Options) Apply(opts Options) Options { if opts.NoCookiesReset.Valid { o.NoCookiesReset = opts.NoCookiesReset } + if opts.Cloud != nil { + o.Cloud = opts.Cloud + } if opts.External != nil { o.External = opts.External }