1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package env
16
17 import (
18 "os"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23
24 ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
25 )
26
27 func TestEnvParse(t *testing.T) {
28 testCases := []struct {
29 name string
30 keys []string
31 f func(int) int
32 }{
33 {
34 name: "BatchSpanProcessorScheduleDelay",
35 keys: []string{BatchSpanProcessorScheduleDelayKey},
36 f: BatchSpanProcessorScheduleDelay,
37 },
38
39 {
40 name: "BatchSpanProcessorExportTimeout",
41 keys: []string{BatchSpanProcessorExportTimeoutKey},
42 f: BatchSpanProcessorExportTimeout,
43 },
44
45 {
46 name: "BatchSpanProcessorMaxQueueSize",
47 keys: []string{BatchSpanProcessorMaxQueueSizeKey},
48 f: BatchSpanProcessorMaxQueueSize,
49 },
50
51 {
52 name: "BatchSpanProcessorMaxExportBatchSize",
53 keys: []string{BatchSpanProcessorMaxExportBatchSizeKey},
54 f: BatchSpanProcessorMaxExportBatchSize,
55 },
56
57 {
58 name: "SpanAttributeValueLength",
59 keys: []string{SpanAttributeValueLengthKey, AttributeValueLengthKey},
60 f: SpanAttributeValueLength,
61 },
62
63 {
64 name: "SpanAttributeCount",
65 keys: []string{SpanAttributeCountKey, AttributeCountKey},
66 f: SpanAttributeCount,
67 },
68
69 {
70 name: "SpanEventCount",
71 keys: []string{SpanEventCountKey},
72 f: SpanEventCount,
73 },
74
75 {
76 name: "SpanEventAttributeCount",
77 keys: []string{SpanEventAttributeCountKey},
78 f: SpanEventAttributeCount,
79 },
80
81 {
82 name: "SpanLinkCount",
83 keys: []string{SpanLinkCountKey},
84 f: SpanLinkCount,
85 },
86
87 {
88 name: "SpanLinkAttributeCount",
89 keys: []string{SpanLinkAttributeCountKey},
90 f: SpanLinkAttributeCount,
91 },
92 }
93
94 const (
95 defVal = 500
96 envVal = 2500
97 envValStr = "2500"
98 invalid = "localhost"
99 empty = ""
100 )
101
102 for _, tc := range testCases {
103 t.Run(tc.name, func(t *testing.T) {
104 for _, key := range tc.keys {
105 t.Run(key, func(t *testing.T) {
106 envStore := ottest.NewEnvStore()
107 t.Cleanup(func() { require.NoError(t, envStore.Restore()) })
108 envStore.Record(key)
109
110 assert.Equal(t, defVal, tc.f(defVal), "environment variable unset")
111
112 require.NoError(t, os.Setenv(key, envValStr))
113 assert.Equal(t, envVal, tc.f(defVal), "environment variable set/valid")
114
115 require.NoError(t, os.Setenv(key, invalid))
116 assert.Equal(t, defVal, tc.f(defVal), "invalid value")
117
118 require.NoError(t, os.Setenv(key, empty))
119 assert.Equal(t, defVal, tc.f(defVal), "empty value")
120 })
121 }
122 })
123 }
124 }
125
View as plain text