|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package otelcol // import "go.opentelemetry.io/collector/otelcol" |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "gopkg.in/yaml.v3" |
| 13 | + |
| 14 | + "go.opentelemetry.io/collector/confmap" |
| 15 | + "go.opentelemetry.io/collector/confmap/provider/fileprovider" |
| 16 | + "go.opentelemetry.io/collector/confmap/provider/yamlprovider" |
| 17 | + "go.opentelemetry.io/collector/featuregate" |
| 18 | +) |
| 19 | + |
| 20 | +func TestPrintCommand(t *testing.T) { |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + set confmap.ResolverSettings |
| 24 | + errString string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "no URIs", |
| 28 | + set: confmap.ResolverSettings{}, |
| 29 | + errString: "at least one config flag must be provided", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "valid URI - file not found", |
| 33 | + set: confmap.ResolverSettings{ |
| 34 | + URIs: []string{"file:blabla.yaml"}, |
| 35 | + ProviderFactories: []confmap.ProviderFactory{ |
| 36 | + fileprovider.NewFactory(), |
| 37 | + }, |
| 38 | + DefaultScheme: "file", |
| 39 | + }, |
| 40 | + errString: "cannot retrieve the configuration: unable to read the file", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "valid URI", |
| 44 | + set: confmap.ResolverSettings{ |
| 45 | + URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, |
| 46 | + ProviderFactories: []confmap.ProviderFactory{ |
| 47 | + yamlprovider.NewFactory(), |
| 48 | + }, |
| 49 | + DefaultScheme: "yaml", |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "valid URI - no provider set", |
| 54 | + set: confmap.ResolverSettings{ |
| 55 | + URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, |
| 56 | + DefaultScheme: "yaml", |
| 57 | + }, |
| 58 | + errString: "at least one Provider must be supplied", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "valid URI - invalid scheme name", |
| 62 | + set: confmap.ResolverSettings{ |
| 63 | + URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, |
| 64 | + DefaultScheme: "foo", |
| 65 | + ProviderFactories: []confmap.ProviderFactory{ |
| 66 | + yamlprovider.NewFactory(), |
| 67 | + }, |
| 68 | + }, |
| 69 | + errString: "configuration: DefaultScheme not found in providers list", |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, test := range tests { |
| 73 | + t.Run(test.name, func(t *testing.T) { |
| 74 | + err := featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), true) |
| 75 | + require.NoError(t, err) |
| 76 | + defer func() { |
| 77 | + _ = featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), false) |
| 78 | + }() |
| 79 | + |
| 80 | + set := ConfigProviderSettings{ |
| 81 | + ResolverSettings: test.set, |
| 82 | + } |
| 83 | + cmd := newConfigPrintSubCommand(CollectorSettings{ConfigProviderSettings: set}, flags(featuregate.GlobalRegistry())) |
| 84 | + err = cmd.Execute() |
| 85 | + if test.errString != "" { |
| 86 | + require.ErrorContains(t, err, test.errString) |
| 87 | + } else { |
| 88 | + require.NoError(t, err) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestPrintCommandFeaturegateDisabled(t *testing.T) { |
| 95 | + cmd := newConfigPrintSubCommand(CollectorSettings{ConfigProviderSettings: ConfigProviderSettings{ |
| 96 | + ResolverSettings: confmap.ResolverSettings{ |
| 97 | + URIs: []string{"yaml:processors::batch/foo::timeout: 3s"}, |
| 98 | + DefaultScheme: "foo", |
| 99 | + ProviderFactories: []confmap.ProviderFactory{ |
| 100 | + yamlprovider.NewFactory(), |
| 101 | + }, |
| 102 | + }, |
| 103 | + }}, flags(featuregate.GlobalRegistry())) |
| 104 | + err := cmd.Execute() |
| 105 | + require.ErrorContains(t, err, "print-initial-config is currently experimental, use the otelcol.printInitialConfig feature gate to enable this command") |
| 106 | +} |
| 107 | + |
| 108 | +func TestConfig(t *testing.T) { |
| 109 | + tests := []struct { |
| 110 | + name string |
| 111 | + configs []string |
| 112 | + finalConfig string |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "two-configs", |
| 116 | + configs: []string{ |
| 117 | + "file:testdata/configs/1-config-first.yaml", |
| 118 | + "file:testdata/configs/1-config-second.yaml", |
| 119 | + }, |
| 120 | + finalConfig: "testdata/configs/1-config-output.yaml", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "two-configs-yaml", |
| 124 | + configs: []string{ |
| 125 | + "file:testdata/configs/1-config-first.yaml", |
| 126 | + "file:testdata/configs/1-config-second.yaml", |
| 127 | + "yaml:service::pipelines::logs::receivers: [foo,bar]", |
| 128 | + "yaml:service::pipelines::logs::exporters: [foo,bar]", |
| 129 | + }, |
| 130 | + finalConfig: "testdata/configs/2-config-output.yaml", |
| 131 | + }, |
| 132 | + } |
| 133 | + for _, test := range tests { |
| 134 | + t.Run(test.name, func(t *testing.T) { |
| 135 | + err := featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), true) |
| 136 | + require.NoError(t, err) |
| 137 | + defer func() { |
| 138 | + _ = featuregate.GlobalRegistry().Set(printCommandFeatureFlag.ID(), false) |
| 139 | + }() |
| 140 | + set := ConfigProviderSettings{ |
| 141 | + ResolverSettings: confmap.ResolverSettings{ |
| 142 | + URIs: test.configs, |
| 143 | + ProviderFactories: []confmap.ProviderFactory{ |
| 144 | + fileprovider.NewFactory(), |
| 145 | + yamlprovider.NewFactory(), |
| 146 | + }, |
| 147 | + DefaultScheme: "file", |
| 148 | + }, |
| 149 | + } |
| 150 | + tmpFile, err := os.CreateTemp(t.TempDir(), "*") |
| 151 | + require.NoError(t, err) |
| 152 | + t.Cleanup(func() { _ = tmpFile.Close() }) |
| 153 | + |
| 154 | + // save the os.Stdout and temporarily set it to temp file |
| 155 | + oldStdout := os.Stdout |
| 156 | + os.Stdout = tmpFile |
| 157 | + |
| 158 | + cmd := newConfigPrintSubCommand(CollectorSettings{ConfigProviderSettings: set}, flags(featuregate.GlobalRegistry())) |
| 159 | + require.NoError(t, cmd.Execute()) |
| 160 | + |
| 161 | + // restore os.Stdout |
| 162 | + os.Stdout = oldStdout |
| 163 | + |
| 164 | + expectedOutput, err := os.ReadFile(test.finalConfig) |
| 165 | + require.NoError(t, err) |
| 166 | + |
| 167 | + actualOutput, err := os.ReadFile(tmpFile.Name()) |
| 168 | + require.NoError(t, err) |
| 169 | + |
| 170 | + actualConfig := make(map[string]any, 0) |
| 171 | + expectedConfig := make(map[string]any, 0) |
| 172 | + |
| 173 | + require.NoError(t, yaml.Unmarshal(bytes.TrimSpace(actualOutput), actualConfig)) |
| 174 | + require.NoError(t, yaml.Unmarshal(bytes.TrimSpace(expectedOutput), expectedConfig)) |
| 175 | + |
| 176 | + require.Equal(t, expectedConfig, actualConfig) |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments