mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-27 07:28:14 +00:00
Merge pull request #51130 from luxas/fake_discovery
Automatic merge from submit-queue (batch tested with PRs 51335, 51364, 51130, 48075, 50920) Add the possibility to set return values for the FakeDiscovery implementation **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: ```go func TestFakingServerVersion(t *testing.T) { client := fakeclientset.NewSimpleClientset() fakeDiscovery, ok := client.Discovery().(*fakediscovery.FakeDiscovery) if !ok { t.Fatalf("couldn't convert Discovery() to *FakeDiscovery") } testGitCommit := "v1.0.0" fakeDiscovery.FakedServerVersion = &version.Info{ GitCommit: testGitCommit, } sv, err := client.Discovery().ServerVersion() if err != nil { t.Fatalf("unexpected error: %v", err) } if sv.GitCommit != testGitCommit { t.Fatalf("unexpected faked discovery return value: %q", sv.GitCommit) } } ``` 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**: ```release-note NONE ``` @kubernetes/sig-api-machinery-pr-reviews Kubernetes-commit: 94d9457e4e4786b735c5be1224f831c6010691b5
This commit is contained in:
commit
48ade6dd30
50
Godeps/Godeps.json
generated
50
Godeps/Godeps.json
generated
@ -372,103 +372,103 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/admissionregistration/v1alpha1",
|
"ImportPath": "k8s.io/api/admissionregistration/v1alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/apps/v1beta1",
|
"ImportPath": "k8s.io/api/apps/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/apps/v1beta2",
|
"ImportPath": "k8s.io/api/apps/v1beta2",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/authentication/v1",
|
"ImportPath": "k8s.io/api/authentication/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/authentication/v1beta1",
|
"ImportPath": "k8s.io/api/authentication/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/authorization/v1",
|
"ImportPath": "k8s.io/api/authorization/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/authorization/v1beta1",
|
"ImportPath": "k8s.io/api/authorization/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/autoscaling/v1",
|
"ImportPath": "k8s.io/api/autoscaling/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/autoscaling/v2alpha1",
|
"ImportPath": "k8s.io/api/autoscaling/v2alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/batch/v1",
|
"ImportPath": "k8s.io/api/batch/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/batch/v1beta1",
|
"ImportPath": "k8s.io/api/batch/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/batch/v2alpha1",
|
"ImportPath": "k8s.io/api/batch/v2alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/certificates/v1beta1",
|
"ImportPath": "k8s.io/api/certificates/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/core/v1",
|
"ImportPath": "k8s.io/api/core/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/extensions/v1beta1",
|
"ImportPath": "k8s.io/api/extensions/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/imagepolicy/v1alpha1",
|
"ImportPath": "k8s.io/api/imagepolicy/v1alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/networking/v1",
|
"ImportPath": "k8s.io/api/networking/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/policy/v1beta1",
|
"ImportPath": "k8s.io/api/policy/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/rbac/v1",
|
"ImportPath": "k8s.io/api/rbac/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/rbac/v1alpha1",
|
"ImportPath": "k8s.io/api/rbac/v1alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/rbac/v1beta1",
|
"ImportPath": "k8s.io/api/rbac/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/scheduling/v1alpha1",
|
"ImportPath": "k8s.io/api/scheduling/v1alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/settings/v1alpha1",
|
"ImportPath": "k8s.io/api/settings/v1alpha1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/storage/v1",
|
"ImportPath": "k8s.io/api/storage/v1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/api/storage/v1beta1",
|
"ImportPath": "k8s.io/api/storage/v1beta1",
|
||||||
"Rev": "039ab3abf602964ac5fc9b4669fd8e7ec58fbbd8"
|
"Rev": "21998e7d64f3b177d533268cfb312523e1d53038"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "k8s.io/apimachinery/pkg/api/equality",
|
"ImportPath": "k8s.io/apimachinery/pkg/api/equality",
|
||||||
|
@ -30,6 +30,9 @@ filegroup(
|
|||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
name = "all-srcs",
|
name = "all-srcs",
|
||||||
srcs = [":package-srcs"],
|
srcs = [
|
||||||
|
":package-srcs",
|
||||||
|
"//staging/src/k8s.io/client-go/discovery/fake/test:all-srcs",
|
||||||
|
],
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
)
|
)
|
||||||
|
@ -31,8 +31,11 @@ import (
|
|||||||
"k8s.io/client-go/testing"
|
"k8s.io/client-go/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FakeDiscovery implements discovery.DiscoveryInterface and sometimes calls testing.Fake.Invoke with an action,
|
||||||
|
// but doesn't respect the return value if any. There is a way to fake static values like ServerVersion by using the Faked... fields on the struct.
|
||||||
type FakeDiscovery struct {
|
type FakeDiscovery struct {
|
||||||
*testing.Fake
|
*testing.Fake
|
||||||
|
FakedServerVersion *version.Info
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
|
func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
|
||||||
@ -74,8 +77,12 @@ func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
|
|||||||
action := testing.ActionImpl{}
|
action := testing.ActionImpl{}
|
||||||
action.Verb = "get"
|
action.Verb = "get"
|
||||||
action.Resource = schema.GroupVersionResource{Resource: "version"}
|
action.Resource = schema.GroupVersionResource{Resource: "version"}
|
||||||
|
|
||||||
c.Invokes(action, nil)
|
c.Invokes(action, nil)
|
||||||
|
|
||||||
|
if c.FakedServerVersion != nil {
|
||||||
|
return c.FakedServerVersion, nil
|
||||||
|
}
|
||||||
|
|
||||||
versionInfo := kubeversion.Get()
|
versionInfo := kubeversion.Get()
|
||||||
return &versionInfo, nil
|
return &versionInfo, nil
|
||||||
}
|
}
|
||||||
|
25
discovery/fake/test/BUILD
Normal file
25
discovery/fake/test/BUILD
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
load("@io_bazel_rules_go//go:def.bzl", "go_test")
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_test",
|
||||||
|
srcs = ["discovery_test.go"],
|
||||||
|
deps = [
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/version:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/discovery/fake:go_default_library",
|
||||||
|
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "package-srcs",
|
||||||
|
srcs = glob(["**"]),
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:private"],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "all-srcs",
|
||||||
|
srcs = [":package-srcs"],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
46
discovery/fake/test/discovery_test.go
Normal file
46
discovery/fake/test/discovery_test.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
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 fake
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/version"
|
||||||
|
fakediscovery "k8s.io/client-go/discovery/fake"
|
||||||
|
fakeclientset "k8s.io/client-go/kubernetes/fake"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFakingServerVersion(t *testing.T) {
|
||||||
|
client := fakeclientset.NewSimpleClientset()
|
||||||
|
fakeDiscovery, ok := client.Discovery().(*fakediscovery.FakeDiscovery)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("couldn't convert Discovery() to *FakeDiscovery")
|
||||||
|
}
|
||||||
|
|
||||||
|
testGitCommit := "v1.0.0"
|
||||||
|
fakeDiscovery.FakedServerVersion = &version.Info{
|
||||||
|
GitCommit: testGitCommit,
|
||||||
|
}
|
||||||
|
|
||||||
|
sv, err := client.Discovery().ServerVersion()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if sv.GitCommit != testGitCommit {
|
||||||
|
t.Fatalf("unexpected faked discovery return value: %q", sv.GitCommit)
|
||||||
|
}
|
||||||
|
}
|
@ -87,10 +87,9 @@ func NewSimpleClientset(objects ...runtime.Object) *Clientset {
|
|||||||
|
|
||||||
fakePtr := testing.Fake{}
|
fakePtr := testing.Fake{}
|
||||||
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
|
fakePtr.AddReactor("*", "*", testing.ObjectReaction(o))
|
||||||
|
|
||||||
fakePtr.AddWatchReactor("*", testing.DefaultWatchReactor(watch.NewFake(), nil))
|
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
|
// 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.
|
// you want to test easier.
|
||||||
type Clientset struct {
|
type Clientset struct {
|
||||||
testing.Fake
|
testing.Fake
|
||||||
|
discovery *fakediscovery.FakeDiscovery
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
|
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
|
||||||
return &fakediscovery.FakeDiscovery{Fake: &c.Fake}
|
return c.discovery
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ clientset.Interface = &Clientset{}
|
var _ clientset.Interface = &Clientset{}
|
||||||
|
Loading…
Reference in New Issue
Block a user