Skip to content

Commit

Permalink
Fix cloud script names properly
Browse files Browse the repository at this point in the history
This actually fixes #923 according to what was discussed in the issue.
  • Loading branch information
na-- committed Feb 23, 2021
1 parent 6de13cc commit b9e01e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
10 changes: 6 additions & 4 deletions stats/cloud/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,23 @@ var _ lib.Collector = &Collector{}
// New creates a new cloud collector
func New(
logger logrus.FieldLogger,
conf cloudapi.Config, scriptPath fmt.Stringer, opts lib.Options, executionPlan []lib.ExecutionStep, version string,
conf cloudapi.Config, scriptURL fmt.Stringer, opts lib.Options, executionPlan []lib.ExecutionStep, version string,
) (*Collector, error) {
if err := cloudapi.MergeFromExternal(opts.External, &conf); err != nil {
return nil, err
}

if conf.AggregationPeriod.Duration > 0 && (opts.SystemTags.Has(stats.TagVU) || opts.SystemTags.Has(stats.TagIter)) {
return nil, errors.New("Aggregation cannot be enabled if the 'vu' or 'iter' system tag is also enabled")
return nil, errors.New("aggregation cannot be enabled if the 'vu' or 'iter' system tag is also enabled")
}

if !conf.Name.Valid || conf.Name.String == "" {
scriptPath := scriptPath.String()
scriptPath := scriptURL.String()
if scriptPath == "" {
scriptPath = "script.js"
// Script from stdin without a name, likely from stdin
return nil, errors.New("script name not set, please specify K6_CLOUD_NAME or options.ext.loadimpact.name")
}

conf.Name = null.StringFrom(filepath.Base(scriptPath))
}
if conf.Name.String == "-" {
Expand Down
31 changes: 27 additions & 4 deletions stats/cloud/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestCloudCollectorStopSendingMetric(t *testing.T) {
data := &cloudapi.TestRun{}
err = json.Unmarshal(body, &data)
require.NoError(t, err)
assert.NotEmpty(t, data.Name)
assert.Equal(t, "my-custom-name", data.Name)

_, err = fmt.Fprint(resp, `{
"reference_id": "12",
Expand All @@ -431,6 +431,7 @@ func TestCloudCollectorStopSendingMetric(t *testing.T) {
Host: null.StringFrom(tb.ServerHTTP.URL),
NoCompress: null.BoolFrom(true),
MaxMetricSamplesPerPackage: null.IntFrom(50),
Name: null.StringFrom("my-custom-name"),
})
collector, err := New(testutils.NewLogger(t), config, &url.URL{Path: ""}, options, []lib.ExecutionStep{}, "1.0")
require.NoError(t, err)
Expand Down Expand Up @@ -522,6 +523,21 @@ func TestCloudCollectorStopSendingMetric(t *testing.T) {
}
}

func TestCloudCollectorRequireScriptName(t *testing.T) {
t.Parallel()
options := lib.Options{
Duration: types.NullDurationFrom(1 * time.Second),
}

config := cloudapi.NewConfig().Apply(cloudapi.Config{
NoCompress: null.BoolFrom(true),
MaxMetricSamplesPerPackage: null.IntFrom(50),
})
_, err := New(testutils.NewLogger(t), config, &url.URL{Path: ""}, options, []lib.ExecutionStep{}, "1.0")
require.Error(t, err)
assert.Contains(t, err.Error(), "K6_CLOUD_NAME")
}

func TestCloudCollectorAggregationPeriodZeroNoBlock(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
Expand Down Expand Up @@ -587,8 +603,15 @@ func TestCloudCollectorAggregationPeriodZeroNoBlock(t *testing.T) {
func TestCloudCollectorRecvIterLIAllIterations(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
tb.Mux.HandleFunc("/v1/tests", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprintf(w, `{"reference_id": "123"}`)
tb.Mux.HandleFunc("/v1/tests", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
require.NoError(t, err)
data := &cloudapi.TestRun{}
err = json.Unmarshal(body, &data)
require.NoError(t, err)
assert.Equal(t, "script.js", data.Name)

_, err = fmt.Fprintf(resp, `{"reference_id": "123"}`)
require.NoError(t, err)
}))
defer tb.Cleanup()
Expand All @@ -601,7 +624,7 @@ func TestCloudCollectorRecvIterLIAllIterations(t *testing.T) {
Host: null.StringFrom(tb.ServerHTTP.URL),
NoCompress: null.BoolFrom(true),
})
collector, err := New(testutils.NewLogger(t), config, &url.URL{Path: "/script.js"}, options, []lib.ExecutionStep{}, "1.0")
collector, err := New(testutils.NewLogger(t), config, &url.URL{Path: "path/to/script.js"}, options, []lib.ExecutionStep{}, "1.0")
require.NoError(t, err)

gotIterations := false
Expand Down

0 comments on commit b9e01e5

Please sign in to comment.