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

scheduler/internal: Improving cache and heap test coverage #114273

Merged

Conversation

TommyStarK
Copy link
Contributor

Signed-off-by: TommyStarK thomasmilox@gmail.com

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

If applied this commit will increase the unit tests code coverage of pkg/scheduler/internal/cache and pkg/scheduler/internal/heap.

  • Initial unit tests code coverage

Screenshot 2022-12-03 at 15 16 26

  • After changes

Screenshot 2022-12-03 at 15 18 47

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

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


@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. labels Dec 3, 2022
@k8s-ci-robot
Copy link
Contributor

Please note that we're already in Test Freeze for the release-1.26 branch. This means every merged PR will be automatically fast-forwarded via the periodic ci-fast-forward job to the release branch of the upcoming v1.26.0 release.

Fast forwards are scheduled to happen every 6 hours, whereas the most recent run was: Sat Dec 3 09:45:01 UTC 2022.

@k8s-ci-robot k8s-ci-robot added 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 Dec 3, 2022
@k8s-ci-robot
Copy link
Contributor

@TommyStarK: 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 @TommyStarK. 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 Dec 3, 2022
@k8s-ci-robot k8s-ci-robot added sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Dec 3, 2022
@TommyStarK
Copy link
Contributor Author

Hello @chendave , @kerthcet

I know guys you are super busy and this PR is far from being a priority. As a very new aspiring contributor I am still trying to become familiar with the code and to start helping on small pieces. Hope you don't mind.

Any feedback will be greatly appreciated 😅.

Best regards,

@kerthcet
Copy link
Member

kerthcet commented Dec 5, 2022

Thanks @TommyStarK
/ok-to-test

@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 Dec 5, 2022
Copy link
Member

@chendave chendave left a comment

Choose a reason for hiding this comment

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

Nice improvement on the test coverage!

roughly, this PR looks to me!

})
}

// should trigger an error log as there is no node info for this given name
Copy link
Member

Choose a reason for hiding this comment

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

could this be verified? or else what's the point to leave these in the testcases?

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 don't think so. You are right there is no point to keep that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved in 1c12a991112

@@ -1854,6 +1860,106 @@ func TestSchedulerCache_updateNodeInfoSnapshotList(t *testing.T) {
}
}

func TestSchedulerCache_ErrorCases(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, you can test this in the related function instead of pull the false cases out and test it here, e.g "forget unknown pod" can go to "TestForgetPod". It might be better to org these testcases, but I am fine with this as well.

Copy link
Contributor Author

@TommyStarK TommyStarK Dec 5, 2022

Choose a reason for hiding this comment

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

It was my first intent but once my code added I felt like messing up with the existing scenarii so I change my mind and gathered all false cases into this function. I'd rather keep it this way but if you don't feel comfortable with that please tell me and I'll go back with the 1st approach :)

Copy link
Member

Choose a reason for hiding this comment

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

+1, this is hard to maintain in the future. I didn't see any precedents in kubernetes. Let's try to make the tests pure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing. I will come back to move those tests to their related function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chendave, @kerthcet, I tried to keep tests as pure as possible and avoid messing too much with their clarity. This is what I ended with 807b29ad6e9120cbca49ae864b74a4f8ae003f36.

Below the new unit tests code coverage for the pkg/scheduler/internal/cache.
Screenshot 2022-12-05 at 16 44 08

All pipelines have passed, remaining at your disposal if you have any remark :)

Best regards,

