github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/runner/jujuc/testing/relations.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 9 "github.com/juju/testing" 10 11 "github.com/juju/juju/worker/uniter/runner/jujuc" 12 ) 13 14 // Relations holds the values for the hook context. 15 type Relations struct { 16 Relations map[int]jujuc.ContextRelation 17 } 18 19 // Reset clears the Relations data. 20 func (r *Relations) Reset() { 21 r.Relations = nil 22 } 23 24 // SetRelation adds the relation to the set of known relations. 25 func (r *Relations) SetRelation(id int, relCtx jujuc.ContextRelation) { 26 if r.Relations == nil { 27 r.Relations = make(map[int]jujuc.ContextRelation) 28 } 29 r.Relations[id] = relCtx 30 } 31 32 // SetNewRelation adds the relation to the set of known relations. 33 func (r *Relations) SetNewRelation(id int, name string, stub *testing.Stub) *Relation { 34 if name == "" { 35 name = fmt.Sprintf("relation-%d", id) 36 } 37 rel := &Relation{ 38 Id: id, 39 Name: name, 40 } 41 relCtx := &ContextRelation{info: rel} 42 relCtx.stub = stub 43 44 r.SetRelation(id, relCtx) 45 return rel 46 } 47 48 // SetRelated adds the provided unit information to the relation. 49 func (r *Relations) SetRelated(id int, unit string, settings Settings) { 50 relation := r.Relations[id].(*ContextRelation).info 51 relation.SetRelated(unit, settings) 52 } 53 54 // ContextRelations is a test double for jujuc.ContextRelations. 55 type ContextRelations struct { 56 contextBase 57 info *Relations 58 } 59 60 // Relation implements jujuc.ContextRelations. 61 func (c *ContextRelations) Relation(id int) (jujuc.ContextRelation, bool) { 62 c.stub.AddCall("Relation", id) 63 c.stub.NextErr() 64 65 r, found := c.info.Relations[id] 66 return r, found 67 } 68 69 // RelationIds implements jujuc.ContextRelations. 70 func (c *ContextRelations) RelationIds() []int { 71 c.stub.AddCall("RelationIds") 72 c.stub.NextErr() 73 74 ids := []int{} 75 for id := range c.info.Relations { 76 ids = append(ids, id) 77 } 78 return ids 79 }