Debian Codegraph @

confmap/internal/e2e/nil_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package e2etest
5
6 import (
7 "context"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11 )
12
13 func TestNilToStringMap(t *testing.T) {
14 tests := []struct {
15 name string
16 expectedMap map[string]any
17 expectedSub map[string]any
18 }{
19 {
20 name: "subsection_null.yaml",
21 expectedMap: map[string]any{
22 "field": map[string]any{
23 "key": nil,
24 },
25 },
26 expectedSub: nil,
27 },
28 {
29 name: "subsection_empty_map.yaml",
30 expectedMap: map[string]any{
31 "field": map[string]any{
32 "key": map[string]any{},
33 },
34 },
35 expectedSub: map[string]any{},
36 },
37 {
38 name: "subsection_set_but_empty.yaml",
39 expectedMap: map[string]any{
40 "field": map[string]any{
41 "key": nil,
42 },
43 },
44 expectedSub: nil,
45 },
46 {
47 name: "subsection_unset.yaml",
48 expectedMap: map[string]any{
49 "field": nil,
50 },
51 expectedSub: nil,
52 },
53
54 {
55 name: "subsection_unset_empty_map.yaml",
56 expectedMap: map[string]any{
57 "field": map[string]any{},
58 },
59 expectedSub: nil,
60 },
61 }
62 for _, tt := range tests {
63 t.Run(tt.name, func(t *testing.T) {
64 resolver := NewResolver(t, tt.name)
65 conf, err := resolver.Resolve(context.Background())
66 require.NoError(t, err)
67 require.Equal(t, tt.expectedMap, conf.ToStringMap())
68
69 sub, err := conf.Sub("field::key")
70 require.NoError(t, err)
71 require.Equal(t, tt.expectedSub, sub.ToStringMap())
72 })
73 }
74 }
75
76 func TestNilToStringMapEnv(t *testing.T) {
77 tests := []struct {
78 envValue string
79 expectedMap map[string]any
80 expectedSub map[string]any
81 }{
82 {
83 envValue: "null",
84 expectedMap: map[string]any{
85 "field": map[string]any{
86 "key": nil,
87 },
88 },
89 expectedSub: nil,
90 },
91 {
92 envValue: "{}",
93 expectedMap: map[string]any{
94 "field": map[string]any{
95 "key": map[string]any{},
96 },
97 },
98 expectedSub: map[string]any{},
99 },
100 {
101 envValue: "",
102 expectedMap: map[string]any{
103 "field": map[string]any{
104 "key": nil,
105 },
106 },
107 expectedSub: nil,
108 },
109 }
110
111 for _, tt := range tests {
112 t.Run(tt.envValue, func(t *testing.T) {
113 t.Setenv("VALUE", tt.envValue)
114 resolver := NewResolver(t, "types_map.yaml")
115 conf, err := resolver.Resolve(context.Background())
116 require.NoError(t, err)
117 require.Equal(t, tt.expectedMap, conf.ToStringMap())
118 sub, err := conf.Sub("field::key")
119 require.NoError(t, err)
120 require.Equal(t, tt.expectedSub, sub.ToStringMap())
121 })
122 }
123 }