github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/jujuc/relation-set_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 "fmt" 8 9 gc "launchpad.net/gocheck" 10 11 "github.com/juju/juju/cmd" 12 "github.com/juju/juju/testing" 13 "github.com/juju/juju/worker/uniter/jujuc" 14 ) 15 16 type RelationSetSuite struct { 17 ContextSuite 18 } 19 20 var _ = gc.Suite(&RelationSetSuite{}) 21 22 var helpTests = []struct { 23 relid int 24 expect string 25 }{{-1, ""}, {0, "peer0:0"}} 26 27 func (s *RelationSetSuite) TestHelp(c *gc.C) { 28 for i, t := range helpTests { 29 c.Logf("test %d", i) 30 hctx := s.GetHookContext(c, t.relid, "") 31 com, err := jujuc.NewCommand(hctx, "relation-set") 32 c.Assert(err, gc.IsNil) 33 ctx := testing.Context(c) 34 code := cmd.Main(com, ctx, []string{"--help"}) 35 c.Assert(code, gc.Equals, 0) 36 c.Assert(bufferString(ctx.Stdout), gc.Equals, fmt.Sprintf(` 37 usage: relation-set [options] key=value [key=value ...] 38 purpose: set relation settings 39 40 options: 41 --format (= "") 42 deprecated format flag 43 -r (= %s) 44 specify a relation by id 45 `[1:], t.expect)) 46 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 47 } 48 } 49 50 var relationSetInitTests = []struct { 51 ctxrelid int 52 args []string 53 err string 54 relid int 55 settings map[string]string 56 }{ 57 { 58 // compatibility: 0 args is valid. 59 }, { 60 ctxrelid: -1, 61 err: `no relation id specified`, 62 }, { 63 ctxrelid: -1, 64 args: []string{"-r", "one"}, 65 err: `invalid value "one" for flag -r: invalid relation id`, 66 }, { 67 ctxrelid: 1, 68 args: []string{"-r", "one"}, 69 err: `invalid value "one" for flag -r: invalid relation id`, 70 }, { 71 ctxrelid: -1, 72 args: []string{"-r", "ignored:one"}, 73 err: `invalid value "ignored:one" for flag -r: invalid relation id`, 74 }, { 75 ctxrelid: 1, 76 args: []string{"-r", "ignored:one"}, 77 err: `invalid value "ignored:one" for flag -r: invalid relation id`, 78 }, { 79 ctxrelid: -1, 80 args: []string{"-r", "2"}, 81 err: `invalid value "2" for flag -r: unknown relation id`, 82 }, { 83 ctxrelid: 1, 84 args: []string{"-r", "ignored:2"}, 85 err: `invalid value "ignored:2" for flag -r: unknown relation id`, 86 }, { 87 ctxrelid: -1, 88 err: `no relation id specified`, 89 }, { 90 ctxrelid: 1, 91 args: []string{"-r", "ignored:0"}, 92 relid: 0, 93 }, { 94 ctxrelid: 1, 95 args: []string{"-r", "0"}, 96 relid: 0, 97 }, { 98 ctxrelid: -1, 99 args: []string{"-r", "1"}, 100 relid: 1, 101 }, { 102 ctxrelid: 0, 103 args: []string{"-r", "1"}, 104 relid: 1, 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 *gc.C) { 152 for i, t := range relationSetInitTests { 153 c.Logf("test %d", i) 154 hctx := s.GetHookContext(c, t.ctxrelid, "") 155 com, err := jujuc.NewCommand(hctx, "relation-set") 156 c.Assert(err, gc.IsNil) 157 err = testing.InitCommand(com, t.args) 158 if t.err == "" { 159 c.Assert(err, gc.IsNil) 160 rset := com.(*jujuc.RelationSetCommand) 161 c.Assert(rset.RelationId, gc.Equals, t.relid) 162 settings := t.settings 163 if settings == nil { 164 settings = map[string]string{} 165 } 166 c.Assert(rset.Settings, gc.DeepEquals, settings) 167 } else { 168 c.Assert(err, gc.ErrorMatches, t.err) 169 } 170 } 171 } 172 173 // Tests start with a relation with the settings {"base": "value"} 174 var relationSetRunTests = []struct { 175 change map[string]string 176 expect Settings 177 }{ 178 { 179 map[string]string{"base": ""}, 180 Settings{}, 181 }, { 182 map[string]string{"foo": "bar"}, 183 Settings{"base": "value", "foo": "bar"}, 184 }, { 185 map[string]string{"base": "changed"}, 186 Settings{"base": "changed"}, 187 }, 188 } 189 190 func (s *RelationSetSuite) TestRun(c *gc.C) { 191 hctx := s.GetHookContext(c, 0, "") 192 for i, t := range relationSetRunTests { 193 c.Logf("test %d", i) 194 195 pristine := Settings{"pristine": "untouched"} 196 hctx.rels[0].units["u/0"] = pristine 197 basic := Settings{"base": "value"} 198 hctx.rels[1].units["u/0"] = basic 199 200 // Run the command. 201 com, err := jujuc.NewCommand(hctx, "relation-set") 202 c.Assert(err, gc.IsNil) 203 rset := com.(*jujuc.RelationSetCommand) 204 rset.RelationId = 1 205 rset.Settings = t.change 206 ctx := testing.Context(c) 207 err = com.Run(ctx) 208 c.Assert(err, gc.IsNil) 209 210 // Check changes. 211 c.Assert(hctx.rels[0].units["u/0"], gc.DeepEquals, pristine) 212 c.Assert(hctx.rels[1].units["u/0"], gc.DeepEquals, t.expect) 213 } 214 } 215 216 func (s *RelationSetSuite) TestRunDeprecationWarning(c *gc.C) { 217 hctx := s.GetHookContext(c, 0, "") 218 com, _ := jujuc.NewCommand(hctx, "relation-set") 219 // The rel= is needed to make this a valid command. 220 ctx, err := testing.RunCommand(c, com, "--format", "foo", "rel=") 221 222 c.Assert(err, gc.IsNil) 223 c.Assert(testing.Stdout(ctx), gc.Equals, "") 224 c.Assert(testing.Stderr(ctx), gc.Equals, "--format flag deprecated for command \"relation-set\"") 225 }