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

[mdatagen] generate goleak package test #9959

Merged
merged 5 commits into from
Apr 17, 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
25 changes: 25 additions & 0 deletions .chloggen/codeboten_mdatagen-goleak.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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. otlpreceiver)
component: mdatagen

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

# One or more tracking issues or pull requests related to the change
issues: [9959]

# (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:

# 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: []
1 change: 1 addition & 0 deletions cmd/mdatagen/embeded_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestEnsureTemplatesLoaded(t *testing.T) {
path.Join(rootDir, "resource_test.go.tmpl"): {},
path.Join(rootDir, "config.go.tmpl"): {},
path.Join(rootDir, "config_test.go.tmpl"): {},
path.Join(rootDir, "package_test.go.tmpl"): {},
path.Join(rootDir, "readme.md.tmpl"): {},
path.Join(rootDir, "status.go.tmpl"): {},
path.Join(rootDir, "testdata", "config.yaml.tmpl"): {},
Expand Down

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

19 changes: 15 additions & 4 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,22 @@ func (a attribute) TestValue() string {
return ""
}

type ignore struct {
Top []string `mapstructure:"top"`
Any []string `mapstructure:"any"`
}

type goLeak struct {
Skip bool `mapstructure:"skip"`
Ignore ignore `mapstructure:"ignore"`
}

type tests struct {
Config any `mapstructure:"config"`
SkipLifecycle bool `mapstructure:"skip_lifecycle"`
SkipShutdown bool `mapstructure:"skip_shutdown"`
ExpectConsumerError bool `mapstructure:"expect_consumer_error"`
Config any `mapstructure:"config"`
SkipLifecycle bool `mapstructure:"skip_lifecycle"`
SkipShutdown bool `mapstructure:"skip_shutdown"`
GoLeak goLeak `mapstructure:"goleak"`
ExpectConsumerError bool `mapstructure:"expect_consumer_error"`
}

type metadata struct {
Expand Down
4 changes: 4 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func run(ymlPath string) error {
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "package_test.go.tmpl"),
Copy link
Member

@crobert-1 crobert-1 Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with the inner-workings of mdatagen, but would it be very hard to modify so that sub-packages also have goleak generated?

For example:

.//consumer/consumertest/package_test.go
.//consumer/consumererror/package_test.go
.//consumer/package_test.go

One of the potential challenges of this is that each subdir/package would need its own goleak options, as it may need skipped, or have its own ignores that we don't want in other packages.

It's likely out of the scope of this PR, just raising as something that could be done here, or will need to be done later.

filepath.Join(ymlDir, "generated_package_test.go"), md, packageName); err != nil {
return err
}
}

if _, err = os.Stat(filepath.Join(ymlDir, "README.md")); err == nil {
Expand Down
33 changes: 33 additions & 0 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestRunContents(t *testing.T) {
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantGoleakIgnore bool
wantGoleakSkip bool
wantErr bool
}{
{
Expand Down Expand Up @@ -64,6 +66,16 @@ func TestRunContents(t *testing.T) {
yml: "with_tests_connector.yaml",
wantStatusGenerated: true,
},
{
yml: "with_goleak_ignores.yaml",
wantStatusGenerated: true,
wantGoleakIgnore: true,
},
{
yml: "with_goleak_skip.yaml",
wantStatusGenerated: true,
wantGoleakSkip: true,
},
}
for _, tt := range tests {
t.Run(tt.yml, func(t *testing.T) {
Expand Down Expand Up @@ -123,6 +135,27 @@ foo
require.Contains(t, string(contents), "func Test")
_, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors)
require.NoError(t, err)

require.FileExists(t, filepath.Join(tmpdir, "generated_package_test.go"))
contents, err = os.ReadFile(filepath.Join(tmpdir, "generated_package_test.go")) // nolint: gosec
require.NoError(t, err)
require.Contains(t, string(contents), "func TestMain")
_, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors)
require.NoError(t, err)

if tt.wantGoleakSkip {
require.Contains(t, string(contents), "skipping goleak test")
} else {
require.NotContains(t, string(contents), "skipping goleak test")
}

if tt.wantGoleakIgnore {
require.Contains(t, string(contents), "IgnoreTopFunction")
require.Contains(t, string(contents), "IgnoreAnyFunction")
} else {
require.NotContains(t, string(contents), "IgnoreTopFunction")
require.NotContains(t, string(contents), "IgnoreAnyFunction")
}
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,8 @@ tests:
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
goleak: # {} by default generates a package_test to enable check for leaks
skip: false # set to true if goleak tests should be skipped
ignore:
top: [string] # Optional: array of strings representing functions that should be ignore via IgnoreTopFunction
any: [string] # Optional: array of strings representing functions that should be ignore via IgnoreAnyFunction
17 changes: 17 additions & 0 deletions cmd/mdatagen/templates/package_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Code generated by mdatagen. DO NOT EDIT.

package {{ .Package }}

import (
"testing"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
{{- if .Tests.GoLeak.Skip }}
// skipping goleak test as per metadata.yml configuration
{{- else }}
goleak.VerifyTestMain(m {{- range $val := .Tests.GoLeak.Ignore.Top}}, goleak.IgnoreTopFunction("{{$val}}"){{end}}{{- range $val := .Tests.GoLeak.Ignore.Any}}, goleak.IgnoreAnyFunction("{{$val}}"){{end}} )
{{- end }}
}
14 changes: 14 additions & 0 deletions cmd/mdatagen/testdata/with_goleak_ignores.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type: foobar

status:
class: connector
stability:
beta: [traces_to_metrics, traces_to_traces, traces_to_logs, metrics_to_logs, metrics_to_metrics, metrics_to_traces, logs_to_logs, logs_to_metrics, logs_to_traces]

tests:
goleak:
ignore:
top:
- "testfunc1"
any:
- "testfunc2"
10 changes: 10 additions & 0 deletions cmd/mdatagen/testdata/with_goleak_skip.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: foobar

status:
class: connector
stability:
beta: [traces_to_metrics, traces_to_traces, traces_to_logs, metrics_to_logs, metrics_to_metrics, metrics_to_traces, logs_to_logs, logs_to_metrics, logs_to_traces]

tests:
goleak:
skip: true

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

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

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

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

1 change: 1 addition & 0 deletions exporter/nopexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
go.opentelemetry.io/collector/pdata v1.5.0
go.opentelemetry.io/otel/metric v1.25.0
go.opentelemetry.io/otel/trace v1.25.0
go.uber.org/goleak v1.3.0
)

require (
Expand Down

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

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

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

13 changes: 13 additions & 0 deletions extension/memorylimiterextension/generated_package_test.go

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

1 change: 1 addition & 0 deletions extension/memorylimiterextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
go.opentelemetry.io/collector/extension v0.98.0
go.opentelemetry.io/otel/metric v1.25.0
go.opentelemetry.io/otel/trace v1.25.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
)

Expand Down

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

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

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

13 changes: 13 additions & 0 deletions receiver/nopreceiver/generated_package_test.go

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

1 change: 1 addition & 0 deletions receiver/nopreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
go.opentelemetry.io/collector/receiver v0.98.0
go.opentelemetry.io/otel/metric v1.25.0
go.opentelemetry.io/otel/trace v1.25.0
go.uber.org/goleak v1.3.0
)

require (
Expand Down

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

Loading