Skip to content

Commit 8629351

Browse files
Stop integration tests from sending telemetry to production (#320)
1 parent b1372f1 commit 8629351

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

test/integration/env/env.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ const (
2222
OtelEndpoint Key = "OTEL_EXPORTER_OTLP_ENDPOINT"
2323
)
2424

25+
// UnreachableAnalyticsEndpoint is a closed local port used as the default
26+
// analytics endpoint for every test environment, so the binary under test never
27+
// reports telemetry to the production analytics backend (which would pollute it,
28+
// e.g. with "start" events tagged as coming from CI or an AI agent). Tests that
29+
// exercise telemetry override it with a mock server URL via With(AnalyticsEndpoint, ...);
30+
// the explicit value wins because exec dedups duplicate keys to the last value.
31+
const UnreachableAnalyticsEndpoint = "http://127.0.0.1:1"
32+
2533
func Get(key Key) string {
2634
return os.Getenv(string(key))
2735
}
@@ -38,11 +46,11 @@ func Require(t testing.TB, key Key) string {
3846
type Environ []string
3947

4048
func Without(keys ...Key) Environ {
41-
return Environ(os.Environ()).Without(keys...)
49+
return Environ(os.Environ()).With(AnalyticsEndpoint, UnreachableAnalyticsEndpoint).Without(keys...)
4250
}
4351

4452
func With(key Key, value string) Environ {
45-
return Environ(os.Environ()).With(key, value)
53+
return Environ(os.Environ()).With(AnalyticsEndpoint, UnreachableAnalyticsEndpoint).With(key, value)
4654
}
4755

4856
func (e Environ) Without(keys ...Key) Environ {

test/integration/env/env_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package env
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
// resolve returns the effective value of key in e, mirroring how os/exec
9+
// dedups duplicate keys: the last occurrence wins.
10+
func resolve(e Environ, key Key) (string, bool) {
11+
value, found := "", false
12+
prefix := string(key) + "="
13+
for _, entry := range e {
14+
if strings.HasPrefix(entry, prefix) {
15+
value, found = strings.TrimPrefix(entry, prefix), true
16+
}
17+
}
18+
return value, found
19+
}
20+
21+
// Guards against telemetry from integration tests reaching the production
22+
// analytics backend: the base env helpers must default the analytics endpoint
23+
// to an unreachable address unless a test explicitly overrides it.
24+
func TestBaseEnvDefaultsAnalyticsEndpointToUnreachable(t *testing.T) {
25+
cases := map[string]Environ{
26+
"With": With("SOME_VAR", "value"),
27+
"Without": Without(AuthToken),
28+
}
29+
for name, e := range cases {
30+
t.Run(name, func(t *testing.T) {
31+
got, found := resolve(e, AnalyticsEndpoint)
32+
if !found {
33+
t.Fatalf("%s did not set %s", name, AnalyticsEndpoint)
34+
}
35+
if got != UnreachableAnalyticsEndpoint {
36+
t.Fatalf("%s analytics endpoint = %q, want %q", name, got, UnreachableAnalyticsEndpoint)
37+
}
38+
})
39+
}
40+
}
41+
42+
func TestExplicitAnalyticsEndpointOverridesDefault(t *testing.T) {
43+
const mock = "http://127.0.0.1:54321"
44+
e := With(APIEndpoint, "http://example").With(AnalyticsEndpoint, mock)
45+
got, _ := resolve(e, AnalyticsEndpoint)
46+
if got != mock {
47+
t.Fatalf("analytics endpoint = %q, want %q (explicit override must win over default)", got, mock)
48+
}
49+
}

0 commit comments

Comments
 (0)