github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/remoterelations/mock_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package remoterelations_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "github.com/juju/testing" 11 "gopkg.in/juju/charm.v6" 12 "gopkg.in/juju/names.v2" 13 "gopkg.in/macaroon.v2-unstable" 14 "gopkg.in/tomb.v2" 15 16 common "github.com/juju/juju/apiserver/common/crossmodel" 17 "github.com/juju/juju/apiserver/facades/controller/remoterelations" 18 "github.com/juju/juju/apiserver/params" 19 "github.com/juju/juju/controller" 20 "github.com/juju/juju/core/crossmodel" 21 "github.com/juju/juju/core/status" 22 "github.com/juju/juju/state" 23 coretesting "github.com/juju/juju/testing" 24 ) 25 26 type mockState struct { 27 remoterelations.RemoteRelationsState 28 testing.Stub 29 relations map[string]*mockRelation 30 remoteApplications map[string]*mockRemoteApplication 31 applications map[string]*mockApplication 32 remoteApplicationsWatcher *mockStringsWatcher 33 remoteRelationsWatcher *mockStringsWatcher 34 applicationRelationsWatchers map[string]*mockStringsWatcher 35 remoteEntities map[names.Tag]string 36 controllerInfo map[string]*mockControllerInfo 37 } 38 39 func newMockState() *mockState { 40 return &mockState{ 41 relations: make(map[string]*mockRelation), 42 remoteApplications: make(map[string]*mockRemoteApplication), 43 applications: make(map[string]*mockApplication), 44 remoteApplicationsWatcher: newMockStringsWatcher(), 45 remoteRelationsWatcher: newMockStringsWatcher(), 46 applicationRelationsWatchers: make(map[string]*mockStringsWatcher), 47 remoteEntities: make(map[names.Tag]string), 48 controllerInfo: make(map[string]*mockControllerInfo), 49 } 50 } 51 52 func (st *mockState) ControllerConfig() (controller.Config, error) { 53 return nil, errors.NotImplementedf("ControllerConfig") 54 } 55 56 func (st *mockState) ControllerInfo(modelUUID string) ([]string, string, error) { 57 if info, ok := st.controllerInfo[modelUUID]; !ok { 58 return nil, "", errors.NotFoundf("controller info for %v", modelUUID) 59 } else { 60 return info.ControllerInfo().Addrs, info.ControllerInfo().CACert, nil 61 } 62 } 63 64 func (st *mockState) ModelUUID() string { 65 return coretesting.ModelTag.Id() 66 } 67 68 func (st *mockState) AddRelation(eps ...state.Endpoint) (common.Relation, error) { 69 rel := &mockRelation{ 70 key: fmt.Sprintf("%v:%v %v:%v", eps[0].ApplicationName, eps[0].Name, eps[1].ApplicationName, eps[1].Name)} 71 st.relations[rel.key] = rel 72 return rel, nil 73 } 74 75 func (st *mockState) ImportRemoteEntity(entity names.Tag, token string) error { 76 st.MethodCall(st, "ImportRemoteEntity", entity, token) 77 if err := st.NextErr(); err != nil { 78 return err 79 } 80 if _, ok := st.remoteEntities[entity]; ok { 81 return errors.AlreadyExistsf(entity.Id()) 82 } 83 st.remoteEntities[entity] = token 84 return nil 85 } 86 87 func (st *mockState) RemoveRemoteEntity(entity names.Tag) error { 88 st.MethodCall(st, "RemoveRemoteEntity", entity) 89 if err := st.NextErr(); err != nil { 90 return err 91 } 92 delete(st.remoteEntities, entity) 93 return nil 94 } 95 96 func (st *mockState) ExportLocalEntity(entity names.Tag) (string, error) { 97 st.MethodCall(st, "ExportLocalEntity", entity) 98 if err := st.NextErr(); err != nil { 99 return "", err 100 } 101 if token, ok := st.remoteEntities[entity]; ok { 102 return token, errors.AlreadyExistsf(entity.Id()) 103 } 104 token := "token-" + entity.Id() 105 st.remoteEntities[entity] = token 106 return token, nil 107 } 108 109 func (st *mockState) GetRemoteEntity(token string) (names.Tag, error) { 110 st.MethodCall(st, "GetRemoteEntity", token) 111 if err := st.NextErr(); err != nil { 112 return nil, err 113 } 114 for e, t := range st.remoteEntities { 115 if t == token { 116 return e, nil 117 } 118 } 119 return nil, errors.NotFoundf("token %v", token) 120 } 121 122 func (st *mockState) GetToken(entity names.Tag) (string, error) { 123 st.MethodCall(st, "GetToken", entity) 124 if err := st.NextErr(); err != nil { 125 return "", err 126 } 127 return "token-" + entity.String(), nil 128 } 129 130 func (st *mockState) SaveMacaroon(entity names.Tag, mac *macaroon.Macaroon) error { 131 st.MethodCall(st, "SaveMacaroon", entity, mac.Id()) 132 return st.NextErr() 133 } 134 135 func (st *mockState) KeyRelation(key string) (common.Relation, error) { 136 st.MethodCall(st, "KeyRelation", key) 137 if err := st.NextErr(); err != nil { 138 return nil, err 139 } 140 r, ok := st.relations[key] 141 if !ok { 142 return nil, errors.NotFoundf("relation %q", key) 143 } 144 return r, nil 145 } 146 147 func (st *mockState) Relation(id int) (common.Relation, error) { 148 st.MethodCall(st, "Relation", id) 149 if err := st.NextErr(); err != nil { 150 return nil, err 151 } 152 for _, r := range st.relations { 153 if r.id == id { 154 return r, nil 155 } 156 } 157 return nil, errors.NotFoundf("relation %d", id) 158 } 159 160 func (st *mockState) RemoteApplication(id string) (common.RemoteApplication, error) { 161 st.MethodCall(st, "RemoteApplication", id) 162 if err := st.NextErr(); err != nil { 163 return nil, err 164 } 165 a, ok := st.remoteApplications[id] 166 if !ok { 167 return nil, errors.NotFoundf("remote application %q", id) 168 } 169 return a, nil 170 } 171 172 func (st *mockState) Application(id string) (common.Application, error) { 173 st.MethodCall(st, "Application", id) 174 if err := st.NextErr(); err != nil { 175 return nil, err 176 } 177 a, ok := st.applications[id] 178 if !ok { 179 return nil, errors.NotFoundf("application %q", id) 180 } 181 return a, nil 182 } 183 184 func (st *mockState) WatchRemoteApplications() state.StringsWatcher { 185 st.MethodCall(st, "WatchRemoteApplications") 186 return st.remoteApplicationsWatcher 187 } 188 189 func (st *mockState) WatchRemoteApplicationRelations(applicationName string) (state.StringsWatcher, error) { 190 st.MethodCall(st, "WatchRemoteApplicationRelations", applicationName) 191 if err := st.NextErr(); err != nil { 192 return nil, err 193 } 194 w, ok := st.applicationRelationsWatchers[applicationName] 195 if !ok { 196 return nil, errors.NotFoundf("application %q", applicationName) 197 } 198 return w, nil 199 } 200 201 func (st *mockState) WatchRemoteRelations() state.StringsWatcher { 202 st.MethodCall(st, "WatchRemoteRelations") 203 return st.remoteRelationsWatcher 204 } 205 206 type mockControllerInfo struct { 207 uuid string 208 info crossmodel.ControllerInfo 209 } 210 211 func (c *mockControllerInfo) Id() string { 212 return c.uuid 213 } 214 215 func (c *mockControllerInfo) ControllerInfo() crossmodel.ControllerInfo { 216 return c.info 217 } 218 219 type mockRelation struct { 220 common.Relation 221 testing.Stub 222 id int 223 key string 224 life state.Life 225 suspended bool 226 units map[string]common.RelationUnit 227 remoteUnits map[string]common.RelationUnit 228 endpoints []state.Endpoint 229 endpointUnitsWatchers map[string]*mockRelationUnitsWatcher 230 } 231 232 func newMockRelation(id int) *mockRelation { 233 return &mockRelation{ 234 id: id, 235 life: state.Alive, 236 units: make(map[string]common.RelationUnit), 237 remoteUnits: make(map[string]common.RelationUnit), 238 endpointUnitsWatchers: make(map[string]*mockRelationUnitsWatcher), 239 } 240 } 241 242 func (r *mockRelation) Id() int { 243 r.MethodCall(r, "Id") 244 return r.id 245 } 246 247 func (r *mockRelation) Tag() names.Tag { 248 r.MethodCall(r, "Tag") 249 return names.NewRelationTag(r.key) 250 } 251 252 func (r *mockRelation) Life() state.Life { 253 r.MethodCall(r, "Life") 254 return r.life 255 } 256 257 func (r *mockRelation) Suspended() bool { 258 r.MethodCall(r, "Suspended") 259 return r.suspended 260 } 261 262 func (r *mockRelation) Unit(unitId string) (common.RelationUnit, error) { 263 r.MethodCall(r, "Unit", unitId) 264 if err := r.NextErr(); err != nil { 265 return nil, err 266 } 267 u, ok := r.units[unitId] 268 if !ok { 269 return nil, errors.NotFoundf("unit %q", unitId) 270 } 271 return u, nil 272 } 273 274 func (r *mockRelation) RemoteUnit(unitId string) (common.RelationUnit, error) { 275 r.MethodCall(r, "RemoteUnit", unitId) 276 if err := r.NextErr(); err != nil { 277 return nil, err 278 } 279 u, ok := r.remoteUnits[unitId] 280 if !ok { 281 return nil, errors.NotFoundf("remote unit %q", unitId) 282 } 283 return u, nil 284 } 285 286 func (r *mockRelation) Endpoints() []state.Endpoint { 287 r.MethodCall(r, "Endpoints") 288 return r.endpoints 289 } 290 291 func (r *mockRelation) WatchUnits(applicationName string) (state.RelationUnitsWatcher, error) { 292 r.MethodCall(r, "WatchUnits", applicationName) 293 if err := r.NextErr(); err != nil { 294 return nil, err 295 } 296 w, ok := r.endpointUnitsWatchers[applicationName] 297 if !ok { 298 return nil, errors.NotFoundf("application %q", applicationName) 299 } 300 return w, nil 301 } 302 303 type mockRemoteApplication struct { 304 common.RemoteApplication 305 testing.Stub 306 name string 307 alias string 308 url string 309 life state.Life 310 status status.Status 311 message string 312 eps []charm.Relation 313 consumerproxy bool 314 } 315 316 func newMockRemoteApplication(name, url string) *mockRemoteApplication { 317 return &mockRemoteApplication{ 318 name: name, alias: name + "-alias", url: url, life: state.Alive, 319 } 320 } 321 322 func (r *mockRemoteApplication) Name() string { 323 r.MethodCall(r, "Name") 324 return r.name 325 } 326 327 func (r *mockRemoteApplication) OfferUUID() string { 328 r.MethodCall(r, "OfferUUID") 329 return r.name + "-uuid" 330 } 331 332 func (r *mockRemoteApplication) Tag() names.Tag { 333 r.MethodCall(r, "Tag") 334 return names.NewApplicationTag(r.name) 335 } 336 337 func (r *mockRemoteApplication) IsConsumerProxy() bool { 338 r.MethodCall(r, "IsConsumerProxy") 339 return r.consumerproxy 340 } 341 342 func (r *mockRemoteApplication) Life() state.Life { 343 r.MethodCall(r, "Life") 344 return r.life 345 } 346 347 func (r *mockRemoteApplication) Status() (status.StatusInfo, error) { 348 r.MethodCall(r, "Status") 349 return status.StatusInfo{Status: r.status}, nil 350 } 351 352 func (r *mockRemoteApplication) URL() (string, bool) { 353 r.MethodCall(r, "URL") 354 return r.url, r.url != "" 355 } 356 357 func (r *mockRemoteApplication) SourceModel() names.ModelTag { 358 r.MethodCall(r, "SourceModel") 359 return names.NewModelTag("model-uuid") 360 } 361 362 func (r *mockRemoteApplication) Macaroon() (*macaroon.Macaroon, error) { 363 r.MethodCall(r, "Macaroon") 364 return macaroon.New(nil, []byte("test"), "") 365 } 366 367 func (r *mockRemoteApplication) SetStatus(info status.StatusInfo) error { 368 r.MethodCall(r, "SetStatus") 369 r.status = info.Status 370 r.message = info.Message 371 return nil 372 } 373 374 type mockApplication struct { 375 common.Application 376 testing.Stub 377 name string 378 life state.Life 379 } 380 381 func newMockApplication(name string) *mockApplication { 382 return &mockApplication{ 383 name: name, 384 } 385 } 386 387 func (a *mockApplication) Name() string { 388 a.MethodCall(a, "Name") 389 return a.name 390 } 391 392 func (a *mockApplication) Tag() names.Tag { 393 a.MethodCall(a, "Tag") 394 return names.NewApplicationTag(a.name) 395 } 396 397 func (a *mockApplication) Life() state.Life { 398 a.MethodCall(a, "Life") 399 return a.life 400 } 401 402 type mockWatcher struct { 403 testing.Stub 404 tomb.Tomb 405 } 406 407 func (w *mockWatcher) Kill() { 408 w.MethodCall(w, "Kill") 409 w.Tomb.Kill(nil) 410 } 411 412 func (w *mockWatcher) Stop() error { 413 w.MethodCall(w, "Stop") 414 if err := w.NextErr(); err != nil { 415 return err 416 } 417 w.Tomb.Kill(nil) 418 return w.Tomb.Wait() 419 } 420 421 type mockStringsWatcher struct { 422 mockWatcher 423 changes chan []string 424 } 425 426 func newMockStringsWatcher() *mockStringsWatcher { 427 w := &mockStringsWatcher{changes: make(chan []string, 1)} 428 w.Tomb.Go(func() error { 429 <-w.Tomb.Dying() 430 return nil 431 }) 432 return w 433 } 434 435 func (w *mockStringsWatcher) Changes() <-chan []string { 436 w.MethodCall(w, "Changes") 437 return w.changes 438 } 439 440 type mockRelationUnitsWatcher struct { 441 mockWatcher 442 changes chan params.RelationUnitsChange 443 } 444 445 func newMockRelationUnitsWatcher() *mockRelationUnitsWatcher { 446 w := &mockRelationUnitsWatcher{ 447 changes: make(chan params.RelationUnitsChange, 1), 448 } 449 w.Tomb.Go(func() error { 450 <-w.Tomb.Dying() 451 return nil 452 }) 453 return w 454 } 455 456 func (w *mockRelationUnitsWatcher) Changes() <-chan params.RelationUnitsChange { 457 w.MethodCall(w, "Changes") 458 return w.changes 459 } 460 461 type mockRelationUnit struct { 462 common.RelationUnit 463 testing.Stub 464 inScope bool 465 settings map[string]interface{} 466 } 467 468 func newMockRelationUnit() *mockRelationUnit { 469 return &mockRelationUnit{ 470 settings: make(map[string]interface{}), 471 } 472 } 473 474 func (u *mockRelationUnit) Settings() (map[string]interface{}, error) { 475 u.MethodCall(u, "Settings") 476 return u.settings, u.NextErr() 477 } 478 479 func (u *mockRelationUnit) InScope() (bool, error) { 480 u.MethodCall(u, "InScope") 481 return u.inScope, u.NextErr() 482 } 483 484 func (u *mockRelationUnit) LeaveScope() error { 485 u.MethodCall(u, "LeaveScope") 486 if err := u.NextErr(); err != nil { 487 return err 488 } 489 u.inScope = false 490 return nil 491 } 492 493 func (u *mockRelationUnit) EnterScope(settings map[string]interface{}) error { 494 u.MethodCall(u, "EnterScope", settings) 495 if err := u.NextErr(); err != nil { 496 return err 497 } 498 u.inScope = true 499 u.settings = make(map[string]interface{}) 500 for k, v := range settings { 501 u.settings[k] = v 502 } 503 return nil 504 }