-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Add the possibility to set return values for the FakeDiscovery implementation #51130
Conversation
@@ -67,7 +80,11 @@ func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResou | |||
} | |||
|
|||
func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) { | |||
return nil, nil | |||
return c.serverGroups, 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.
This appears to be building the layer the wrong location. It seems like you want a simple skin on the *testing.Fake
which setups up the "proper" handling for the various Invokes
calls by the PrependReactor
methods. That way you can use the normal check of Actions
if you need to.
I'm fine with making it easier to set those, but I'd like to see these calls making their proper remote calls. That may require refactoring the DiscoveryClient
itself to separate the accessors from the helpers, but I think that is more productive than doing it like this.
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.
/assign @caesarxuchao |
@caesarxuchao @sttts @deads2k Current proposal: /*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package foo
import (
"testing"
fakediscovery "k8s.io/client-go/discovery/fake"
fakeclientset "k8s.io/client-go/kubernetes/fake"
apimachineryversion "k8s.io/apimachinery/pkg/version"
)
func TestSettingFakeServerVersion(t *testing.T) {
client := fakeclientset.NewSimpleClientset()
fakeDiscovery, ok := client.Discovery().(*fakediscovery.FakeDiscovery)
if !ok {
t.Fatalf("couldn't convert")
}
testGitCommit := "v2.0.0"
fakeDiscovery.FakedServerVersion = &apimachineryversion.Info{
GitCommit: testGitCommit,
}
sv, err := client.Discovery().ServerVersion()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if sv.GitCommit != testGitCommit {
t.Fatalf("unexpected gitcommit: %q, %v", sv.GitCommit, *sv)
}
} Changes: diff --git a/staging/src/k8s.io/client-go/discovery/fake/discovery.go b/staging/src/k8s.io/client-go/discovery/fake/discovery.go
index 02e77cfe71..aae63da3e3 100644
--- a/staging/src/k8s.io/client-go/discovery/fake/discovery.go
+++ b/staging/src/k8s.io/client-go/discovery/fake/discovery.go
@@ -33,6 +33,7 @@ import (
type FakeDiscovery struct {
*testing.Fake
+ FakedServerVersion *version.Info
}
func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
@@ -76,6 +77,11 @@ func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
action.Resource = schema.GroupVersionResource{Resource: "version"}
c.Invokes(action, nil)
+
+ if c.FakedServerVersion != nil {
+ return c.FakedServerVersion, nil
+ }
+
versionInfo := kubeversion.Get()
return &versionInfo, nil
}
diff --git a/staging/src/k8s.io/client-go/kubernetes/fake/clientset_generated.go b/staging/src/k8s.io/client-go/kubernetes/fake/clientset_generated.go
index 5569e92b7d..22937ccecd 100644
--- a/staging/src/k8s.io/client-go/kubernetes/fake/clientset_generated.go
+++ b/staging/src/k8s.io/client-go/kubernetes/fake/clientset_generated.go
@@ -87,10 +87,9 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
fakePtr := testing.Fake{}
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
-
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil))
- return &Clientset{fakePtr}
+ return &Clientset{fakePtr, &fakediscovery.FakeDiscovery{Fake: &fakePtr}}
}
// Clientset implements clientset.Interface. Meant to be embedded into a
@@ -98,10 +97,11 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
// you want to test easier.
type Clientset struct {
testing.Fake
+ discovery *fakediscovery.FakeDiscovery
}
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
- return &fakediscovery.FakeDiscovery{Fake: &c.Fake}
+ return c.discovery
}
var _ clientset.Interface = &Clientset{} WDYT? Can implement for the other methods as well... |
The apiserver fixture uses How do you plan to represent request sent to "/apis", "apis/extension", or "apis/extensions/v1beta1"? |
discovery.DiscoveryInterface | ||
SetServerGroups(*metav1.APIGroupList) | ||
SetServerVersion(*version.Info) | ||
SetRESTClient(restclient.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.
why the rest client?
@deads2k @caesarxuchao @sttts PTAL at the latest version. WDYT? |
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil)) | ||
|
||
return &Clientset{fakePtr} | ||
return &Clientset{fakePtr, &fakediscovery.FakeDiscovery{Fake: &fakePtr}} |
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 must go into the generator
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.
What generator are you talking about?
type FakeDiscovery struct { | ||
*testing.Fake | ||
FakedServerVersion *version.Info | ||
FakedRESTClient restclient.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.
I didn't notice this one in the comment in your diff. Can we avoid 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.
Before this; RESTClient()
always returned nil
. With this, the user can set this field if they want to make client.Discovery().RESTClient()
for a faked clientset to work, but it will still default to nil
.
WDYT?
One comment on a field that I think is unused right now, then @sttts comment and this lgtm. |
lgtm |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: caesarxuchao, luxas Assign the PR to them by writing No associated issue. Update pull-request body to add a reference to an issue, or get approval with The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these OWNERS Files:
You can indicate your approval by writing |
@caesarxuchao just updated bazel as I noticed I didn't do that before. |
I think this needs @smarterclayton's or @lavalamp's approval though. |
Should hopefully fix small verification failure... |
/retest |
/assign @lavalamp for approval. |
/retest |
1 similar comment
/retest |
I'm going to manually approve this one. It's lgtm'ed long ago, I don't want it to slip the code freeze. |
/retest Thank you @caesarxuchao, yeah, this is pretty trivial in any case so it's much more about review bandwidth than someone not wanting to approve this PR. |
/retest
… On 02 Sep 2017, at 13:38, k8s-ci-robot ***@***.***> wrote:
@luxas: The following tests failed, say /retest to rerun them all:
Test name Commit Details Rerun command
pull-kubernetes-verify e1cff67 link /test pull-kubernetes-verify
pull-kubernetes-kubemark-e2e-gce e1cff67 link /test pull-kubernetes-kubemark-e2e-gce
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
/test pull-kubernetes-verify |
/retest |
1 similar comment
/retest |
@luxas: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. 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. I understand the commands that are listed here. |
Automatic merge from submit-queue (batch tested with PRs 51335, 51364, 51130, 48075, 50920) |
What this PR does / why we need it:
As an user of the fake clientset (with the fake discovery), I want to be able to set the fake server's version on demand like this for example:
This PR makes that possible, in wait for a more sophisticated FakeDiscovery implementation generally.
Which issue this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close that issue when PR gets merged): fixes #Special notes for your reviewer:
Release note:
@kubernetes/sig-api-machinery-pr-reviews