2 Copyright 2016 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
22 "github.com/googleapis/gnostic/OpenAPIv2"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/runtime/schema"
26 "k8s.io/apimachinery/pkg/version"
27 kubeversion "k8s.io/client-go/pkg/version"
28 restclient "k8s.io/client-go/rest"
29 "k8s.io/client-go/testing"
32 // FakeDiscovery implements discovery.DiscoveryInterface and sometimes calls testing.Fake.Invoke with an action,
33 // 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.
34 type FakeDiscovery struct {
36 FakedServerVersion *version.Info
39 // ServerResourcesForGroupVersion returns the supported resources for a group
41 func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
42 action := testing.ActionImpl{
44 Resource: schema.GroupVersionResource{Resource: "resource"},
46 c.Invokes(action, nil)
47 for _, resourceList := range c.Resources {
48 if resourceList.GroupVersion == groupVersion {
49 return resourceList, nil
52 return nil, fmt.Errorf("GroupVersion %q not found", groupVersion)
55 // ServerResources returns the supported resources for all groups and versions.
56 func (c *FakeDiscovery) ServerResources() ([]*metav1.APIResourceList, error) {
57 action := testing.ActionImpl{
59 Resource: schema.GroupVersionResource{Resource: "resource"},
61 c.Invokes(action, nil)
62 return c.Resources, nil
65 // ServerPreferredResources returns the supported resources with the version
66 // preferred by the server.
67 func (c *FakeDiscovery) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
71 // ServerPreferredNamespacedResources returns the supported namespaced resources
72 // with the version preferred by the server.
73 func (c *FakeDiscovery) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
77 // ServerGroups returns the supported groups, with information like supported
78 // versions and the preferred version.
79 func (c *FakeDiscovery) ServerGroups() (*metav1.APIGroupList, error) {
80 action := testing.ActionImpl{
82 Resource: schema.GroupVersionResource{Resource: "group"},
84 c.Invokes(action, nil)
86 groups := map[string]*metav1.APIGroup{}
88 for _, res := range c.Resources {
89 gv, err := schema.ParseGroupVersion(res.GroupVersion)
93 group := groups[gv.Group]
95 group = &metav1.APIGroup{
97 PreferredVersion: metav1.GroupVersionForDiscovery{
98 GroupVersion: res.GroupVersion,
102 groups[gv.Group] = group
105 group.Versions = append(group.Versions, metav1.GroupVersionForDiscovery{
106 GroupVersion: res.GroupVersion,
111 list := &metav1.APIGroupList{}
112 for _, apiGroup := range groups {
113 list.Groups = append(list.Groups, *apiGroup)
120 // ServerVersion retrieves and parses the server's version.
121 func (c *FakeDiscovery) ServerVersion() (*version.Info, error) {
122 action := testing.ActionImpl{}
124 action.Resource = schema.GroupVersionResource{Resource: "version"}
125 c.Invokes(action, nil)
127 if c.FakedServerVersion != nil {
128 return c.FakedServerVersion, nil
131 versionInfo := kubeversion.Get()
132 return &versionInfo, nil
135 // OpenAPISchema retrieves and parses the swagger API schema the server supports.
136 func (c *FakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
137 return &openapi_v2.Document{}, nil
140 // RESTClient returns a RESTClient that is used to communicate with API server
141 // by this client implementation.
142 func (c *FakeDiscovery) RESTClient() restclient.Interface {