Debian Codegraph @

otelcol/internal/configunmarshaler/configs_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package configunmarshaler
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11
12 "go.opentelemetry.io/collector/component"
13 "go.opentelemetry.io/collector/confmap"
14 "go.opentelemetry.io/collector/connector/connectortest"
15 "go.opentelemetry.io/collector/exporter/exportertest"
16 "go.opentelemetry.io/collector/extension/extensiontest"
17 "go.opentelemetry.io/collector/processor/processortest"
18 "go.opentelemetry.io/collector/receiver/receivertest"
19 )
20
21 var nopType = component.MustNewType("nop")
22
23 var testKinds = []struct {
24 kind string
25 factories map[component.Type]component.Factory
26 }{
27 {
28 kind: "receiver",
29 factories: map[component.Type]component.Factory{
30 nopType: receivertest.NewNopFactory(),
31 },
32 },
33 {
34 kind: "processor",
35 factories: map[component.Type]component.Factory{
36 nopType: processortest.NewNopFactory(),
37 },
38 },
39 {
40 kind: "exporter",
41 factories: map[component.Type]component.Factory{
42 nopType: exportertest.NewNopFactory(),
43 },
44 },
45 {
46 kind: "connector",
47 factories: map[component.Type]component.Factory{
48 nopType: connectortest.NewNopFactory(),
49 },
50 },
51 {
52 kind: "extension",
53 factories: map[component.Type]component.Factory{
54 nopType: extensiontest.NewNopFactory(),
55 },
56 },
57 }
58
59 func TestUnmarshal(t *testing.T) {
60 for _, tk := range testKinds {
61 t.Run(tk.kind, func(t *testing.T) {
62 cfgs := NewConfigs(tk.factories)
63 conf := confmap.NewFromStringMap(map[string]any{
64 "nop": nil,
65 "nop/my" + tk.kind: nil,
66 })
67 require.NoError(t, cfgs.Unmarshal(conf))
68
69 assert.Equal(t, map[component.ID]component.Config{
70 component.NewID(nopType): tk.factories[nopType].CreateDefaultConfig(),
71 component.NewIDWithName(nopType, "my"+tk.kind): tk.factories[nopType].CreateDefaultConfig(),
72 }, cfgs.Configs())
73 })
74 }
75 }
76
77 func TestUnmarshalError(t *testing.T) {
78 for _, tk := range testKinds {
79 t.Run(tk.kind, func(t *testing.T) {
80 testCases := []struct {
81 name string
82 conf *confmap.Conf
83 // string that the error must contain
84 expectedError string
85 }{
86 {
87 name: "invalid-type",
88 conf: confmap.NewFromStringMap(map[string]any{
89 "nop": nil,
90 "/custom": nil,
91 }),
92 expectedError: "the part before / should not be empty",
93 },
94 {
95 name: "invalid-name-after-slash",
96 conf: confmap.NewFromStringMap(map[string]any{
97 "nop": nil,
98 "nop/": nil,
99 }),
100 expectedError: "the part after / should not be empty",
101 },
102 {
103 name: "unknown-type",
104 conf: confmap.NewFromStringMap(map[string]any{
105 "nosuch" + tk.kind: nil,
106 }),
107 expectedError: "unknown type: \"nosuch" + tk.kind + "\" for id: \"nosuch" + tk.kind + "\" (valid values: [nop])",
108 },
109 {
110 name: "duplicate",
111 conf: confmap.NewFromStringMap(map[string]any{
112 "nop /my" + tk.kind + " ": nil,
113 " nop/ my" + tk.kind: nil,
114 }),
115 expectedError: "duplicate name",
116 },
117 {
118 name: "invalid-section",
119 conf: confmap.NewFromStringMap(map[string]any{
120 "nop": map[string]any{
121 "unknown_section": tk.kind,
122 },
123 }),
124 expectedError: "error reading configuration for \"nop\"",
125 },
126 {
127 name: "invalid-sub-config",
128 conf: confmap.NewFromStringMap(map[string]any{
129 "nop": "tests",
130 }),
131 expectedError: "'[nop]' expected type 'map[string]interface {}', got unconvertible type 'string'",
132 },
133 }
134
135 for _, tt := range testCases {
136 t.Run(tt.name, func(t *testing.T) {
137 cfgs := NewConfigs(tk.factories)
138 err := cfgs.Unmarshal(tt.conf)
139 assert.ErrorContains(t, err, tt.expectedError)
140 })
141 }
142 })
143 }
144 }
145
146 func TestUnmarshal_LoggingExporter(t *testing.T) {
147 conf := confmap.NewFromStringMap(map[string]any{
148 "logging": nil,
149 })
150 factories := map[component.Type]component.Factory{
151 nopType: exportertest.NewNopFactory(),
152 }
153 cfgs := NewConfigs(factories)
154 err := cfgs.Unmarshal(conf)
155 assert.ErrorContains(t, err, "the logging exporter has been deprecated, use the debug exporter instead")
156 }