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.
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/apimachinery/pkg/watch"
26 restclient "k8s.io/client-go/rest"
29 // Fake implements client.Interface. Meant to be embedded into a struct to get
30 // a default implementation. This makes faking out just the method you want to
34 actions []Action // these may be castable to other types, but "Action" is the minimum
36 // ReactionChain is the list of reactors that will be attempted for every
37 // request in the order they are tried.
38 ReactionChain []Reactor
39 // WatchReactionChain is the list of watch reactors that will be attempted
40 // for every request in the order they are tried.
41 WatchReactionChain []WatchReactor
42 // ProxyReactionChain is the list of proxy reactors that will be attempted
43 // for every request in the order they are tried.
44 ProxyReactionChain []ProxyReactor
46 Resources []*metav1.APIResourceList
49 // Reactor is an interface to allow the composition of reaction functions.
50 type Reactor interface {
51 // Handles indicates whether or not this Reactor deals with a given
53 Handles(action Action) bool
54 // React handles the action and returns results. It may choose to
55 // delegate by indicated handled=false.
56 React(action Action) (handled bool, ret runtime.Object, err error)
59 // WatchReactor is an interface to allow the composition of watch functions.
60 type WatchReactor interface {
61 // Handles indicates whether or not this Reactor deals with a given
63 Handles(action Action) bool
64 // React handles a watch action and returns results. It may choose to
65 // delegate by indicating handled=false.
66 React(action Action) (handled bool, ret watch.Interface, err error)
69 // ProxyReactor is an interface to allow the composition of proxy get
71 type ProxyReactor interface {
72 // Handles indicates whether or not this Reactor deals with a given
74 Handles(action Action) bool
75 // React handles a watch action and returns results. It may choose to
76 // delegate by indicating handled=false.
77 React(action Action) (handled bool, ret restclient.ResponseWrapper, err error)
80 // ReactionFunc is a function that returns an object or error for a given
81 // Action. If "handled" is false, then the test client will ignore the
82 // results and continue to the next ReactionFunc. A ReactionFunc can describe
83 // reactions on subresources by testing the result of the action's
84 // GetSubresource() method.
85 type ReactionFunc func(action Action) (handled bool, ret runtime.Object, err error)
87 // WatchReactionFunc is a function that returns a watch interface. If
88 // "handled" is false, then the test client will ignore the results and
89 // continue to the next ReactionFunc.
90 type WatchReactionFunc func(action Action) (handled bool, ret watch.Interface, err error)
92 // ProxyReactionFunc is a function that returns a ResponseWrapper interface
93 // for a given Action. If "handled" is false, then the test client will
94 // ignore the results and continue to the next ProxyReactionFunc.
95 type ProxyReactionFunc func(action Action) (handled bool, ret restclient.ResponseWrapper, err error)
97 // AddReactor appends a reactor to the end of the chain.
98 func (c *Fake) AddReactor(verb, resource string, reaction ReactionFunc) {
99 c.ReactionChain = append(c.ReactionChain, &SimpleReactor{verb, resource, reaction})
102 // PrependReactor adds a reactor to the beginning of the chain.
103 func (c *Fake) PrependReactor(verb, resource string, reaction ReactionFunc) {
104 c.ReactionChain = append([]Reactor{&SimpleReactor{verb, resource, reaction}}, c.ReactionChain...)
107 // AddWatchReactor appends a reactor to the end of the chain.
108 func (c *Fake) AddWatchReactor(resource string, reaction WatchReactionFunc) {
109 c.WatchReactionChain = append(c.WatchReactionChain, &SimpleWatchReactor{resource, reaction})
112 // PrependWatchReactor adds a reactor to the beginning of the chain.
113 func (c *Fake) PrependWatchReactor(resource string, reaction WatchReactionFunc) {
114 c.WatchReactionChain = append([]WatchReactor{&SimpleWatchReactor{resource, reaction}}, c.WatchReactionChain...)
117 // AddProxyReactor appends a reactor to the end of the chain.
118 func (c *Fake) AddProxyReactor(resource string, reaction ProxyReactionFunc) {
119 c.ProxyReactionChain = append(c.ProxyReactionChain, &SimpleProxyReactor{resource, reaction})
122 // PrependProxyReactor adds a reactor to the beginning of the chain.
123 func (c *Fake) PrependProxyReactor(resource string, reaction ProxyReactionFunc) {
124 c.ProxyReactionChain = append([]ProxyReactor{&SimpleProxyReactor{resource, reaction}}, c.ProxyReactionChain...)
127 // Invokes records the provided Action and then invokes the ReactionFunc that
128 // handles the action if one exists. defaultReturnObj is expected to be of the
129 // same type a normal call would return.
130 func (c *Fake) Invokes(action Action, defaultReturnObj runtime.Object) (runtime.Object, error) {
134 c.actions = append(c.actions, action.DeepCopy())
135 for _, reactor := range c.ReactionChain {
136 if !reactor.Handles(action) {
140 handled, ret, err := reactor.React(action.DeepCopy())
148 return defaultReturnObj, nil
151 // InvokesWatch records the provided Action and then invokes the ReactionFunc
152 // that handles the action if one exists.
153 func (c *Fake) InvokesWatch(action Action) (watch.Interface, error) {
157 c.actions = append(c.actions, action.DeepCopy())
158 for _, reactor := range c.WatchReactionChain {
159 if !reactor.Handles(action) {
163 handled, ret, err := reactor.React(action.DeepCopy())
171 return nil, fmt.Errorf("unhandled watch: %#v", action)
174 // InvokesProxy records the provided Action and then invokes the ReactionFunc
175 // that handles the action if one exists.
176 func (c *Fake) InvokesProxy(action Action) restclient.ResponseWrapper {
180 c.actions = append(c.actions, action.DeepCopy())
181 for _, reactor := range c.ProxyReactionChain {
182 if !reactor.Handles(action) {
186 handled, ret, err := reactor.React(action.DeepCopy())
187 if !handled || err != nil {
197 // ClearActions clears the history of actions called on the fake client.
198 func (c *Fake) ClearActions() {
202 c.actions = make([]Action, 0)
205 // Actions returns a chronologically ordered slice fake actions called on the
207 func (c *Fake) Actions() []Action {
210 fa := make([]Action, len(c.actions))