| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package confmaptest |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/confmap" |
| 15 |
) |
| 16 |
|
| 17 |
func TestLoadConfFileNotFound(t *testing.T) { |
| 18 |
_, err := LoadConf("file/not/found") |
| 19 |
assert.Error(t, err) |
| 20 |
} |
| 21 |
|
| 22 |
func TestLoadConfInvalidYAML(t *testing.T) { |
| 23 |
_, err := LoadConf(filepath.Join("testdata", "invalid.yaml")) |
| 24 |
require.Error(t, err) |
| 25 |
} |
| 26 |
|
| 27 |
func TestLoadConf(t *testing.T) { |
| 28 |
cfg, err := LoadConf(filepath.Join("testdata", "simple.yaml")) |
| 29 |
require.NoError(t, err) |
| 30 |
assert.Equal(t, map[string]any{"floating": 3.14}, cfg.ToStringMap()) |
| 31 |
} |
| 32 |
|
| 33 |
func TestToStringMapSanitizeEmptySlice(t *testing.T) { |
| 34 |
cfg, err := LoadConf(filepath.Join("testdata", "empty-slice.yaml")) |
| 35 |
require.NoError(t, err) |
| 36 |
assert.Equal(t, map[string]any{"slice": []any{}}, cfg.ToStringMap()) |
| 37 |
} |
| 38 |
|
| 39 |
func TestValidateProviderScheme(t *testing.T) { |
| 40 |
assert.NoError(t, ValidateProviderScheme(&schemeProvider{scheme: "file"})) |
| 41 |
assert.NoError(t, ValidateProviderScheme(&schemeProvider{scheme: "s3"})) |
| 42 |
assert.NoError(t, ValidateProviderScheme(&schemeProvider{scheme: "a.l-l+"})) |
| 43 |
// Too short. |
| 44 |
require.Error(t, ValidateProviderScheme(&schemeProvider{scheme: "a"})) |
| 45 |
// Invalid first character. |
| 46 |
require.Error(t, ValidateProviderScheme(&schemeProvider{scheme: "3s"})) |
| 47 |
// Invalid underscore character. |
| 48 |
assert.Error(t, ValidateProviderScheme(&schemeProvider{scheme: "all_"})) |
| 49 |
} |
| 50 |
|
| 51 |
type schemeProvider struct { |
| 52 |
scheme string |
| 53 |
} |
| 54 |
|
| 55 |
func (s schemeProvider) Retrieve(context.Context, string, confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 56 |
return nil, nil |
| 57 |
} |
| 58 |
|
| 59 |
func (s schemeProvider) Scheme() string { |
| 60 |
return s.scheme |
| 61 |
} |
| 62 |
|
| 63 |
func (s schemeProvider) Shutdown(context.Context) error { |
| 64 |
return nil |
| 65 |
} |