Skip to content

Commit

Permalink
Add more unit test cases, especially when any of the telemetry var is…
Browse files Browse the repository at this point in the history
… not there in the env
  • Loading branch information
rm3l committed Oct 28, 2022
1 parent 48669d3 commit 75f2321
Showing 1 changed file with 81 additions and 10 deletions.
91 changes: 81 additions & 10 deletions pkg/segment/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,87 @@ func TestIsTelemetryEnabled(t *testing.T) {
want func(odoDisableTelemetry, odoTrackingConsent string, consentTelemetry bool) bool
}
var tests []testStruct

// When there is no telemetry-related environment variable defined, rely on the ConsentTelemetry preference
for _, consentTelemetry := range []bool{true, false} {
consentTelemetry := consentTelemetry
tests = append(tests, testStruct{
name: fmt.Sprintf(
"ODO_DISABLE_TELEMETRY and ODO_TRACKING_CONSENT not set, consentTelemetry=%v", consentTelemetry),
consentTelemetryPref: consentTelemetry,
want: func(_, _ string, consentTelemetry bool) bool {
return consentTelemetry
},
})
}

// When only ODO_TRACKING_CONSENT is present in the env, it takes precedence over the ConsentTelemetry preference,
// unless it has an invalid value.
for _, odoTrackingConsent := range []string{"", "yes", "no", "bar"} {
for _, consentTelemetry := range []bool{true, false} {
odoTrackingConsent := odoTrackingConsent
consentTelemetry := consentTelemetry
tests = append(tests, testStruct{
name: fmt.Sprintf(
"ODO_DISABLE_TELEMETRY not set, ODO_TRACKING_CONSENT=%q, consentTelemetry=%v", odoTrackingConsent, consentTelemetry),
consentTelemetryPref: consentTelemetry,
env: map[string]string{
TrackingConsentEnv: odoTrackingConsent,
},
want: func(_, odoTrackingConsent string, consentTelemetry bool) bool {
// ODO_TRACKING_CONSENT takes precedence
switch odoTrackingConsent {
case "yes":
return true
case "no":
return false
default:
return consentTelemetry
}
},
})
}
}

// When only ODO_DISABLE_TELEMETRY is present in the env, it takes precedence over the ConsentTelemetry preference,
// only if ODO_DISABLE_TELEMETRY=true
for _, odoDisableTelemetry := range []string{"", "true", "false", "foo"} {
for _, consentTelemetry := range []bool{true, false} {
odoDisableTelemetry := odoDisableTelemetry
consentTelemetry := consentTelemetry
tests = append(tests, testStruct{
name: fmt.Sprintf("ODO_DISABLE_TELEMETRY=%q,ODO_TRACKING_CONSENT not set,ConsentTelemetry=%v",
odoDisableTelemetry, consentTelemetry),
env: map[string]string{
//lint:ignore SA1019 We deprecated this env var, but until it is removed, we still want to test it
DisableTelemetryEnv: odoDisableTelemetry,
},
want: func(odoDisableTelemetry, _ string, consentTelemetry bool) bool {
if odoDisableTelemetry == "true" {
return false
}
//All other cases, we rely on the ConsentTelemetry preference
return consentTelemetry
},
})
}
}
//Cases where all the environment variables are there.
for _, odoDisableTelemetry := range []string{"", "true", "false", "foo"} {
for _, odoTrackingConsent := range []string{"", "yes", "no", "bar"} {
for _, consentTelemetry := range []bool{true, false} {
odoDisableTelemetry := odoDisableTelemetry
odoTrackingConsent := odoTrackingConsent
consentTelemetry := consentTelemetry
tests = append(tests, testStruct{
name: fmt.Sprintf("ODO_DISABLE_TELEMETRY=%q,ODO_TRACKING_CONSENT=%q,ConsentTelemetry=%v",
odoDisableTelemetry, odoTrackingConsent, consentTelemetry),
env: map[string]string{
//lint:ignore SA1019 We deprecated this env var, but until it is removed, we still want to test it
DisableTelemetryEnv: odoDisableTelemetry,
TrackingConsentEnv: odoTrackingConsent,
},
consentTelemetryPref: consentTelemetry,
want: func(odoDisableTelemetry, odoTrackingConsent string, consentTelemetry bool) bool {
if odoDisableTelemetry == "true" || odoTrackingConsent == "no" {
return false
Expand All @@ -212,18 +286,11 @@ func TestIsTelemetryEnabled(t *testing.T) {
}
return consentTelemetry
},
name: fmt.Sprintf("ODO_DISABLE_TELEMETRY=%q,ODO_TRACKING_CONSENT=%q,ConsentTelemetry=%v",
odoDisableTelemetry, odoTrackingConsent, consentTelemetry),
env: map[string]string{
//lint:ignore SA1019 We deprecated this env var, but until it is removed, we still want to test it
DisableTelemetryEnv: odoDisableTelemetry,
TrackingConsentEnv: odoTrackingConsent,
},
consentTelemetryPref: consentTelemetry,
})
}
}
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.env {
Expand All @@ -233,9 +300,13 @@ func TestIsTelemetryEnabled(t *testing.T) {
cfg := preference.NewMockClient(ctrl)
cfg.EXPECT().GetConsentTelemetry().Return(tt.consentTelemetryPref).AnyTimes()

got := IsTelemetryEnabled(cfg)

//lint:ignore SA1019 We deprecated this env var, but until it is removed, we still want to test it
if IsTelemetryEnabled(cfg) != tt.want(tt.env[DisableTelemetryEnv], tt.env[TrackingConsentEnv], tt.consentTelemetryPref) {
t.Errorf(tt.name, "env is set to %v. %s is set to %q.", tt.env, preference.ConsentTelemetrySetting, tt.consentTelemetryPref)
want := tt.want(tt.env[DisableTelemetryEnv], tt.env[TrackingConsentEnv], tt.consentTelemetryPref)
if got != want {
t.Errorf(tt.name, "IsTelemetryEnabled: got %v, wanted %v. Env is set to %v. %s is set to %q.",
got, want, tt.env, preference.ConsentTelemetrySetting, tt.consentTelemetryPref)
}
})
}
Expand Down

0 comments on commit 75f2321

Please sign in to comment.