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

Refactor scheduler factory test #69412

Merged

Conversation

tossmilestone
Copy link
Member

What this PR does / why we need it:
Use k8s.io/client-go/kubernetes/fake.Clientset as the fake k8s client in pkg/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:

Refactor factory_test.go to use a fake k8s client.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 4, 2018
@k8s-ci-robot k8s-ci-robot added needs-kind Indicates a PR lacks a `kind/foo` label and requires one. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 4, 2018
@tossmilestone
Copy link
Member Author

/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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link

@misterikkit misterikkit left a 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),

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

Copy link
Member Author

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.

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.

Copy link
Member Author

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) {

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?

Copy link
Member Author

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.

Copy link
Member Author

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.


bind, err := pod.GetBinding("foo")
if err != nil {
t.Errorf("Unexpected error: %v", err)

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.

Copy link
Member Author

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")

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.

Copy link
Member Author

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.

@tossmilestone tossmilestone force-pushed the scheduler-factory-test branch 3 times, most recently from 38aa570 to 26ecb74 Compare October 6, 2018 02:22
@tossmilestone
Copy link
Member Author

@misterikkit Updated, PTAL.

@k82cn
Copy link
Member

k82cn commented Oct 7, 2018

/ok-to-test
/kind cleanup

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 7, 2018
@tossmilestone tossmilestone force-pushed the scheduler-factory-test branch 2 times, most recently from 789b518 to 5172ff6 Compare October 7, 2018 02:16
@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. labels Oct 7, 2018
@tossmilestone
Copy link
Member Author

/retest

Copy link

@misterikkit misterikkit left a 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 {

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.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 8, 2018
@caesarxuchao
Copy link
Member

The client-go changes lgtm.

/approve

Copy link
Member

@bsalamat bsalamat left a comment

Choose a reason for hiding this comment

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

/lgtm

Thanks, @tossmilestone!

@bsalamat
Copy link
Member

bsalamat commented Oct 9, 2018

/approve

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 11, 2018
@misterikkit
Copy link

Looks like you need to do bazel update again.

@apelisse
Copy link
Member

From a kubectl perspective:
/approve

@k8s-ci-robot
Copy link
Contributor

[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 /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 Oct 11, 2018
Use `k8s.io/client-go/kubernetes/fake.Clientset` as the fake k8s client.

Signed-off-by: He Xiaoxi <xxhe@alauda.io>
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 15, 2018
Copy link

@misterikkit misterikkit left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot merged commit 060218a into kubernetes:master Oct 15, 2018
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. area/kubectl 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. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/cli Categorizes an issue or PR as relevant to SIG CLI. 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.

[scheduler cleanup phase 1]: Refactor factory_test.go to use a fake k8s client
8 participants