|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License 2.0; |
| 3 | +// you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +package otel |
| 6 | + |
| 7 | +import ( |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "go.opentelemetry.io/collector/featuregate" |
| 15 | + "go.opentelemetry.io/collector/otelcol" |
| 16 | + "go.uber.org/zap" |
| 17 | + "go.uber.org/zap/zapcore" |
| 18 | +) |
| 19 | + |
| 20 | +func TestSamples(t *testing.T) { |
| 21 | + // Set environment variables required by the sample configurations. |
| 22 | + // These are just sample values to allow the collector to start. |
| 23 | + t.Setenv("STORAGE_DIR", t.TempDir()) |
| 24 | + t.Setenv("ELASTIC_ENDPOINT", "http://localhost:9200") |
| 25 | + t.Setenv("ELASTIC_API_KEY", "test_api_key") |
| 26 | + t.Setenv("ELASTIC_OTLP_ENDPOINT", "http://localhost:4318") |
| 27 | + t.Setenv("AUTOOPS_ES_URL", "http://localhost:9200") |
| 28 | + t.Setenv("AUTOOPS_TOKEN", "token") |
| 29 | + t.Setenv("AUTOOPS_TEMP_RESOURCE_ID", "temp") |
| 30 | + t.Setenv("AUTOOPS_OTEL_URL", "http://localhost:4318") |
| 31 | + |
| 32 | + // Enable service.profilesSupport featuregate to test the profiling samples. |
| 33 | + assert.NoError(t, featuregate.GlobalRegistry().Set("service.profilesSupport", true)) |
| 34 | + defer func() { |
| 35 | + assert.NoError(t, featuregate.GlobalRegistry().Set("service.profilesSupport", false)) |
| 36 | + }() |
| 37 | + |
| 38 | + err := filepath.WalkDir(filepath.Join(".", "samples", runtime.GOOS), func(path string, d os.DirEntry, err error) error { |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + if !d.IsDir() && (filepath.Ext(d.Name()) == ".yaml" || filepath.Ext(d.Name()) == ".yml") { |
| 43 | + t.Run(d.Name(), func(t *testing.T) { |
| 44 | + testSample(t, filepath.Join(".", "samples", runtime.GOOS, d.Name())) |
| 45 | + }) |
| 46 | + } |
| 47 | + return nil |
| 48 | + }) |
| 49 | + assert.NoError(t, err) |
| 50 | +} |
| 51 | + |
| 52 | +func testSample(t *testing.T, configFile string) { |
| 53 | + settings := NewSettings("test", []string{configFile}) |
| 54 | + settings.LoggingOptions = []zap.Option{zap.WrapCore(func(zapcore.Core) zapcore.Core { |
| 55 | + // TODO: Replace with observer core. Right now, it results in a race condition. |
| 56 | + return zapcore.NewNopCore() |
| 57 | + })} |
| 58 | + collector, err := otelcol.NewCollector(*settings) |
| 59 | + assert.NoError(t, err) |
| 60 | + assert.NotNil(t, collector) |
| 61 | + assert.NoError(t, collector.DryRun(t.Context())) |
| 62 | + collector.Shutdown() |
| 63 | +} |
0 commit comments