Skip to content

Commit

Permalink
Use new ScriptAborted (108) exit code for test.abort()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić authored and oleiade committed Jan 10, 2022
1 parent bc403e7 commit 1f99b6d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
7 changes: 3 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ a commandline interface for interacting with it.`,
initBar.Modify(pb.WithConstProgress(0, "Init VUs..."))
engineRun, engineWait, err := engine.Init(globalCtx, runCtx)
if err != nil {
if common.IsInterruptError(err) {
return errext.WithExitCodeIfNone(err, exitcodes.ScriptException)
}
// Add a generic engine exit code if we don't have a more specific one
return errext.WithExitCodeIfNone(err, exitcodes.GenericEngine)
}
Expand Down Expand Up @@ -281,6 +278,8 @@ a commandline interface for interacting with it.`,
err = engineRun()
if err != nil {
if common.IsInterruptError(err) {
// Don't return here since we need to work with --linger,
// show the end-of-test summary and exit cleanly.
interrupt = err
}
if !conf.Linger.Bool && interrupt == nil {
Expand Down Expand Up @@ -335,7 +334,7 @@ a commandline interface for interacting with it.`,
engineWait()
logger.Debug("Everything has finished, exiting k6!")
if interrupt != nil {
return errext.WithExitCodeIfNone(interrupt, exitcodes.ScriptException)
return interrupt
}
if engine.IsTainted() {
return errext.WithExitCodeIfNone(errors.New("some thresholds have failed"), exitcodes.ThresholdsHaveFailed)
Expand Down
8 changes: 5 additions & 3 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestHandleSummaryResultError(t *testing.T) {
func TestAbortTest(t *testing.T) { //nolint: tparallel
t.Parallel()

t.Run("Check status code is 107", func(t *testing.T) { //nolint: paralleltest
t.Run("Check correct status code", func(t *testing.T) { //nolint: paralleltest
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := testutils.NewLogger(t)
Expand All @@ -148,7 +148,8 @@ func TestAbortTest(t *testing.T) { //nolint: tparallel
err = cmd.Execute()
var e errext.HasExitCode
require.ErrorAs(t, err, &e)
require.Equal(t, exitcodes.ScriptException, e.ExitCode(), "Status code must be 107")
assert.Equalf(t, exitcodes.ScriptAborted, e.ExitCode(),
"Status code must be %d", exitcodes.ScriptAborted)
require.Contains(t, e.Error(), common.AbortTest)
})

Expand All @@ -172,7 +173,8 @@ func TestAbortTest(t *testing.T) { //nolint: tparallel
err = cmd.Execute()
var e errext.HasExitCode
require.ErrorAs(t, err, &e)
assert.Equal(t, exitcodes.ScriptException, e.ExitCode(), "Status code must be 107")
assert.Equalf(t, exitcodes.ScriptAborted, e.ExitCode(),
"Status code must be %d", exitcodes.ScriptAborted)
assert.Contains(t, e.Error(), common.AbortTest)
assert.Contains(t, buf.String(), msg)
})
Expand Down
1 change: 1 addition & 0 deletions errext/exitcodes/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ const (
ExternalAbort errext.ExitCode = 105
CannotStartRESTAPI errext.ExitCode = 106
ScriptException errext.ExitCode = 107
ScriptAborted errext.ExitCode = 108
)
15 changes: 14 additions & 1 deletion js/common/interrupt_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,30 @@

package common

import "errors"
import (
"errors"

"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
)

// InterruptError is an error that halts engine execution
type InterruptError struct {
Reason string
}

var _ errext.HasExitCode = &InterruptError{}

// Error returns the reason of the interruption.
func (i *InterruptError) Error() string {
return i.Reason
}

// ExitCode returns the status code used when the k6 process exits.
func (i *InterruptError) ExitCode() errext.ExitCode {
return exitcodes.ScriptAborted
}

// AbortTest is the reason emitted when a test script calls test.abort()
const AbortTest = "test aborted"

Expand Down

0 comments on commit 1f99b6d

Please sign in to comment.