diff --git a/output/cloud/expv2/output.go b/output/cloud/expv2/output.go index b2092450e6e..af6fb73cb37 100644 --- a/output/cloud/expv2/output.go +++ b/output/cloud/expv2/output.go @@ -15,6 +15,7 @@ import ( "go.k6.io/k6/cloudapi/insights" "go.k6.io/k6/errext" "go.k6.io/k6/errext/exitcodes" + "go.k6.io/k6/lib/consts" "go.k6.io/k6/lib/types" "go.k6.io/k6/metrics" "go.k6.io/k6/output" @@ -63,13 +64,20 @@ type Output struct { } // New creates a new cloud output. -func New(logger logrus.FieldLogger, conf cloudapi.Config, cloudClient *cloudapi.Client) (*Output, error) { +func New(logger logrus.FieldLogger, conf cloudapi.Config, _ *cloudapi.Client) (*Output, error) { return &Output{ - config: conf, - logger: logger.WithField("output", "cloudv2"), - cloudClient: cloudClient, - abort: make(chan struct{}), - stop: make(chan struct{}), + config: conf, + logger: logger.WithField("output", "cloudv2"), + abort: make(chan struct{}), + stop: make(chan struct{}), + + // TODO: move this creation operation to the centralized output. Reducing the probability to + // break the logic for the config overwriting. + // + // It creates a new client because in the case the backend has overwritten + // the config we need to use the new set. + cloudClient: cloudapi.NewClient( + logger, conf.Token.String, conf.Host.String, consts.Version, conf.Timeout.TimeDuration()), }, nil } diff --git a/output/cloud/expv2/output_test.go b/output/cloud/expv2/output_test.go index 56c8c10adaf..852329baa60 100644 --- a/output/cloud/expv2/output_test.go +++ b/output/cloud/expv2/output_test.go @@ -37,6 +37,18 @@ func TestNew(t *testing.T) { assert.Equal(t, int64(99), o.config.APIVersion.Int64) } +func TestNewWithConfigOverwritten(t *testing.T) { + t.Parallel() + + logger := testutils.NewLogger(t) + c := cloudapi.NewClient(logger, "my-token", "the-host", "v/foo", 1*time.Second) + conf := cloudapi.Config{Host: null.StringFrom("the-new-host")} + o, err := New(logger, conf, c) + require.NoError(t, err) + require.NotNil(t, o) + assert.Equal(t, "the-new-host/v1", o.cloudClient.BaseURL()) +} + func TestOutputSetTestRunID(t *testing.T) { t.Parallel() o := Output{} diff --git a/output/cloud/output.go b/output/cloud/output.go index 8ed43d03fcb..7c5f4f0a1f7 100644 --- a/output/cloud/output.go +++ b/output/cloud/output.go @@ -337,6 +337,14 @@ func (out *Output) startVersionedOutput() error { return errors.New("TestRunID is required") } var err error + + // TODO: move here the creation of a new cloudapi.Client + // so in the case the config has been overwritten the client uses the correct + // value. + // + // This logic is handled individually by each single output, it has the downside + // that we could break the logic and not catch easly it. + switch out.config.APIVersion.Int64 { case int64(apiVersion1): out.versionedOutput, err = cloudv1.New(out.logger, out.config, out.client)