From 6168cf18e6ab1eeff909eba4b558b0b4f60cb91b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Sat, 24 Feb 2024 11:37:03 +0100 Subject: [PATCH] fix: reports creation and potential nil ref panic (#965) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/runner/processors/step.go | 27 ++++++++++++++++++--------- pkg/runner/processors/test.go | 17 +++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/pkg/runner/processors/step.go b/pkg/runner/processors/step.go index 5abd7a746..f34992bb9 100644 --- a/pkg/runner/processors/step.go +++ b/pkg/runner/processors/step.go @@ -314,8 +314,9 @@ func (p *stepProcessor) applyOperation(ctx context.Context, bindings binding.Bin return nil, err } var ops []operation - operationReport := report.NewOperation("Apply "+op.File, report.OperationTypeApply) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Apply "+op.File, report.OperationTypeApply) p.stepReport.AddOperation(operationReport) } dryRun := op.DryRun != nil && *op.DryRun @@ -344,8 +345,9 @@ func (p *stepProcessor) assertOperation(ctx context.Context, bindings binding.Bi return nil, err } var ops []operation - operationReport := report.NewOperation("Assert ", report.OperationTypeAssert) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Assert ", report.OperationTypeAssert) p.stepReport.AddOperation(operationReport) } template := runnertemplate.Get(op.Template, p.step.Template, p.test.Spec.Template, p.config.Template) @@ -365,8 +367,9 @@ func (p *stepProcessor) assertOperation(ctx context.Context, bindings binding.Bi } func (p *stepProcessor) commandOperation(ctx context.Context, bindings binding.Bindings, op v1alpha1.Command) operation { - operationReport := report.NewOperation("Command ", report.OperationTypeCommand) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Command ", report.OperationTypeCommand) p.stepReport.AddOperation(operationReport) } ns := "" @@ -390,8 +393,9 @@ func (p *stepProcessor) createOperation(ctx context.Context, bindings binding.Bi return nil, err } var ops []operation - operationReport := report.NewOperation("Create ", report.OperationTypeCreate) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Create ", report.OperationTypeCreate) p.stepReport.AddOperation(operationReport) } dryRun := op.DryRun != nil && *op.DryRun @@ -421,8 +425,9 @@ func (p *stepProcessor) deleteOperation(ctx context.Context, bindings binding.Bi resource.SetName(op.Name) resource.SetNamespace(op.Namespace) resource.SetLabels(op.Labels) - operationReport := report.NewOperation("Delete ", report.OperationTypeDelete) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Delete ", report.OperationTypeDelete) p.stepReport.AddOperation(operationReport) } template := runnertemplate.Get(op.Template, p.step.Template, p.test.Spec.Template, p.config.Template) @@ -444,8 +449,9 @@ func (p *stepProcessor) errorOperation(ctx context.Context, bindings binding.Bin return nil, err } var ops []operation - operationReport := report.NewOperation("Error ", report.OperationTypeCommand) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Error ", report.OperationTypeCommand) p.stepReport.AddOperation(operationReport) } template := runnertemplate.Get(op.Template, p.step.Template, p.test.Spec.Template, p.config.Template) @@ -470,8 +476,9 @@ func (p *stepProcessor) patchOperation(ctx context.Context, bindings binding.Bin return nil, err } var ops []operation - operationReport := report.NewOperation("Patch ", report.OperationTypeCreate) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Patch ", report.OperationTypeCreate) p.stepReport.AddOperation(operationReport) } dryRun := op.DryRun != nil && *op.DryRun @@ -495,8 +502,9 @@ func (p *stepProcessor) patchOperation(ctx context.Context, bindings binding.Bin } func (p *stepProcessor) scriptOperation(ctx context.Context, bindings binding.Bindings, op v1alpha1.Script) operation { - operationReport := report.NewOperation("Script ", report.OperationTypeScript) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Script ", report.OperationTypeScript) p.stepReport.AddOperation(operationReport) } ns := "" @@ -515,8 +523,9 @@ func (p *stepProcessor) scriptOperation(ctx context.Context, bindings binding.Bi } func (p *stepProcessor) sleepOperation(ctx context.Context, bindings binding.Bindings, op v1alpha1.Sleep) operation { - operationReport := report.NewOperation("Sleep ", report.OperationTypeSleep) + var operationReport *report.OperationReport if p.stepReport != nil { + operationReport = report.NewOperation("Sleep ", report.OperationTypeSleep) p.stepReport.AddOperation(operationReport) } return newOperation( diff --git a/pkg/runner/processors/test.go b/pkg/runner/processors/test.go index 993b5508b..7d02cdbf8 100644 --- a/pkg/runner/processors/test.go +++ b/pkg/runner/processors/test.go @@ -69,14 +69,14 @@ type testProcessor struct { func (p *testProcessor) Run(ctx context.Context, nspacer namespacer.Namespacer) { t := testing.FromContext(ctx) - t.Cleanup(func() { - if t.Failed() { - p.testReport.NewFailure("test failed") - } - if p.testReport != nil { + if p.testReport != nil { + t.Cleanup(func() { + if t.Failed() { + p.testReport.NewFailure("test failed") + } p.testReport.MarkTestEnd() - } - }) + }) + } size := len("@cleanup") for i, step := range p.test.Spec.Steps { name := step.Name @@ -191,8 +191,9 @@ func (p *testProcessor) Run(ctx context.Context, nspacer namespacer.Namespacer) } func (p *testProcessor) CreateStepProcessor(nspacer namespacer.Namespacer, bindings binding.Bindings, cleaner *cleaner, step v1alpha1.TestSpecStep) StepProcessor { - stepReport := report.NewTestSpecStep(step.Name) + var stepReport *report.TestSpecStepReport if p.testReport != nil { + stepReport = report.NewTestSpecStep(step.Name) p.testReport.AddTestStep(stepReport) } return NewStepProcessor(p.config, p.clusters, nspacer, p.clock, p.test, step, stepReport, cleaner, bindings)