Skip to content

Commit

Permalink
[mdatagen] generate goleak package test (open-telemetry#9959)
Browse files Browse the repository at this point in the history
This automates the generation of package_test.go for any component that
uses mdatagen.

The following configuration can be used to skip or ignore certain funcs:

```yaml
tests:
  goleak:
    skip: true

tests:
  goleak:
    ignore:
      top:
        - "go.opencensus.io/stats/view.(*worker).start"
```

---------

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten authored Apr 17, 2024
1 parent 29240d9 commit f3305aa
Show file tree
Hide file tree
Showing 26 changed files with 167 additions and 30 deletions.
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"),
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.

0 comments on commit f3305aa

Please sign in to comment.