@@ -18,10 +18,11 @@ var reservedGCk6EnvVars = map[string]struct{}{}
1818const (
1919 // Reserved vars set for PLZ tests, as described here:
2020 // https://grafana.com/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-execution-context-variables/
21+ // These are not passed from GCk6, but set by k6-operator directly.
2122 lzCloudExecVar = "K6_CLOUDRUN_LOAD_ZONE"
2223 distrCloudExecVar = "K6_CLOUDRUN_DISTRIBUTION"
2324 trIDCloudExecVar = "K6_CLOUDRUN_TEST_RUN_ID"
24- // it's exported as it must be set in external package, as part of TestRun CRD flow
25+ // IIDCloudExecVar is exported as it must be set in external package, as part of TestRun CRD flow
2526 IIDCloudExecVar = "K6_CLOUDRUN_INSTANCE_ID"
2627
2728 secretSourceEnvVar = "K6_SECRET_SOURCE"
@@ -96,14 +97,19 @@ type TestRunData struct {
9697 // LZDistribution holds label -> distribution mapping relevant
9798 // for the given script and PLZ
9899 LZDistribution `json:"load_zone_distribution,omitempty"`
100+
101+ // TagArgs and EnvArgs are sorted CLI argument strings, populated by Build().
102+ TagArgs string `json:"-"`
103+ EnvArgs string `json:"-"`
99104}
100105
101106func (trd * TestRunData ) TestRunID () string {
102107 return fmt .Sprintf ("%d" , trd .TestRunId )
103108}
104109
105- // Build adds specific for GCk6 tags and env vars to data.
106- // Returns error if it's impossible.
110+ // Build adds specific for GCk6 tags and env vars to data,
111+ // and produces sorted CLI argument strings for tags and environment.
112+ // Returns error if distribution is empty.
107113func (trd * TestRunData ) Build () error {
108114 if len (trd .LZDistribution ) != 1 {
109115 return fmt .Errorf ("only tests with one load zone are supported, provided: %+v" , trd .LZDistribution )
@@ -119,9 +125,27 @@ func (trd *TestRunData) Build() error {
119125 trd .Environment [distrCloudExecVar ] = trd .LZLabel ()
120126 trd .Environment [trIDCloudExecVar ] = trd .TestRunID ()
121127
128+ trd .TagArgs = sortedArgs ("--tag" , trd .Tags )
129+ trd .EnvArgs = sortedArgs ("-e" , trd .Environment )
130+
122131 return nil
123132}
124133
134+ // sortedArgs builds a CLI argument string from a map, sorted by key.
135+ func sortedArgs (flag string , m map [string ]string ) string {
136+ keys := make ([]string , 0 , len (m ))
137+ for k := range m {
138+ keys = append (keys , k )
139+ }
140+ sort .Strings (keys )
141+
142+ var s string
143+ for _ , k := range keys {
144+ s += " " + flag + " " + fmt .Sprintf (`%s=%s` , k , m [k ])
145+ }
146+ return s
147+ }
148+
125149type LZConfig struct {
126150 RunnerImage string `json:"load_runner_image,omitempty"`
127151 InstanceCount int `json:"instance_count,omitempty"`
@@ -145,9 +169,9 @@ type CLIArgs struct {
145169 UserAgent string `json:"user_agent,omitempty"`
146170}
147171
148- type LZDistribution map [string ]distribution
172+ type LZDistribution map [string ]Distribution
149173
150- type distribution struct {
174+ type Distribution struct {
151175 LoadZone string `json:"loadZone"`
152176 Percent int `json:"percent"`
153177}
0 commit comments