Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/mdatagen] Make lifecycle tests generated by default #31532

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .chloggen/mdata-gen-generate-lifecycle-tests-by-default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use this changelog template to create an entry for release notes.

# 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. filelogreceiver)
component: 'cmd/mdatagen'

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Make lifecycle tests generated by default

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [31532]

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func (a attribute) TestValue() string {
type tests struct {
Config any `mapstructure:"config"`
SkipLifecycle bool `mapstructure:"skip_lifecycle"`
SkipShutdown bool `mapstructure:"skip_shutdown"`
ExpectConsumerError bool `mapstructure:"expect_consumer_error"`
}

Expand All @@ -225,7 +226,7 @@ type metadata struct {
// ShortFolderName is the shortened folder name of the component, removing class if present
ShortFolderName string `mapstructure:"-"`

Tests *tests `mapstructure:"tests"`
Tests tests `mapstructure:"tests"`
}

func setAttributesFullName(attrs map[attributeName]attribute) {
Expand Down
1 change: 0 additions & 1 deletion cmd/mdatagen/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ func TestLoadMetadata(t *testing.T) {
},
ScopeName: "otelcol/samplereceiver",
ShortFolderName: "sample",
Tests: &tests{},
},
},
{
Expand Down
16 changes: 6 additions & 10 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func run(ymlPath string) error {
filepath.Join(codeDir, "generated_status.go"), md, "metadata"); err != nil {
return err
}
if !md.Tests.SkipLifecycle || !md.Tests.SkipShutdown {
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err
}
}
}

if _, err = os.Stat(filepath.Join(ymlDir, "README.md")); err == nil {
Expand All @@ -74,13 +80,6 @@ func run(ymlPath string) error {
}
}

if md.Tests != nil {
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err
}
}

if len(md.Metrics) == 0 && len(md.ResourceAttributes) == 0 {
return nil
}
Expand Down Expand Up @@ -195,9 +194,6 @@ func templatize(tmplFile string, md metadata) *template.Template {
"isConnector": func() bool {
return md.Status.Class == "connector"
},
"skipLifecycle": func() bool {
return md.Tests.SkipLifecycle
},
"supportsLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
Expand Down
4 changes: 4 additions & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,9 @@ metrics:
# Lifecycle tests generated for this component.
tests:
config: # {} by default, specific testing configuration for lifecycle tests.
# Skip lifecycle tests for this component. Not recommended for components that are not in development.
skip_lifecycle: false # false by default
# Skip shutdown tests for this component. Not recommended for components that are not in development.
skip_shutdown: false # false by default
# Whether it's expected that the Consume[Logs|Metrics|Traces] method will return an error with the given configuration.
expect_consumer_error: true # false by default
76 changes: 39 additions & 37 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by mdatagen. DO NOT EDIT.

{{ if len .Status.UnsupportedPlatforms -}}
{{- if len .Status.UnsupportedPlatforms }}
//go:build {{ range $i, $v := .Status.UnsupportedPlatforms }}{{ if $i }} && {{ end }}!{{ . }}{{ end }}
{{- end }}

Expand All @@ -9,45 +9,47 @@ package {{ .Package }}
import (
"context"
"testing"
{{- if or isExporter isProcessor }}
"time"
{{- end }}
{{- if or isExporter isProcessor }}
"time"
{{- end }}

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
{{- if not .Tests.SkipLifecycle }}
"go.opentelemetry.io/collector/component/componenttest"
{{ if isExporter }}
{{- end }}
{{- if isExporter }}
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exportertest"
{{ end }}
{{ if isProcessor }}
{{- end }}
{{- if isProcessor }}
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processortest"
{{ end }}
{{ if isReceiver }}
{{- end }}
{{- if isReceiver }}
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
{{ end }}
{{ if isExtension }}
{{- end }}
{{- if isExtension }}
"go.opentelemetry.io/collector/extension/extensiontest"
{{ end }}
{{ if isConnector }}
{{- end }}
{{- if isConnector }}
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/connector/connectortest"
{{ end }}
{{- end }}
"go.opentelemetry.io/collector/confmap/confmaptest"
{{- if or isExporter isProcessor }}
{{- if or isExporter isProcessor }}
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
{{- end }}
{{- end }}
)

{{ if isExporter }}
{{ if isExporter -}}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

Expand Down Expand Up @@ -89,18 +91,17 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, component.UnmarshalConfig(sub, cfg))

