Skip to content

Commit

Permalink
telemetry: Add nil check in SetBundledUsage
Browse files Browse the repository at this point in the history
Invoking Packer with the  CHECKPOINT_DISABLE env. variable the telemetry reporter is left uninitialized in order to disable telemetry reporting.
Any method calls on the nil reporter is expected to check if the reporter is active or in NOOP mode. This change fixes a crash when calling SetBundledUsage()
on a nil CheckpointTelemetry type that occurs when using a bundled plugin with CHECKPOINT_DISABLE=1.
  • Loading branch information
nywilken committed Aug 18, 2023
1 parent eb9e1a4 commit f8ebf69
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packer/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func (c *CheckpointTelemetry) SetTemplateType(t PackerTemplateType) {

// SetBundledUsage marks the template as using bundled plugins
func (c *CheckpointTelemetry) SetBundledUsage() {
if c == nil {
return
}
c.useBundled = true
}

Expand Down
22 changes: 22 additions & 0 deletions packer/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package packer

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -33,3 +34,24 @@ func TestFlattenConfigKeys_nested(t *testing.T) {
"Input didn't flatten correctly.",
)
}

func TestCheckpointTelemetry(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("a noop CheckpointTelemetry should not to panic but it did\n", r)
}
}()

// A null CheckpointTelemetry obtained in Packer when the CHECKPOINT_DISABLE env var is set results in a NOOP reporter
// The null reporter can be executable as a configured reporter but does not report any telemetry data.
var c *CheckpointTelemetry
c.SetTemplateType(HCL2Template)
c.SetBundledUsage()
c.AddSpan("mockprovisioner", "provisioner", nil)
if err := c.ReportPanic("Bogus Panic"); err != nil {
t.Errorf("calling ReportPanic on a nil checkpoint reporter should not error")
}
if err := c.Finalize("test", 1, errors.New("Bogus Error")); err != nil {
t.Errorf("calling Finalize on a nil checkpoint reporter should not error")
}
}

0 comments on commit f8ebf69

Please sign in to comment.