| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelcol |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/featuregate" |
| 13 |
) |
| 14 |
|
| 15 |
func TestSetFlag(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
args []string |
| 19 |
expectedConfigs []string |
| 20 |
expectedErr string |
| 21 |
}{ |
| 22 |
{ |
| 23 |
name: "simple set", |
| 24 |
args: []string{"--set=key=value"}, |
| 25 |
expectedConfigs: []string{"yaml:key: value"}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
name: "complex nested key", |
| 29 |
args: []string{"--set=outer.inner=value"}, |
| 30 |
expectedConfigs: []string{"yaml:outer::inner: value"}, |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "set array", |
| 34 |
args: []string{"--set=key=[a, b, c]"}, |
| 35 |
expectedConfigs: []string{"yaml:key: [a, b, c]"}, |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "set map", |
| 39 |
args: []string{"--set=key={a: c}"}, |
| 40 |
expectedConfigs: []string{"yaml:key: {a: c}"}, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "set and config", |
| 44 |
args: []string{"--set=key=value", "--config=file:testdata/otelcol-nop.yaml"}, |
| 45 |
expectedConfigs: []string{"file:testdata/otelcol-nop.yaml", "yaml:key: value"}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "config and set", |
| 49 |
args: []string{"--config=file:testdata/otelcol-nop.yaml", "--set=key=value"}, |
| 50 |
expectedConfigs: []string{"file:testdata/otelcol-nop.yaml", "yaml:key: value"}, |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "invalid set", |
| 54 |
args: []string{"--set=key:name"}, |
| 55 |
expectedErr: `invalid value "key:name" for flag -set: missing equal sign`, |
| 56 |
}, |
| 57 |
} |
| 58 |
|
| 59 |
for _, tt := range tests { |
| 60 |
t.Run(tt.name, func(t *testing.T) { |
| 61 |
flgs := flags(featuregate.NewRegistry()) |
| 62 |
err := flgs.Parse(tt.args) |
| 63 |
if tt.expectedErr != "" { |
| 64 |
assert.EqualError(t, err, tt.expectedErr) |
| 65 |
return |
| 66 |
} |
| 67 |
require.NoError(t, err) |
| 68 |
assert.Equal(t, tt.expectedConfigs, getConfigFlag(flgs)) |
| 69 |
}) |
| 70 |
} |
| 71 |
} |