telemetry: Add nil check in SetBundledUsage

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.
pull/12595/head
Wilken Rivera 3 years ago
parent eb9e1a4795
commit f8ebf69c0d

@ -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
}

@ -4,6 +4,7 @@
package packer
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
@ -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")
}
}

Loading…
Cancel
Save