From 33d10eefd63894a4fd8ae67f9c90806c4ed6e4ff Mon Sep 17 00:00:00 2001 From: Christian Rohmann Date: Fri, 23 Aug 2024 09:19:32 +0200 Subject: [PATCH] Combine all initializer commands with && to catch any failing commands By running two commands instead of one (the second being the cat | grep), any failures (non-zero exit code) of the first part (containing `k6 inspect`) will be lost and masked away. By chaining them all with `&&` the first non-zero RC will fail the whole command and return. Fixes: #435 --- controllers/common.go | 2 +- pkg/resources/jobs/initializer.go | 2 +- pkg/resources/jobs/initializer_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/controllers/common.go b/controllers/common.go index 078d96b5..6359aa26 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -100,7 +100,7 @@ func inspectTestRun(ctx context.Context, log logr.Logger, k6 v1alpha1.TestRunI, defer podLogs.Close() buf := new(bytes.Buffer) - _, returnErr = io.Copy(buf, podLogs) + _, err = io.Copy(buf, podLogs) if err != nil { log.Error(err, "unable to copy logs from the pod") return diff --git a/pkg/resources/jobs/initializer.go b/pkg/resources/jobs/initializer.go index 64bb2de4..fa7d10a7 100644 --- a/pkg/resources/jobs/initializer.go +++ b/pkg/resources/jobs/initializer.go @@ -76,7 +76,7 @@ func NewInitializerJob(k6 v1alpha1.TestRunI, argLine string) (*batchv1.Job, erro // printing JSON as usual. Then parse temp file only for errors, ignoring // any other log messages. // Related: https://github.com/grafana/k6-docs/issues/877 - "mkdir -p $(dirname %s) && k6 archive %s -O %s %s 2> /tmp/k6logs && k6 inspect --execution-requirements %s 2> /tmp/k6logs ; ! cat /tmp/k6logs | grep 'level=error'", + "mkdir -p $(dirname %s) && k6 archive %s -O %s %s 2> /tmp/k6logs && k6 inspect --execution-requirements %s 2> /tmp/k6logs && ! cat /tmp/k6logs | grep 'level=error'", archiveName, scriptName, archiveName, argLine, archiveName)) diff --git a/pkg/resources/jobs/initializer_test.go b/pkg/resources/jobs/initializer_test.go index 868d13ef..0e1ad2d7 100644 --- a/pkg/resources/jobs/initializer_test.go +++ b/pkg/resources/jobs/initializer_test.go @@ -63,7 +63,7 @@ func TestNewInitializerJob(t *testing.T) { Name: "k6", Command: []string{ "sh", "-c", - "mkdir -p $(dirname /tmp/test.js.archived.tar) && k6 archive /test/test.js -O /tmp/test.js.archived.tar --out cloud 2> /tmp/k6logs && k6 inspect --execution-requirements /tmp/test.js.archived.tar 2> /tmp/k6logs ; ! cat /tmp/k6logs | grep 'level=error'", + "mkdir -p $(dirname /tmp/test.js.archived.tar) && k6 archive /test/test.js -O /tmp/test.js.archived.tar --out cloud 2> /tmp/k6logs && k6 inspect --execution-requirements /tmp/test.js.archived.tar 2> /tmp/k6logs && ! cat /tmp/k6logs | grep -e 'level=error'", }, Env: []corev1.EnvVar{}, EnvFrom: []corev1.EnvFromSource{