1// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package ballastextension
16
17import (
18	"path"
19	"testing"
20
21	"github.com/stretchr/testify/assert"
22	"github.com/stretchr/testify/require"
23
24	"go.opentelemetry.io/collector/component/componenttest"
25	"go.opentelemetry.io/collector/config"
26	"go.opentelemetry.io/collector/config/configtest"
27)
28
29func TestLoadConfig(t *testing.T) {
30	factories, err := componenttest.NopFactories()
31	assert.NoError(t, err)
32
33	factory := NewFactory()
34	factories.Extensions[typeStr] = factory
35	cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
36
37	require.Nil(t, err)
38	require.NotNil(t, cfg)
39
40	ext0 := cfg.Extensions[config.NewID(typeStr)]
41	assert.Equal(t, factory.CreateDefaultConfig(), ext0)
42
43	ext1 := cfg.Extensions[config.NewIDWithName(typeStr, "1")]
44	assert.Equal(t,
45		&Config{
46			ExtensionSettings: config.NewExtensionSettings(config.NewIDWithName(typeStr, "1")),
47			SizeMiB:           123,
48			SizeInPercentage:  20,
49		},
50		ext1)
51
52	assert.Equal(t, 1, len(cfg.Service.Extensions))
53	assert.Equal(t, config.NewIDWithName(typeStr, "1"), cfg.Service.Extensions[0])
54}
55
56func TestLoadInvalidConfig(t *testing.T) {
57	factories, err := componenttest.NopFactories()
58	assert.NoError(t, err)
59
60	factory := NewFactory()
61	factories.Extensions[typeStr] = factory
62	_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config_invalid.yaml"), factories)
63
64	require.NotNil(t, err)
65	assert.Equal(t, err.Error(), "extension \"memory_ballast\" has invalid configuration: size_in_percentage is not in range 0 to 100")
66
67}
68