Skip to content

Commit

Permalink
Respect options.cloud during archiving
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Oct 18, 2023
1 parent 692d92d commit 7da005e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
104 changes: 104 additions & 0 deletions cmd/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
5 changes: 4 additions & 1 deletion lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7da005e

Please sign in to comment.