github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/testing/relation.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 "sort" 9 10 "github.com/juju/errors" 11 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/worker/uniter/runner/jujuc" 14 ) 15 16 // Relation holds the data for the test double. 17 type Relation struct { 18 // Id is data for jujuc.ContextRelation. 19 Id int 20 // Name is data for jujuc.ContextRelation. 21 Name string 22 // Units is data for jujuc.ContextRelation. 23 Units map[string]Settings 24 // UnitName is data for jujuc.ContextRelation. 25 UnitName string 26 } 27 28 // Reset clears the Relation's settings. 29 func (r *Relation) Reset() { 30 r.Units = nil 31 } 32 33 // SetRelated adds the relation settings for the unit. 34 func (r *Relation) SetRelated(name string, settings Settings) { 35 if r.Units == nil { 36 r.Units = make(map[string]Settings) 37 } 38 r.Units[name] = settings 39 } 40 41 // ContextRelation is a test double for jujuc.ContextRelation. 42 type ContextRelation struct { 43 contextBase 44 info *Relation 45 } 46 47 // Id implements jujuc.ContextRelation. 48 func (r *ContextRelation) Id() int { 49 r.stub.AddCall("Id") 50 r.stub.NextErr() 51 52 return r.info.Id 53 } 54 55 // Name implements jujuc.ContextRelation. 56 func (r *ContextRelation) Name() string { 57 r.stub.AddCall("Name") 58 r.stub.NextErr() 59 60 return r.info.Name 61 } 62 63 // FakeId implements jujuc.ContextRelation. 64 func (r *ContextRelation) FakeId() string { 65 r.stub.AddCall("FakeId") 66 r.stub.NextErr() 67 68 return fmt.Sprintf("%s:%d", r.info.Name, r.info.Id) 69 } 70 71 // Settings implements jujuc.ContextRelation. 72 func (r *ContextRelation) Settings() (jujuc.Settings, error) { 73 r.stub.AddCall("Settings") 74 if err := r.stub.NextErr(); err != nil { 75 return nil, errors.Trace(err) 76 } 77 78 settings, ok := r.info.Units[r.info.UnitName] 79 if !ok { 80 return nil, errors.Errorf("no settings for %q", r.info.UnitName) 81 } 82 return settings, nil 83 } 84 85 // UnitNames implements jujuc.ContextRelation. 86 func (r *ContextRelation) UnitNames() []string { 87 r.stub.AddCall("UnitNames") 88 r.stub.NextErr() 89 90 var s []string // initially nil to match the true context. 91 for name := range r.info.Units { 92 s = append(s, name) 93 } 94 sort.Strings(s) 95 return s 96 } 97 98 // ReadSettings implements jujuc.ContextRelation. 99 func (r *ContextRelation) ReadSettings(name string) (params.Settings, error) { 100 r.stub.AddCall("ReadSettings", name) 101 if err := r.stub.NextErr(); err != nil { 102 return nil, errors.Trace(err) 103 } 104 105 s, found := r.info.Units[name] 106 if !found { 107 return nil, fmt.Errorf("unknown unit %s", name) 108 } 109 return s.Map(), nil 110 }