@@ -97,12 +141,17 @@ func TestHeap_Add(t *testing.T) {
// TestHeap_Delete tests Heap.Delete and ensures that heap invariant is
// preserved after deleting items.
func TestHeap_Delete(t *testing.T) {
h := New(testHeapObjectKeyFunc, compareInts)
h := NewWithRecorder(testHeapObjectKeyFunc, compareInts, new(testMetricRecorder)) // cover conditions on metric recorder
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to add some statements to cover the metric inc/dec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved in 1c12a991112

@TommyStarK TommyStarK requested review from chendave and kerthcet and removed request for kerthcet and chendave December 6, 2022 11:52
@chendave
Copy link
Member

chendave commented Dec 7, 2022

this looks good to me now, pls remember to squash or org your commits properly.

/lgtm

/assign @Huang-Wei (per the suggestion from bot)

@TommyStarK
Copy link
Contributor Author

@kerthcet I applied changes to address your latest comments. Hope it matches to your expectations :)
ps: Job pull-kubernetes-e2e-gce-100-performance failed tho. I reran it but it seems to be out of the scope of my changes

Remaining at your disposal.

Best,

@@ -158,6 +164,14 @@ func (h *Heap) Update(obj interface{}) error {
return h.Add(obj)
}

// Cleanup removes all items.
func (h *Heap) Cleanup() {
Copy link
Member

Choose a reason for hiding this comment

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

Why are you adding this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I misunderstood the comment of the last review. Removed.

Copy link
Member

Choose a reason for hiding this comment

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

It's called by UnschedulablePods. Just remove this function if it's not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. I removed it. sorry

@TommyStarK TommyStarK force-pushed the unit-tests/pkg-scheduler-internal branch from 59bc26f to 5db1983 Compare December 12, 2022 14:29
@TommyStarK
Copy link
Contributor Author

/retest-required

if info == nil {
t.Error("node infos should not be nil")
}
if test.expectedNodesInfos[i] != info.String() {
Copy link
Member

Choose a reason for hiding this comment

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

/approve cancel
don't compare strings.

Compare the objects and use cmp.Diff to be able to tell the differences.

@alculquicondor
Copy link
Member

/cancel-approve

@alculquicondor
Copy link
Member

Once comments are addressed, I'll wait for another pass from @kerthcet

@ahg-g
Copy link
Member

ahg-g commented Dec 12, 2022

General question, why are we pushing back on testify? it seems more natural than cmp in some places?

@TommyStarK TommyStarK force-pushed the unit-tests/pkg-scheduler-internal branch from 5db1983 to a1a4b2b Compare December 12, 2022 18:18
@TommyStarK
Copy link
Contributor Author

@alculquicondor I updated my PR to address your comments. Had issues using cmp.Diff on NodeInfo due to the underlying unexported fields. Hope it matches your expectations

@alculquicondor
Copy link
Member

General question, why are we pushing back on testify?

Mostly for consistency and because errors.Is is a better practice than reflect.DeepEqual (the behavior that testify has) when checking errors.

@ahg-g
Copy link
Member

ahg-g commented Dec 12, 2022

General question, why are we pushing back on testify?

Mostly for consistency and because errors.Is is a better practice than reflect.DeepEqual (the behavior that testify has) when checking errors.

I am not sure if we want to be that strict, I liked how simple the code was with testify.

@ahg-g
Copy link
Member

ahg-g commented Dec 12, 2022

General question, why are we pushing back on testify?

Mostly for consistency and because errors.Is is a better practice than reflect.DeepEqual (the behavior that testify has) when checking errors.

I am not sure if we want to be that strict, I liked how simple the code was with testify.

As discussed offline with Aldo, assertions are not generally preferred, which I agree with. My concern with cmp is that it sometimes bloats the test code (comparing to the exact same error); but if we can use cmp with options to avoid that, then that is great.

@TommyStarK
Copy link
Contributor Author

/retest-required

Copy link
Member

@alculquicondor alculquicondor left a comment

Choose a reason for hiding this comment

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

/approve
Thanks
I will leave the lgtm to @kerthcet

nodes: []*v1.Node{
{
ObjectMeta: metav1.ObjectMeta{Name: "node-0"},
Status: v1.NodeStatus{},
Copy link
Member

Choose a reason for hiding this comment

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

this line is unnecessary, same below

Comment on lines 276 to 278
{
Pod: podWithPort,
},
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
{
Pod: podWithPort,
},
{ Pod: podWithPort },

}
}

if !sets.String.Equal(test.expectedUsedPVCSet, snapshot.usedPVCSet) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: cmp.Diff can make it easier to spot the differences.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahg-g, alculquicondor, TommyStarK

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

}
}

if !sets.String.Equal(test.expectedUsedPVCSet, snapshot.usedPVCSet) {
Copy link
Member

Choose a reason for hiding this comment

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

you can use cmp here as well


for i, node := range test.nodes {
info, err := snapshot.Get(node.Name)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Every err != nil is now replaced with three lines, similar with info == nil below. Do we have a package to do those checks in a single line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this is the case please tell me and I will update the PR

Copy link
Member

Choose a reason for hiding this comment

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

Also not sure about this package. LGTM it's easy to read.

Signed-off-by: TommyStarK <thomasmilox@gmail.com>
@TommyStarK TommyStarK force-pushed the unit-tests/pkg-scheduler-internal branch from a1a4b2b to 94a29ef Compare December 12, 2022 19:09
@TommyStarK
Copy link
Contributor Author

Thank you very much @alculquicondor @ahg-g for your time and review. I updated the code according your suggestions.
Best,

@kerthcet
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 13, 2022
@k8s-ci-robot k8s-ci-robot merged commit 6228b91 into kubernetes:master Dec 13, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.26 milestone Dec 13, 2022
@TommyStarK TommyStarK deleted the unit-tests/pkg-scheduler-internal branch December 13, 2022 10:04
@liggitt liggitt modified the milestones: v1.26, v1.27 Dec 13, 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/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants