forked from open-telemetry/opentelemetry-collector
/
provider_test.go
89 lines (76 loc) · 3.01 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fileprovider
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)
const fileSchemePrefix = schemeName + ":"
func TestValidateProviderScheme(t *testing.T) {
assert.NoError(t, confmaptest.ValidateProviderScheme(NewWithSettings(confmap.ProviderSettings{})))
}
func TestEmptyName(t *testing.T) {
fp := NewWithSettings(confmap.ProviderSettings{})
_, err := fp.Retrieve(context.Background(), "", nil)
require.Error(t, err)
require.NoError(t, fp.Shutdown(context.Background()))
}
func TestUnsupportedScheme(t *testing.T) {
fp := NewWithSettings(confmap.ProviderSettings{})
_, err := fp.Retrieve(context.Background(), "https://", nil)
assert.Error(t, err)
assert.NoError(t, fp.Shutdown(context.Background()))
}
func TestNonExistent(t *testing.T) {
fp := NewWithSettings(confmap.ProviderSettings{})
_, err := fp.Retrieve(context.Background(), fileSchemePrefix+filepath.Join("testdata", "non-existent.yaml"), nil)
assert.Error(t, err)
_, err = fp.Retrieve(context.Background(), fileSchemePrefix+absolutePath(t, filepath.Join("testdata", "non-existent.yaml")), nil)
assert.Error(t, err)
require.NoError(t, fp.Shutdown(context.Background()))
}
func TestInvalidYAML(t *testing.T) {
fp := NewWithSettings(confmap.ProviderSettings{})
_, err := fp.Retrieve(context.Background(), fileSchemePrefix+filepath.Join("testdata", "invalid-yaml.yaml"), nil)
assert.Error(t, err)
_, err = fp.Retrieve(context.Background(), fileSchemePrefix+absolutePath(t, filepath.Join("testdata", "invalid-yaml.yaml")), nil)
assert.Error(t, err)
require.NoError(t, fp.Shutdown(context.Background()))
}
func TestRelativePath(t *testing.T) {
fp := NewWithSettings(confmap.ProviderSettings{})
ret, err := fp.Retrieve(context.Background(), fileSchemePrefix+filepath.Join("testdata", "default-config.yaml"), 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, retMap)
assert.NoError(t, fp.Shutdown(context.Background()))
}
func TestAbsolutePath(t *testing.T) {
fp := NewWithSettings(confmap.ProviderSettings{})
ret, err := fp.Retrieve(context.Background(), fileSchemePrefix+absolutePath(t, filepath.Join("testdata", "default-config.yaml")), 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, retMap)
assert.NoError(t, fp.Shutdown(context.Background()))
}
func absolutePath(t *testing.T, relativePath string) string {
dir, err := os.Getwd()
require.NoError(t, err)
return filepath.Join(dir, relativePath)
}