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