| 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 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"path/filepath" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/collector/component" |
| 18 |
"go.opentelemetry.io/collector/config/configopaque" |
| 19 |
"go.opentelemetry.io/collector/confmap" |
| 20 |
"go.opentelemetry.io/collector/confmap/provider/fileprovider" |
| 21 |
"go.opentelemetry.io/collector/consumer" |
| 22 |
"go.opentelemetry.io/collector/exporter" |
| 23 |
"go.opentelemetry.io/collector/exporter/xexporter" |
| 24 |
"go.opentelemetry.io/collector/featuregate" |
| 25 |
"go.opentelemetry.io/collector/receiver" |
| 26 |
"go.opentelemetry.io/collector/receiver/xreceiver" |
| 27 |
"go.opentelemetry.io/collector/service/telemetry" |
| 28 |
) |
| 29 |
|
| 30 |
type printExporterConfig struct { |
| 31 |
Timeout time.Duration `mapstructure:"timeout"` |
| 32 |
} |
| 33 |
|
| 34 |
type printReceiverConfig struct { |
| 35 |
Opaque configopaque.String `mapstructure:"opaque"` |
| 36 |
Other string `mapstructure:"other,omitempty"` |
| 37 |
} |
| 38 |
|
| 39 |
func (c *printExporterConfig) Validate() error { |
| 40 |
if c.Timeout < 0 { |
| 41 |
return errors.New("timeout cannot be negative") |
| 42 |
} |
| 43 |
return nil |
| 44 |
} |
| 45 |
|
| 46 |
func TestPrintCommand(t *testing.T) { |
| 47 |
const nonexistentConfig = "file:nope.yaml" |
| 48 |
|
| 49 |
validConfig := fmt.Sprint("file:", filepath.Join("testdata", "print.yaml")) |
| 50 |
invalidConfig1 := fmt.Sprint("file:", filepath.Join("testdata", "print_invalid.yaml")) |
| 51 |
invalidConfig2 := fmt.Sprint("file:", filepath.Join("testdata", "print_negative.yaml")) |
| 52 |
defaultConfig := fmt.Sprint("file:", filepath.Join("testdata", "print_default.yaml")) |
| 53 |
|
| 54 |
tests := []struct { |
| 55 |
name string |
| 56 |
ofmt string |
| 57 |
path string |
| 58 |
errString string |
| 59 |
outString map[string]string |
| 60 |
disableFF bool // disable the feature flag |
| 61 |
validate bool // add validation (even redacted) |
| 62 |
errOnlyRedacted bool // error applies only in redacted mode |
| 63 |
}{ |
| 64 |
{ |
| 65 |
name: "file not found", |
| 66 |
path: nonexistentConfig, |
| 67 |
errString: "cannot retrieve the configuration: unable to read the file", |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "valid yaml", |
| 71 |
path: validConfig, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "invalid syntax without validate", |
| 75 |
path: invalidConfig1, |
| 76 |
errString: "'timeout' time: invalid duration", |
| 77 |
errOnlyRedacted: true, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "validation fail", |
| 81 |
path: invalidConfig2, |
| 82 |
validate: true, |
| 83 |
errString: "timeout cannot be negative", |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "no feature flag", |
| 87 |
path: validConfig, |
| 88 |
disableFF: true, |
| 89 |
errString: "use the otelcol.printInitialConfig feature gate", |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "field is set yaml", |
| 93 |
path: validConfig, |
| 94 |
outString: map[string]string{ |
| 95 |
"redacted": `timeout: 5s`, |
| 96 |
"unredacted": `timeout: 5s`, |
| 97 |
}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "default field value", |
| 101 |
path: defaultConfig, |
| 102 |
outString: map[string]string{ |
| 103 |
"redacted": `timeout: 1s`, |
| 104 |
|
| 105 |
// Since the structure is empty before |
| 106 |
// interpretation, no default is expanded. |
| 107 |
"unredacted": `e: null`, |
| 108 |
}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "field is set json", |
| 112 |
ofmt: "json", |
| 113 |
path: validConfig, |
| 114 |
outString: map[string]string{ |
| 115 |
// Note: JSON does not format as a time.Duration |
| 116 |
"redacted": `"timeout": 5000000000`, |
| 117 |
|
| 118 |
// Note: the original input is "5s" |
| 119 |
"unredacted": `"timeout": "5s"`, |
| 120 |
}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "opaque field", |
| 124 |
path: validConfig, |
| 125 |
outString: map[string]string{ |
| 126 |
"redacted": `opaque: '[REDACTED]'`, |
| 127 |
"unredacted": `opaque: OOO`, |
| 128 |
}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "opaque default", |
| 132 |
path: defaultConfig, |
| 133 |
outString: map[string]string{ |
| 134 |
"redacted": `opaque: '[REDACTED]'`, |
| 135 |
|
| 136 |
// Note: the default opaque value does not print, |
| 137 |
// the other value is set in defaultConfig so that |
| 138 |
// the whole component config is not defaulted. |
| 139 |
"unredacted": `other: lala`, |
| 140 |
}, |
| 141 |
}, |
| 142 |
} |
| 143 |
for _, test := range tests { |
| 144 |
testModes := []string{"redacted", "unredacted", "unrecognized"} |
| 145 |
for _, mode := range testModes { |
| 146 |
t.Run(fmt.Sprint(test.name, "_", mode), func(t *testing.T) { |
| 147 |
// Save current feature flag state and restore after test |
| 148 |
fg := featuregate.GlobalRegistry() |
| 149 |
|
| 150 |
fg.VisitAll(func(g *featuregate.Gate) { |
| 151 |
if g.ID() == featureGateName { |
| 152 |
defer func() { |
| 153 |
_ = fg.Set(featureGateName, g.IsEnabled()) |
| 154 |
}() |
| 155 |
} |
| 156 |
}) |
| 157 |
if test.disableFF { |
| 158 |
require.NoError(t, fg.Set(featureGateName, false)) |
| 159 |
} else { |
| 160 |
require.NoError(t, fg.Set(featureGateName, true)) |
| 161 |
} |
| 162 |
|
| 163 |
testR := component.MustNewType("r") |
| 164 |
testE := component.MustNewType("e") |
| 165 |
testReceiver := xreceiver.NewFactory( |
| 166 |
testR, |
| 167 |
func() component.Config { |
| 168 |
return printReceiverConfig{ |
| 169 |
Opaque: "1234", |
| 170 |
Other: "", |
| 171 |
} |
| 172 |
}, |
| 173 |
xreceiver.WithLogs(func(context.Context, receiver.Settings, component.Config, consumer.Logs) (receiver.Logs, error) { |
| 174 |
return nil, nil |
| 175 |
}, component.StabilityLevelStable), |
| 176 |
) |
| 177 |
testExporter := xexporter.NewFactory( |
| 178 |
testE, |
| 179 |
func() component.Config { |
| 180 |
return printExporterConfig{ |
| 181 |
Timeout: time.Second, |
| 182 |
} |
| 183 |
}, |
| 184 |
xexporter.WithLogs(func(context.Context, exporter.Settings, component.Config) (exporter.Logs, error) { |
| 185 |
return nil, nil |
| 186 |
}, component.StabilityLevelStable), |
| 187 |
) |
| 188 |
var stdout bytes.Buffer |
| 189 |
|
| 190 |
set := confmap.ResolverSettings{} |
| 191 |
set.ProviderFactories = []confmap.ProviderFactory{ |
| 192 |
fileprovider.NewFactory(), |
| 193 |
} |
| 194 |
set.DefaultScheme = "file" |
| 195 |
set.URIs = []string{test.path} |
| 196 |
|
| 197 |
cmd := newConfigPrintSubCommand(CollectorSettings{ |
| 198 |
Factories: func() (Factories, error) { |
| 199 |
return Factories{ |
| 200 |
Receivers: map[component.Type]receiver.Factory{ |
| 201 |
testR: testReceiver, |
| 202 |
}, |
| 203 |
Exporters: map[component.Type]exporter.Factory{ |
| 204 |
testE: testExporter, |
| 205 |
}, |
| 206 |
Telemetry: telemetry.NewFactory(func() component.Config { |
| 207 |
return fakeTelemetryConfig{} |
| 208 |
}), |
| 209 |
}, nil |
| 210 |
}, |
| 211 |
ConfigProviderSettings: ConfigProviderSettings{ |
| 212 |
ResolverSettings: set, |
| 213 |
}, |
| 214 |
}, flags(featuregate.GlobalRegistry())) |
| 215 |
cmd.SetOut(&stdout) |
| 216 |
args := []string{ |
| 217 |
"--mode", mode, |
| 218 |
"--format", test.ofmt, |
| 219 |
} |
| 220 |
if test.validate { |
| 221 |
args = append(args, "--validate=true") |
| 222 |
} else { |
| 223 |
args = append(args, "--validate=false") |
| 224 |
} |
| 225 |
cmd.SetArgs(args) |
| 226 |
err := cmd.Execute() |
| 227 |
|
| 228 |
expectErr := test.errString != "" |
| 229 |
expectErrMsg := test.errString |
| 230 |
|
| 231 |
switch mode { |
| 232 |
case "redacted": |
| 233 |
case "unredacted": |
| 234 |
if test.errOnlyRedacted { |
| 235 |
expectErr = false |
| 236 |
expectErrMsg = "" |
| 237 |
} |
| 238 |
default: |
| 239 |
expectErr = true |
| 240 |
if test.disableFF { |
| 241 |
expectErrMsg = "feature gate" |
| 242 |
} else { |
| 243 |
expectErrMsg = "unrecognized" |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if expectErr { |
| 248 |
require.Error(t, err) |
| 249 |
require.ErrorContains(t, err, expectErrMsg) |
| 250 |
} else { |
| 251 |
require.NoError(t, err) |
| 252 |
} |
| 253 |
if test.outString[mode] != "" { |
| 254 |
require.Contains(t, stdout.String(), test.outString[mode]) |
| 255 |
} |
| 256 |
}) |
| 257 |
} |
| 258 |
} |
| 259 |
} |