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 test coverage for pkg/util/node and Remove duplicate testcases for func TestGetNodeHostIPs #110825

Merged
merged 3 commits into from
Jul 11, 2022

Conversation

zhoumingcheng
Copy link
Contributor

@zhoumingcheng zhoumingcheng commented Jun 28, 2022

Signed-off-by: zhoumingcheng zhoumingcheng@beyondcent.com

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Improve test coverage

Run test coverage command:

go test -cover ./pkg/util/node/

Befor:

ok      k8s.io/kubernetes/pkg/util/node/     0.019s  coverage: 60.3% of statements

After:

ok      k8s.io/kubernetes/pkg/util/node/   0.022s  coverage: 67.2% of statements

Remove duplicate testcases for func TestGetNodeHostIPs

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Thank you!

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


Signed-off-by: zhoumingcheng <zhoumingcheng@beyondcent.com>
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 28, 2022
@k8s-ci-robot
Copy link
Contributor

@zhoumingcheng: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot
Copy link
Contributor

Hi @zhoumingcheng. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 28, 2022
pkg/util/node/node_test.go Outdated Show resolved Hide resolved
pkg/util/node/node_test.go Outdated Show resolved Hide resolved
pkg/util/node/node_test.go Outdated Show resolved Hide resolved
@aojea
Copy link
Member

aojea commented Jun 28, 2022

/ok-to-test
/sig-node

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 28, 2022
Remove duplicate testcases for func TestGetNodeHostIPs

Signed-off-by: zhoumingcheng <zhoumingcheng@beyondcent.com>
@zhoumingcheng
Copy link
Contributor Author

/assign @thockin
please take a look, thanks!

t.Error("create node error")
}
}
result := GetNodeIP(kubeClientSet, test.nodeName)
Copy link
Member

Choose a reason for hiding this comment

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

Tell me if I am misunderstanding:

Testing through GetNodeIP() seems to give coverage of the wait.ExponentialBackoff() logic but ultimately calls GetNodeHostIP(). This test doesn't actually exercise any of the retry logic, so you're moving test cases around to score coverage "points" but it's bogus coverage.

I think I'd rather see all the test cases in the GetNodeHostIPs() (plural) test, with a subset of those in a test for GetNodeHostIP() (singular). This function (GetNodeIP()) seems to only be called from 2 places and I think it would be better to move this function to be internal to both. "A little copy is better than a little dep".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I did notice that GetNodeIP() ends up calling GetNodeHostIP(). My GetNodeIP() test case and TestGetNodeHostIPs() both cover GetNodeHostIP(). I wonder if it's possible to cover wait.ExponentialBackoff() over GetNodeHostIP() directly via TestGetNodeIP(). So I removed TestGetNodeHostIPs. Not sure if this is possible?

Copy link
Member

Choose a reason for hiding this comment

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

I think this moves in the wrong direction. Tests should focus on the lowest level possible.

I'd set this up as a comprehensive test of GetNodeHostIPs() - since that seems to be where the real work is happening.

Testing GetNodeIP() (if done at all) should focus on the logic that is unique to that function, which is the backoff (which is mostly boilerplate and does not really belong in this utils lib in the first place).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm sorry, with all your replies to me, I'm a little bit confused about what you mean here. Do I wish I could move the tests for GetNodeIP() to the called function directory, or should the tests be concentrated at the lowest level possible. If it is the second, I will remove the test cases that are repeated with TestGetNodeHostIPs() in TestGetNodeIP(). Then TestGetNodeIP() will only focus on testing GetNodeIP(). Not sure if I misunderstood what you meant if I changed it like this. Or should I move the test of GetNodeIP() to the corresponding test function of the called function, such as pkg/util/node/node_test.go Test_detectNodeIP(). Sorry to bother you.

@thockin
Copy link
Member

thockin commented Jul 7, 2022 via email

Signed-off-by: zhoumingcheng <zhoumingcheng@beyondcent.com>
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 7, 2022
@zhoumingcheng zhoumingcheng changed the title add unit test coverage for pkg/util/node add unit test coverage for pkg/util/node and Remove duplicate testcases for func TestGetNodeHostIPs Jul 7, 2022
@zhoumingcheng
Copy link
Contributor Author

/retest

@zhoumingcheng
Copy link
Contributor Author

Don't bother to test GetNodeIP() until the lower level functions have complete, full coverage.

On Wed, Jul 6, 2022, 7:03 PM zhoumingcheng @.> wrote: @.* commented on this pull request. ------------------------------ In pkg/util/node/node_test.go <#110825 (comment)> : > + {Type: v1.NodeExternalIP, Address: "4.3.2.1"}, + {Type: v1.NodeExternalIP, Address: "4.3.2.2"}, + }}, + }, + expect: netutils.ParseIPSloppy("a🅱️:c:d"), + }, + } + for _, test := range testCases { + t.Run(test.name, func(t testing.T) { + kubeClientSet := kubeclientfake.NewSimpleClientset() + if test.node != nil { + if _, err := kubeClientSet.CoreV1().Nodes().Create(context.Background(), test.node, metav1.CreateOptions{}); err != nil { + t.Error("create node error") + } + } + result := GetNodeIP(kubeClientSet, test.nodeName) I'm sorry, with all your replies to me, I'm a little bit confused about what you mean here. Do I wish I could move the tests for GetNodeIP() to the called function directory, or should the tests be concentrated at the lowest level possible. If it is the second, I will remove the test cases that are repeated with TestGetNodeHostIPs() in TestGetNodeIP(). Then TestGetNodeIP() will only focus on testing GetNodeIP(). Not sure if I misunderstood what you meant if I changed it like this. Or should I move the test of GetNodeIP() to the corresponding test function of the called function, such as pkg/util/node/node_test.go Test_detectNodeIP(). Sorry to bother you. — Reply to this email directly, view it on GitHub <#110825 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKWAVASYAJFSJKLSZJI35LVSY3HFANCNFSM52BHMHRQ . You are receiving this because you were assigned.Message ID: @.**>

Thanks for your suggestion, I have removed TestGetNodeIP() and removed the duplicated test cases in TestGetNodeHostIPs().

@thockin
Copy link
Member

thockin commented Jul 8, 2022

Thanks!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 8, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: thockin, zhoumingcheng

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 8, 2022
@pacoxu
Copy link
Member

pacoxu commented Jul 11, 2022

/sig node

@k8s-ci-robot k8s-ci-robot added sig/node Categorizes an issue or PR as relevant to SIG Node. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jul 11, 2022
@zhoumingcheng
Copy link
Contributor Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit a3f7776 into kubernetes:master Jul 11, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.25 milestone Jul 11, 2022
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/node Categorizes an issue or PR as relevant to SIG Node. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants