mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-24 14:12:18 +00:00
client-go: properly return error in fake discovery (#79612)
Fake discovery should return an error if an error-returning reactor was prepended. This is relevant e.g. for unit tests which test a function which relies on discovery to check if an API Server is available. Matches existing behavior of other fake types, for example fake_namespace. Adds unit test coverage as well. Kubernetes-commit: b47748103a53c0fe5ba1ddc950662ffc9bd9ed0f
This commit is contained in:
parent
089645a7c5
commit
24248303bf
@ -141,7 +141,10 @@ func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
|
||||
action := testing.ActionImpl{}
|
||||
action.Verb = "get"
|
||||
action.Resource = schema.GroupVersionResource{Resource: "version"}
|
||||
c.Invokes(action, nil)
|
||||
_, err := c.Invokes(action, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.FakedServerVersion != nil {
|
||||
return c.FakedServerVersion, nil
|
||||
|
@ -17,11 +17,14 @@ limitations under the License.
|
||||
package fake_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/version"
|
||||
fakediscovery "k8s.io/client-go/discovery/fake"
|
||||
fakeclientset "k8s.io/client-go/kubernetes/fake"
|
||||
kubetesting "k8s.io/client-go/testing"
|
||||
)
|
||||
|
||||
func TestFakingServerVersion(t *testing.T) {
|
||||
@ -44,3 +47,19 @@ func TestFakingServerVersion(t *testing.T) {
|
||||
t.Fatalf("unexpected faked discovery return value: %q", sv.GitCommit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFakingServerVersionWithError(t *testing.T) {
|
||||
expectedError := errors.New("an error occurred")
|
||||
fakeClient := fakeclientset.NewSimpleClientset()
|
||||
fakeClient.Discovery().(*fakediscovery.FakeDiscovery).PrependReactor("*", "*", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return true, nil, expectedError
|
||||
})
|
||||
|
||||
_, err := fakeClient.Discovery().ServerVersion()
|
||||
if err == nil {
|
||||
t.Fatal("ServerVersion should return error, returned nil instead")
|
||||
}
|
||||
if err != expectedError {
|
||||
t.Fatal("ServerVersion should return expected error, returned different error instead")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user