Skip to content

Commit

Permalink
[chore] add generated tests for receivers (#29682)
Browse files Browse the repository at this point in the history
Continuation of the work on
#27849
- now opening a way to create lifecycle tests for receivers.
  • Loading branch information
atoulme authored Dec 7, 2023
1 parent 01927d4 commit cda887d
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 8 deletions.
77 changes: 76 additions & 1 deletion cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ import (
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processortest"
{{ end }}
{{ if isReceiver }}
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
{{ end }}
"go.opentelemetry.io/collector/confmap/confmaptest"

{{ if or (isExporter) (isProcessor) }}
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
{{ end }}
)

// assertNoErrorHost implements a component.Host that asserts that there were no errors.
Expand Down Expand Up @@ -224,3 +230,72 @@ func Test_ComponentLifecycle(t *testing.T) {
}
}
{{ end }}

{{ if isReceiver }}
func Test_ComponentLifecycle(t *testing.T) {
factory := NewFactory()

tests := []struct{
name string
createFn func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error)
}{
{{ if supportsLogs }}
{
name: "logs",
createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsReceiver(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetrics }}
{
name: "metrics",
createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsReceiver(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTraces }}
{
name: "traces",
createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesReceiver(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
}

cm, err := confmaptest.LoadConf("metadata.yaml")
require.NoError(t, err)
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub("tests::config")
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

for _, test := range tests {
t.Run(test.name + "-shutdown", func(t *testing.T) {
c, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})

t.Run(test.name + "-lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ end }}
firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := newAssertNoErrorHost(t)
require.NoError(t, err)
require.NoError(t, firstRcvr.Start(context.Background(), host))
require.NoError(t, firstRcvr.Shutdown(context.Background()))
secondRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, secondRcvr.Start(context.Background(), host))
require.NoError(t, secondRcvr.Shutdown(context.Background()))
})
}
}
{{ end }}
93 changes: 93 additions & 0 deletions receiver/splunkhecreceiver/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions receiver/splunkhecreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ status:
codeowners:
active: ["atoulme"]
emeritus:
tests:
config:
14 changes: 7 additions & 7 deletions receiver/splunkhecreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func Test_splunkhecReceiver_TLS(t *testing.T) {
require.NoError(t, r.Shutdown(context.Background()))
}()

mh := newAssertNoErrorHost(t)
mh := newAssertNoErrHost(t)
require.NoError(t, r.Start(context.Background(), mh), "should not have failed to start log reception")
require.NoError(t, r.Start(context.Background(), mh), "should not fail to start log on second Start call")

Expand Down Expand Up @@ -898,21 +898,21 @@ func (b badReqBody) Close() error {
return nil
}

// assertNoErrorHost implements a component.Host that asserts that there were no errors.
type assertNoErrorHost struct {
// assertNoErrHost implements a component.Host that asserts that there were no errors.
type assertNoErrHost struct {
component.Host
*testing.T
}

// newAssertNoErrorHost returns a new instance of assertNoErrorHost.
func newAssertNoErrorHost(t *testing.T) component.Host {
return &assertNoErrorHost{
// newAssertNoErrHost returns a new instance of assertNoErrHost.
func newAssertNoErrHost(t *testing.T) component.Host {
return &assertNoErrHost{
Host: componenttest.NewNopHost(),
T: t,
}
}

func (aneh *assertNoErrorHost) ReportFatalError(err error) {
func (aneh *assertNoErrHost) ReportFatalError(err error) {
assert.NoError(aneh, err)
}

Expand Down

0 comments on commit cda887d

Please sign in to comment.