| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package extension |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component" |
| 14 |
) |
| 15 |
|
| 16 |
type nopExtension struct { |
| 17 |
component.StartFunc |
| 18 |
component.ShutdownFunc |
| 19 |
Settings |
| 20 |
} |
| 21 |
|
| 22 |
func TestNewFactory(t *testing.T) { |
| 23 |
testType := component.MustNewType("test") |
| 24 |
defaultCfg := struct{}{} |
| 25 |
nopExtensionInstance := new(nopExtension) |
| 26 |
|
| 27 |
factory := NewFactory( |
| 28 |
testType, |
| 29 |
func() component.Config { return &defaultCfg }, |
| 30 |
func(context.Context, Settings, component.Config) (Extension, error) { |
| 31 |
return nopExtensionInstance, nil |
| 32 |
}, |
| 33 |
component.StabilityLevelDevelopment) |
| 34 |
assert.Equal(t, testType, factory.Type()) |
| 35 |
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig()) |
| 36 |
|
| 37 |
assert.Equal(t, component.StabilityLevelDevelopment, factory.Stability()) |
| 38 |
ext, err := factory.Create(context.Background(), Settings{ID: component.NewID(testType)}, &defaultCfg) |
| 39 |
require.NoError(t, err) |
| 40 |
assert.Same(t, nopExtensionInstance, ext) |
| 41 |
|
| 42 |
_, err = factory.Create(context.Background(), Settings{ID: component.NewID(component.MustNewType("mismatch"))}, &defaultCfg) |
| 43 |
require.Error(t, err) |
| 44 |
} |