Skip to content

Commit eba6529

Browse files
committed
use observer and t.Setenv
1 parent ba1336c commit eba6529

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

internal/pkg/otel/samples_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/elastic/elastic-agent/pkg/core/logger/loggertest"
1516
"github.com/stretchr/testify/assert"
1617
"go.opentelemetry.io/collector/otelcol"
1718
"go.uber.org/zap"
@@ -21,25 +22,15 @@ import (
2122
func TestSamples(t *testing.T) {
2223
// Set environment variables required by the sample configurations.
2324
// These are just sample values to allow the collector to start.
24-
os.Setenv("STORAGE_DIR", t.TempDir())
25-
os.Setenv("ELASTIC_ENDPOINT", "http://localhost:9200")
26-
os.Setenv("ELASTIC_API_KEY", "test_api_key")
27-
os.Setenv("ELASTIC_OTLP_ENDPOINT", "http://localhost:4318")
28-
os.Setenv("AUTOOPS_ES_URL", "http://localhost:9200")
29-
os.Setenv("AUTOOPS_TOKEN", "token")
30-
os.Setenv("AUTOOPS_TEMP_RESOURCE_ID", "temp")
31-
os.Setenv("AUTOOPS_OTEL_URL", "http://localhost:4318")
25+
t.Setenv("STORAGE_DIR", t.TempDir())
26+
t.Setenv("ELASTIC_ENDPOINT", "http://localhost:9200")
27+
t.Setenv("ELASTIC_API_KEY", "test_api_key")
28+
t.Setenv("ELASTIC_OTLP_ENDPOINT", "http://localhost:4318")
29+
t.Setenv("AUTOOPS_ES_URL", "http://localhost:9200")
30+
t.Setenv("AUTOOPS_TOKEN", "token")
31+
t.Setenv("AUTOOPS_TEMP_RESOURCE_ID", "temp")
32+
t.Setenv("AUTOOPS_OTEL_URL", "http://localhost:4318")
3233

33-
defer func() {
34-
os.Unsetenv("STORAGE_DIR")
35-
os.Unsetenv("ELASTIC_ENDPOINT")
36-
os.Unsetenv("ELASTIC_API_KEY")
37-
os.Unsetenv("ELASTIC_OTLP_ENDPOINT")
38-
os.Unsetenv("AUTOOPS_ES_URL")
39-
os.Unsetenv("AUTOOPS_TOKEN")
40-
os.Unsetenv("AUTOOPS_TEMP_RESOURCE_ID")
41-
os.Unsetenv("AUTOOPS_OTEL_URL")
42-
}()
4334
err := filepath.WalkDir(filepath.Join(".", "samples", runtime.GOOS), func(path string, d os.DirEntry, err error) error {
4435
if err != nil {
4536
return err
@@ -55,9 +46,17 @@ func TestSamples(t *testing.T) {
5546
}
5647

5748
func testSample(t *testing.T, configFile string) {
49+
base, obs := loggertest.New("otel")
50+
t.Cleanup(func() {
51+
if t.Failed() {
52+
for _, log := range obs.All() {
53+
t.Logf("%+v", log)
54+
}
55+
}
56+
})
5857
settings := NewSettings("test", []string{configFile})
5958
settings.LoggingOptions = []zap.Option{zap.WrapCore(func(zapcore.Core) zapcore.Core {
60-
return zapcore.NewNopCore()
59+
return base.Core()
6160
})}
6261
collector, err := otelcol.NewCollector(*settings)
6362
assert.NoError(t, err)

0 commit comments

Comments
 (0)