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] Automatically generate goleak tests for packages #30483

Closed
crobert-1 opened this issue Jan 12, 2024 · 9 comments · Fixed by #32499
Closed

[mdatagen] Automatically generate goleak tests for packages #30483

crobert-1 opened this issue Jan 12, 2024 · 9 comments · Fixed by #32499
Labels
cmd/mdatagen mdatagen command enhancement New feature or request

Comments

@crobert-1
Copy link
Member

crobert-1 commented Jan 12, 2024

Component(s)

cmd/mdatagen

Is your feature request related to a problem? Please describe.

While working on #30438 a lot of identical code is being copied into many different places in the repository.

Describe the solution you'd like

As @dmitryax suggested here:

It would be great to generate this test along with #27849. If we need to skip it for some packages we can add skip_goleak: true with a comment to the tests section metadata.yaml

Configurable metadata options:

  1. There should be an option to skip the goleak tests if necessary. This would likely mean the package_test.go file is not generated, and even deleted if it currently exists. To my knowledge, there's no way to include TestMain with the goleak call and also skip the resulting goleak check.
  2. A configurable list of leaks to ignore. This would make use of the function goleak.IgnoreTopFunction. There are sometimes known issues that can't be addressed and irrelevant to the collector itself that can be safely ignored. PR [chore] Enable goleak for tests failing on opencensus-go #30457 has examples of this.
@crobert-1 crobert-1 added enhancement New feature or request needs triage New item requiring triage cmd/mdatagen mdatagen command labels Jan 12, 2024
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

Copy link
Contributor

Pinging code owners for cmd/mdatagen: @dmitryax. See Adding Labels via Comments if you do not have permissions to add labels yourself.

@crobert-1
Copy link
Member Author

Removing needs triage as this suggestion was made by the mdatagen code owner, and multiple maintainers/approvers agreed with the idea.

@crobert-1 crobert-1 removed the needs triage New item requiring triage label Jan 12, 2024
@crobert-1
Copy link
Member Author

Currently, the level of granularity between metadata.yaml files and goleak test packages is different. There's currently one metadata.yaml file for each component, but goleak is required for each golang package. An example is helpful to understand the difference.

The spanmetrics connector has a single top-level metadata.yaml file. However, for all tests to be covered by goleak, it requires ./package_test.go, internal/cache/package_test.go, and internal/metrics/package_test.go.

This means there will need to be a way to have multiple paths provided for generating goleak tests, each with its own configuration options, as proposed.

@crobert-1
Copy link
Member Author

Another thing to consider is what I pointed out here, the fact that some packages already have TestMain. I need to investigate how to get existing functionality to work with adding goleak for those cases.

@dmitryax
Copy link
Member

Currently, the level of granularity between metadata.yaml files and goleak test packages is different. There's currently one metadata.yaml file for each component, but goleak is required for each golang package. An example is helpful to understand the difference.

The spanmetrics connector has a single top-level metadata.yaml file. However, for all tests to be covered by goleak, it requires ./package_test.go, internal/cache/package_test.go, and internal/metrics/package_test.go.

This means there will need to be a way to have multiple paths provided for generating goleak tests, each with its own configuration options, as proposed.

As the first step, we should make sure that the test is generated per component/module. Then, we will see if it can be generated per module

dmitryax pushed a commit that referenced this issue Mar 12, 2024
…1689)

**Description:** 
The goal is for `goleak` to be required for every package, and also to
be [generated by
mdatagen](#30483).
Once this is generated by `mdatagen` we'll be able to remove this from
manual steps done by the user, but for now it would be good to state the
it's required in docs.

**Link to tracking Issue:** #30438
DougManton pushed a commit to DougManton/opentelemetry-collector-contrib that referenced this issue Mar 13, 2024
…en-telemetry#31689)

**Description:** 
The goal is for `goleak` to be required for every package, and also to
be [generated by
mdatagen](open-telemetry#30483).
Once this is generated by `mdatagen` we'll be able to remove this from
manual steps done by the user, but for now it would be good to state the
it's required in docs.

**Link to tracking Issue:** open-telemetry#30438
XinRanZhAWS pushed a commit to XinRanZhAWS/opentelemetry-collector-contrib that referenced this issue Mar 13, 2024
…en-telemetry#31689)

**Description:** 
The goal is for `goleak` to be required for every package, and also to
be [generated by
mdatagen](open-telemetry#30483).
Once this is generated by `mdatagen` we'll be able to remove this from
manual steps done by the user, but for now it would be good to state the
it's required in docs.

**Link to tracking Issue:** open-telemetry#30438
Copy link
Contributor

This issue has been inactive for 60 days. It will be closed in 60 days if there is no activity. To ping code owners by adding a component label, see Adding Labels via Comments, or if you are unsure of which component this issue relates to, please ping @open-telemetry/collector-contrib-triagers. If this issue is still relevant, please ping the code owners or leave a comment explaining why it is still relevant. Otherwise, please close it.

@dmitryax
Copy link
Member

dmitryax commented Apr 12, 2024

Another thing to consider is what I pointed out #30438 (comment) the fact that some packages already have TestMain. I need to investigate how to get existing functionality to work with adding goleak for those cases.

We can have another option in metadata like tests.goleak.addtional_test=true which would generate a function call like

func TestMain(m *testing.M) {
	goleak.VerifyTestMain(m)
	additionalTestMain(m)
}

end expect users to implement additionalTestMain in a separate file

@crobert-1
Copy link
Member Author

crobert-1 commented Apr 12, 2024

We can have another option in metadata like tests.goleak.addtional_test=true which would generate a function call like

That would be super helpful. There are a number of test suites that rely on an existing TestMain 👍

However, I believe the way it's written (with additionalTestMain) would just execute all of the tests twice. The first test run would have goleak checks enabled, but tests would possibly fail because the original TestMain's setup wasn't done. It would also leak resources as the original TestMain's teardown isn't run, leading to potentially flaky tests. The second test suite run, additionalTestMain, runs with the package's proper setup and teardown for tests, but without goleak checks enabled.

The options added here may need to be something like:

tests.goleak.addtional_test_setup=true
tests.goleak.addtional_test_teardown=true

And then:

func TestMain(m *testing.M) {
        additionalTestSetup()
	goleak.VerifyTestMain(m)
        additionalTestTeardown()
}

codeboten added a commit that referenced this issue Apr 22, 2024
This PR pulls in the latest mdatagen that auto-generates goleak package
tests. There are still some issues to sort out before being able to use
it, but it's mostly there.

I've found a few more modules where goleak can be enabled with no
additional tests so i've done so as part of this PR

Fixes #30483

---------

Signed-off-by: Alex Boten <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd/mdatagen mdatagen command enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants