From afcafa70fb07b2340779ed7ef1dd347722916842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Mon, 29 Apr 2024 13:43:16 +0200 Subject: [PATCH] osbuild-worker: improve error "reason" in case of stage failures --- cmd/osbuild-worker/export_test.go | 5 +- cmd/osbuild-worker/jobimpl-osbuild.go | 38 +++++++--- cmd/osbuild-worker/jobimpl-osbuild_test.go | 81 ++++++++++++++++++++++ 3 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 cmd/osbuild-worker/jobimpl-osbuild_test.go diff --git a/cmd/osbuild-worker/export_test.go b/cmd/osbuild-worker/export_test.go index bd6e314361..df64dcdfe1 100644 --- a/cmd/osbuild-worker/export_test.go +++ b/cmd/osbuild-worker/export_test.go @@ -1,3 +1,6 @@ package main -var WorkerClientErrorFrom = workerClientErrorFrom +var ( + WorkerClientErrorFrom = workerClientErrorFrom + MakeJobErrorFromOsbuildOutput = makeJobErrorFromOsbuildOutput +) diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index eca86fb5bf..54e2be8d82 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -351,6 +351,33 @@ func (impl *OSBuildJobImpl) getPulpClient(targetOptions *target.PulpOSTreeTarget return pulp.NewClientFromFile(address, impl.PulpConfig.CredsFilePath) } +func makeJobErrorFromOsbuildOutput(osbuildOutput *osbuild.Result) *clienterrors.Error { + var osbErrors []string + if osbuildOutput.Error != nil { + osbErrors = append(osbErrors, fmt.Sprintf("osbuild error: %s", string(osbuildOutput.Error))) + } + if osbuildOutput.Errors != nil { + for _, err := range osbuildOutput.Errors { + osbErrors = append(osbErrors, fmt.Sprintf("manifest validation error: %v", err)) + } + } + var failedStage string + for _, pipelineLog := range osbuildOutput.Log { + for _, stageResult := range pipelineLog { + if !stageResult.Success { + failedStage = stageResult.Type + break + } + } + } + + reason := "osbuild build failed" + if len(failedStage) > 0 { + reason += " in stage:\n" + failedStage + } + return clienterrors.WorkerClientError(clienterrors.ErrorBuildJob, reason, osbErrors) +} + func (impl *OSBuildJobImpl) Run(job worker.Job) error { logWithId := logrus.WithField("jobId", job.Id().String()) // Initialize variable needed for reporting back to osbuild-composer. @@ -565,16 +592,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { // Second handle the case when the build failed, but osbuild finished successfully if !osbuildJobResult.OSBuildOutput.Success { - var osbErrors []string - if osbuildJobResult.OSBuildOutput.Error != nil { - osbErrors = append(osbErrors, fmt.Sprintf("osbuild error: %s", string(osbuildJobResult.OSBuildOutput.Error))) - } - if osbuildJobResult.OSBuildOutput.Errors != nil { - for _, err := range osbuildJobResult.OSBuildOutput.Errors { - osbErrors = append(osbErrors, fmt.Sprintf("manifest validation error: %v", err)) - } - } - osbuildJobResult.JobError = clienterrors.WorkerClientError(clienterrors.ErrorBuildJob, "osbuild build failed", osbErrors) + osbuildJobResult.JobError = makeJobErrorFromOsbuildOutput(osbuildJobResult.OSBuildOutput) return nil } diff --git a/cmd/osbuild-worker/jobimpl-osbuild_test.go b/cmd/osbuild-worker/jobimpl-osbuild_test.go new file mode 100644 index 0000000000..5e65570632 --- /dev/null +++ b/cmd/osbuild-worker/jobimpl-osbuild_test.go @@ -0,0 +1,81 @@ +package main_test + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/osbuild/images/pkg/osbuild" + + main "github.com/osbuild/osbuild-composer/cmd/osbuild-worker" +) + +func TestMakeJobErrorFromOsbuildOutput(t *testing.T) { + tests := []struct { + inputData *osbuild.Result + expected string + }{ + { + inputData: &osbuild.Result{ + Success: false, + Log: map[string]osbuild.PipelineResult{ + "fake-os": []osbuild.StageResult{ + { + Type: "good-stage", + Success: true, + Output: "good-output", + }, + { + Type: "bad-stage", + Success: false, + Output: "bad-failure", + }, + }, + }, + }, + expected: `Code: 10, Reason: osbuild build failed in stage: +bad-stage, Details: []`, + }, + { + inputData: &osbuild.Result{ + Success: false, + Log: map[string]osbuild.PipelineResult{ + "fake-os": []osbuild.StageResult{}, + }, + }, + expected: `Code: 10, Reason: osbuild build failed, Details: []`, + }, + { + inputData: &osbuild.Result{ + Error: json.RawMessage("some_osbuild_error"), + Success: false, + Log: map[string]osbuild.PipelineResult{ + "fake-os": []osbuild.StageResult{}, + }, + }, + expected: `Code: 10, Reason: osbuild build failed, Details: [osbuild error: some_osbuild_error]`, + }, + { + inputData: &osbuild.Result{ + Errors: []osbuild.ValidationError{ + { + Message: "validation error message", + Path: []string{"error path"}, + }, + }, + Success: false, + Log: map[string]osbuild.PipelineResult{ + "fake-os": []osbuild.StageResult{}, + }, + }, + expected: `Code: 10, Reason: osbuild build failed, Details: [manifest validation error: {validation error message [error path]}]`, + }, + } + for _, testData := range tests { + fakeOsbuildResult := testData.inputData + + wce := main.MakeJobErrorFromOsbuildOutput(fakeOsbuildResult) + require.Equal(t, testData.expected, wce.String()) + } +}