From d7f6b56fb75748d3363e17ee7ea9103613c8f16b Mon Sep 17 00:00:00 2001 From: Dan Jaglowski Date: Thu, 8 Dec 2022 13:52:14 -0500 Subject: [PATCH] Add connector/connectortest package --- .chloggen/connectortest.yaml | 16 ++++ connector/connectortest/connector.go | 102 ++++++++++++++++++++++ connector/connectortest/connector_test.go | 93 ++++++++++++++++++++ 3 files changed, 211 insertions(+) create mode 100755 .chloggen/connectortest.yaml create mode 100644 connector/connectortest/connector.go create mode 100644 connector/connectortest/connector_test.go diff --git a/.chloggen/connectortest.yaml b/.chloggen/connectortest.yaml new file mode 100755 index 00000000000..d1be4bf8ee1 --- /dev/null +++ b/.chloggen/connectortest.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: connectortest + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add connector/connectortest package + +# One or more tracking issues or pull requests related to the change +issues: [6711] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/connector/connectortest/connector.go b/connector/connectortest/connector.go new file mode 100644 index 00000000000..61c3d59a33f --- /dev/null +++ b/connector/connectortest/connector.go @@ -0,0 +1,102 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package componenttest // import "go.opentelemetry.io/collector/connector/connectortest" + +import ( + "context" + + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/connector" + "go.opentelemetry.io/collector/consumer" + "go.opentelemetry.io/collector/consumer/consumertest" +) + +// NewNopCreateSettings returns a new nop settings for Create* functions. +func NewNopCreateSettings() connector.CreateSettings { + return connector.CreateSettings{ + TelemetrySettings: componenttest.NewNopTelemetrySettings(), + BuildInfo: component.NewDefaultBuildInfo(), + } +} + +type nopConfig struct { + config.ConnectorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct +} + +// NewNopFactory returns a connector.Factory that constructs nop processors. +func NewNopFactory() connector.Factory { + return connector.NewFactory( + "nop", + func() component.Config { + return &nopConfig{ + ConnectorSettings: config.NewConnectorSettings(component.NewID("nop")), + } + }, + connector.WithTracesToTraces(createTracesToTracesConnector, component.StabilityLevelDevelopment), + connector.WithTracesToMetrics(createTracesToMetricsConnector, component.StabilityLevelDevelopment), + connector.WithTracesToLogs(createTracesToLogsConnector, component.StabilityLevelDevelopment), + connector.WithMetricsToTraces(createMetricsToTracesConnector, component.StabilityLevelDevelopment), + connector.WithMetricsToMetrics(createMetricsToMetricsConnector, component.StabilityLevelDevelopment), + connector.WithMetricsToLogs(createMetricsToLogsConnector, component.StabilityLevelDevelopment), + connector.WithLogsToTraces(createLogsToTracesConnector, component.StabilityLevelDevelopment), + connector.WithLogsToMetrics(createLogsToMetricsConnector, component.StabilityLevelDevelopment), + connector.WithLogsToLogs(createLogsToLogsConnector, component.StabilityLevelDevelopment), + ) +} + +func createTracesToTracesConnector(context.Context, connector.CreateSettings, component.Config, consumer.Traces) (connector.Traces, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createTracesToMetricsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Metrics) (connector.Traces, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createTracesToLogsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Logs) (connector.Traces, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createMetricsToTracesConnector(context.Context, connector.CreateSettings, component.Config, consumer.Traces) (connector.Metrics, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createMetricsToMetricsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Metrics) (connector.Metrics, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createMetricsToLogsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Logs) (connector.Metrics, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createLogsToTracesConnector(context.Context, connector.CreateSettings, component.Config, consumer.Traces) (connector.Logs, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createLogsToMetricsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Metrics) (connector.Logs, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +func createLogsToLogsConnector(context.Context, connector.CreateSettings, component.Config, consumer.Logs) (connector.Logs, error) { + return &nopConnector{Consumer: consumertest.NewNop()}, nil +} + +// nopConnector stores consumed traces and metrics for testing purposes. +type nopConnector struct { + component.StartFunc + component.ShutdownFunc + consumertest.Consumer +} diff --git a/connector/connectortest/connector_test.go b/connector/connectortest/connector_test.go new file mode 100644 index 00000000000..7a838671bdd --- /dev/null +++ b/connector/connectortest/connector_test.go @@ -0,0 +1,93 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package componenttest + +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/config" + "go.opentelemetry.io/collector/consumer/consumertest" + "go.opentelemetry.io/collector/pdata/plog" + "go.opentelemetry.io/collector/pdata/pmetric" + "go.opentelemetry.io/collector/pdata/ptrace" +) + +func TestNewNopConnectorFactory(t *testing.T) { + factory := NewNopFactory() + require.NotNil(t, factory) + assert.Equal(t, component.Type("nop"), factory.Type()) + cfg := factory.CreateDefaultConfig() + assert.Equal(t, &nopConfig{ConnectorSettings: config.NewConnectorSettings(component.NewID("nop"))}, cfg) + + tracesToTraces, err := factory.CreateTracesToTraces(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, tracesToTraces.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, tracesToTraces.ConsumeTraces(context.Background(), ptrace.NewTraces())) + assert.NoError(t, tracesToTraces.Shutdown(context.Background())) + + tracesToMetrics, err := factory.CreateTracesToMetrics(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, tracesToMetrics.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, tracesToMetrics.ConsumeTraces(context.Background(), ptrace.NewTraces())) + assert.NoError(t, tracesToMetrics.Shutdown(context.Background())) + + tracesToLogs, err := factory.CreateTracesToLogs(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, tracesToLogs.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, tracesToLogs.ConsumeTraces(context.Background(), ptrace.NewTraces())) + assert.NoError(t, tracesToLogs.Shutdown(context.Background())) + + metricsToTraces, err := factory.CreateMetricsToTraces(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, metricsToTraces.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, metricsToTraces.ConsumeMetrics(context.Background(), pmetric.NewMetrics())) + assert.NoError(t, metricsToTraces.Shutdown(context.Background())) + + metricsToMetrics, err := factory.CreateMetricsToMetrics(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, metricsToMetrics.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, metricsToMetrics.ConsumeMetrics(context.Background(), pmetric.NewMetrics())) + assert.NoError(t, metricsToMetrics.Shutdown(context.Background())) + + metricsToLogs, err := factory.CreateMetricsToLogs(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, metricsToLogs.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, metricsToLogs.ConsumeMetrics(context.Background(), pmetric.NewMetrics())) + assert.NoError(t, metricsToLogs.Shutdown(context.Background())) + + logsToTraces, err := factory.CreateLogsToTraces(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, logsToTraces.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, logsToTraces.ConsumeLogs(context.Background(), plog.NewLogs())) + assert.NoError(t, logsToTraces.Shutdown(context.Background())) + + logsToMetrics, err := factory.CreateLogsToMetrics(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, logsToMetrics.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, logsToMetrics.ConsumeLogs(context.Background(), plog.NewLogs())) + assert.NoError(t, logsToMetrics.Shutdown(context.Background())) + + logsToLogs, err := factory.CreateLogsToLogs(context.Background(), NewNopCreateSettings(), cfg, consumertest.NewNop()) + require.NoError(t, err) + assert.NoError(t, logsToLogs.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, logsToLogs.ConsumeLogs(context.Background(), plog.NewLogs())) + assert.NoError(t, logsToLogs.Shutdown(context.Background())) +}