github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/jujuc/util_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc_test 5 6 import ( 7 "bytes" 8 "fmt" 9 "io" 10 "sort" 11 stdtesting "testing" 12 13 "github.com/juju/utils/set" 14 gc "launchpad.net/gocheck" 15 16 "github.com/juju/juju/charm" 17 "github.com/juju/juju/state" 18 "github.com/juju/juju/state/api/params" 19 "github.com/juju/juju/testing" 20 "github.com/juju/juju/worker/uniter/jujuc" 21 ) 22 23 func TestPackage(t *stdtesting.T) { gc.TestingT(t) } 24 25 func bufferBytes(stream io.Writer) []byte { 26 return stream.(*bytes.Buffer).Bytes() 27 } 28 29 func bufferString(w io.Writer) string { 30 return w.(*bytes.Buffer).String() 31 } 32 33 type ContextSuite struct { 34 testing.BaseSuite 35 rels map[int]*ContextRelation 36 } 37 38 func (s *ContextSuite) SetUpTest(c *gc.C) { 39 s.BaseSuite.SetUpTest(c) 40 s.rels = map[int]*ContextRelation{ 41 0: { 42 id: 0, 43 name: "peer0", 44 units: map[string]Settings{ 45 "u/0": {"private-address": "u-0.testing.invalid"}, 46 }, 47 }, 48 1: { 49 id: 1, 50 name: "peer1", 51 units: map[string]Settings{ 52 "u/0": {"private-address": "u-0.testing.invalid"}, 53 }, 54 }, 55 } 56 } 57 58 func (s *ContextSuite) GetHookContext(c *gc.C, relid int, remote string) *Context { 59 if relid != -1 { 60 _, found := s.rels[relid] 61 c.Assert(found, gc.Equals, true) 62 } 63 return &Context{ 64 relid: relid, 65 remote: remote, 66 rels: s.rels, 67 } 68 } 69 70 func setSettings(c *gc.C, ru *state.RelationUnit, settings map[string]interface{}) { 71 node, err := ru.Settings() 72 c.Assert(err, gc.IsNil) 73 for _, k := range node.Keys() { 74 node.Delete(k) 75 } 76 node.Update(settings) 77 _, err = node.Write() 78 c.Assert(err, gc.IsNil) 79 } 80 81 type Context struct { 82 ports set.Strings 83 relid int 84 remote string 85 rels map[int]*ContextRelation 86 } 87 88 func (c *Context) UnitName() string { 89 return "u/0" 90 } 91 92 func (c *Context) PublicAddress() (string, bool) { 93 return "gimli.minecraft.testing.invalid", true 94 } 95 96 func (c *Context) PrivateAddress() (string, bool) { 97 return "192.168.0.99", true 98 } 99 100 func (c *Context) OpenPort(protocol string, port int) error { 101 c.ports.Add(fmt.Sprintf("%d/%s", port, protocol)) 102 return nil 103 } 104 105 func (c *Context) ClosePort(protocol string, port int) error { 106 c.ports.Remove(fmt.Sprintf("%d/%s", port, protocol)) 107 return nil 108 } 109 110 func (c *Context) ConfigSettings() (charm.Settings, error) { 111 return charm.Settings{ 112 "empty": nil, 113 "monsters": false, 114 "spline-reticulation": 45.0, 115 "title": "My Title", 116 "username": "admin001", 117 }, nil 118 } 119 120 func (c *Context) HookRelation() (jujuc.ContextRelation, bool) { 121 return c.Relation(c.relid) 122 } 123 124 func (c *Context) RemoteUnitName() (string, bool) { 125 return c.remote, c.remote != "" 126 } 127 128 func (c *Context) Relation(id int) (jujuc.ContextRelation, bool) { 129 r, found := c.rels[id] 130 return r, found 131 } 132 133 func (c *Context) RelationIds() []int { 134 ids := []int{} 135 for id := range c.rels { 136 ids = append(ids, id) 137 } 138 return ids 139 } 140 141 func (c *Context) OwnerTag() string { 142 return "test-owner" 143 } 144 145 type ContextRelation struct { 146 id int 147 name string 148 units map[string]Settings 149 } 150 151 func (r *ContextRelation) Id() int { 152 return r.id 153 } 154 155 func (r *ContextRelation) Name() string { 156 return r.name 157 } 158 159 func (r *ContextRelation) FakeId() string { 160 return fmt.Sprintf("%s:%d", r.name, r.id) 161 } 162 163 func (r *ContextRelation) Settings() (jujuc.Settings, error) { 164 return r.units["u/0"], nil 165 } 166 167 func (r *ContextRelation) UnitNames() []string { 168 var s []string // initially nil to match the true context. 169 for name := range r.units { 170 s = append(s, name) 171 } 172 sort.Strings(s) 173 return s 174 } 175 176 func (r *ContextRelation) ReadSettings(name string) (params.RelationSettings, error) { 177 s, found := r.units[name] 178 if !found { 179 return nil, fmt.Errorf("unknown unit %s", name) 180 } 181 return s.Map(), nil 182 } 183 184 type Settings params.RelationSettings 185 186 func (s Settings) Get(k string) (interface{}, bool) { 187 v, f := s[k] 188 return v, f 189 } 190 191 func (s Settings) Set(k, v string) { 192 s[k] = v 193 } 194 195 func (s Settings) Delete(k string) { 196 delete(s, k) 197 } 198 199 func (s Settings) Map() params.RelationSettings { 200 r := params.RelationSettings{} 201 for k, v := range s { 202 r[k] = v 203 } 204 return r 205 }