From 5da752fdfac7262e715ae05b072bdbe9e6f5789f Mon Sep 17 00:00:00 2001 From: Oleg Bespalov Date: Tue, 10 Oct 2023 17:04:37 +0200 Subject: [PATCH] Respect options.cloud during archiving --- cmd/archive_test.go | 104 ++++++++++++++++++++++++++++++++++++++++++++ lib/options.go | 5 ++- 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/cmd/archive_test.go b/cmd/archive_test.go index dee80b72666..f12c22e1e67 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 06721cc001f..f6704962f56 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 }