Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / client-go / testing / fake.go
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package testing
18
19 import (
20         "fmt"
21         "sync"
22
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"
27 )
28
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
31 // test easier.
32 type Fake struct {
33         sync.RWMutex
34         actions []Action // these may be castable to other types, but "Action" is the minimum
35
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
45
46         Resources []*metav1.APIResourceList
47 }
48
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
52         // action.
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)
57 }
58
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
62         // action.
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)
67 }
68
69 // ProxyReactor is an interface to allow the composition of proxy get
70 // functions.
71 type ProxyReactor interface {
72         // Handles indicates whether or not this Reactor deals with a given
73         // action.
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)
78 }
79
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)
86
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)
91
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)
96
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})
100 }
101
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...)
105 }
106
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})
110 }
111
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...)
115 }
116
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})
120 }
121
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...)
125 }
126
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) {
131         c.Lock()
132         defer c.Unlock()
133
134         c.actions = append(c.actions, action.DeepCopy())
135         for _, reactor := range c.ReactionChain {
136                 if !reactor.Handles(action) {
137                         continue
138                 }
139
140                 handled, ret, err := reactor.React(action.DeepCopy())
141                 if !handled {
142                         continue
143                 }
144
145                 return ret, err
146         }
147
148         return defaultReturnObj, nil
149 }
150
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) {
154         c.Lock()
155         defer c.Unlock()
156
157         c.actions = append(c.actions, action.DeepCopy())
158         for _, reactor := range c.WatchReactionChain {
159                 if !reactor.Handles(action) {
160                         continue
161                 }
162
163                 handled, ret, err := reactor.React(action.DeepCopy())
164                 if !handled {
165                         continue
166                 }
167
168                 return ret, err
169         }
170
171         return nil, fmt.Errorf("unhandled watch: %#v", action)
172 }
173
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 {
177         c.Lock()
178         defer c.Unlock()
179
180         c.actions = append(c.actions, action.DeepCopy())
181         for _, reactor := range c.ProxyReactionChain {
182                 if !reactor.Handles(action) {
183                         continue
184                 }
185
186                 handled, ret, err := reactor.React(action.DeepCopy())
187                 if !handled || err != nil {
188                         continue
189                 }
190
191                 return ret
192         }
193
194         return nil
195 }
196
197 // ClearActions clears the history of actions called on the fake client.
198 func (c *Fake) ClearActions() {
199         c.Lock()
200         defer c.Unlock()
201
202         c.actions = make([]Action, 0)
203 }
204
205 // Actions returns a chronologically ordered slice fake actions called on the
206 // fake client.
207 func (c *Fake) Actions() []Action {
208         c.RLock()
209         defer c.RUnlock()
210         fa := make([]Action, len(c.actions))
211         copy(fa, c.actions)
212         return fa
213 }