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 unit tests for the priority package #228

Merged

Conversation

shuheiktgw
Copy link
Contributor

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Added missing unit tests to the priority package (and fix a minor bug in it)

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

@k8s-ci-robot k8s-ci-robot added 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. labels Apr 21, 2022
@k8s-ci-robot
Copy link
Contributor

Welcome @shuheiktgw!

It looks like this is your first PR to kubernetes-sigs/kueue 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kueue has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 21, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @shuheiktgw. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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 the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 21, 2022
@@ -77,6 +77,7 @@ func getDefaultPriorityClass(ctx context.Context, client client.Client) (*schedu
// we pick the one with the lowest priority value.
var defaultPC *schedulingv1.PriorityClass
for _, pci := range pcs.Items {
pci := pci
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a pretty common mistake we sometimes see. You can test why it is a problem with the "priorityClass is unspecified and multiple global default exist" test case 🙂

Copy link
Member

Choose a reason for hiding this comment

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

I still can't understand this. Can you add more details or informations for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The article below seems to explain the problem in detail. In short, the variable pci is reused for each iteration, so referring to the pointer of pci ends up pointing to the last element of pcs.Items🙂

https://medium.com/swlh/use-pointer-of-for-range-loop-variable-in-go-3d3481f7ffc9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pci := pci may be confusing so I renamed it

@denkensk
Copy link
Member

/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 Apr 21, 2022
expectedValue: 40,
},
{
desc: "priorityClass is unspecified and multiple global default exist",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it possible that priorityClass is unspecified and no global default exists?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe. Then it will use constants.DefaultPriority

pkg/util/priority/priority_test.go Show resolved Hide resolved
Comment on lines 76 to 77
expectedName string
expectedValue int32
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expectedName string
expectedValue int32
expectedPriorityClassName string
expectedPriorityClassValue int32

}{
{
desc: "priority is specified",
workload: &kueue.Workload{
Copy link
Member

Choose a reason for hiding this comment

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

use MakeWorkload in wrappers.go

expectedValue: 40,
},
{
desc: "priorityClass is unspecified and multiple global default exist",
Copy link
Member

Choose a reason for hiding this comment

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

Maybe. Then it will use constants.DefaultPriority

@@ -77,6 +77,7 @@ func getDefaultPriorityClass(ctx context.Context, client client.Client) (*schedu
// we pick the one with the lowest priority value.
var defaultPC *schedulingv1.PriorityClass
for _, pci := range pcs.Items {
pci := pci
Copy link
Member

Choose a reason for hiding this comment

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

I still can't understand this. Can you add more details or informations for it?

Items: []schedulingv1.PriorityClass{},
},
priorityClassName: "test",
expectedErr: true,
Copy link
Member

Choose a reason for hiding this comment

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

Can you also check the error message here?

@shuheiktgw
Copy link
Contributor Author

@denkensk Fixed it! Would you review the PR again? 🙏

@denkensk
Copy link
Member

denkensk commented Apr 22, 2022

Can you squash your commits to one? LGTM overall

@shuheiktgw
Copy link
Contributor Author

@denkensk Thanks! Squashed 🙏

@shuheiktgw shuheiktgw mentioned this pull request Apr 23, 2022
@denkensk
Copy link
Member

/lgtm

Thanks. @shuheiktgw

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 23, 2022
@denkensk
Copy link
Member

ping @ahg-g for approve.

Copy link
Contributor

@ahg-g ahg-g left a comment

Choose a reason for hiding this comment

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

thanks, a couple of nits

name, value, err := GetPriorityFromPriorityClass(context.Background(), client, tt.priorityClassName)
if tt.expectedErr != "" {
if err == nil {
t.Fatalf("expected error but error is nil")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
t.Fatalf("expected error but error is nil")
t.Fatalf("expected an error")

Comment on lines 154 to 149
if !strings.Contains(err.Error(), tt.expectedErr) {
t.Fatalf("unexpecter error message: got: %s, expected: %s", err.Error(), tt.expectedErr)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if !strings.Contains(err.Error(), tt.expectedErr) {
t.Fatalf("unexpecter error message: got: %s, expected: %s", err.Error(), tt.expectedErr)
}
if diff := cmp.Diff(tt.expectedErr, err); diff != "" {
t.Errorf("unexpected error (-want,+got):\n%s", diff)
}

pkg/util/priority/priority_test.go Outdated Show resolved Hide resolved
@@ -77,9 +77,10 @@ func getDefaultPriorityClass(ctx context.Context, client client.Client) (*schedu
// we pick the one with the lowest priority value.
var defaultPC *schedulingv1.PriorityClass
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe keep a copy, instead of a pointer.

Also returning a copy instead of a pointer should be fine.

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 not sure if I understand it correctly but you meant like the one below?

	var defaultPC schedulingv1.PriorityClass
	var found bool
	for _, pci := range pcs.Items {
		if pci.GlobalDefault {
			if !found || defaultPC.Value > pci.Value {
				found = true
				defaultPC = pci
			}
		}
	}

	if !found {
		return nil, nil
	}
	
	return &defaultPC, nil

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, but also just return defaultPC, nil.

What you have is fine too though.

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 see! It currently checks if the default priority class is nil or not so returning schedulingv1.PriorityClass from the getDefaultPriorityClass may complicate the check a little. Let's stick with returning *schedulingv1.PriorityClass then 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alculquicondor I've updated it and squashed the commits. Would you review the PR again? 🙏

@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed lgtm "Looks good to me", 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 Apr 26, 2022
@@ -75,13 +75,20 @@ func getDefaultPriorityClass(ctx context.Context, client client.Client) (*schedu

// In case more than one global default priority class is added as a result of a race condition,
// we pick the one with the lowest priority value.
var defaultPC *schedulingv1.PriorityClass
var defaultPC schedulingv1.PriorityClass
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: if you are still returning a pointer, I think the initial implementation was simpler, as it does not need a boolean.

t.Fatalf("Failed adding scheduling scheme: %v", err)
}

tests := []struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, just to be consistent with other tests: use map[string]struct{}

Comment on lines 74 to 76
expectedPriorityClassName string
expectedPriorityClassValue int32
expectedErr string
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expectedPriorityClassName string
expectedPriorityClassValue int32
expectedErr string
wantPriorityClassName string
wantPriorityClassValue int32
wantErr string

@shuheiktgw
Copy link
Contributor Author

/reopen

@k8s-ci-robot k8s-ci-robot reopened this May 11, 2022
@k8s-ci-robot
Copy link
Contributor

@shuheiktgw: Reopened this PR.

In response to this:

/reopen

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 the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 11, 2022
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 11, 2022
@shuheiktgw
Copy link
Contributor Author

shuheiktgw commented May 11, 2022

@alculquicondor Sorry that I missed Prow somehow closed the PR! I've updated the PR again so would you review it? 🙇 (I'll squash the commits again once it's been approved)

Copy link
Contributor

@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.

Thanks,
please squash

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alculquicondor, shuheiktgw

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 May 11, 2022
@shuheiktgw
Copy link
Contributor Author

Thanks, I squashed the commits 👍

@alculquicondor
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 12, 2022
@k8s-ci-robot k8s-ci-robot merged commit 68ae767 into kubernetes-sigs:main May 12, 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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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

6 participants