2022-01-29 00:07:21 +08:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 07:05:28 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-01-29 00:07:21 +08:00
|
|
|
|
2022-02-07 07:57:44 -08:00
|
|
|
package env
|
2022-01-29 00:07:21 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-03-16 14:33:59 -07:00
|
|
|
func TestEnvParse(t *testing.T) {
|
2022-01-29 00:07:21 +08:00
|
|
|
testCases := []struct {
|
2022-03-16 14:33:59 -07:00
|
|
|
name string
|
|
|
|
keys []string
|
|
|
|
f func(int) int
|
2022-01-29 00:07:21 +08:00
|
|
|
}{
|
|
|
|
{
|
2022-03-16 14:33:59 -07:00
|
|
|
name: "BatchSpanProcessorScheduleDelay",
|
|
|
|
keys: []string{BatchSpanProcessorScheduleDelayKey},
|
|
|
|
f: BatchSpanProcessorScheduleDelay,
|
2022-01-29 00:07:21 +08:00
|
|
|
},
|
2022-03-16 14:33:59 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
name: "BatchSpanProcessorExportTimeout",
|
|
|
|
keys: []string{BatchSpanProcessorExportTimeoutKey},
|
|
|
|
f: BatchSpanProcessorExportTimeout,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "BatchSpanProcessorMaxQueueSize",
|
|
|
|
keys: []string{BatchSpanProcessorMaxQueueSizeKey},
|
|
|
|
f: BatchSpanProcessorMaxQueueSize,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "BatchSpanProcessorMaxExportBatchSize",
|
|
|
|
keys: []string{BatchSpanProcessorMaxExportBatchSizeKey},
|
|
|
|
f: BatchSpanProcessorMaxExportBatchSize,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "SpanAttributeValueLength",
|
|
|
|
keys: []string{SpanAttributeValueLengthKey, AttributeValueLengthKey},
|
|
|
|
f: SpanAttributeValueLength,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "SpanAttributeCount",
|
|
|
|
keys: []string{SpanAttributeCountKey, AttributeCountKey},
|
|
|
|
f: SpanAttributeCount,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "SpanEventCount",
|
|
|
|
keys: []string{SpanEventCountKey},
|
|
|
|
f: SpanEventCount,
|
|
|
|
},
|
|
|
|
|
2022-01-29 00:07:21 +08:00
|
|
|
{
|
2022-03-16 14:33:59 -07:00
|
|
|
name: "SpanEventAttributeCount",
|
|
|
|
keys: []string{SpanEventAttributeCountKey},
|
|
|
|
f: SpanEventAttributeCount,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "SpanLinkCount",
|
|
|
|
keys: []string{SpanLinkCountKey},
|
|
|
|
f: SpanLinkCount,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "SpanLinkAttributeCount",
|
|
|
|
keys: []string{SpanLinkAttributeCountKey},
|
|
|
|
f: SpanLinkAttributeCount,
|
2022-01-29 00:07:21 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-16 14:33:59 -07:00
|
|
|
const (
|
|
|
|
defVal = 500
|
|
|
|
envVal = 2500
|
|
|
|
envValStr = "2500"
|
|
|
|
invalid = "localhost"
|
2023-02-28 21:43:48 +01:00
|
|
|
empty = ""
|
2022-03-16 14:33:59 -07:00
|
|
|
)
|
|
|
|
|
2022-01-29 00:07:21 +08:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-16 14:33:59 -07:00
|
|
|
for _, key := range tc.keys {
|
|
|
|
t.Run(key, func(t *testing.T) {
|
|
|
|
assert.Equal(t, defVal, tc.f(defVal), "environment variable unset")
|
|
|
|
|
2024-08-21 09:37:56 +02:00
|
|
|
t.Setenv(key, envValStr)
|
2022-03-16 14:33:59 -07:00
|
|
|
assert.Equal(t, envVal, tc.f(defVal), "environment variable set/valid")
|
|
|
|
|
2024-08-21 09:37:56 +02:00
|
|
|
t.Setenv(key, invalid)
|
2022-03-16 14:33:59 -07:00
|
|
|
assert.Equal(t, defVal, tc.f(defVal), "invalid value")
|
2023-02-28 21:43:48 +01:00
|
|
|
|
2024-08-21 09:37:56 +02:00
|
|
|
t.Setenv(key, empty)
|
2023-02-28 21:43:48 +01:00
|
|
|
assert.Equal(t, defVal, tc.f(defVal), "empty value")
|
2022-03-16 14:33:59 -07:00
|
|
|
})
|
|
|
|
}
|
2022-01-29 00:07:21 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|