Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the exit code and run_status value when the cloud output aborts a test #2907

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions errext/abort_reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
AbortedByScriptError
AbortedByScriptAbort
AbortedByTimeout
AbortedByOutput
)

// HasAbortReason is a wrapper around an error with an attached abort reason.
Expand Down
13 changes: 10 additions & 3 deletions output/cloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/output"

"go.k6.io/k6/lib"
Expand Down Expand Up @@ -59,7 +60,7 @@ type Output struct {
aggregationDone *sync.WaitGroup
stopOutput chan struct{}
outputDone *sync.WaitGroup
engineStopFunc func(error)
testStopFunc func(error)
}

// Verify that Output implements the wanted interfaces
Expand Down Expand Up @@ -311,6 +312,8 @@ func (out *Output) getRunStatus(testErr error) cloudapi.RunStatus {
return cloudapi.RunStatusAbortedUser // TODO: have a better value than this?
case errext.AbortedByTimeout:
return cloudapi.RunStatusAbortedLimit
case errext.AbortedByOutput:
return cloudapi.RunStatusAbortedSystem
case errext.AbortedByThresholdsAfterTestEnd:
// The test run finished normally, it wasn't prematurely aborted by
// anything while running, but the thresholds failed at the end and
Expand Down Expand Up @@ -349,7 +352,7 @@ func (out *Output) SetThresholds(scriptThresholds map[string]metrics.Thresholds)

// SetTestRunStopCallback receives the function that stops the engine on error
func (out *Output) SetTestRunStopCallback(stopFunc func(error)) {
out.engineStopFunc = stopFunc
out.testStopFunc = stopFunc
}

// AddMetricSamples receives a set of metric samples. This method is never
Expand Down Expand Up @@ -652,8 +655,12 @@ func (out *Output) pushMetrics() {
if err != nil {
if out.shouldStopSendingMetrics(err) {
out.logger.WithError(err).Warn("Stopped sending metrics to cloud due to an error")
serr := errext.WithAbortReasonIfNone(
errext.WithExitCodeIfNone(err, exitcodes.ExternalAbort),
errext.AbortedByOutput,
)
if out.config.StopOnError.Bool {
out.engineStopFunc(err)
out.testStopFunc(serr)
}
close(out.stopSendingMetrics)
break
Expand Down
12 changes: 6 additions & 6 deletions output/cloud/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,13 @@ func testCloudOutputStopSendingMetric(t *testing.T, stopOnError bool) {
},
ScriptPath: &url.URL{Path: "/script.js"},
})
var expectedEngineStopFuncCalled int64
var expectedTestStopFuncCalled int64
if stopOnError {
expectedEngineStopFuncCalled = 1
expectedTestStopFuncCalled = 1
}
var engineStopFuncCalled int64
out.engineStopFunc = func(error) {
atomic.AddInt64(&engineStopFuncCalled, 1)
var TestStopFuncCalled int64
out.testStopFunc = func(error) {
atomic.AddInt64(&TestStopFuncCalled, 1)
}
require.NoError(t, err)
now := time.Now()
Expand Down Expand Up @@ -513,7 +513,7 @@ func testCloudOutputStopSendingMetric(t *testing.T, stopOnError bool) {
t.Fatal("sending metrics wasn't stopped")
}
require.Equal(t, max, count)
require.Equal(t, expectedEngineStopFuncCalled, engineStopFuncCalled)
require.Equal(t, expectedTestStopFuncCalled, TestStopFuncCalled)

nBufferSamples := len(out.bufferSamples)
nBufferHTTPTrails := len(out.bufferHTTPTrails)
Expand Down
8 changes: 4 additions & 4 deletions output/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ type WithThresholds interface {
SetThresholds(map[string]metrics.Thresholds)
}

// WithTestRunStop is an output that can stop the Engine mid-test, interrupting
// the whole test run execution if some internal condition occurs, completely
// independently from the thresholds. It requires a callback function which
// expects an error and triggers the Engine to stop.
// WithTestRunStop is an output that can stop the test run mid-way through,
// interrupting the whole test run execution if some internal condition occurs,
// completely independently from the thresholds. It requires a callback function
// which expects an error and triggers the Engine to stop.
type WithTestRunStop interface {
Output
SetTestRunStopCallback(func(error))
Expand Down