From 8940d18f0d77ba215dbfc026e392e80269830989 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Thu, 11 Jan 2024 23:10:09 -0800 Subject: [PATCH] [chore] add tests for connectors, add test for countconnector --- cmd/mdatagen/main.go | 94 ++++++++++++++ cmd/mdatagen/templates/component_test.go.tmpl | 122 ++++++++++++++++++ .../generated_component_test.go | 100 ++++++++++++++ connector/countconnector/metadata.yaml | 3 + 4 files changed, 319 insertions(+) create mode 100644 connector/countconnector/generated_component_test.go diff --git a/cmd/mdatagen/main.go b/cmd/mdatagen/main.go index d42ce8dbef7a..2aa239b5ad8c 100644 --- a/cmd/mdatagen/main.go +++ b/cmd/mdatagen/main.go @@ -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 }, @@ -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 }, diff --git a/cmd/mdatagen/templates/component_test.go.tmpl b/cmd/mdatagen/templates/component_test.go.tmpl index 4417e14637c3..2f087060b2e7 100644 --- a/cmd/mdatagen/templates/component_test.go.tmpl +++ b/cmd/mdatagen/templates/component_test.go.tmpl @@ -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) }} @@ -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 }} \ No newline at end of file diff --git a/connector/countconnector/generated_component_test.go b/connector/countconnector/generated_component_test.go new file mode 100644 index 000000000000..307cab8136a3 --- /dev/null +++ b/connector/countconnector/generated_component_test.go @@ -0,0 +1,100 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package countconnector + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" + + "go.opentelemetry.io/collector/connector" + "go.opentelemetry.io/collector/connector/connectortest" + "go.opentelemetry.io/collector/consumer/consumertest" + + "go.opentelemetry.io/collector/confmap/confmaptest" +) + +// assertNoErrorHost implements a component.Host that asserts that there were no errors. +type assertNoErrorHost struct { + component.Host + *testing.T +} + +var _ component.Host = (*assertNoErrorHost)(nil) + +// newAssertNoErrorHost returns a new instance of assertNoErrorHost. +func newAssertNoErrorHost(t *testing.T) component.Host { + return &assertNoErrorHost{ + componenttest.NewNopHost(), + t, + } +} + +func (aneh *assertNoErrorHost) ReportFatalError(err error) { + assert.NoError(aneh, err) +} + +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) + }{ + + { + 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()) + }, + }, + + { + 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()) + }, + }, + + { + 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()) + }, + }, + } + + 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) { + + 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())) + }) + } +} diff --git a/connector/countconnector/metadata.yaml b/connector/countconnector/metadata.yaml index 42ab007b9cad..14c8a3a5990b 100644 --- a/connector/countconnector/metadata.yaml +++ b/connector/countconnector/metadata.yaml @@ -7,3 +7,6 @@ status: distributions: [contrib, sumo] codeowners: active: [djaglowski, jpkrohling] + +tests: + config: