Skip to content

Commit

Permalink
Add test for OTel Collector start (elastic#4787)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzej-stencel authored May 23, 2024
1 parent 0e34a69 commit 03707a8
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 1 deletion.
96 changes: 96 additions & 0 deletions internal/pkg/otel/run_test.go
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
}
3 changes: 3 additions & 0 deletions internal/pkg/otel/testdata/all-components.windows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exporters:
file:
path: ${env:TMP}\file-exporter-output.json
87 changes: 87 additions & 0 deletions internal/pkg/otel/testdata/all-components.yml
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
20 changes: 20 additions & 0 deletions internal/pkg/otel/testdata/nonexistent-component.yml
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
2 changes: 1 addition & 1 deletion internal/pkg/otel/testdata/otel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ service:
receivers: [filelog]
processors: [resource]
exporters:
- debug
- debug

0 comments on commit 03707a8

Please sign in to comment.