[mdatagen] generate goleak package test (#9959)

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 <223565+codeboten@users.noreply.github.com>
This commit is contained in:
Alex Boten 2024-04-17 13:10:34 -07:00 committed by GitHub
parent 29240d97b6
commit f3305aa6b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 167 additions and 30 deletions

View File

@ -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: []

View File

@ -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"): {},

View File

@ -1,7 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package exportertest
package samplereceiver
import (
"testing"

View File

@ -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 {

View File

@ -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 {

View File

@ -23,6 +23,8 @@ func TestRunContents(t *testing.T) {
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantGoleakIgnore bool
wantGoleakSkip bool
wantErr bool
}{
{
@ -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) {
@ -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")
}
})
}
}

View File

@ -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

View File

@ -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 }}
}

View File

@ -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"

View File

@ -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

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package forwardconnector

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package debugexporter

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package loggingexporter

View File

@ -1,7 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package extensiontest
package nopexporter
import (
"testing"

View File

@ -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 (

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package otlpexporter

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package otlphttpexporter

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package ballastextension

View File

@ -0,0 +1,13 @@
// Code generated by mdatagen. DO NOT EDIT.
package memorylimiterextension
import (
"testing"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}

View File

@ -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
)

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package zpagesextension

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package batchprocessor

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package memorylimiterprocessor

View File

@ -0,0 +1,13 @@
// Code generated by mdatagen. DO NOT EDIT.
package nopreceiver
import (
"testing"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}

View File

@ -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 (

View File

@ -1,5 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Code generated by mdatagen. DO NOT EDIT.
package otlpreceiver