From aa7dd0510b1f214db330af6e0a17af7f4961eacf Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 12 Jan 2024 14:15:57 -0800 Subject: [PATCH] [chore] add generated lifecycle tests (#30489) Relates to https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/27849 --- .../generated_component_test.go | 86 +++++++++++++++++++ connector/servicegraphconnector/go.mod | 2 +- connector/servicegraphconnector/metadata.yaml | 3 + 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 connector/servicegraphconnector/generated_component_test.go diff --git a/connector/servicegraphconnector/generated_component_test.go b/connector/servicegraphconnector/generated_component_test.go new file mode 100644 index 000000000000..9f84efa6bee6 --- /dev/null +++ b/connector/servicegraphconnector/generated_component_test.go @@ -0,0 +1,86 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package servicegraphconnector + +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: "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/servicegraphconnector/go.mod b/connector/servicegraphconnector/go.mod index 5a8bfb39b091..fbe7002dad1f 100644 --- a/connector/servicegraphconnector/go.mod +++ b/connector/servicegraphconnector/go.mod @@ -6,6 +6,7 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/processor/servicegraphprocessor v0.92.0 github.com/stretchr/testify v1.8.4 go.opentelemetry.io/collector/component v0.92.1-0.20240112172857-83d463ceba06 + go.opentelemetry.io/collector/confmap v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/connector v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/collector/consumer v0.92.1-0.20240112172857-83d463ceba06 go.opentelemetry.io/otel/metric v1.21.0 @@ -44,7 +45,6 @@ require ( go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/collector v0.92.1-0.20240112172857-83d463ceba06 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.92.1-0.20240112172857-83d463ceba06 // indirect - go.opentelemetry.io/collector/confmap v0.92.1-0.20240112172857-83d463ceba06 // indirect go.opentelemetry.io/collector/exporter v0.92.1-0.20240112172857-83d463ceba06 // indirect go.opentelemetry.io/collector/featuregate v1.0.2-0.20240112172857-83d463ceba06 // indirect go.opentelemetry.io/collector/pdata v1.0.2-0.20240112172857-83d463ceba06 // indirect diff --git a/connector/servicegraphconnector/metadata.yaml b/connector/servicegraphconnector/metadata.yaml index b6475ebc66cb..7a27fb79dbfd 100644 --- a/connector/servicegraphconnector/metadata.yaml +++ b/connector/servicegraphconnector/metadata.yaml @@ -7,3 +7,6 @@ status: distributions: [contrib, sumo, grafana] codeowners: active: [jpkrohling, mapno] + +tests: + config: