Debian Codegraph @

otelcol/unmarshaler_test.go

raw symbols SCIP
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/confmap"
13 "go.opentelemetry.io/collector/service"
14 "go.opentelemetry.io/collector/service/pipelines"
15 )
16
17 func TestUnmarshalEmpty(t *testing.T) {
18 factories, err := nopFactories()
19 require.NoError(t, err)
20
21 _, err = unmarshal(confmap.New(), factories)
22 assert.NoError(t, err)
23 }
24
25 func TestUnmarshalEmptyAllSections(t *testing.T) {
26 factories, err := nopFactories()
27 require.NoError(t, err)
28
29 conf := confmap.NewFromStringMap(map[string]any{
30 "receivers": nil,
31 "processors": nil,
32 "exporters": nil,
33 "connectors": nil,
34 "extensions": nil,
35 "service": nil,
36 })
37 cfg, err := unmarshal(conf, factories)
38 require.NoError(t, err)
39
40 assert.Equal(t, fakeTelemetryConfig{}, cfg.Service.Telemetry)
41 }
42
43 func TestUnmarshalUnknownTopLevel(t *testing.T) {
44 factories, err := nopFactories()
45 require.NoError(t, err)
46
47 conf := confmap.NewFromStringMap(map[string]any{
48 "unknown_section": nil,
49 })
50 _, err = unmarshal(conf, factories)
51 assert.ErrorContains(t, err, "'' has invalid keys: unknown_section")
52 }
53
54 func TestPipelineConfigUnmarshalError(t *testing.T) {
55 testCases := []struct {
56 // test case name (also file name containing config yaml)
57 name string
58 conf *confmap.Conf
59 // string that the error must contain
60 expectError string
61 }{
62 {
63 name: "duplicate-pipeline",
64 conf: confmap.NewFromStringMap(map[string]any{
65 "traces/ pipe": nil,
66 "traces /pipe": nil,
67 }),
68 expectError: "duplicate name",
69 },
70 {
71 name: "invalid-pipeline-name-after-slash",
72 conf: confmap.NewFromStringMap(map[string]any{
73 "metrics/": nil,
74 }),
75 expectError: "in \"metrics/\" id: the part after / should not be empty",
76 },
77 {
78 name: "invalid-pipeline-section",
79 conf: confmap.NewFromStringMap(map[string]any{
80 "traces": map[string]any{
81 "unknown_section": nil,
82 },
83 }),
84 expectError: "'[traces]' has invalid keys: unknown_section",
85 },
86 {
87 name: "invalid-pipeline-sub-config",
88 conf: confmap.NewFromStringMap(map[string]any{
89 "traces": "string",
90 }),
91 expectError: "'[traces]' expected a map or struct, got \"string\"",
92 },
93 {
94 name: "invalid-pipeline-type",
95 conf: confmap.NewFromStringMap(map[string]any{
96 "/metrics": nil,
97 }),
98 expectError: "in \"/metrics\" id: the part before / should not be empty",
99 },
100 {
101 name: "invalid-sequence-value",
102 conf: confmap.NewFromStringMap(map[string]any{
103 "traces": map[string]any{
104 "receivers": map[string]any{
105 "nop": map[string]any{
106 "some": "config",
107 },
108 },
109 },
110 }),
111 expectError: "'[traces].receivers' source data must be an array or slice, got map",
112 },
113 }
114
115 for _, tt := range testCases {
116 t.Run(tt.name, func(t *testing.T) {
117 pips := new(pipelines.Config)
118 err := tt.conf.Unmarshal(&pips)
119 assert.ErrorContains(t, err, tt.expectError)
120 })
121 }
122 }
123
124 func TestServiceUnmarshalError(t *testing.T) {
125 testCases := []struct {
126 // test case name (also file name containing config yaml)
127 name string
128 conf *confmap.Conf
129 // string that the error must contain
130 expectError string
131 }{
132 {
133 name: "invalid-telemetry-unknown-key",
134 conf: confmap.NewFromStringMap(map[string]any{
135 "telemetry": map[string]any{
136 "unknown": "key",
137 },
138 }),
139 expectError: "decoding failed due to the following error(s):\n\n'telemetry' has invalid keys: unknown",
140 },
141 {
142 name: "invalid-service-extensions-section",
143 conf: confmap.NewFromStringMap(map[string]any{
144 "extensions": []any{
145 map[string]any{
146 "nop": map[string]any{
147 "some": "config",
148 },
149 },
150 },
151 }),
152 expectError: "'extensions[0]' has invalid keys: nop",
153 },
154 {
155 name: "invalid-service-section",
156 conf: confmap.NewFromStringMap(map[string]any{
157 "unknown_section": "string",
158 }),
159 expectError: "'' has invalid keys: unknown_section",
160 },
161 {
162 name: "invalid-pipelines-config",
163 conf: confmap.NewFromStringMap(map[string]any{
164 "pipelines": "string",
165 }),
166 expectError: "'pipelines' expected type 'pipelines.Config', got unconvertible type 'string'",
167 },
168 }
169
170 for _, tt := range testCases {
171 t.Run(tt.name, func(t *testing.T) {
172 err := tt.conf.Unmarshal(&service.Config{
173 Telemetry: fakeTelemetryConfig{},
174 })
175 require.ErrorContains(t, err, tt.expectError)
176 })
177 }
178 }