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

test: add code coverage support #550

Merged
merged 1 commit into from
Nov 17, 2023
Merged

Conversation

rchincha
Copy link
Contributor

@rchincha rchincha commented Nov 14, 2023

With golang 1.20.x, we can build a binary with coverage support.

Cover

Go 1.20 supports collecting code coverage profiles for programs
(applications and integration tests), as opposed to just unit tests.

To collect coverage data for a program, build it with go build's -cover
flag, then run the resulting binary with the environment variable
GOCOVERDIR set to an output directory for coverage profiles. See the
'coverage for integration tests' landing page for more on how to get
started. For details on the design and implementation, see the
proposal.

What type of PR is this?

Which issue does this PR fix:

What does this PR do / Why do we need it:

If an issue # is not available please add repro steps and logs showing the issue:

Testing done on this change:

Automation added to e2e:

Will this break upgrades or downgrades?

Does this PR introduce any user-facing change?:


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@rchincha rchincha force-pushed the cov-new branch 2 times, most recently from c4ac426 to e106109 Compare November 14, 2023 22:22
Copy link

codecov bot commented Nov 14, 2023

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (8d233ed) 13.01% compared to head (4177c91) 54.33%.
Report is 3 commits behind head on main.

❗ Current head 4177c91 differs from pull request most recent head 37c0cd3. Consider uploading reports for the commit 37c0cd3 to get more accurate results

Files Patch % Lines
pkg/stacker/build.go 53.84% 4 Missing and 2 partials ⚠️
pkg/test/cover.go 88.88% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #550       +/-   ##
===========================================
+ Coverage   13.01%   54.33%   +41.32%     
===========================================
  Files          40       64       +24     
  Lines        6003     7479     +1476     
===========================================
+ Hits          781     4064     +3283     
+ Misses       5094     2724     -2370     
- Partials      128      691      +563     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rchincha rchincha force-pushed the cov-new branch 5 times, most recently from b029e55 to 81de581 Compare November 15, 2023 02:41
Makefile Show resolved Hide resolved
Copy link
Contributor

@smoser smoser left a comment

Choose a reason for hiding this comment

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

this looks really good, thank you. It will be really great to get "coverage credit" for the integration tests.

two overall comments:

  1. Would it be simpler to just build both static-stacker and stacker-cov in the same stacker.yaml invocation? it messes up make dependencies a bit, but you can kind of just say "oh well" as stacker-cov always gets built when static-stacker is built.
  2. Lets put the gocov into /stacker/working or something. https://github.com/project-stacker/stacker/pull/543/files does a reasonable job of putting all our work under a single point inside.

unless there is some reason to not run integration adn unit tests with coverage, I really think we should just do that all the time.

@rchincha rchincha force-pushed the cov-new branch 12 times, most recently from 0cbfd6c to 4177c91 Compare November 15, 2023 22:32
@rchincha rchincha requested a review from smoser November 16, 2023 17:25
@rchincha
Copy link
Contributor Author

Also, doesn't make sense to trust integration testing on an instrumented non-production binary - these should be kept separate.

Copy link
Contributor

@smoser smoser left a comment

Choose a reason for hiding this comment

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

I have lots of comments on this, but none of them compare to a "+40% coverage" change.

I'll just put them here though for my later reference.

  1. I don't think a separate "coverage" workflow is useful. c-i should always run coverage. Im' not sure what caching we get between the 'coverage' and 'build' workflow, which means just doing lots of extra things. While they do seem to happen in parallel, that must multiplies the transient failure rate by 2.
  2. i think there are cleanups to be had in the makefile or stacker file changes. we can do a better job there.

again, none of this comes near the huge win of getting coverage on integration tests. thank you.

@@ -33,6 +33,12 @@ func MaybeRunInNamespace(config types.StackerConfig, userCmd []string) error {
euid := os.Geteuid()
env = append(env, fmt.Sprintf("%s=%d", envName, euid))

/*
Copy link
Contributor

Choose a reason for hiding this comment

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

we should drop this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

    With golang 1.20.x, we can build a binary with coverage support.

    "
    Cover

    Go 1.20 supports collecting code coverage profiles for programs
    (applications and integration tests), as opposed to just unit tests.

    To collect coverage data for a program, build it with go build's
-cover
    flag, then run the resulting binary with the environment variable
    GOCOVERDIR set to an output directory for coverage profiles. See the
    'coverage for integration tests' landing page for more on how to get
    started. For details on the design and implementation, see the
proposal.
    "

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
@rchincha
Copy link
Contributor Author

rchincha commented Nov 17, 2023

I don't think a separate "coverage" workflow is useful. c-i should always run coverage.

^ it is separate because binaries being built and shipped are separate (unfortunately) - we want the binary from one workflow and coverage from the other. But both must be independently validated for different reasons.

@smoser
Copy link
Contributor

smoser commented Nov 17, 2023

I don't think a separate "coverage" workflow is useful. c-i should always run coverage.

^ it is separate because binaries being built and shipped are separate (unfortunately) - we want the binary from one workflow and coverage from the other.

Just build them both from the same run. A 'make' should build both. One is named 'stacker-coverage' one is named 'stacker'.

I can do a PR with my suggested cleanups at some point.

@rchincha rchincha merged commit a34ebfa into project-stacker:main Nov 17, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants