Skip to content

Commit

Permalink
add integration test for envvar config source (open-telemetry#2229)
Browse files Browse the repository at this point in the history
* add integration test for envvar config source

* use CollectorContainer EffectiveConfig method

* include initial config in env config source test

Co-authored-by: Ryan Fitzpatrick <[email protected]>
  • Loading branch information
ccordi and rmfitzpatrick authored Nov 9, 2022
1 parent 831a718 commit 31cc998
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
97 changes: 96 additions & 1 deletion tests/general/envvar_expansion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package tests

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -95,7 +96,101 @@ func TestExpandedYamlViaEnvConfigSource(t *testing.T) {
)

defer shutdown()

expectedResourceMetrics := tc.ResourceMetrics("yaml_from_env.yaml")
require.NoError(t, tc.OTLPReceiverSink.AssertAllMetricsReceived(t, *expectedResourceMetrics, 30*time.Second))
}

func TestEnvConfigSource(t *testing.T) {
tc := testutils.NewTestcase(t)
defer tc.PrintLogsOnFailure()
defer tc.ShutdownOTLPReceiverSink()

configServerPort := testutils.GetAvailablePort(t)
tc.SkipIfNotContainer()
c, shutdown := tc.SplunkOtelCollectorWithEnv(
"envvar_config.yaml",
map[string]string{
"OVERRIDDEN_DEFAULT": "{ grpc: , http: , }",
"SPLUNK_DEBUG_CONFIG_SERVER_PORT": fmt.Sprintf("%d", configServerPort),
},
)
defer shutdown()
cc := c.(*testutils.CollectorContainer)

expectedInitial := map[string]any{
"file": map[string]any{
"config_sources": map[string]any{
"env": map[string]any{
"defaults": map[string]any{
"OVERRIDDEN_DEFAULT": "{ http: , }",
"USED_DEFAULT": "localhost:23456",
},
},
},
"receivers": map[string]any{
"otlp": map[string]any{
"protocols": "${env:OVERRIDDEN_DEFAULT}",
},
"hostmetrics": map[string]any{
"scrapers": map[string]any{
"cpu": nil,
"memory": nil,
},
},
},
"exporters": map[string]any{
"otlp": map[string]any{
"endpoint": "${env:USED_DEFAULT}",
"tls": map[string]any{
"insecure": true,
},
},
},
"service": map[string]any{
"pipelines": map[string]any{
"metrics": map[string]any{
"receivers": []any{"hostmetrics"},
"exporters": []any{"otlp"},
},
},
},
},
}

require.Equal(t, expectedInitial, cc.InitialConfig(t, configServerPort))

expectedEffective := map[string]any{
"receivers": map[string]any{
"otlp": map[string]any{
"protocols": map[string]any{
"grpc": nil,
"http": nil,
},
},
"hostmetrics": map[string]any{
"scrapers": map[string]any{
"cpu": nil,
"memory": nil,
},
},
},
"exporters": map[string]any{
"otlp": map[string]any{
"endpoint": "localhost:23456",
"tls": map[string]any{
"insecure": true,
},
},
},
"service": map[string]any{
"pipelines": map[string]any{
"metrics": map[string]any{
"receivers": []any{"hostmetrics"},
"exporters": []any{"otlp"},
},
},
},
}

require.Equal(t, expectedEffective, cc.EffectiveConfig(t, configServerPort))
}
24 changes: 24 additions & 0 deletions tests/general/testdata/envvar_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
config_sources:
env:
defaults:
OVERRIDDEN_DEFAULT: "{ http: , }"
USED_DEFAULT: localhost:23456

receivers:
otlp:
protocols:
${env:OVERRIDDEN_DEFAULT}
hostmetrics:
scrapers:
cpu:
memory:
exporters:
otlp:
endpoint: ${env:USED_DEFAULT}
tls:
insecure: true
service:
pipelines:
metrics:
receivers: [ hostmetrics ]
exporters: [ otlp ]
4 changes: 2 additions & 2 deletions tests/testutils/collector_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (l collectorLogConsumer) Accept(log testcontainers.Log) {
}
}

func (collector *CollectorContainer) InitialConfig(t testing.TB, port int) map[string]any {
func (collector *CollectorContainer) InitialConfig(t testing.TB, port uint16) map[string]any {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
n, r, err := collector.Container.Exec(ctx, []string{"curl", "-s", fmt.Sprintf("http://localhost:%d/debug/configz/initial", port)})
Expand All @@ -267,7 +267,7 @@ func (collector *CollectorContainer) InitialConfig(t testing.TB, port int) map[s
return confmap.NewFromStringMap(actual).ToStringMap()
}

func (collector *CollectorContainer) EffectiveConfig(t testing.TB, port int) map[string]any {
func (collector *CollectorContainer) EffectiveConfig(t testing.TB, port uint16) map[string]any {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
n, r, err := collector.Container.Exec(ctx, []string{"curl", "-s", fmt.Sprintf("http://localhost:%d/debug/configz/effective", port)})
Expand Down

0 comments on commit 31cc998

Please sign in to comment.