github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/jujuctesting/relation.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuctesting 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/core/relation" 14 "github.com/juju/juju/worker/uniter/runner/jujuc" 15 ) 16 17 // Relation holds the data for the test double. 18 type Relation struct { 19 // Id is data for jujuc.ContextRelation. 20 Id int 21 // Name is data for jujuc.ContextRelation. 22 Name string 23 // Units is data for jujuc.ContextRelation. 24 Units map[string]Settings 25 // UnitName is data for jujuc.ContextRelation. 26 UnitName string 27 } 28 29 // Reset clears the Relation's settings. 30 func (r *Relation) Reset() { 31 r.Units = nil 32 } 33 34 // SetRelated adds the relation settings for the unit. 35 func (r *Relation) SetRelated(name string, settings Settings) { 36 if r.Units == nil { 37 r.Units = make(map[string]Settings) 38 } 39 r.Units[name] = settings 40 } 41 42 // ContextRelation is a test double for jujuc.ContextRelation. 43 type ContextRelation struct { 44 contextBase 45 info *Relation 46 } 47 48 // Id implements jujuc.ContextRelation. 49 func (r *ContextRelation) Id() int { 50 r.stub.AddCall("Id") 51 r.stub.NextErr() 52 53 return r.info.Id 54 } 55 56 // Name implements jujuc.ContextRelation. 57 func (r *ContextRelation) Name() string { 58 r.stub.AddCall("Name") 59 r.stub.NextErr() 60 61 return r.info.Name 62 } 63 64 // FakeId implements jujuc.ContextRelation. 65 func (r *ContextRelation) FakeId() string { 66 r.stub.AddCall("FakeId") 67 r.stub.NextErr() 68 69 return fmt.Sprintf("%s:%d", r.info.Name, r.info.Id) 70 } 71 72 // Settings implements jujuc.ContextRelation. 73 func (r *ContextRelation) Settings() (jujuc.Settings, error) { 74 r.stub.AddCall("Settings") 75 if err := r.stub.NextErr(); err != nil { 76 return nil, errors.Trace(err) 77 } 78 79 settings, ok := r.info.Units[r.info.UnitName] 80 if !ok { 81 return nil, errors.Errorf("no settings for %q", r.info.UnitName) 82 } 83 return settings, nil 84 } 85 86 // UnitNames implements jujuc.ContextRelation. 87 func (r *ContextRelation) UnitNames() []string { 88 r.stub.AddCall("UnitNames") 89 r.stub.NextErr() 90 91 var s []string // initially nil to match the true context. 92 for name := range r.info.Units { 93 s = append(s, name) 94 } 95 sort.Strings(s) 96 return s 97 } 98 99 // ReadSettings implements jujuc.ContextRelation. 100 func (r *ContextRelation) ReadSettings(name string) (params.Settings, error) { 101 r.stub.AddCall("ReadSettings", name) 102 if err := r.stub.NextErr(); err != nil { 103 return nil, errors.Trace(err) 104 } 105 106 s, found := r.info.Units[name] 107 if !found { 108 return nil, fmt.Errorf("unknown unit %s", name) 109 } 110 return s.Map(), nil 111 } 112 113 // Suspended implements jujuc.ContextRelation. 114 func (r *ContextRelation) Suspended() bool { 115 return true 116 } 117 118 // SetStatus implements jujuc.ContextRelation. 119 func (r *ContextRelation) SetStatus(status relation.Status) error { 120 return nil 121 }