Skip to content

Commit

Permalink
Report ThresholdsHaveFailed on Cloud runs
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed Jul 30, 2024
1 parent aab3bc8 commit 87c60a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
9 changes: 6 additions & 3 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/fatih/color"
"go.k6.io/k6/cloudapi"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib"
Expand All @@ -22,8 +23,6 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"go.k6.io/k6/cmd/state"
)

// cmdCloud handles the `k6 cloud` sub-command
Expand Down Expand Up @@ -313,9 +312,13 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
logger.WithField("run_status", testProgress.RunStatusText).Debug("Test finished")
}

//nolint:stylecheck,golint
if testProgress.ResultStatus == cloudapi.ResultStatusFailed {
if testProgress.RunStatus == cloudapi.RunStatusFinished {
return errext.WithExitCodeIfNone(errors.New("Thresholds have been crossed"), exitcodes.ThresholdsHaveFailed)
}

// TODO: use different exit codes for failed thresholds vs failed test (e.g. aborted by system/limit)
//nolint:stylecheck,golint
return errext.WithExitCodeIfNone(errors.New("The test has failed"), exitcodes.CloudTestRunFailed)
}

Expand Down
29 changes: 25 additions & 4 deletions cmd/tests/cmd_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"path/filepath"
"testing"

"go.k6.io/k6/lib/testutils"

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/cmd"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/testutils"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/cloudapi"
"go.k6.io/k6/lib/fsext"
)

func TestK6Cloud(t *testing.T) {
Expand Down Expand Up @@ -236,6 +236,27 @@ func runCloudTests(t *testing.T, setupCmd setupCommandFunc) {
assert.Contains(t, stdout, `output: https://app.k6.io/runs/123`)
assert.Contains(t, stdout, `test status: Finished`)
})

t.Run("TestCloudThresholdsHaveFailed", func(t *testing.T) {
t.Parallel()

progressCallback := func() cloudapi.TestProgressResponse {
return cloudapi.TestProgressResponse{
RunStatusText: "Finished",
RunStatus: cloudapi.RunStatusFinished,
ResultStatus: cloudapi.ResultStatusFailed,
Progress: 1.0,
}
}
ts := getSimpleCloudTestState(t, nil, setupCmd, nil, nil, progressCallback)
ts.ExpectedExitCode = int(exitcodes.ThresholdsHaveFailed)

cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
t.Log(stdout)
assert.Contains(t, stdout, `Thresholds have been crossed`)
})
}

func cloudTestStartSimple(tb testing.TB, testRunID int) http.Handler {
Expand Down

0 comments on commit 87c60a2

Please sign in to comment.