forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for OTel Collector start (elastic#4787)
- Loading branch information
1 parent
0e34a69
commit 03707a8
Showing
5 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package otel | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/otelcol" | ||
) | ||
|
||
func TestStartCollector(t *testing.T) { | ||
testCases := []struct { | ||
configFile string | ||
expectedErrorMessage string | ||
}{ | ||
{ | ||
configFile: "all-components.yml", | ||
expectedErrorMessage: "", // empty string means no error is expected | ||
}, | ||
{ | ||
configFile: "nonexistent-component.yml", | ||
expectedErrorMessage: `error decoding 'extensions': unknown type: "zpages"`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.configFile, func(t *testing.T) { | ||
configFiles := getConfigFiles(tc.configFile) | ||
settings, err := newSettings("test", configFiles) | ||
require.NoError(t, err) | ||
|
||
collector, err := otelcol.NewCollector(*settings) | ||
require.NoError(t, err) | ||
require.NotNil(t, collector) | ||
|
||
wg := startCollector(context.Background(), t, collector, tc.expectedErrorMessage) | ||
|
||
if tc.expectedErrorMessage == "" { | ||
assert.Eventually(t, func() bool { | ||
return otelcol.StateRunning == collector.GetState() | ||
}, 2*time.Second, 200*time.Millisecond) | ||
} | ||
collector.Shutdown() | ||
wg.Wait() | ||
assert.Equal(t, otelcol.StateClosed, collector.GetState()) | ||
}) | ||
} | ||
} | ||
|
||
// getConfigFiles returns a collection of config file paths for the collector to use. | ||
// In the simplest scenario, the collection will contains only one path. | ||
// In case there is an operating system-specific override file found, it will be added to the collection. | ||
// E.g. if the input file name is `all-components.yml` and a file named `all-components.windows.yml` exists, | ||
// the config path collection will have two elements on Windows, and only one element on other OSes. | ||
// Use `darwin` for MacOS, `linux` for Linux and `windows` for Windows. | ||
func getConfigFiles(configFileName string) []string { | ||
// Add base file to the collection. | ||
baseFilePath := filepath.Join(".", "testdata", configFileName) | ||
configFiles := []string{"file:" + baseFilePath} | ||
|
||
// Check if an os-specific override file exists; if it does, add it to the collection. | ||
overrideFileName := strings.TrimSuffix(configFileName, filepath.Ext(configFileName)) + "." + runtime.GOOS + filepath.Ext(configFileName) | ||
overrideFilePath := filepath.Join(".", "testdata", overrideFileName) | ||
if _, err := os.Stat(overrideFilePath); err == nil { | ||
configFiles = append(configFiles, "file:"+overrideFilePath) | ||
} | ||
|
||
return configFiles | ||
} | ||
|
||
func startCollector(ctx context.Context, t *testing.T, col *otelcol.Collector, expectedErrorMessage string) *sync.WaitGroup { | ||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err := col.Run(ctx) | ||
if expectedErrorMessage == "" { | ||
require.NoError(t, err) | ||
} else { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), expectedErrorMessage) | ||
} | ||
}() | ||
return wg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exporters: | ||
file: | ||
path: ${env:TMP}\file-exporter-output.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
exporters: | ||
debug: | ||
elasticsearch: | ||
endpoints: | ||
- localhost:9200 | ||
file: | ||
path: /tmp/file-exporter-output.json | ||
otlp: | ||
endpoint: localhots:4317 | ||
|
||
extensions: | ||
memory_limiter: | ||
check_interval: 5s | ||
limit_mib: 100 | ||
|
||
processors: | ||
attributes: | ||
actions: | ||
- action: insert | ||
key: key1 | ||
value: value1 | ||
batch: | ||
filter: | ||
resource: | ||
attributes: | ||
- action: insert | ||
key: key1 | ||
value: value1 | ||
transform: | ||
|
||
receivers: | ||
filelog: | ||
include: | ||
- /filelog/path | ||
otlp: | ||
protocols: | ||
grpc: | ||
http: | ||
|
||
service: | ||
extensions: | ||
- memory_limiter | ||
pipelines: | ||
logs: | ||
exporters: | ||
- debug | ||
- elasticsearch | ||
- file | ||
- otlp | ||
processors: | ||
- attributes | ||
- batch | ||
- filter | ||
- resource | ||
- transform | ||
receivers: | ||
- filelog | ||
- otlp | ||
|
||
metrics: | ||
exporters: | ||
- debug | ||
- file | ||
- otlp | ||
processors: | ||
- attributes | ||
- batch | ||
- filter | ||
- resource | ||
- transform | ||
receivers: | ||
- otlp | ||
|
||
traces: | ||
exporters: | ||
- debug | ||
- elasticsearch | ||
- file | ||
- otlp | ||
processors: | ||
- attributes | ||
- batch | ||
- filter | ||
- resource | ||
- transform | ||
receivers: | ||
- otlp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
|
||
exporters: | ||
debug: | ||
|
||
extensions: | ||
zpages: | ||
|
||
service: | ||
extensions: | ||
- zpages | ||
pipelines: | ||
logs: | ||
exporters: | ||
- debug | ||
receivers: | ||
- otlp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,4 @@ service: | |
receivers: [filelog] | ||
processors: [resource] | ||
exporters: | ||
- debug | ||
- debug |