github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/jujuclient/jujuclienttesting/stub.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuclienttesting 5 6 import ( 7 "github.com/juju/testing" 8 9 "github.com/juju/juju/cloud" 10 "github.com/juju/juju/jujuclient" 11 ) 12 13 type StubStore struct { 14 *testing.Stub 15 16 AllControllersFunc func() (map[string]jujuclient.ControllerDetails, error) 17 ControllerByNameFunc func(name string) (*jujuclient.ControllerDetails, error) 18 AddControllerFunc func(name string, one jujuclient.ControllerDetails) error 19 UpdateControllerFunc func(name string, one jujuclient.ControllerDetails) error 20 RemoveControllerFunc func(name string) error 21 SetCurrentControllerFunc func(name string) error 22 CurrentControllerFunc func() (string, error) 23 24 UpdateModelFunc func(controller, model string, details jujuclient.ModelDetails) error 25 SetCurrentModelFunc func(controller, model string) error 26 RemoveModelFunc func(controller, model string) error 27 AllModelsFunc func(controller string) (map[string]jujuclient.ModelDetails, error) 28 CurrentModelFunc func(controller string) (string, error) 29 ModelByNameFunc func(controller, model string) (*jujuclient.ModelDetails, error) 30 31 UpdateAccountFunc func(controllerName string, details jujuclient.AccountDetails) error 32 AccountDetailsFunc func(controllerName string) (*jujuclient.AccountDetails, error) 33 RemoveAccountFunc func(controllerName string) error 34 35 CredentialForCloudFunc func(string) (*cloud.CloudCredential, error) 36 AllCredentialsFunc func() (map[string]cloud.CloudCredential, error) 37 UpdateCredentialFunc func(cloudName string, details cloud.CloudCredential) error 38 39 BootstrapConfigForControllerFunc func(controllerName string) (*jujuclient.BootstrapConfig, error) 40 UpdateBootstrapConfigFunc func(controllerName string, cfg jujuclient.BootstrapConfig) error 41 } 42 43 func NewStubStore() *StubStore { 44 result := &StubStore{ 45 Stub: &testing.Stub{}, 46 } 47 result.AllControllersFunc = func() (map[string]jujuclient.ControllerDetails, error) { 48 return nil, result.Stub.NextErr() 49 } 50 result.ControllerByNameFunc = func(name string) (*jujuclient.ControllerDetails, error) { 51 return nil, result.Stub.NextErr() 52 } 53 result.AddControllerFunc = func(name string, one jujuclient.ControllerDetails) error { 54 return result.Stub.NextErr() 55 } 56 result.UpdateControllerFunc = func(name string, one jujuclient.ControllerDetails) error { 57 return result.Stub.NextErr() 58 } 59 result.RemoveControllerFunc = func(name string) error { 60 return result.Stub.NextErr() 61 } 62 result.SetCurrentControllerFunc = func(name string) error { 63 return result.Stub.NextErr() 64 } 65 result.CurrentControllerFunc = func() (string, error) { 66 return "", result.Stub.NextErr() 67 } 68 69 result.UpdateModelFunc = func(controller, model string, details jujuclient.ModelDetails) error { 70 return result.Stub.NextErr() 71 } 72 result.SetCurrentModelFunc = func(controller, model string) error { 73 return result.Stub.NextErr() 74 } 75 result.RemoveModelFunc = func(controller, model string) error { 76 return result.Stub.NextErr() 77 } 78 result.AllModelsFunc = func(controller string) (map[string]jujuclient.ModelDetails, error) { 79 return nil, result.Stub.NextErr() 80 } 81 result.CurrentModelFunc = func(controller string) (string, error) { 82 return "", result.Stub.NextErr() 83 } 84 result.ModelByNameFunc = func(controller, model string) (*jujuclient.ModelDetails, error) { 85 return nil, result.Stub.NextErr() 86 } 87 88 result.UpdateAccountFunc = func(controllerName string, details jujuclient.AccountDetails) error { 89 return result.Stub.NextErr() 90 } 91 result.AccountDetailsFunc = func(controllerName string) (*jujuclient.AccountDetails, error) { 92 return nil, result.Stub.NextErr() 93 } 94 result.RemoveAccountFunc = func(controllerName string) error { 95 return result.Stub.NextErr() 96 } 97 98 result.CredentialForCloudFunc = func(string) (*cloud.CloudCredential, error) { 99 return nil, result.Stub.NextErr() 100 } 101 result.AllCredentialsFunc = func() (map[string]cloud.CloudCredential, error) { 102 return nil, result.Stub.NextErr() 103 } 104 result.UpdateCredentialFunc = func(cloudName string, details cloud.CloudCredential) error { 105 return result.Stub.NextErr() 106 } 107 108 result.BootstrapConfigForControllerFunc = func(controllerName string) (*jujuclient.BootstrapConfig, error) { 109 return nil, result.Stub.NextErr() 110 } 111 result.UpdateBootstrapConfigFunc = func(controllerName string, cfg jujuclient.BootstrapConfig) error { 112 return result.Stub.NextErr() 113 } 114 return result 115 } 116 117 // WrapClientStore wraps a ClientStore with a StubStore, where each method calls 118 // through to the wrapped store. This can be used to override specific 119 // methods, or just to check which calls have been made. 120 func WrapClientStore(underlying jujuclient.ClientStore) *StubStore { 121 stub := NewStubStore() 122 stub.AllControllersFunc = underlying.AllControllers 123 stub.ControllerByNameFunc = underlying.ControllerByName 124 stub.AddControllerFunc = underlying.AddController 125 stub.UpdateControllerFunc = underlying.UpdateController 126 stub.RemoveControllerFunc = underlying.RemoveController 127 stub.SetCurrentControllerFunc = underlying.SetCurrentController 128 stub.CurrentControllerFunc = underlying.CurrentController 129 stub.UpdateModelFunc = underlying.UpdateModel 130 stub.SetCurrentModelFunc = underlying.SetCurrentModel 131 stub.RemoveModelFunc = underlying.RemoveModel 132 stub.AllModelsFunc = underlying.AllModels 133 stub.CurrentModelFunc = underlying.CurrentModel 134 stub.ModelByNameFunc = underlying.ModelByName 135 stub.UpdateAccountFunc = underlying.UpdateAccount 136 stub.AccountDetailsFunc = underlying.AccountDetails 137 stub.RemoveAccountFunc = underlying.RemoveAccount 138 stub.BootstrapConfigForControllerFunc = underlying.BootstrapConfigForController 139 stub.UpdateBootstrapConfigFunc = underlying.UpdateBootstrapConfig 140 return stub 141 } 142 143 // AllControllers implements ControllersGetter.AllControllers 144 func (c *StubStore) AllControllers() (map[string]jujuclient.ControllerDetails, error) { 145 c.MethodCall(c, "AllControllers") 146 return c.AllControllersFunc() 147 } 148 149 // ControllerByName implements ControllersGetter.ControllerByName 150 func (c *StubStore) ControllerByName(name string) (*jujuclient.ControllerDetails, error) { 151 c.MethodCall(c, "ControllerByName", name) 152 return c.ControllerByNameFunc(name) 153 } 154 155 // AddController implements ControllerUpdater.AddController 156 func (c *StubStore) AddController(name string, one jujuclient.ControllerDetails) error { 157 c.MethodCall(c, "AddController", name, one) 158 return c.AddControllerFunc(name, one) 159 } 160 161 // UpdateController implements ControllerUpdater.UpdateController 162 func (c *StubStore) UpdateController(name string, one jujuclient.ControllerDetails) error { 163 c.MethodCall(c, "UpdateController", name, one) 164 return c.UpdateControllerFunc(name, one) 165 } 166 167 // RemoveController implements ControllersRemover.RemoveController 168 func (c *StubStore) RemoveController(name string) error { 169 c.MethodCall(c, "RemoveController", name) 170 return c.RemoveControllerFunc(name) 171 } 172 173 // SetCurrentController implements ControllerUpdater.SetCurrentController. 174 func (c *StubStore) SetCurrentController(name string) error { 175 c.MethodCall(c, "SetCurrentController", name) 176 return c.SetCurrentControllerFunc(name) 177 } 178 179 // CurrentController implements ControllersGetter.CurrentController. 180 func (c *StubStore) CurrentController() (string, error) { 181 c.MethodCall(c, "CurrentController") 182 return c.CurrentControllerFunc() 183 } 184 185 // UpdateModel implements ModelUpdater. 186 func (c *StubStore) UpdateModel(controller, model string, details jujuclient.ModelDetails) error { 187 c.MethodCall(c, "UpdateModel", controller, model, details) 188 return c.UpdateModelFunc(controller, model, details) 189 } 190 191 // SetCurrentModel implements ModelUpdater. 192 func (c *StubStore) SetCurrentModel(controller, model string) error { 193 c.MethodCall(c, "SetCurrentModel", controller, model) 194 return c.SetCurrentModelFunc(controller, model) 195 } 196 197 // RemoveModel implements ModelRemover. 198 func (c *StubStore) RemoveModel(controller, model string) error { 199 c.MethodCall(c, "RemoveModel", controller, model) 200 return c.RemoveModelFunc(controller, model) 201 } 202 203 // AllModels implements ModelGetter. 204 func (c *StubStore) AllModels(controller string) (map[string]jujuclient.ModelDetails, error) { 205 c.MethodCall(c, "AllModels", controller) 206 return c.AllModelsFunc(controller) 207 } 208 209 // CurrentModel implements ModelGetter. 210 func (c *StubStore) CurrentModel(controller string) (string, error) { 211 c.MethodCall(c, "CurrentModel", controller) 212 return c.CurrentModelFunc(controller) 213 } 214 215 // ModelByName implements ModelGetter. 216 func (c *StubStore) ModelByName(controller, model string) (*jujuclient.ModelDetails, error) { 217 c.MethodCall(c, "ModelByName", controller, model) 218 return c.ModelByNameFunc(controller, model) 219 } 220 221 // UpdateAccount implements AccountUpdater. 222 func (c *StubStore) UpdateAccount(controllerName string, details jujuclient.AccountDetails) error { 223 c.MethodCall(c, "UpdateAccount", controllerName, details) 224 return c.UpdateAccountFunc(controllerName, details) 225 } 226 227 // AccountDetails implements AccountGetter. 228 func (c *StubStore) AccountDetails(controllerName string) (*jujuclient.AccountDetails, error) { 229 c.MethodCall(c, "AccountDetails", controllerName) 230 return c.AccountDetailsFunc(controllerName) 231 } 232 233 // RemoveAccount implements AccountRemover. 234 func (c *StubStore) RemoveAccount(controllerName string) error { 235 c.MethodCall(c, "RemoveAccount", controllerName) 236 return c.RemoveAccountFunc(controllerName) 237 } 238 239 // CredentialForCloud implements CredentialsGetter. 240 func (c *StubStore) CredentialForCloud(cloudName string) (*cloud.CloudCredential, error) { 241 c.MethodCall(c, "CredentialForCloud", cloudName) 242 return c.CredentialForCloudFunc(cloudName) 243 } 244 245 // AllCredentials implements CredentialsGetter. 246 func (c *StubStore) AllCredentials() (map[string]cloud.CloudCredential, error) { 247 c.MethodCall(c, "AllCredentials") 248 return c.AllCredentialsFunc() 249 } 250 251 // UpdateCredential implements CredentialsUpdater. 252 func (c *StubStore) UpdateCredential(cloudName string, details cloud.CloudCredential) error { 253 c.MethodCall(c, "UpdateCredential", cloudName, details) 254 return c.UpdateCredentialFunc(cloudName, details) 255 } 256 257 // BootstrapConfigForController implements BootstrapConfigGetter. 258 func (c *StubStore) BootstrapConfigForController(controllerName string) (*jujuclient.BootstrapConfig, error) { 259 c.MethodCall(c, "BootstrapConfigForController", controllerName) 260 return c.BootstrapConfigForControllerFunc(controllerName) 261 } 262 263 // UpdateBootstrapConfig implements BootstrapConfigUpdater. 264 func (c *StubStore) UpdateBootstrapConfig(controllerName string, cfg jujuclient.BootstrapConfig) error { 265 c.MethodCall(c, "UpdateBootstrapConfig", controllerName, cfg) 266 return c.UpdateBootstrapConfigFunc(controllerName, cfg) 267 }