@@ -18,6 +18,13 @@ import (
1818 "github.com/nginx/agent/v3/test/stub"
1919 "github.com/stretchr/testify/assert"
2020 "github.com/stretchr/testify/require"
21+ "go.opentelemetry.io/collector/confmap"
22+ "go.opentelemetry.io/collector/confmap/provider/envprovider"
23+ "go.opentelemetry.io/collector/confmap/provider/fileprovider"
24+ "go.opentelemetry.io/collector/confmap/provider/httpprovider"
25+ "go.opentelemetry.io/collector/confmap/provider/httpsprovider"
26+ "go.opentelemetry.io/collector/confmap/provider/yamlprovider"
27+
2128 "go.opentelemetry.io/collector/otelcol"
2229
2330 "github.com/nginx/agent/v3/internal/bus"
@@ -994,6 +1001,73 @@ func TestCollector_findAvailableSyslogServers(t *testing.T) {
9941001 }
9951002}
9961003
1004+ func TestCollector_writeRunningConfig (t * testing.T ) {
1005+ tempDir := t .TempDir ()
1006+
1007+ tests := []struct {
1008+ name string
1009+ writeConfigErr error
1010+ settings otelcol.CollectorSettings
1011+ }{
1012+ {
1013+ name : "Test 1: Write Config Success" ,
1014+ settings : otelcol.CollectorSettings {
1015+ ConfigProviderSettings : otelcol.ConfigProviderSettings {
1016+ ResolverSettings : confmap.ResolverSettings {
1017+ URIs : []string {"./testdata/otel_config.yaml" , "./testdata/custom_otel_config.yaml" },
1018+ ProviderFactories : []confmap.ProviderFactory {
1019+ envprovider .NewFactory (),
1020+ fileprovider .NewFactory (),
1021+ httpprovider .NewFactory (),
1022+ httpsprovider .NewFactory (),
1023+ yamlprovider .NewFactory (),
1024+ },
1025+ DefaultScheme : "" ,
1026+ ProviderSettings : confmap.ProviderSettings {},
1027+ ConverterFactories : nil ,
1028+ ConverterSettings : confmap.ConverterSettings {},
1029+ },
1030+ },
1031+ },
1032+ writeConfigErr : nil ,
1033+ },
1034+ {
1035+ name : "Test 2: Write Config Failed" ,
1036+ settings : otelcol.CollectorSettings {
1037+ ConfigProviderSettings : otelcol.ConfigProviderSettings {
1038+ ResolverSettings : confmap.ResolverSettings {},
1039+ },
1040+ },
1041+ writeConfigErr : errors .New ("unable to create resolver: invalid " +
1042+ "'confmap.ResolverSettings' configuration: no URIs" ),
1043+ },
1044+ }
1045+
1046+ for _ , tt := range tests {
1047+ t .Run (tt .name , func (t * testing.T ) {
1048+ conf := types .OTelConfig (t )
1049+ conf .Collector .Log .Path = filepath .Join (tempDir , "otel-collector-test.log" )
1050+ newCollector , err := NewCollector (conf )
1051+ newCollector .debugOTelConfigPath = filepath .Join (tempDir , "otel-collector-debug-config.yaml" )
1052+ require .NoError (t , err )
1053+
1054+ writeErr := newCollector .writeRunningConfig (context .Background (), tt .settings )
1055+
1056+ if tt .writeConfigErr == nil {
1057+ actual , readErr := os .ReadFile (newCollector .debugOTelConfigPath )
1058+ require .NoError (t , readErr )
1059+
1060+ expected , expectedFileErr := os .ReadFile ("./testdata/merge_config.yaml" )
1061+ require .NoError (t , expectedFileErr )
1062+
1063+ assert .Equal (t , string (expected ), string (actual ))
1064+ } else {
1065+ assert .Equal (t , tt .writeConfigErr .Error (), writeErr .Error ())
1066+ }
1067+ })
1068+ }
1069+ }
1070+
9971071func createFakeCollector () * typesfakes.FakeCollectorInterface {
9981072 fakeCollector := & typesfakes.FakeCollectorInterface {}
9991073 fakeCollector .RunStub = func (ctx context.Context ) error { return nil }
0 commit comments