fake.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "fmt"
  16. "sync"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/watch"
  20. restclient "k8s.io/client-go/rest"
  21. )
  22. // Fake implements client.Interface. Meant to be embedded into a struct to get
  23. // a default implementation. This makes faking out just the method you want to
  24. // test easier.
  25. type Fake struct {
  26. sync.RWMutex
  27. actions []Action // these may be castable to other types, but "Action" is the minimum
  28. // ReactionChain is the list of reactors that will be attempted for every
  29. // request in the order they are tried.
  30. ReactionChain []Reactor
  31. // WatchReactionChain is the list of watch reactors that will be attempted
  32. // for every request in the order they are tried.
  33. WatchReactionChain []WatchReactor
  34. // ProxyReactionChain is the list of proxy reactors that will be attempted
  35. // for every request in the order they are tried.
  36. ProxyReactionChain []ProxyReactor
  37. Resources []*metav1.APIResourceList
  38. }
  39. // Reactor is an interface to allow the composition of reaction functions.
  40. type Reactor interface {
  41. // Handles indicates whether or not this Reactor deals with a given
  42. // action.
  43. Handles(action Action) bool
  44. // React handles the action and returns results. It may choose to
  45. // delegate by indicated handled=false.
  46. React(action Action) (handled bool, ret runtime.Object, err error)
  47. }
  48. // WatchReactor is an interface to allow the composition of watch functions.
  49. type WatchReactor interface {
  50. // Handles indicates whether or not this Reactor deals with a given
  51. // action.
  52. Handles(action Action) bool
  53. // React handles a watch action and returns results. It may choose to
  54. // delegate by indicating handled=false.
  55. React(action Action) (handled bool, ret watch.Interface, err error)
  56. }
  57. // ProxyReactor is an interface to allow the composition of proxy get
  58. // functions.
  59. type ProxyReactor interface {
  60. // Handles indicates whether or not this Reactor deals with a given
  61. // action.
  62. Handles(action Action) bool
  63. // React handles a watch action and returns results. It may choose to
  64. // delegate by indicating handled=false.
  65. React(action Action) (handled bool, ret restclient.ResponseWrapper, err error)
  66. }
  67. // ReactionFunc is a function that returns an object or error for a given
  68. // Action. If "handled" is false, then the test client will ignore the
  69. // results and continue to the next ReactionFunc. A ReactionFunc can describe
  70. // reactions on subresources by testing the result of the action's
  71. // GetSubresource() method.
  72. type ReactionFunc func(action Action) (handled bool, ret runtime.Object, err error)
  73. // WatchReactionFunc is a function that returns a watch interface. If
  74. // "handled" is false, then the test client will ignore the results and
  75. // continue to the next ReactionFunc.
  76. type WatchReactionFunc func(action Action) (handled bool, ret watch.Interface, err error)
  77. // ProxyReactionFunc is a function that returns a ResponseWrapper interface
  78. // for a given Action. If "handled" is false, then the test client will
  79. // ignore the results and continue to the next ProxyReactionFunc.
  80. type ProxyReactionFunc func(action Action) (handled bool, ret restclient.ResponseWrapper, err error)
  81. // AddReactor appends a reactor to the end of the chain.
  82. func (c *Fake) AddReactor(verb, resource string, reaction ReactionFunc) {
  83. c.ReactionChain = append(c.ReactionChain, &SimpleReactor{verb, resource, reaction})
  84. }
  85. // PrependReactor adds a reactor to the beginning of the chain.
  86. func (c *Fake) PrependReactor(verb, resource string, reaction ReactionFunc) {
  87. c.ReactionChain = append([]Reactor{&SimpleReactor{verb, resource, reaction}}, c.ReactionChain...)
  88. }
  89. // AddWatchReactor appends a reactor to the end of the chain.
  90. func (c *Fake) AddWatchReactor(resource string, reaction WatchReactionFunc) {
  91. c.Lock()
  92. defer c.Unlock()
  93. c.WatchReactionChain = append(c.WatchReactionChain, &SimpleWatchReactor{resource, reaction})
  94. }
  95. // PrependWatchReactor adds a reactor to the beginning of the chain.
  96. func (c *Fake) PrependWatchReactor(resource string, reaction WatchReactionFunc) {
  97. c.Lock()
  98. defer c.Unlock()
  99. c.WatchReactionChain = append([]WatchReactor{&SimpleWatchReactor{resource, reaction}}, c.WatchReactionChain...)
  100. }
  101. // AddProxyReactor appends a reactor to the end of the chain.
  102. func (c *Fake) AddProxyReactor(resource string, reaction ProxyReactionFunc) {
  103. c.ProxyReactionChain = append(c.ProxyReactionChain, &SimpleProxyReactor{resource, reaction})
  104. }
  105. // PrependProxyReactor adds a reactor to the beginning of the chain.
  106. func (c *Fake) PrependProxyReactor(resource string, reaction ProxyReactionFunc) {
  107. c.ProxyReactionChain = append([]ProxyReactor{&SimpleProxyReactor{resource, reaction}}, c.ProxyReactionChain...)
  108. }
  109. // Invokes records the provided Action and then invokes the ReactionFunc that
  110. // handles the action if one exists. defaultReturnObj is expected to be of the
  111. // same type a normal call would return.
  112. func (c *Fake) Invokes(action Action, defaultReturnObj runtime.Object) (runtime.Object, error) {
  113. c.Lock()
  114. defer c.Unlock()
  115. actionCopy := action.DeepCopy()
  116. c.actions = append(c.actions, action.DeepCopy())
  117. for _, reactor := range c.ReactionChain {
  118. if !reactor.Handles(actionCopy) {
  119. continue
  120. }
  121. handled, ret, err := reactor.React(actionCopy)
  122. if !handled {
  123. continue
  124. }
  125. return ret, err
  126. }
  127. return defaultReturnObj, nil
  128. }
  129. // InvokesWatch records the provided Action and then invokes the ReactionFunc
  130. // that handles the action if one exists.
  131. func (c *Fake) InvokesWatch(action Action) (watch.Interface, error) {
  132. c.Lock()
  133. defer c.Unlock()
  134. actionCopy := action.DeepCopy()
  135. c.actions = append(c.actions, action.DeepCopy())
  136. for _, reactor := range c.WatchReactionChain {
  137. if !reactor.Handles(actionCopy) {
  138. continue
  139. }
  140. handled, ret, err := reactor.React(actionCopy)
  141. if !handled {
  142. continue
  143. }
  144. return ret, err
  145. }
  146. return nil, fmt.Errorf("unhandled watch: %#v", action)
  147. }
  148. // InvokesProxy records the provided Action and then invokes the ReactionFunc
  149. // that handles the action if one exists.
  150. func (c *Fake) InvokesProxy(action Action) restclient.ResponseWrapper {
  151. c.Lock()
  152. defer c.Unlock()
  153. actionCopy := action.DeepCopy()
  154. c.actions = append(c.actions, action.DeepCopy())
  155. for _, reactor := range c.ProxyReactionChain {
  156. if !reactor.Handles(actionCopy) {
  157. continue
  158. }
  159. handled, ret, err := reactor.React(actionCopy)
  160. if !handled || err != nil {
  161. continue
  162. }
  163. return ret
  164. }
  165. return nil
  166. }
  167. // ClearActions clears the history of actions called on the fake client.
  168. func (c *Fake) ClearActions() {
  169. c.Lock()
  170. defer c.Unlock()
  171. c.actions = make([]Action, 0)
  172. }
  173. // Actions returns a chronologically ordered slice fake actions called on the
  174. // fake client.
  175. func (c *Fake) Actions() []Action {
  176. c.RLock()
  177. defer c.RUnlock()
  178. fa := make([]Action, len(c.actions))
  179. copy(fa, c.actions)
  180. return fa
  181. }