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