/
provider_test.go
70 lines (57 loc) · 1.89 KB
/
provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package envprovider
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)
const envSchemePrefix = schemeName + ":"
const validYAML = `
processors:
batch:
exporters:
otlp:
endpoint: "localhost:4317"
`
func TestValidateProviderScheme(t *testing.T) {
assert.NoError(t, confmaptest.ValidateProviderScheme(NewWithSettings(confmap.ProviderSettings{})))
}
func TestEmptyName(t *testing.T) {
env := NewWithSettings(confmap.ProviderSettings{})
_, err := env.Retrieve(context.Background(), "", nil)
require.Error(t, err)
assert.NoError(t, env.Shutdown(context.Background()))
}
func TestUnsupportedScheme(t *testing.T) {
env := NewWithSettings(confmap.ProviderSettings{})
_, err := env.Retrieve(context.Background(), "https://", nil)
assert.Error(t, err)
assert.NoError(t, env.Shutdown(context.Background()))
}
func TestInvalidYAML(t *testing.T) {
const envName = "invalid-yaml"
t.Setenv(envName, "[invalid,")
env := NewWithSettings(confmap.ProviderSettings{})
_, err := env.Retrieve(context.Background(), envSchemePrefix+envName, nil)
assert.Error(t, err)
assert.NoError(t, env.Shutdown(context.Background()))
}
func TestEnv(t *testing.T) {
const envName = "default-config"
t.Setenv(envName, validYAML)
env := NewWithSettings(confmap.ProviderSettings{})
ret, err := env.Retrieve(context.Background(), envSchemePrefix+envName, nil)
require.NoError(t, err)
retMap, err := ret.AsConf()
assert.NoError(t, err)
expectedMap := confmap.NewFromStringMap(map[string]any{
"processors::batch": nil,
"exporters::otlp::endpoint": "localhost:4317",
})
assert.Equal(t, expectedMap.ToStringMap(), retMap.ToStringMap())
assert.NoError(t, env.Shutdown(context.Background()))
}