From cfc646466b55ff1d5152e7886b562a73832474b9 Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Thu, 27 Oct 2022 18:17:36 +0200 Subject: [PATCH] Make ODO_TRACKING_CONSENT env var take precedence over the ConsentTelemetry preference See [1] for more context. [1] https://github.com/redhat-developer/odo/pull/6258\#issuecomment-1293736398 --- pkg/segment/segment.go | 12 +++++++++--- pkg/segment/segment_test.go | 3 +++ tests/integration/cmd_devfile_init_test.go | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/segment/segment.go b/pkg/segment/segment.go index 2d574d9bb8e..ce05971f7de 100644 --- a/pkg/segment/segment.go +++ b/pkg/segment/segment.go @@ -277,12 +277,18 @@ func IsTelemetryEnabled(cfg preference.Client) bool { klog.V(4).Info("Sending telemetry disabled by env variable\n") return false } + + if trackingConsent == "yes" { + klog.V(4).Info("Sending telemetry enabled by env variable\n") + return true + } + isEnabled := cfg.GetConsentTelemetry() - s := "Telemetry is disabled" + s := "Sending telemetry disabled by preference" if isEnabled { - s = "Telemetry is disabled" + s = "Sending telemetry enabled by preference" } - klog.V(4).Infof("%s!\n", s) + klog.V(4).Infof("%s\n", s) return isEnabled } diff --git a/pkg/segment/segment_test.go b/pkg/segment/segment_test.go index 610e0d34c41..ec0637e49ab 100644 --- a/pkg/segment/segment_test.go +++ b/pkg/segment/segment_test.go @@ -207,6 +207,9 @@ func TestIsTelemetryEnabled(t *testing.T) { if odoDisableTelemetry == "true" || odoTrackingConsent == "no" { return false } + if odoTrackingConsent == "yes" { + return true + } return consentTelemetry }, name: fmt.Sprintf("ODO_DISABLE_TELEMETRY=%q,ODO_TRACKING_CONSENT=%q,ConsentTelemetry=%v", diff --git a/tests/integration/cmd_devfile_init_test.go b/tests/integration/cmd_devfile_init_test.go index 44a7e80b4e3..2195783dba4 100644 --- a/tests/integration/cmd_devfile_init_test.go +++ b/tests/integration/cmd_devfile_init_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "github.com/redhat-developer/odo/pkg/odo/cli/messages" + "github.com/redhat-developer/odo/pkg/preference" "github.com/redhat-developer/odo/pkg/segment" segmentContext "github.com/redhat-developer/odo/pkg/segment/context" @@ -402,6 +403,7 @@ var _ = Describe("odo devfile init command tests", func() { type telemetryTest struct { title string env map[string]string + setupFunc func(cfg preference.Client) callerChecker func(stdout, stderr string, data segment.TelemetryData) } allowedTelemetryCallers := []string{segmentContext.VSCode, segmentContext.IntelliJ, segmentContext.JBoss} @@ -441,6 +443,19 @@ var _ = Describe("odo devfile init command tests", func() { }) }, }, + { + title: "ODO_TRACKING_CONSENT=yes env var should take precedence over ConsentTelemetry preference", + env: map[string]string{segment.TrackingConsentEnv: "yes"}, + callerChecker: func(_, _ string, td segment.TelemetryData) { + cmdProperties := td.Properties.CmdProperties + Expect(cmdProperties).Should(HaveKey(segmentContext.Caller)) + Expect(cmdProperties[segmentContext.Caller]).To(BeEmpty()) + }, + setupFunc: func(cfg preference.Client) { + err := cfg.SetConfiguration(preference.ConsentTelemetrySetting, "false") + Expect(err).ShouldNot(HaveOccurred()) + }, + }, } for _, c := range allowedTelemetryCallers { c := c @@ -461,6 +476,13 @@ var _ = Describe("odo devfile init command tests", func() { var stderr string BeforeEach(func() { helper.EnableTelemetryDebug() + + cfg, err := preference.NewClient() + Expect(err).ShouldNot(HaveOccurred()) + if tt.setupFunc != nil { + tt.setupFunc(cfg) + } + cmd := helper.Cmd("odo", "init", "--name", "aname", "--devfile", "go") for k, v := range tt.env { cmd = cmd.AddEnv(fmt.Sprintf("%s=%s", k, v))