Skip to content

Commit

Permalink
output/cloud: Enable cloud version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed May 12, 2023
1 parent 0628ed6 commit 401dbc9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
61 changes: 61 additions & 0 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,25 @@ func getCloudTestEndChecker(
return srv
}

func getCloudMetricsServer(t *testing.T, testRunID int) *httptest.Server {
metricsFlushed := false
testStart := cloudTestStartSimple(t, testRunID)

srv := getTestServer(t, map[string]http.Handler{
"POST ^/v1/tests$": testStart,
fmt.Sprintf("POST ^/v2/metrics/%d$", testRunID): http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
metricsFlushed = true
}),
})

t.Cleanup(func() {
assert.Truef(t, metricsFlushed, "expected test to have called the cloud API endpoint to flush the metrics")
srv.Close()
})

return srv
}

func getSimpleCloudOutputTestState(
tb testing.TB, script string, cliFlags []string,
expRunStatus cloudapi.RunStatus, expResultStatus cloudapi.ResultStatus, expExitCode exitcodes.ExitCode,
Expand Down Expand Up @@ -1960,3 +1979,45 @@ func TestBadLogOutput(t *testing.T) {
})
}
}

func TestCloudOutputV2(t *testing.T) {
t.Parallel()
script := `
import { sleep } from 'k6';
export let options = {
scenarios: {
sc1: {
executor: 'per-vu-iterations',
vus: 1, iterations: 5,
}
},
thresholds: {
'iterations': ['count == 5'],
},
};
export default function () { };
`
cliFlags := []string{"-v", "--log-output=stdout", "--out", "cloud"}

srv := getCloudMetricsServer(t, 123)
ts := getSingleFileTestState(t, script, cliFlags, 0)
ts.Env["K6_CLOUD_HOST"] = srv.URL
ts.Env["K6_CLOUD_API_VERSION"] = "2"
ts.Env["K6_CLOUD_TOKEN"] = "fake-token"

cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
t.Log(stdout)

assert.Contains(t, stdout, `execution: local`)
assert.Contains(t, stdout, `output: cloud (https://app.k6.io/runs/123)`)
assert.Contains(t, stdout, `Started!" output=cloudv2`)
assert.Contains(t, stdout, `✓ iterations...........: 5`)
assert.Contains(t, stdout, `Successfully flushed buffered samples to the cloud`)
assert.Contains(t, stdout, `Cloud output successfully stopped!`)
assert.Contains(t, stdout, `Stopped!" output=cloudv2`)

assert.NotContains(t, stdout, `failed to flush metrics`)
}
8 changes: 6 additions & 2 deletions output/cloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
cloudv1 "go.k6.io/k6/output/cloud/v1"
"gopkg.in/guregu/null.v3"

cloudv2 "go.k6.io/k6/output/cloud/expv2"
cloudv1 "go.k6.io/k6/output/cloud/v1"
)

// TestName is the default k6 Cloud test name
Expand Down Expand Up @@ -44,7 +46,7 @@ type apiVersion int64
const (
apiVersionUndefined apiVersion = iota
apiVersion1
// apiVersion2 // TODO: add version 2
apiVersion2
)

// Output sends result data to the k6 Cloud service.
Expand Down Expand Up @@ -335,6 +337,8 @@ func (out *Output) startVersionedOutput() error {
switch out.config.APIVersion.Int64 {
case int64(apiVersion1):
out.versionedOutput, err = cloudv1.New(out.logger, out.config, out.client)
case int64(apiVersion2):
out.versionedOutput, err = cloudv2.New(out.logger, out.config)
default:
err = fmt.Errorf("v%d is an unexpected version", out.config.APIVersion.Int64)
}
Expand Down

0 comments on commit 401dbc9

Please sign in to comment.