Skip to content

Commit

Permalink
[chore] add tests for connectors, add test for countconnector (open-t…
Browse files Browse the repository at this point in the history
…elemetry#30471)

Relates to open-telemetry#27849 - this adds a template to test connectors, and shows
its use for countconnector.
  • Loading branch information
atoulme authored and cparkins committed Feb 1, 2024
1 parent fa6572e commit 27df619
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 0 deletions.
94 changes: 94 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ func templatize(tmplFile string, md metadata) *template.Template {
"isExtension": func() bool {
return md.Status.Class == "extension"
},
"isConnector": func() bool {
return md.Status.Class == "connector"
},
"skipLifecycle": func() bool {
return md.Tests.SkipLifecycle
},
Expand Down Expand Up @@ -223,6 +226,97 @@ func templatize(tmplFile string, md metadata) *template.Template {
}
return false
},
"supportsLogsToLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "logs_to_logs" {
return true
}
}
}
return false
},
"supportsLogsToMetrics": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "logs_to_metrics" {
return true
}
}
}
return false
},
"supportsLogsToTraces": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "logs_to_traces" {
return true
}
}
}
return false
},
"supportsMetricsToLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "metrics_to_logs" {
return true
}
}
}
return false
},
"supportsMetricsToMetrics": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "metrics_to_metrics" {
return true
}
}
}
return false
},
"supportsMetricsToTraces": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "metrics_to_traces" {
return true
}
}
}
return false
},

"supportsTracesToLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "traces_to_logs" {
return true
}
}
}
return false
},
"supportsTracesToMetrics": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "traces_to_metrics" {
return true
}
}
}
return false
},
"supportsTracesToTraces": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "traces_to_traces" {
return true
}
}
}
return false
},
"expectConsumerError": func() bool {
return md.Tests.ExpectConsumerError
},
Expand Down
122 changes: 122 additions & 0 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (
{{ end }}
{{ if isExtension }}
"go.opentelemetry.io/collector/extension/extensiontest"
{{ end }}
{{ if isConnector }}
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/connector/connectortest"
{{ end }}
"go.opentelemetry.io/collector/confmap/confmaptest"
{{ if or (isExporter) (isProcessor) }}
Expand Down Expand Up @@ -338,3 +343,120 @@ func TestComponentLifecycle(t *testing.T) {
})
}
{{ end }}

{{ if isConnector }}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

tests := []struct{
name string
createFn func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error)
}{
{{ if supportsLogsToLogs }}
{
name: "logs_to_logs",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsToLogs(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsLogsToMetrics }}
{
name: "logs_to_metrics",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsToMetrics(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsLogsToTraces }}
{
name: "logs_to_traces",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsToTraces(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetricsToLogs }}
{
name: "metrics_to_logs",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsToLogs(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetricsToMetrics }}
{
name: "metrics_to_metrics",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsToMetrics(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetricsToTraces }}
{
name: "metrics_to_traces",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsToTraces(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTracesToLogs }}
{
name: "traces_to_logs",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesToLogs(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTracesToMetrics }}
{
name: "traces_to_metrics",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesToMetrics(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTracesToTraces }}
{
name: "traces_to_traces",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesToTraces(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(), connectortest.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 }}
firstConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := newAssertNoErrorHost(t)
require.NoError(t, err)
require.NoError(t, firstConnector.Start(context.Background(), host))
require.NoError(t, firstConnector.Shutdown(context.Background()))
secondConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, secondConnector.Start(context.Background(), host))
require.NoError(t, secondConnector.Shutdown(context.Background()))
})
}
}
{{ end }}
100 changes: 100 additions & 0 deletions connector/countconnector/generated_component_test.go

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

3 changes: 3 additions & 0 deletions connector/countconnector/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ status:
distributions: [contrib, sumo]
codeowners:
active: [djaglowski, jpkrohling]

tests:
config:

0 comments on commit 27df619

Please sign in to comment.