Skip to content

Commit

Permalink
[chore] Improve factory tests for all components (#11658)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Nov 12, 2024
1 parent 410b4f7 commit 924375f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
37 changes: 19 additions & 18 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,47 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pipeline"
)

func TestNewFactory(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
f := NewFactory(
testType,
func() component.Config { return &defaultCfg })
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
_, err := factory.CreateTraces(context.Background(), Settings{}, &defaultCfg)
require.Error(t, err)
_, err = factory.CreateMetrics(context.Background(), Settings{}, &defaultCfg)
require.Error(t, err)
_, err = factory.CreateLogs(context.Background(), Settings{}, &defaultCfg)
assert.Error(t, err)
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())
_, err := f.CreateTraces(context.Background(), Settings{}, &defaultCfg)
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateMetrics(context.Background(), Settings{}, &defaultCfg)
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg)
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
}

func TestNewFactoryWithOptions(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
f := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithTraces(createTraces, component.StabilityLevelDevelopment),
WithMetrics(createMetrics, component.StabilityLevelAlpha),
WithLogs(createLogs, component.StabilityLevelDeprecated))
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())

assert.Equal(t, component.StabilityLevelDevelopment, factory.TracesStability())
_, err := factory.CreateTraces(context.Background(), Settings{}, &defaultCfg)
assert.Equal(t, component.StabilityLevelDevelopment, f.TracesStability())
_, err := f.CreateTraces(context.Background(), Settings{}, &defaultCfg)
require.NoError(t, err)

assert.Equal(t, component.StabilityLevelAlpha, factory.MetricsStability())
_, err = factory.CreateMetrics(context.Background(), Settings{}, &defaultCfg)
assert.Equal(t, component.StabilityLevelAlpha, f.MetricsStability())
_, err = f.CreateMetrics(context.Background(), Settings{}, &defaultCfg)
require.NoError(t, err)

assert.Equal(t, component.StabilityLevelDeprecated, factory.LogsStability())
_, err = factory.CreateLogs(context.Background(), Settings{}, &defaultCfg)
assert.Equal(t, component.StabilityLevelDeprecated, f.LogsStability())
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg)
assert.NoError(t, err)
}

Expand Down
37 changes: 19 additions & 18 deletions processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,47 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pipeline"
)

func TestNewFactory(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
f := NewFactory(
testType,
func() component.Config { return &defaultCfg })
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
_, err := factory.CreateTraces(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.Error(t, err)
_, err = factory.CreateMetrics(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.Error(t, err)
_, err = factory.CreateLogs(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
assert.Error(t, err)
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())
_, err := f.CreateTraces(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateMetrics(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
}

func TestNewFactoryWithOptions(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
f := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithTraces(createTraces, component.StabilityLevelAlpha),
WithMetrics(createMetrics, component.StabilityLevelBeta),
WithLogs(createLogs, component.StabilityLevelUnmaintained))
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())

assert.Equal(t, component.StabilityLevelAlpha, factory.TracesStability())
_, err := factory.CreateTraces(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, component.StabilityLevelAlpha, f.TracesStability())
_, err := f.CreateTraces(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.NoError(t, err)

assert.Equal(t, component.StabilityLevelBeta, factory.MetricsStability())
_, err = factory.CreateMetrics(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, component.StabilityLevelBeta, f.MetricsStability())
_, err = f.CreateMetrics(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.NoError(t, err)

assert.Equal(t, component.StabilityLevelUnmaintained, factory.LogsStability())
_, err = factory.CreateLogs(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
assert.Equal(t, component.StabilityLevelUnmaintained, f.LogsStability())
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
assert.NoError(t, err)
}

Expand Down
37 changes: 19 additions & 18 deletions receiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,47 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pipeline"
)

func TestNewFactory(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
f := NewFactory(
testType,
func() component.Config { return &defaultCfg })
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
_, err := factory.CreateTraces(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.Error(t, err)
_, err = factory.CreateMetrics(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.Error(t, err)
_, err = factory.CreateLogs(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
assert.Error(t, err)
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())
_, err := f.CreateTraces(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateMetrics(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg, consumertest.NewNop())
require.ErrorIs(t, err, pipeline.ErrSignalNotSupported)
}

func TestNewFactoryWithOptions(t *testing.T) {
var testType = component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
f := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithTraces(createTraces, component.StabilityLevelDeprecated),
WithMetrics(createMetrics, component.StabilityLevelAlpha),
WithLogs(createLogs, component.StabilityLevelStable))
assert.EqualValues(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
assert.EqualValues(t, testType, f.Type())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())

assert.Equal(t, component.StabilityLevelDeprecated, factory.TracesStability())
_, err := factory.CreateTraces(context.Background(), Settings{}, &defaultCfg, nil)
assert.Equal(t, component.StabilityLevelDeprecated, f.TracesStability())
_, err := f.CreateTraces(context.Background(), Settings{}, &defaultCfg, nil)
require.NoError(t, err)

assert.Equal(t, component.StabilityLevelAlpha, factory.MetricsStability())
_, err = factory.CreateMetrics(context.Background(), Settings{}, &defaultCfg, nil)
assert.Equal(t, component.StabilityLevelAlpha, f.MetricsStability())
_, err = f.CreateMetrics(context.Background(), Settings{}, &defaultCfg, nil)
require.NoError(t, err)

assert.Equal(t, component.StabilityLevelStable, factory.LogsStability())
_, err = factory.CreateLogs(context.Background(), Settings{}, &defaultCfg, nil)
assert.Equal(t, component.StabilityLevelStable, f.LogsStability())
_, err = f.CreateLogs(context.Background(), Settings{}, &defaultCfg, nil)
assert.NoError(t, err)
}

Expand Down

0 comments on commit 924375f

Please sign in to comment.