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

Add unit tests for wrapped.go #6873

Merged
merged 4 commits into from
Dec 22, 2021

Conversation

iholder101
Copy link
Contributor

What this PR does / why we need it:
This PR will increase our test value and test coverage and reach our goal of 73% coverage.

Release note:

NONE

@kubevirt-bot kubevirt-bot added release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. size/M labels Nov 28, 2021
@iholder101
Copy link
Contributor Author

/cc @stu-gott @dhiller

@iholder101
Copy link
Contributor Author

/retest

@iholder101
Copy link
Contributor Author

/retest

Copy link
Member

@EdDev EdDev left a comment

Choose a reason for hiding this comment

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

Adding tests is always a good thing, thank you for that.

I do have some issues with tests that use global variables and perform the check against unexported functions/methods.
Testable code requires a level of modularity that benefits tests but also future changes to the production code. Using global functions does not contribute to the code modularity, nor to the ability to maintain it during future refactoring.

Golang provides light interfaces to assist and make code both modular and testable.
I think we should try and use that and avoid function pointers that are replaced by tests. It is unsafe and hard to maintain.

The same goes with testing internal functions. These functions are expected to change as refactoring happens or functionality expands. But if tests depend on the internal implementation, they will break frequently and make it hard to safely change code which is not affecting the external API of the package.
If it does make sense to tests internal functions, it is probably a sign they need to appear in a separate package with a proper API and a small scoped tests to cover them.

  • I am aware that the current codebase is not agreeing with me, but it is worth raising this from time to time, trying to change the current norm.

@dhiller
Copy link
Contributor

dhiller commented Nov 29, 2021

Adding tests is always a good thing, thank you for that.

I do have some issues with tests that use global variables and perform the check against unexported functions/methods. Testable code requires a level of modularity that benefits tests but also future changes to the production code. Using global functions does not contribute to the code modularity, nor to the ability to maintain it during future refactoring.

Can you elaborate and be more concrete on this please? I can't follow.

Golang provides light interfaces to assist and make code both modular and testable. I think we should try and use that and avoid function pointers that are replaced by tests. It is unsafe and hard to maintain.

The same goes with testing internal functions. These functions are expected to change as refactoring happens or functionality expands. But if tests depend on the internal implementation, they will break frequently and make it hard to safely change code which is not affecting the external API of the package. If it does make sense to tests internal functions, it is probably a sign they need to appear in a separate package with a proper API and a small scoped tests to cover them.

So you are saying that we shouldn't write test for code that is expected to change? I think the opposite is true? In general a test would verify that the refactoring didn't break anything, as that is exactly the definition of refactoring, so this would indicate that the tests are bad? Can you elaborate?

* I am aware that the current codebase is not agreeing with me, but it is worth raising this from time to time, trying to change the current norm.

@dhiller
Copy link
Contributor

dhiller commented Nov 29, 2021

In general I think that breaking up isolation to make code testable is a sacrifice well worth to be made. While I agree that global functions and variables are better avoided in general, when this is done for testing, I'd say this is ok.

Copy link
Contributor

@dhiller dhiller left a comment

Choose a reason for hiding this comment

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

/approve

/hold waiting for the discussion to settle.

@kubevirt-bot kubevirt-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 29, 2021
@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Nov 29, 2021
@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dhiller

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 29, 2021
@EdDev
Copy link
Member

EdDev commented Nov 30, 2021

Testable code requires a level of modularity that benefits tests but also future changes to the production code. Using global functions does not contribute to the code modularity, nor to the ability to maintain it during future refactoring.

Can you elaborate and be more concrete on this please? I can't follow.

Using a global variable to hold a function, just because you need to replace it in a test is a hack. It does not make the production code modular or serve it in any other way, it actually makes it harder to understand, harder to debug, fragile in testing because changes may leak between tests and even has performance penalty (not that we care much in this case, but still).
It also has dependencies on how tests are ran, e.g. if tests are executed in parallel using threads or goroutines, it will cause a mess.

The goal is to write testable code that not only serves tests, but serves the production code in making it modular, easy to change and readable. The test job (as I see it) is also to show how to use the portion of code it tests, it is after all another user of it.

The same goes with testing internal functions. These functions are expected to change as refactoring happens or functionality expands. But if tests depend on the internal implementation, they will break frequently and make it hard to safely change code which is not affecting the external API of the package. If it does make sense to tests internal functions, it is probably a sign they need to appear in a separate package with a proper API and a small scoped tests to cover them.

So you are saying that we shouldn't write test for code that is expected to change? I think the opposite is true? In general a test would verify that the refactoring didn't break anything, as that is exactly the definition of refactoring, so this would indicate that the tests are bad? Can you elaborate?

It is expected that all code paths are covered through the package API, leaving you room to change the internal details without touching the tests. If you trigger an internal function/method directly, you most likely will break the test when you refactor and will not be able to trust the existing tests to protect you.
If you cannot cover a path through the API (the exported package entities), it means the code is not testable and it requires some work to make it so.

I am experiencing this on every line of code I change these days, which is both painful and risky.

/hold waiting for the discussion to settle

By approving, you have made your decision already. I have only commented and not voted intentionally as I know this is the existing standard in this project. I have no intention in blocking contributions in areas I do not maintain, I just noticed the change when the bot sent me a review request and felt the need to point out the issues I see with this.

No need to hold it because of me.

@kubevirt-bot kubevirt-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 2, 2021
@kubevirt-bot kubevirt-bot removed lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Dec 6, 2021
@iholder101
Copy link
Contributor Author

/retest

1 similar comment
@iholder101
Copy link
Contributor Author

/retest

@iholder101
Copy link
Contributor Author

/retest
@dhiller ping

@iholder101
Copy link
Contributor Author

/retest

Copy link
Contributor

@dhiller dhiller left a comment

Choose a reason for hiding this comment

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

/unhold

@kubevirt-bot kubevirt-bot added lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Dec 14, 2021
Signed-off-by: Itamar Holder <iholder@redhat.com>
Signed-off-by: Itamar Holder <iholder@redhat.com>
Signed-off-by: Itamar Holder <iholder@redhat.com>
Signed-off-by: Itamar Holder <iholder@redhat.com>
@kubevirt-bot kubevirt-bot removed lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Dec 16, 2021
@iholder101
Copy link
Contributor Author

@dhiller rebased

@iholder101
Copy link
Contributor Author

/retest

@Barakmor1
Copy link
Member

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 20, 2021
@iholder101
Copy link
Contributor Author

@dhiller seems like pull-kubevirt-e2e-k8s-1.20-cgroupsv2 is stuck?

@dhiller
Copy link
Contributor

dhiller commented Dec 21, 2021

/test pull-kubevirt-e2e-k8s-1.20-cgroupsv2

@kubevirt-commenter-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs.
Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@iholder101
Copy link
Contributor Author

/test pull-kubevirt-e2e-k8s-1.20-cgroupsv2

@dhiller
Copy link
Contributor

dhiller commented Dec 22, 2021

/hold

Disabling retest-bot due to high cluster load.

@kubevirt-bot kubevirt-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 22, 2021
@dhiller
Copy link
Contributor

dhiller commented Dec 22, 2021

/unhold

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 22, 2021
@kubevirt-bot kubevirt-bot merged commit 733503d into kubevirt:main Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants