From 9cb46134b2a97f5e1880775e29745384e7e532bb Mon Sep 17 00:00:00 2001 From: Murphy Chen Date: Thu, 29 Feb 2024 05:20:16 +0800 Subject: [PATCH] [chore] move file exporter to generated lifecycle tests (#30523) Relates to https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/27849 --------- Co-authored-by: Antoine Toulme --- .../fileexporter/generated_component_test.go | 108 ++++++++++++++++++ exporter/fileexporter/metadata.yaml | 5 + 2 files changed, 113 insertions(+) create mode 100644 exporter/fileexporter/generated_component_test.go diff --git a/exporter/fileexporter/generated_component_test.go b/exporter/fileexporter/generated_component_test.go new file mode 100644 index 000000000000..53e25c36a807 --- /dev/null +++ b/exporter/fileexporter/generated_component_test.go @@ -0,0 +1,108 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package fileexporter + +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) + }, + }, + + { + name: "traces", + createFn: func(ctx context.Context, set exporter.CreateSettings, cfg component.Config) (component.Component, error) { + return factory.CreateTracesExporter(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) { + + 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) + } + }) + + err = c.Shutdown(context.Background()) + require.NoError(t, err) + }) + } +} diff --git a/exporter/fileexporter/metadata.yaml b/exporter/fileexporter/metadata.yaml index 7610d9a67c61..a200e3c3eebf 100644 --- a/exporter/fileexporter/metadata.yaml +++ b/exporter/fileexporter/metadata.yaml @@ -7,3 +7,8 @@ status: distributions: [core, contrib, observiq, splunk, sumo, aws] codeowners: active: [atingchen] + +tests: + config: + path: testdata/log.json + expect_consumer_error: true \ No newline at end of file