OLD | NEW |
(Empty) | |
| 1 package server_test |
| 2 |
| 3 import ( |
| 4 "fmt" |
| 5 . "launchpad.net/gocheck" |
| 6 "launchpad.net/juju-core/cmd" |
| 7 "launchpad.net/juju-core/cmd/jujuc/server" |
| 8 "launchpad.net/juju-core/state" |
| 9 ) |
| 10 |
| 11 type RelationSetSuite struct { |
| 12 HookContextSuite |
| 13 } |
| 14 |
| 15 var _ = Suite(&RelationSetSuite{}) |
| 16 |
| 17 var helpTests = []struct { |
| 18 relid int |
| 19 expect string |
| 20 }{{-1, ""}, {0, "peer0:0"}} |
| 21 |
| 22 func (s *RelationSetSuite) TestHelp(c *C) { |
| 23 for i, t := range helpTests { |
| 24 c.Logf("test %d", i) |
| 25 hctx := s.GetHookContext(c, t.relid, "") |
| 26 com, err := hctx.NewCommand("relation-set") |
| 27 c.Assert(err, IsNil) |
| 28 ctx := dummyContext(c) |
| 29 code := cmd.Main(com, ctx, []string{"--help"}) |
| 30 c.Assert(code, Equals, 0) |
| 31 c.Assert(bufferString(ctx.Stdout), Equals, "") |
| 32 c.Assert(bufferString(ctx.Stderr), Equals, fmt.Sprintf(` |
| 33 usage: relation-set [options] key=value [key=value ...] |
| 34 purpose: set relation settings |
| 35 |
| 36 options: |
| 37 -r (= "%s") |
| 38 relation id |
| 39 `[1:], t.expect)) |
| 40 } |
| 41 } |
| 42 |
| 43 var relationSetInitTests = []struct { |
| 44 ctxrelid int |
| 45 args []string |
| 46 err string |
| 47 relid int |
| 48 settings map[string]string |
| 49 }{ |
| 50 { |
| 51 ctxrelid: -1, |
| 52 err: `no relation specified`, |
| 53 }, { |
| 54 ctxrelid: 1, |
| 55 err: `expected "key=value" parameters, got nothing`, |
| 56 }, { |
| 57 ctxrelid: -1, |
| 58 args: []string{"-r", "one"}, |
| 59 err: `invalid relation id "one"`, |
| 60 }, { |
| 61 ctxrelid: 1, |
| 62 args: []string{"-r", "one"}, |
| 63 err: `invalid relation id "one"`, |
| 64 }, |
| 65 { |
| 66 ctxrelid: -1, |
| 67 args: []string{"-r", "ignored:one"}, |
| 68 err: `invalid relation id "ignored:one"`, |
| 69 }, { |
| 70 ctxrelid: 1, |
| 71 args: []string{"-r", "ignored:one"}, err: `invalid relation
id "ignored:one"`, |
| 72 }, { |
| 73 ctxrelid: -1, |
| 74 args: []string{"-r", "2"}, |
| 75 err: `unknown relation id "2"`, |
| 76 }, { |
| 77 ctxrelid: 1, |
| 78 args: []string{"-r", "ignored:2"}, |
| 79 err: `unknown relation id "ignored:2"`, |
| 80 }, |
| 81 { |
| 82 ctxrelid: -1, |
| 83 args: []string{"-r", "ignored:0"}, |
| 84 err: `expected "key=value" parameters, got nothing`, |
| 85 }, { |
| 86 ctxrelid: 1, |
| 87 args: []string{"-r", "ignored:0"}, |
| 88 err: `expected "key=value" parameters, got nothing`, |
| 89 }, { |
| 90 ctxrelid: -1, |
| 91 args: []string{"-r", "0"}, |
| 92 err: `expected "key=value" parameters, got nothing`, |
| 93 }, { |
| 94 ctxrelid: 1, |
| 95 args: []string{"-r", "0"}, |
| 96 err: `expected "key=value" parameters, got nothing`, |
| 97 }, { |
| 98 ctxrelid: -1, |
| 99 args: []string{"-r", "1"}, |
| 100 err: `expected "key=value" parameters, got nothing`, |
| 101 }, { |
| 102 ctxrelid: 0, |
| 103 args: []string{"-r", "1"}, |
| 104 err: `expected "key=value" parameters, got nothing`, |
| 105 }, { |
| 106 ctxrelid: 1, |
| 107 args: []string{"haha"}, |
| 108 err: `expected "key=value", got "haha"`, |
| 109 }, { |
| 110 ctxrelid: 1, |
| 111 args: []string{"=haha"}, |
| 112 err: `expected "key=value", got "=haha"`, |
| 113 }, { |
| 114 ctxrelid: 1, |
| 115 args: []string{"foo="}, |
| 116 relid: 1, |
| 117 settings: map[string]string{"foo": ""}, |
| 118 }, { |
| 119 ctxrelid: 1, |
| 120 args: []string{"foo='"}, |
| 121 relid: 1, |
| 122 settings: map[string]string{"foo": "'"}, |
| 123 }, { |
| 124 ctxrelid: 1, |
| 125 args: []string{"foo=bar"}, |
| 126 relid: 1, |
| 127 settings: map[string]string{"foo": "bar"}, |
| 128 }, { |
| 129 ctxrelid: 1, |
| 130 args: []string{"foo=bar=baz=qux"}, |
| 131 relid: 1, |
| 132 settings: map[string]string{"foo": "bar=baz=qux"}, |
| 133 }, { |
| 134 ctxrelid: 1, |
| 135 args: []string{"foo=foo: bar"}, |
| 136 relid: 1, |
| 137 settings: map[string]string{"foo": "foo: bar"}, |
| 138 }, { |
| 139 ctxrelid: 0, |
| 140 args: []string{"-r", "1", "foo=bar"}, |
| 141 relid: 1, |
| 142 settings: map[string]string{"foo": "bar"}, |
| 143 }, { |
| 144 ctxrelid: 1, |
| 145 args: []string{"foo=123", "bar=true", "baz=4.5", "qux="}, |
| 146 relid: 1, |
| 147 settings: map[string]string{"foo": "123", "bar": "true", "baz":
"4.5", "qux": ""}, |
| 148 }, |
| 149 } |
| 150 |
| 151 func (s *RelationSetSuite) TestInit(c *C) { |
| 152 for i, t := range relationSetInitTests { |
| 153 c.Logf("test %d", i) |
| 154 hctx := s.GetHookContext(c, t.ctxrelid, "") |
| 155 com, err := hctx.NewCommand("relation-set") |
| 156 c.Assert(err, IsNil) |
| 157 err = com.Init(dummyFlagSet(), t.args) |
| 158 if t.err == "" { |
| 159 c.Assert(err, IsNil) |
| 160 rset := com.(*server.RelationSetCommand) |
| 161 c.Assert(rset.RelationId, Equals, t.relid) |
| 162 c.Assert(rset.Settings, DeepEquals, t.settings) |
| 163 } else { |
| 164 c.Assert(err, ErrorMatches, t.err) |
| 165 } |
| 166 } |
| 167 } |
| 168 |
| 169 // Tests start with a relation with the settings {"base": "value"} |
| 170 var relationSetRunTests = []struct { |
| 171 change map[string]string |
| 172 expect map[string]interface{} |
| 173 }{ |
| 174 { |
| 175 map[string]string{"base": ""}, |
| 176 map[string]interface{}{}, |
| 177 }, { |
| 178 map[string]string{"foo": "bar"}, |
| 179 map[string]interface{}{"base": "value", "foo": "bar"}, |
| 180 }, { |
| 181 map[string]string{"base": "changed"}, |
| 182 map[string]interface{}{"base": "changed"}, |
| 183 }, |
| 184 } |
| 185 |
| 186 func (s *RelationSetSuite) TestRun(c *C) { |
| 187 hctx := s.GetHookContext(c, 0, "") |
| 188 for i, t := range relationSetRunTests { |
| 189 c.Logf("test %d", i) |
| 190 |
| 191 // Set base settings for this test's relations. |
| 192 hctx.Relations[1].ClearCache() |
| 193 pristine := map[string]interface{}{"pristine": "untouched"} |
| 194 setSettings(c, s.relunits[0], pristine) |
| 195 basic := map[string]interface{}{"base": "value"} |
| 196 setSettings(c, s.relunits[1], basic) |
| 197 settings, err := s.relunits[1].ReadSettings("u/0") |
| 198 |
| 199 // Run the command. |
| 200 c.Assert(err, IsNil) |
| 201 com, err := hctx.NewCommand("relation-set") |
| 202 c.Assert(err, IsNil) |
| 203 rset := com.(*server.RelationSetCommand) |
| 204 rset.RelationId = 1 |
| 205 rset.Settings = t.change |
| 206 ctx := dummyContext(c) |
| 207 err = com.Run(ctx) |
| 208 c.Assert(err, IsNil) |
| 209 |
| 210 // Check that the changes are present in the RelationContext's |
| 211 // settings... |
| 212 node, err := hctx.Relations[1].Settings() |
| 213 c.Assert(err, IsNil) |
| 214 c.Assert(node.Map(), DeepEquals, t.expect) |
| 215 |
| 216 // ...but are not persisted to state... |
| 217 settings, err = s.relunits[1].ReadSettings("u/0") |
| 218 c.Assert(err, IsNil) |
| 219 c.Assert(settings, DeepEquals, basic) |
| 220 |
| 221 // ...until we ask for it. |
| 222 err = hctx.Relations[1].WriteSettings() |
| 223 c.Assert(err, IsNil) |
| 224 settings, err = s.relunits[1].ReadSettings("u/0") |
| 225 c.Assert(settings, DeepEquals, t.expect) |
| 226 |
| 227 // For paranoia's sake, check that the other relation's settings
have |
| 228 // not been touched... |
| 229 settings, err = s.relunits[0].ReadSettings("u/0") |
| 230 c.Assert(err, IsNil) |
| 231 c.Assert(settings, DeepEquals, pristine) |
| 232 |
| 233 // ...even when flushed. |
| 234 err = hctx.Relations[0].WriteSettings() |
| 235 c.Assert(err, IsNil) |
| 236 settings, err = s.relunits[0].ReadSettings("u/0") |
| 237 c.Assert(err, IsNil) |
| 238 c.Assert(settings, DeepEquals, pristine) |
| 239 } |
| 240 } |
| 241 |
| 242 func setSettings(c *C, ru *state.RelationUnit, settings map[string]interface{})
{ |
| 243 node, err := ru.Settings() |
| 244 c.Assert(err, IsNil) |
| 245 for _, k := range node.Keys() { |
| 246 node.Delete(k) |
| 247 } |
| 248 node.Update(settings) |
| 249 _, err = node.Write() |
| 250 c.Assert(err, IsNil) |
| 251 } |
OLD | NEW |