Skip to content

Commit 3df0a93

Browse files
authored
chore: do not depend on user env for tests (envoyproxy#1434)
**Description** Always reset the environment variables before running tests to avoid test failures if the user environment uses them. Otherwise, if users have some env vars (such as the OTEL or Anthropic key and run `make test`, tests may fail with no proper feedback. **Related Issues/PRs (if applicable)** N/A **Special notes for reviewers (if applicable)** N/A --------- Signed-off-by: Ignasi Barrera <[email protected]>
1 parent 6604b49 commit 3df0a93

File tree

7 files changed

+61
-6
lines changed

7 files changed

+61
-6
lines changed

cmd/aigw/config_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ var testMcpServers = &autoconfig.MCPServers{
2424
},
2525
}
2626

27+
// clearEnv clears any credential environment variable that can affect the autoconfig.
28+
func clearEnv(t *testing.T) {
29+
t.Helper()
30+
t.Setenv("OPENAI_API_KEY", "")
31+
t.Setenv("OPENAI_BASE_URL", "")
32+
t.Setenv("AZURE_OPENAI_API_KEY", "")
33+
t.Setenv("ANTHROPIC_API_KEY", "")
34+
t.Setenv("ANTHROPIC_BASE_URL", "")
35+
}
36+
2737
// TestReadConfig is mainly for coverage as the autoconfig package is tested more thoroughly.
2838
func TestReadConfig(t *testing.T) {
2939
tests := []struct {
@@ -98,12 +108,7 @@ func TestReadConfig(t *testing.T) {
98108

99109
for _, tt := range tests {
100110
t.Run(tt.name, func(t *testing.T) {
101-
// Clear any existing env vars
102-
t.Setenv("OPENAI_API_KEY", "")
103-
t.Setenv("OPENAI_BASE_URL", "")
104-
t.Setenv("ANTHROPIC_API_KEY", "")
105-
t.Setenv("ANTHROPIC_BASE_URL", "")
106-
111+
clearEnv(t)
107112
for k, v := range tt.envVars {
108113
t.Setenv(k, v)
109114
}
@@ -123,6 +128,7 @@ func TestReadConfig(t *testing.T) {
123128
}
124129

125130
t.Run("error when file and no OPENAI_API_KEY", func(t *testing.T) {
131+
clearEnv(t)
126132
_, err := readConfig("", nil, false)
127133
require.Error(t, err)
128134
require.EqualError(t, err, "you must supply at least OPENAI_API_KEY, AZURE_OPENAI_API_KEY, ANTHROPIC_API_KEY, or a config file path")
@@ -185,6 +191,7 @@ func TestExpandPath(t *testing.T) {
185191
}
186192
for _, tt := range tests {
187193
t.Run(tt.name, func(t *testing.T) {
194+
clearEnv(t)
188195
for k, v := range tt.envVars {
189196
t.Setenv(k, v)
190197
}

cmd/aigw/main_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Flags:
152152
}
153153
for _, tt := range tests {
154154
t.Run(tt.name, func(t *testing.T) {
155+
clearEnv(t)
155156
for k, v := range tt.env {
156157
t.Setenv(k, v)
157158
}
@@ -221,6 +222,7 @@ func TestCmd_BeforeApply(t *testing.T) {
221222

222223
for _, tt := range tests {
223224
t.Run(tt.name, func(t *testing.T) {
225+
clearEnv(t)
224226
for k, v := range tt.envVars {
225227
t.Setenv(k, v)
226228
}
@@ -418,6 +420,7 @@ func TestCmdRun_Validate(t *testing.T) {
418420

419421
for _, tt := range tests {
420422
t.Run(tt.name, func(t *testing.T) {
423+
clearEnv(t)
421424
for k, v := range tt.envVars {
422425
t.Setenv(k, v)
423426
}

internal/metrics/metrics_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ import (
2121
"go.opentelemetry.io/otel/sdk/metric/metricdata"
2222
)
2323

24+
// clearEnv clears any OTEL configuration that could exist in the environment.
25+
func clearEnv(t *testing.T) {
26+
t.Helper()
27+
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "")
28+
t.Setenv("OTEL_METRICS_EXPORTER", "")
29+
t.Setenv("OTEL_SERVICE_NAME", "")
30+
}
31+
2432
// TestNewMetricsFromEnv_ConsoleExporter tests console/none exporter configuration.
2533
// We use synctest here because console output relies on time.Sleep to wait for
2634
// the periodic exporter, and synctest makes these sleeps instant in wall-clock time.
@@ -95,6 +103,7 @@ func TestNewMetricsFromEnv_ConsoleExporter(t *testing.T) {
95103
t.Run(tt.name, func(t *testing.T) {
96104
synctest.Test(t, func(t *testing.T) {
97105
t.Helper()
106+
clearEnv(t)
98107
t.Setenv("OTEL_METRIC_EXPORT_INTERVAL", "100")
99108
for k, v := range tt.env {
100109
t.Setenv(k, v)
@@ -166,6 +175,7 @@ func TestNewMetricsFromEnv_ConsoleExporter(t *testing.T) {
166175
func TestNewMetricsFromEnv_ConsoleExporter_NoMetrics(t *testing.T) {
167176
synctest.Test(t, func(t *testing.T) {
168177
t.Helper()
178+
clearEnv(t)
169179
t.Setenv("OTEL_METRIC_EXPORT_INTERVAL", "100")
170180
t.Setenv("OTEL_METRICS_EXPORTER", "console")
171181

@@ -255,6 +265,7 @@ func TestNewMetricsFromEnv_NetworkExporters(t *testing.T) {
255265

256266
for _, tt := range tests {
257267
t.Run(tt.name, func(t *testing.T) {
268+
clearEnv(t)
258269
for k, v := range tt.env {
259270
t.Setenv(k, v)
260271
}
@@ -344,6 +355,7 @@ func TestNewMetricsFromEnv_PrometheusReader(t *testing.T) {
344355

345356
for _, tt := range tests {
346357
t.Run(tt.name, func(t *testing.T) {
358+
clearEnv(t)
347359
for k, v := range tt.env {
348360
t.Setenv(k, v)
349361
}
@@ -416,6 +428,7 @@ func TestNewMetricsFromEnv_ErrorHandling(t *testing.T) {
416428

417429
for _, tt := range tests {
418430
t.Run(tt.name, func(t *testing.T) {
431+
clearEnv(t)
419432
for k, v := range tt.env {
420433
t.Setenv(k, v)
421434
}
@@ -439,6 +452,7 @@ func TestNewMetricsFromEnv_OTLPHeaders(t *testing.T) {
439452
}))
440453
t.Cleanup(ts.Close)
441454

455+
clearEnv(t)
442456
t.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "Authorization="+expectedAuthorization)
443457
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", ts.URL)
444458
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")

internal/metrics/nonempty_exporter_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func TestNewNonEmptyConsoleExporter(t *testing.T) {
5454

5555
for _, tt := range tests {
5656
t.Run(tt.name, func(t *testing.T) {
57+
clearEnv(t)
5758
t.Setenv("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE", tt.envValue)
5859

5960
var buf bytes.Buffer
@@ -105,6 +106,7 @@ func TestParseTemporalityPreference(t *testing.T) {
105106

106107
for _, tt := range tests {
107108
t.Run(tt.name, func(t *testing.T) {
109+
clearEnv(t)
108110
t.Setenv("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE", tt.envValue)
109111

110112
temporality, err := parseTemporalityPreference()

internal/tracing/tracing_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import (
2525
"github.com/envoyproxy/ai-gateway/internal/tracing/openinference"
2626
)
2727

28+
// clearEnv clears any OTEL configuration that could exist in the environment.
29+
func clearEnv(t *testing.T) {
30+
t.Helper()
31+
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "")
32+
t.Setenv("OTEL_METRICS_EXPORTER", "")
33+
t.Setenv("OTEL_SERVICE_NAME", "")
34+
}
35+
2836
// TestNewTracingFromEnv_DefaultServiceName tests that the service name.
2937
// defaults to "ai-gateway" when OTEL_SERVICE_NAME is not set.
3038
func TestNewTracingFromEnv_DefaultServiceName(t *testing.T) {
@@ -52,6 +60,7 @@ func TestNewTracingFromEnv_DefaultServiceName(t *testing.T) {
5260

5361
for _, tt := range tests {
5462
t.Run(tt.name, func(t *testing.T) {
63+
clearEnv(t)
5564
for k, v := range tt.env {
5665
t.Setenv(k, v)
5766
}
@@ -109,6 +118,7 @@ func TestNewTracingFromEnv_DisabledByEnv(t *testing.T) {
109118

110119
for _, tt := range tests {
111120
t.Run(tt.name, func(t *testing.T) {
121+
clearEnv(t)
112122
for k, v := range tt.env {
113123
t.Setenv(k, v)
114124
}
@@ -162,6 +172,7 @@ func TestNewTracingFromEnv_EndpointHierarchy(t *testing.T) {
162172

163173
for _, tt := range tests {
164174
t.Run(tt.name, func(t *testing.T) {
175+
clearEnv(t)
165176
for k, v := range tt.env {
166177
t.Setenv(k, v)
167178
}
@@ -226,6 +237,7 @@ func TestNewTracingFromEnv_ConsoleExporter(t *testing.T) {
226237

227238
for _, tt := range tests {
228239
t.Run(tt.name, func(t *testing.T) {
240+
clearEnv(t)
229241
for k, v := range tt.env {
230242
t.Setenv(k, v)
231243
}
@@ -274,6 +286,7 @@ func TestNewTracingFromEnv_Exporter(t *testing.T) {
274286
// Just test 2 exporters to prove the SDK is wired up correctly.
275287
for _, exporter := range []string{"console", "otlp"} {
276288
t.Run(exporter, func(t *testing.T) {
289+
clearEnv(t)
277290
t.Setenv("OTEL_TRACES_EXPORTER", exporter)
278291

279292
var stdout bytes.Buffer
@@ -313,6 +326,7 @@ func TestNewTracingFromEnv_TracesSampler(t *testing.T) {
313326

314327
for _, tt := range tests {
315328
t.Run(tt.sampler, func(t *testing.T) {
329+
clearEnv(t)
316330
t.Setenv("OTEL_TRACES_SAMPLER", tt.sampler)
317331
collector, tracing := newTracingFromEnvForTest(t, io.Discard)
318332

@@ -359,6 +373,7 @@ func TestNewTracingFromEnv_OtelPropagators(t *testing.T) {
359373

360374
for _, tt := range tests {
361375
t.Run(tt.propagator, func(t *testing.T) {
376+
clearEnv(t)
362377
t.Setenv("OTEL_PROPAGATORS", tt.propagator)
363378
collector, tracing := newTracingFromEnvForTest(t, io.Discard)
364379

@@ -424,6 +439,7 @@ func TestNewTracingFromEnv_ChatCompletion_Redaction(t *testing.T) {
424439

425440
for _, tt := range tests {
426441
t.Run(tt.name, func(t *testing.T) {
442+
clearEnv(t)
427443
t.Setenv(openinference.EnvHideInputs, strconv.FormatBool(tt.hideInputs))
428444
t.Setenv(openinference.EnvHideOutputs, strconv.FormatBool(tt.hideOutputs))
429445

@@ -530,6 +546,7 @@ func TestNewTracingFromEnv_OTLPHeaders(t *testing.T) {
530546
}))
531547
t.Cleanup(ts.Close)
532548

549+
clearEnv(t)
533550
t.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "Authorization="+expectedAuthorization)
534551
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", ts.URL)
535552
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")
@@ -558,6 +575,7 @@ func TestNewTracingFromEnv_OTLPHeaders(t *testing.T) {
558575
func TestNewTracingFromEnv_HeaderAttributeMapping(t *testing.T) {
559576
collector := testotel.StartOTLPCollector()
560577
t.Cleanup(collector.Close)
578+
clearEnv(t)
561579
collector.SetEnv(t.Setenv)
562580

563581
mapping := map[string]string{
@@ -626,6 +644,7 @@ func TestNewTracingFromEnv_Embeddings_Redaction(t *testing.T) {
626644

627645
for _, tt := range tests {
628646
t.Run(tt.name, func(t *testing.T) {
647+
clearEnv(t)
629648
t.Setenv(openinference.EnvHideEmbeddingsText, strconv.FormatBool(tt.hideEmbeddingsText))
630649
t.Setenv(openinference.EnvHideEmbeddingsVectors, strconv.FormatBool(tt.hideEmbeddingsVectors))
631650

tests/extproc/mcp/env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ const (
7070
)
7171

7272
func requireNewMCPEnv(t *testing.T, forceJSONResponse bool, writeTimeout time.Duration, path string) *mcpEnv {
73+
// clear env vars before starting the tests
74+
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "")
75+
t.Setenv("OTEL_METRICS_EXPORTER", "")
76+
t.Setenv("OTEL_SERVICE_NAME", "")
77+
7378
collector := testotel.StartOTLPCollector()
7479
t.Cleanup(collector.Close)
7580
mcpConfig := &filterapi.MCPConfig{

tests/extproc/vcr/otel_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type otelTestEnvironment struct {
3232

3333
// setupOtelTestEnvironment starts all required services and returns ports and a closer.
3434
func setupOtelTestEnvironment(t *testing.T, extraExtProcEnv ...string) *otelTestEnvironment {
35+
// clear env vars before starting the tests
36+
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "")
37+
t.Setenv("OTEL_METRICS_EXPORTER", "")
38+
t.Setenv("OTEL_SERVICE_NAME", "")
39+
3540
// Start OTLP collector.
3641
collector := testotel.StartOTLPCollector()
3742
t.Cleanup(collector.Close)

0 commit comments

Comments
 (0)