From 33e8d2091d9ae67d135944b6ed942ffc28c2e4fe Mon Sep 17 00:00:00 2001 From: Tine Date: Wed, 21 Feb 2024 14:16:22 +0100 Subject: [PATCH] fix(k6): config reading --- internal/activities/healthcheck.go | 2 +- pkg/k6/k6.go | 2 +- pkg/k6/k6_test.go | 34 +++++++++++++++++++++++++++--- pkg/k6/test.go | 2 +- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/internal/activities/healthcheck.go b/internal/activities/healthcheck.go index aa609f6..77eff19 100644 --- a/internal/activities/healthcheck.go +++ b/internal/activities/healthcheck.go @@ -22,7 +22,7 @@ func Healthcheck(ctx context.Context, param HealtcheckParam) (*HealthcheckResult execution := k6.NewExecution(slog.Default(), param.Script) - err := execution.Start(ctx) + err := execution.Run(ctx) if err != nil { return nil, err } diff --git a/pkg/k6/k6.go b/pkg/k6/k6.go index 0fe58f3..50b4877 100644 --- a/pkg/k6/k6.go +++ b/pkg/k6/k6.go @@ -136,7 +136,7 @@ func (e *Execution) setupTracerProvider(ctx context.Context, test *loadedAndConf return nil } -func (e *Execution) Start(ctx context.Context) error { +func (e *Execution) Run(ctx context.Context) error { var err error var logger logrus.FieldLogger = logrus.StandardLogger() diff --git a/pkg/k6/k6_test.go b/pkg/k6/k6_test.go index 41e694d..feccd90 100644 --- a/pkg/k6/k6_test.go +++ b/pkg/k6/k6_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func TestK6(t *testing.T) { +func TestK6Success(t *testing.T) { ctx := context.Background() logger := slog.Default() @@ -16,7 +16,7 @@ import { sleep } from 'k6'; export const options = { vus: 10, - duration: '30s', + duration: '5s', }; export default function () { @@ -27,7 +27,35 @@ export default function () { execution := NewExecution(logger, script) - err := execution.Start(ctx) + err := execution.Run(ctx) + if err != nil { + t.Errorf("Error starting execution: %v", err) + } +} + +func TestK6Fail(t *testing.T) { + ctx := context.Background() + logger := slog.Default() + + script := ` +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + thresholds: { + http_req_failed: ['rate<0.01'], // http errors should be less than 1% + }, +}; + +export default function () { + http.get('https://fail.broken.example'); + sleep(1); +} +` + + execution := NewExecution(logger, script) + + err := execution.Run(ctx) if err != nil { t.Errorf("Error starting execution: %v", err) } diff --git a/pkg/k6/test.go b/pkg/k6/test.go index 63d41be..bb41b3b 100644 --- a/pkg/k6/test.go +++ b/pkg/k6/test.go @@ -52,8 +52,8 @@ func (lt *loadedTest) consolidateDeriveAndValidateConfig() (*loadedAndConfigured lt.logger.Debug("Consolidating config layers...") config := lib.Options{} + config = config.Apply(lt.initRunner.GetOptions()) - config.Apply(lt.initRunner.GetOptions()) if config.SystemTags == nil { config.SystemTags = &metrics.DefaultSystemTagSet }