for _, test := range tests {
{{- if not .Tests.SkipShutdown }}
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)
})
{{- end }}

{{- if not .Tests.SkipLifecycle }}
t.Run(test.name + "-lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ end }}
c, err := test.createFn(context.Background(), exportertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := componenttest.NewNopHost()
Expand Down Expand Up @@ -134,6 +135,7 @@ func TestComponentLifecycle(t *testing.T) {
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
{{- end }}
}
}
{{ end }}
Expand Down Expand Up @@ -180,18 +182,17 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, component.UnmarshalConfig(sub, cfg))

for _, test := range tests {
{{- if not .Tests.SkipShutdown }}
t.Run(test.name + "-shutdown", func(t *testing.T) {
c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
{{- end }}

{{- if not .Tests.SkipLifecycle }}
t.Run(test.name + "-lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ end }}
c, err := test.createFn(context.Background(), processortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := componenttest.NewNopHost()
Expand Down Expand Up @@ -223,6 +224,7 @@ func TestComponentLifecycle(t *testing.T) {
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
{{- end }}
}
}
{{ end }}
Expand Down Expand Up @@ -269,18 +271,17 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, component.UnmarshalConfig(sub, cfg))

for _, test := range tests {
{{- if not .Tests.SkipShutdown }}
t.Run(test.name + "-shutdown", func(t *testing.T) {
c, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
{{- end }}

{{- if not .Tests.SkipLifecycle }}
t.Run(test.name + "-lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ end }}
firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := componenttest.NewNopHost()
Expand All @@ -292,6 +293,7 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, secondRcvr.Start(context.Background(), host))
require.NoError(t, secondRcvr.Shutdown(context.Background()))
})
{{- end }}
}
}
{{ end }}
Expand All @@ -307,18 +309,17 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

{{- if not .Tests.SkipShutdown }}
t.Run("shutdown", func(t *testing.T) {
e, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
err = e.Shutdown(context.Background())
require.NoError(t, err)
})
{{- end }}

{{- if not .Tests.SkipLifecycle }}
t.Run("lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ end }}
firstExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, firstExt.Start(context.Background(), componenttest.NewNopHost()))
Expand All @@ -329,6 +330,7 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, secondExt.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, secondExt.Shutdown(context.Background()))
})
{{- end }}
}
{{ end }}

Expand Down Expand Up @@ -422,18 +424,17 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, component.UnmarshalConfig(sub, cfg))

for _, test := range tests {
{{- if not .Tests.SkipShutdown }}
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)
})
{{- end }}

{{- if not .Tests.SkipLifecycle }}
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 := componenttest.NewNopHost()
Expand All @@ -445,6 +446,7 @@ func TestComponentLifecycle(t *testing.T) {
require.NoError(t, secondConnector.Start(context.Background(), host))
require.NoError(t, secondConnector.Shutdown(context.Background()))
})
{{- end }}
}
}
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions cmd/mdatagen/templates/status.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
package {{ .Package }}

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)

{{ if eq .Parent "" }}
{{ if eq .Parent "" -}}
var (
Type = component.MustNewType("{{ .Type }}")
)
Expand Down
4 changes: 4 additions & 0 deletions cmd/mdatagen/testdata/metrics_and_type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ metrics:
unit: s
gauge:
value_type: double

tests:
skip_lifecycle: true
skip_shutdown: true
4 changes: 4 additions & 0 deletions cmd/mdatagen/testdata/resource_attributes_only.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ resource_attributes:
description: Resource attribute 1.
type: string
enabled: true

tests:
skip_lifecycle: true
skip_shutdown: true
4 changes: 4 additions & 0 deletions cmd/mdatagen/testdata/status_only.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ status:
stability:
beta: [traces, metrics, logs]
distributions: [contrib]

tests:
skip_lifecycle: true
skip_shutdown: true
Loading
Loading