From 7a5874da3d2f299b9cfb5538c0ab54952085db46 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 31 Jan 2024 21:17:59 -0800 Subject: [PATCH] [chore] add generated lifecycle tests to skywalking exporter (#30976) Relates to https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/27849 --- .../generated_component_test.go | 106 ++++++++++++++++++ exporter/skywalkingexporter/metadata.yaml | 4 + 2 files changed, 110 insertions(+) create mode 100644 exporter/skywalkingexporter/generated_component_test.go diff --git a/exporter/skywalkingexporter/generated_component_test.go b/exporter/skywalkingexporter/generated_component_test.go new file mode 100644 index 000000000000..9fa2c39c6375 --- /dev/null +++ b/exporter/skywalkingexporter/generated_component_test.go @@ -0,0 +1,106 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package skywalkingexporter + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" + + "go.opentelemetry.io/collector/exporter" + "go.opentelemetry.io/collector/exporter/exportertest" + + "go.opentelemetry.io/collector/confmap/confmaptest" + + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" +) + +// assertNoErrorHost implements a component.Host that asserts that there were no errors. +type assertNoErrorHost struct { + component.Host + *testing.T +} + +var _ component.Host = (*assertNoErrorHost)(nil) + +func TestComponentLifecycle(t *testing.T) { + factory := NewFactory() + + tests := []struct { + name string + createFn func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) + }{ + + { + name: "logs", + createFn: func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) { + return factory.CreateLogsExporter(ctx, set, cfg) + }, + }, + + { + name: "metrics", + createFn: func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) { + return factory.CreateMetricsExporter(ctx, set, cfg) + }, + }, + } + + 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(), exportertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + err = c.Shutdown(context.Background()) + require.NoError(t, err) + }) + + t.Run(test.name+"-lifecycle", func(t *testing.T) { + + // TODO support lifecycle + t.SkipNow() + + c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + host := componenttest.NewNopHost() + err = c.Start(context.Background(), host) + require.NoError(t, err) + require.NotPanics(t, func() { + switch e := c.(type) { + case exporter.Logs: + logs := testdata.GenerateLogsManyLogRecordsSameResource(2) + if !e.Capabilities().MutatesData { + logs.MarkReadOnly() + } + err = e.ConsumeLogs(context.Background(), logs) + case exporter.Metrics: + metrics := testdata.GenerateMetricsTwoMetrics() + if !e.Capabilities().MutatesData { + metrics.MarkReadOnly() + } + err = e.ConsumeMetrics(context.Background(), metrics) + case exporter.Traces: + traces := testdata.GenerateTracesTwoSpansSameResource() + if !e.Capabilities().MutatesData { + traces.MarkReadOnly() + } + err = e.ConsumeTraces(context.Background(), traces) + } + }) + + require.NoError(t, err) + + err = c.Shutdown(context.Background()) + require.NoError(t, err) + }) + } +} diff --git a/exporter/skywalkingexporter/metadata.yaml b/exporter/skywalkingexporter/metadata.yaml index 3a9b824d294c..8ee4d617a205 100644 --- a/exporter/skywalkingexporter/metadata.yaml +++ b/exporter/skywalkingexporter/metadata.yaml @@ -8,3 +8,7 @@ status: codeowners: active: [] emeritus: [liqiangz] + +tests: + config: + skip_lifecycle: true