-
Notifications
You must be signed in to change notification settings - Fork 40.1k
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
Refactor scheduler factory test #69412
Refactor scheduler factory test #69412
Conversation
/assign @misterikkit @bsalamat @liggitt |
@@ -26,6 +26,7 @@ import ( | |||
func (c *FakePods) Bind(binding *v1.Binding) error { | |||
action := core.CreateActionImpl{} | |||
action.Verb = "create" | |||
action.Namespace = binding.Namespace |
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 name needed as well?
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.
@liggitt The action is create
, so the name is not required in action and already in the binding
object.
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 overall change is fewer lines than I was anticipating! 😄
Anyway, I'd like us to avoid reactors when possible, as that is a pretty detailed interface. It lets us fake some pretty sophisticated interactions, but my hope is that most tests don't need that level of sophistication.
mux := http.NewServeMux() | ||
client := fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{*testPod}}) | ||
requestReceived := false | ||
client.PrependReactor("get", string(v1.ResourcePods), |
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 seems like new behavior in the test. What problem does it solve, and is this test flaky? (I notice the 10ms sleep that's probably there for this reason.)
If we're asserting very specific interactions with the k8s API, then using reactors might be the way to go. I've found that it's often sufficient to inject objects and check state through the Clientset API itself. For example, /~https://github.com/kubernetes/client-go/tree/master/examples/fake-client
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.
@misterikkit It's not a new behaviour, I just want to use reactor to validate that if the request is received or the request is expected, like what the original test does using ValidateRequest
. I found no other method to do the validation in a simple way.
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.
Ah, I see that now. What do you think about calling client.Actions()
and doing checks on that slice, rather than using the reactor interface? I think that code would be a little simpler for others to understand.
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.
@misterikkit client.Actions()
would be a better way, thanks.
client := fake.NewSimpleClientset(&v1.PodList{Items: []v1.Pod{*testPod}}) | ||
expectedBody := runtime.EncodeOrDie(schedulertesting.Test.Codec(), binding) | ||
requestReceived := false | ||
client.PrependReactor("create", string(v1.ResourcePods), func(action clienttesting.Action) (bool, runtime.Object, error) { |
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.
Do you know what the default reactor does with bindings? Does it do anything that can be observed by going in through the Clientset interface?
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.
@misterikkit The default reactor is to react to the action using a object tracker, and will store the object states. You're right, we could check the object using the Clientset interface here instead of the reactor.
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.
@misterikkit Updated, now it's simpler, PTAL.
722ff92
to
68df88a
Compare
|
||
bind, err := pod.GetBinding("foo") | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) |
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.
Errorf + return could be replaced with Fatalf, but since it matches existing code, I don't care if it gets cleaned up in this PR.
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.
Agree, will update soon.
|
||
pod := client.CoreV1().Pods(metav1.NamespaceDefault).(*fakeV1.FakePods) | ||
|
||
bind, err := pod.GetBinding("foo") |
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.
Just some side commentary, but the original design of this helper is assuming that the passed in binding object is on a pod named "foo". It makes the test fragile and harder to read. Not something to fix in this PR - I just had to point it out.
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.
Emm... it's really easy to make some misunderstanding.
38aa570
to
26ecb74
Compare
@misterikkit Updated, PTAL. |
/ok-to-test |
789b518
to
5172ff6
Compare
/retest |
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.
/lgtm
handler.ValidateRequest(t, schedulertesting.Test.ResourcePath(string(v1.ResourcePods), "bar", "foo"), "GET", nil) | ||
requestReceived := false | ||
actions := client.Actions() | ||
for _, a := range actions { |
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 for updating this. It is easier to read (to me) than the reactor stuff. I almost wonder if this test should assert len(actions)==1
since the test fails otherwise.
The client-go changes lgtm. /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.
/lgtm
Thanks, @tossmilestone!
/approve |
Looks like you need to do bazel update again. |
From a kubectl perspective: |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: apelisse, bsalamat, caesarxuchao, tossmilestone 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 |
Use `k8s.io/client-go/kubernetes/fake.Clientset` as the fake k8s client. Signed-off-by: He Xiaoxi <xxhe@alauda.io>
e7edc3a
to
a96a390
Compare
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.
/lgtm
What this PR does / why we need it:
Use
k8s.io/client-go/kubernetes/fake.Clientset
as the fake k8s client inpkg/scheduler/factory/factory_test.go
.Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #68963
Special notes for your reviewer:
@misterikkit
Release note: