Debian Codegraph @

confmap/provider/yamlprovider/provider_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package yamlprovider
5
6 import (
7 "context"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/collector/confmap"
14 "go.opentelemetry.io/collector/confmap/confmaptest"
15 )
16
17 func TestValidateProviderScheme(t *testing.T) {
18 assert.NoError(t, confmaptest.ValidateProviderScheme(createProvider()))
19 }
20
21 func TestEmpty(t *testing.T) {
22 sp := createProvider()
23 _, err := sp.Retrieve(context.Background(), "", nil)
24 require.Error(t, err)
25 assert.NoError(t, sp.Shutdown(context.Background()))
26 }
27
28 func TestInvalidYAML(t *testing.T) {
29 sp := createProvider()
30 ret, err := sp.Retrieve(context.Background(), "yaml:[invalid,", nil)
31 require.NoError(t, err)
32 raw, err := ret.AsRaw()
33 require.NoError(t, err)
34 assert.IsType(t, "", raw)
35
36 assert.NoError(t, sp.Shutdown(context.Background()))
37 }
38
39 func TestOneValue(t *testing.T) {
40 sp := createProvider()
41 ret, err := sp.Retrieve(context.Background(), "yaml:processors::test::timeout: 2s", nil)
42 require.NoError(t, err)
43 retMap, err := ret.AsConf()
44 require.NoError(t, err)
45 assert.Equal(t, map[string]any{
46 "processors": map[string]any{
47 "test": map[string]any{
48 "timeout": "2s",
49 },
50 },
51 }, retMap.ToStringMap())
52 assert.NoError(t, sp.Shutdown(context.Background()))
53 }
54
55 func TestNamedComponent(t *testing.T) {
56 sp := createProvider()
57 ret, err := sp.Retrieve(context.Background(), "yaml:processors::test/foo::timeout: 3s", nil)
58 require.NoError(t, err)
59 retMap, err := ret.AsConf()
60 require.NoError(t, err)
61 assert.Equal(t, map[string]any{
62 "processors": map[string]any{
63 "test/foo": map[string]any{
64 "timeout": "3s",
65 },
66 },
67 }, retMap.ToStringMap())
68 assert.NoError(t, sp.Shutdown(context.Background()))
69 }
70
71 func TestMapEntry(t *testing.T) {
72 sp := createProvider()
73 ret, err := sp.Retrieve(context.Background(), "yaml:processors: {test/foo::timeout: 3s, test::timeout: 2s}", nil)
74 require.NoError(t, err)
75 retMap, err := ret.AsConf()
76 require.NoError(t, err)
77 assert.Equal(t, map[string]any{
78 "processors": map[string]any{
79 "test/foo": map[string]any{
80 "timeout": "3s",
81 },
82 "test": map[string]any{
83 "timeout": "2s",
84 },
85 },
86 }, retMap.ToStringMap())
87 assert.NoError(t, sp.Shutdown(context.Background()))
88 }
89
90 func TestArrayEntry(t *testing.T) {
91 sp := createProvider()
92 ret, err := sp.Retrieve(context.Background(), "yaml:service::extensions: [zpages, zpages/foo]", nil)
93 require.NoError(t, err)
94 retMap, err := ret.AsConf()
95 require.NoError(t, err)
96 assert.Equal(t, map[string]any{
97 "service": map[string]any{
98 "extensions": []any{
99 "zpages",
100 "zpages/foo",
101 },
102 },
103 }, retMap.ToStringMap())
104 assert.NoError(t, sp.Shutdown(context.Background()))
105 }
106
107 func TestNewLine(t *testing.T) {
108 sp := createProvider()
109 ret, err := sp.Retrieve(context.Background(), "yaml:processors::test/foo::timeout: 3s\nprocessors::test::timeout: 2s", nil)
110 require.NoError(t, err)
111 retMap, err := ret.AsConf()
112 require.NoError(t, err)
113 assert.Equal(t, map[string]any{
114 "processors": map[string]any{
115 "test/foo": map[string]any{
116 "timeout": "3s",
117 },
118 "test": map[string]any{
119 "timeout": "2s",
120 },
121 },
122 }, retMap.ToStringMap())
123 assert.NoError(t, sp.Shutdown(context.Background()))
124 }
125
126 func TestDotSeparator(t *testing.T) {
127 sp := createProvider()
128 ret, err := sp.Retrieve(context.Background(), "yaml:processors.test.timeout: 4s", nil)
129 require.NoError(t, err)
130 retMap, err := ret.AsConf()
131 require.NoError(t, err)
132 assert.Equal(t, map[string]any{"processors.test.timeout": "4s"}, retMap.ToStringMap())
133 assert.NoError(t, sp.Shutdown(context.Background()))
134 }
135
136 func createProvider() confmap.Provider {
137 return NewFactory().Create(confmaptest.NewNopProviderSettings())
138 }