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
test: add unit tests for the priority package #228
Conversation
Welcome @shuheiktgw! |
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 Once the patch is verified, the new status will be reflected by the 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. |
pkg/util/priority/priority.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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 🙂
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
/ok-to-test |
pkg/util/priority/priority_test.go
Outdated
expectedValue: 40, | ||
}, | ||
{ | ||
desc: "priorityClass is unspecified and multiple global default exist", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Outdated
expectedName string | ||
expectedValue int32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectedName string | |
expectedValue int32 | |
expectedPriorityClassName string | |
expectedPriorityClassValue int32 |
pkg/util/priority/priority_test.go
Outdated
}{ | ||
{ | ||
desc: "priority is specified", | ||
workload: &kueue.Workload{ |
There was a problem hiding this comment.
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
pkg/util/priority/priority_test.go
Outdated
expectedValue: 40, | ||
}, | ||
{ | ||
desc: "priorityClass is unspecified and multiple global default exist", |
There was a problem hiding this comment.
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.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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?
pkg/util/priority/priority_test.go
Outdated
Items: []schedulingv1.PriorityClass{}, | ||
}, | ||
priorityClassName: "test", | ||
expectedErr: true, |
There was a problem hiding this comment.
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?
@denkensk Fixed it! Would you review the PR again? 🙏 |
Can you squash your commits to one? LGTM overall |
396660d
to
0acb185
Compare
@denkensk Thanks! Squashed 🙏 |
/lgtm Thanks. @shuheiktgw |
ping @ahg-g for approve. |
There was a problem hiding this 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
pkg/util/priority/priority_test.go
Outdated
name, value, err := GetPriorityFromPriorityClass(context.Background(), client, tt.priorityClassName) | ||
if tt.expectedErr != "" { | ||
if err == nil { | ||
t.Fatalf("expected error but error is nil") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Fatalf("expected error but error is nil") | |
t.Fatalf("expected an error") |
pkg/util/priority/priority_test.go
Outdated
if !strings.Contains(err.Error(), tt.expectedErr) { | ||
t.Fatalf("unexpecter error message: got: %s, expected: %s", err.Error(), tt.expectedErr) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) | |
} |
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
There was a problem hiding this comment.
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? 🙏
3996013
to
46e50e7
Compare
pkg/util/priority/priority.go
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
pkg/util/priority/priority_test.go
Outdated
t.Fatalf("Failed adding scheduling scheme: %v", err) | ||
} | ||
|
||
tests := []struct { |
There was a problem hiding this comment.
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{}
pkg/util/priority/priority_test.go
Outdated
expectedPriorityClassName string | ||
expectedPriorityClassValue int32 | ||
expectedErr string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectedPriorityClassName string | |
expectedPriorityClassValue int32 | |
expectedErr string | |
wantPriorityClassName string | |
wantPriorityClassValue int32 | |
wantErr string |
/reopen |
@shuheiktgw: Reopened this PR. In response to this:
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. |
46e50e7
to
af08e93
Compare
@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) |
There was a problem hiding this 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
[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 |
c095746
to
41de246
Compare
Thanks, I squashed the commits 👍 |
/lgtm |
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: