| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package env |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
func TestEnvParse(t *testing.T) { |
| 13 |
testCases := []struct { |
| 14 |
name string |
| 15 |
keys []string |
| 16 |
f func(int) int |
| 17 |
}{ |
| 18 |
{ |
| 19 |
name: "BatchSpanProcessorScheduleDelay", |
| 20 |
keys: []string{BatchSpanProcessorScheduleDelayKey}, |
| 21 |
f: BatchSpanProcessorScheduleDelay, |
| 22 |
}, |
| 23 |
|
| 24 |
{ |
| 25 |
name: "BatchSpanProcessorExportTimeout", |
| 26 |
keys: []string{BatchSpanProcessorExportTimeoutKey}, |
| 27 |
f: BatchSpanProcessorExportTimeout, |
| 28 |
}, |
| 29 |
|
| 30 |
{ |
| 31 |
name: "BatchSpanProcessorMaxQueueSize", |
| 32 |
keys: []string{BatchSpanProcessorMaxQueueSizeKey}, |
| 33 |
f: BatchSpanProcessorMaxQueueSize, |
| 34 |
}, |
| 35 |
|
| 36 |
{ |
| 37 |
name: "BatchSpanProcessorMaxExportBatchSize", |
| 38 |
keys: []string{BatchSpanProcessorMaxExportBatchSizeKey}, |
| 39 |
f: BatchSpanProcessorMaxExportBatchSize, |
| 40 |
}, |
| 41 |
|
| 42 |
{ |
| 43 |
name: "SpanAttributeValueLength", |
| 44 |
keys: []string{SpanAttributeValueLengthKey, AttributeValueLengthKey}, |
| 45 |
f: SpanAttributeValueLength, |
| 46 |
}, |
| 47 |
|
| 48 |
{ |
| 49 |
name: "SpanAttributeCount", |
| 50 |
keys: []string{SpanAttributeCountKey, AttributeCountKey}, |
| 51 |
f: SpanAttributeCount, |
| 52 |
}, |
| 53 |
|
| 54 |
{ |
| 55 |
name: "SpanEventCount", |
| 56 |
keys: []string{SpanEventCountKey}, |
| 57 |
f: SpanEventCount, |
| 58 |
}, |
| 59 |
|
| 60 |
{ |
| 61 |
name: "SpanEventAttributeCount", |
| 62 |
keys: []string{SpanEventAttributeCountKey}, |
| 63 |
f: SpanEventAttributeCount, |
| 64 |
}, |
| 65 |
|
| 66 |
{ |
| 67 |
name: "SpanLinkCount", |
| 68 |
keys: []string{SpanLinkCountKey}, |
| 69 |
f: SpanLinkCount, |
| 70 |
}, |
| 71 |
|
| 72 |
{ |
| 73 |
name: "SpanLinkAttributeCount", |
| 74 |
keys: []string{SpanLinkAttributeCountKey}, |
| 75 |
f: SpanLinkAttributeCount, |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
const ( |
| 80 |
defVal = 500 |
| 81 |
envVal = 2500 |
| 82 |
envValStr = "2500" |
| 83 |
invalid = "localhost" |
| 84 |
empty = "" |
| 85 |
) |
| 86 |
|
| 87 |
for _, tc := range testCases { |
| 88 |
t.Run(tc.name, func(t *testing.T) { |
| 89 |
for _, key := range tc.keys { |
| 90 |
t.Run(key, func(t *testing.T) { |
| 91 |
assert.Equal(t, defVal, tc.f(defVal), "environment variable unset") |
| 92 |
|
| 93 |
t.Setenv(key, envValStr) |
| 94 |
assert.Equal(t, envVal, tc.f(defVal), "environment variable set/valid") |
| 95 |
|
| 96 |
t.Setenv(key, invalid) |
| 97 |
assert.Equal(t, defVal, tc.f(defVal), "invalid value") |
| 98 |
|
| 99 |
t.Setenv(key, empty) |
| 100 |
assert.Equal(t, defVal, tc.f(defVal), "empty value") |
| 101 |
}) |
| 102 |
} |
| 103 |
}) |
| 104 |
} |
| 105 |
} |