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