Skip to content

Commit

Permalink
Require cloud scripts to always have a name
Browse files Browse the repository at this point in the history
This fixes #923
  • Loading branch information
na-- committed Feb 26, 2021
1 parent 0f431d9 commit 82cab8b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
10 changes: 8 additions & 2 deletions stats/cloud/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ func New(
}

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 == "" {
conf.Name = null.StringFrom(filepath.Base(scriptURL.String()))
scriptPath := scriptURL.String()
if scriptPath == "" {
// 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 == "-" {
conf.Name = null.StringFrom(TestName)
Expand Down
38 changes: 34 additions & 4 deletions stats/cloud/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,15 @@ func TestCloudCollectorMaxPerPacket(t *testing.T) {
func TestCloudCollectorStopSendingMetric(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.Fprint(w, `{
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, "my-custom-name", data.Name)

_, err = fmt.Fprint(resp, `{
"reference_id": "12",
"config": {
"metricPushInterval": "200ms",
Expand All @@ -424,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 @@ -515,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(), "script name not set")
}

func TestCloudCollectorAggregationPeriodZeroNoBlock(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
Expand Down Expand Up @@ -580,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 Down

0 comments on commit 82cab8b

Please sign in to comment.