github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/runner/jujuc/util_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Copyright 2014 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package jujuc_test 6 7 import ( 8 "bytes" 9 "fmt" 10 "io" 11 "sort" 12 "time" 13 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 "gopkg.in/juju/charm.v4" 17 18 "github.com/juju/juju/apiserver/params" 19 "github.com/juju/juju/network" 20 "github.com/juju/juju/state" 21 "github.com/juju/juju/storage" 22 "github.com/juju/juju/testing" 23 "github.com/juju/juju/worker/uniter/runner/jujuc" 24 ) 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 testing.BaseSuite 36 rels map[int]*ContextRelation 37 } 38 39 func (s *ContextSuite) SetUpTest(c *gc.C) { 40 s.BaseSuite.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, jc.IsTrue) 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, jc.ErrorIsNil) 74 for _, k := range node.Keys() { 75 node.Delete(k) 76 } 77 node.Update(settings) 78 _, err = node.Write() 79 c.Assert(err, jc.ErrorIsNil) 80 } 81 82 type Context struct { 83 ports []network.PortRange 84 relid int 85 remote string 86 rels map[int]*ContextRelation 87 metrics []jujuc.Metric 88 canAddMetrics bool 89 rebootPriority jujuc.RebootPriority 90 shouldError bool 91 } 92 93 func (c *Context) AddMetric(key, value string, created time.Time) error { 94 if !c.canAddMetrics { 95 return fmt.Errorf("metrics disabled") 96 } 97 c.metrics = append(c.metrics, jujuc.Metric{key, value, created}) 98 return nil 99 } 100 101 func (c *Context) UnitName() string { 102 return "u/0" 103 } 104 105 func (c *Context) PublicAddress() (string, bool) { 106 return "gimli.minecraft.testing.invalid", true 107 } 108 109 func (c *Context) PrivateAddress() (string, bool) { 110 return "192.168.0.99", true 111 } 112 113 func (c *Context) AvailabilityZone() (string, bool) { 114 return "us-east-1a", true 115 } 116 117 func (c *Context) StorageInstance(storageId string) (*storage.StorageInstance, bool) { 118 return &storage.StorageInstance{ 119 "1234", 120 storage.StorageKindBlock, 121 "/dev/sda", 122 }, true 123 } 124 125 func (c *Context) HookStorageInstance() (*storage.StorageInstance, bool) { 126 return &storage.StorageInstance{ 127 "1234", 128 storage.StorageKindBlock, 129 "/dev/sda", 130 }, true 131 } 132 133 func (c *Context) OpenPorts(protocol string, fromPort, toPort int) error { 134 c.ports = append(c.ports, network.PortRange{ 135 Protocol: protocol, 136 FromPort: fromPort, 137 ToPort: toPort, 138 }) 139 network.SortPortRanges(c.ports) 140 return nil 141 } 142 143 func (c *Context) ClosePorts(protocol string, fromPort, toPort int) error { 144 portRange := network.PortRange{ 145 Protocol: protocol, 146 FromPort: fromPort, 147 ToPort: toPort, 148 } 149 for i, port := range c.ports { 150 if port == portRange { 151 c.ports = append(c.ports[:i], c.ports[i+1:]...) 152 break 153 } 154 } 155 network.SortPortRanges(c.ports) 156 return nil 157 } 158 159 func (c *Context) OpenedPorts() []network.PortRange { 160 return c.ports 161 } 162 163 func (c *Context) ConfigSettings() (charm.Settings, error) { 164 return charm.Settings{ 165 "empty": nil, 166 "monsters": false, 167 "spline-reticulation": 45.0, 168 "title": "My Title", 169 "username": "admin001", 170 }, nil 171 } 172 173 func (c *Context) ActionParams() (map[string]interface{}, error) { 174 return nil, fmt.Errorf("not running an action") 175 } 176 177 func (c *Context) UpdateActionResults(keys []string, value string) error { 178 return fmt.Errorf("not running an action") 179 } 180 181 func (c *Context) SetActionFailed() error { 182 return fmt.Errorf("not running an action") 183 } 184 185 func (c *Context) SetActionMessage(message string) error { 186 return fmt.Errorf("not running an action") 187 } 188 189 func (c *Context) HookRelation() (jujuc.ContextRelation, bool) { 190 return c.Relation(c.relid) 191 } 192 193 func (c *Context) RemoteUnitName() (string, bool) { 194 return c.remote, c.remote != "" 195 } 196 197 func (c *Context) Relation(id int) (jujuc.ContextRelation, bool) { 198 r, found := c.rels[id] 199 return r, found 200 } 201 202 func (c *Context) RelationIds() []int { 203 ids := []int{} 204 for id := range c.rels { 205 ids = append(ids, id) 206 } 207 return ids 208 } 209 210 func (c *Context) OwnerTag() string { 211 return "test-owner" 212 } 213 214 type ContextRelation struct { 215 id int 216 name string 217 units map[string]Settings 218 } 219 220 func (r *ContextRelation) Id() int { 221 return r.id 222 } 223 224 func (r *ContextRelation) Name() string { 225 return r.name 226 } 227 228 func (r *ContextRelation) FakeId() string { 229 return fmt.Sprintf("%s:%d", r.name, r.id) 230 } 231 232 func (r *ContextRelation) Settings() (jujuc.Settings, error) { 233 return r.units["u/0"], nil 234 } 235 236 func (r *ContextRelation) UnitNames() []string { 237 var s []string // initially nil to match the true context. 238 for name := range r.units { 239 s = append(s, name) 240 } 241 sort.Strings(s) 242 return s 243 } 244 245 func (r *ContextRelation) ReadSettings(name string) (params.Settings, error) { 246 s, found := r.units[name] 247 if !found { 248 return nil, fmt.Errorf("unknown unit %s", name) 249 } 250 return s.Map(), nil 251 } 252 253 type Settings params.Settings 254 255 func (s Settings) Get(k string) (interface{}, bool) { 256 v, f := s[k] 257 return v, f 258 } 259 260 func (s Settings) Set(k, v string) { 261 s[k] = v 262 } 263 264 func (s Settings) Delete(k string) { 265 delete(s, k) 266 } 267 268 func (s Settings) Map() params.Settings { 269 r := params.Settings{} 270 for k, v := range s { 271 r[k] = v 272 } 273 return r 274 } 275 276 func (c *Context) RequestReboot(priority jujuc.RebootPriority) error { 277 c.rebootPriority = priority 278 if c.shouldError { 279 return fmt.Errorf("RequestReboot error!") 280 } else { 281 return nil 282 } 283 } 284 285 func cmdString(cmd string) string { 286 return cmd + jujuc.CmdSuffix 287 }