github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/runner/jujuc/relation-set_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 "fmt" 9 10 "github.com/juju/cmd" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/testing" 15 "github.com/juju/juju/worker/uniter/runner/jujuc" 16 ) 17 18 type RelationSetSuite struct { 19 ContextSuite 20 } 21 22 var _ = gc.Suite(&RelationSetSuite{}) 23 24 var helpTests = []struct { 25 relid int 26 expect string 27 }{{-1, ""}, {0, "peer0:0"}} 28 29 func (s *RelationSetSuite) TestHelp(c *gc.C) { 30 for i, t := range helpTests { 31 c.Logf("test %d", i) 32 hctx := s.GetHookContext(c, t.relid, "") 33 com, err := jujuc.NewCommand(hctx, cmdString("relation-set")) 34 c.Assert(err, jc.ErrorIsNil) 35 ctx := testing.Context(c) 36 code := cmd.Main(com, ctx, []string{"--help"}) 37 c.Assert(code, gc.Equals, 0) 38 c.Assert(bufferString(ctx.Stdout), gc.Equals, fmt.Sprintf(` 39 usage: relation-set [options] key=value [key=value ...] 40 purpose: set relation settings 41 42 options: 43 --format (= "") 44 deprecated format flag 45 -r, --relation (= %s) 46 specify a relation by id 47 `[1:], t.expect)) 48 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 49 } 50 } 51 52 var relationSetInitTests = []struct { 53 ctxrelid int 54 args []string 55 err string 56 relid int 57 settings map[string]string 58 }{ 59 { 60 // compatibility: 0 args is valid. 61 }, { 62 ctxrelid: -1, 63 err: `no relation id specified`, 64 }, { 65 ctxrelid: -1, 66 args: []string{"-r", "one"}, 67 err: `invalid value "one" for flag -r: invalid relation id`, 68 }, { 69 ctxrelid: 1, 70 args: []string{"-r", "one"}, 71 err: `invalid value "one" for flag -r: invalid relation id`, 72 }, { 73 ctxrelid: -1, 74 args: []string{"-r", "ignored:one"}, 75 err: `invalid value "ignored:one" for flag -r: invalid relation id`, 76 }, { 77 ctxrelid: 1, 78 args: []string{"-r", "ignored:one"}, 79 err: `invalid value "ignored:one" for flag -r: invalid relation id`, 80 }, { 81 ctxrelid: -1, 82 args: []string{"-r", "2"}, 83 err: `invalid value "2" for flag -r: unknown relation id`, 84 }, { 85 ctxrelid: 1, 86 args: []string{"-r", "ignored:2"}, 87 err: `invalid value "ignored:2" for flag -r: unknown relation id`, 88 }, { 89 ctxrelid: -1, 90 err: `no relation id specified`, 91 }, { 92 ctxrelid: 1, 93 args: []string{"-r", "ignored:0"}, 94 relid: 0, 95 }, { 96 ctxrelid: 1, 97 args: []string{"-r", "0"}, 98 relid: 0, 99 }, { 100 ctxrelid: -1, 101 args: []string{"-r", "1"}, 102 relid: 1, 103 }, { 104 ctxrelid: 0, 105 args: []string{"-r", "1"}, 106 relid: 1, 107 }, { 108 ctxrelid: 1, 109 args: []string{"haha"}, 110 err: `expected "key=value", got "haha"`, 111 }, { 112 ctxrelid: 1, 113 args: []string{"=haha"}, 114 err: `expected "key=value", got "=haha"`, 115 }, { 116 ctxrelid: 1, 117 args: []string{"foo="}, 118 relid: 1, 119 settings: map[string]string{"foo": ""}, 120 }, { 121 ctxrelid: 1, 122 args: []string{"foo='"}, 123 relid: 1, 124 settings: map[string]string{"foo": "'"}, 125 }, { 126 ctxrelid: 1, 127 args: []string{"foo=bar"}, 128 relid: 1, 129 settings: map[string]string{"foo": "bar"}, 130 }, { 131 ctxrelid: 1, 132 args: []string{"foo=bar=baz=qux"}, 133 relid: 1, 134 settings: map[string]string{"foo": "bar=baz=qux"}, 135 }, { 136 ctxrelid: 1, 137 args: []string{"foo=foo: bar"}, 138 relid: 1, 139 settings: map[string]string{"foo": "foo: bar"}, 140 }, { 141 ctxrelid: 0, 142 args: []string{"-r", "1", "foo=bar"}, 143 relid: 1, 144 settings: map[string]string{"foo": "bar"}, 145 }, { 146 ctxrelid: 1, 147 args: []string{"foo=123", "bar=true", "baz=4.5", "qux="}, 148 relid: 1, 149 settings: map[string]string{"foo": "123", "bar": "true", "baz": "4.5", "qux": ""}, 150 }, 151 } 152 153 func (s *RelationSetSuite) TestInit(c *gc.C) { 154 for i, t := range relationSetInitTests { 155 c.Logf("test %d", i) 156 hctx := s.GetHookContext(c, t.ctxrelid, "") 157 com, err := jujuc.NewCommand(hctx, cmdString("relation-set")) 158 c.Assert(err, jc.ErrorIsNil) 159 err = testing.InitCommand(com, t.args) 160 if t.err == "" { 161 c.Assert(err, jc.ErrorIsNil) 162 rset := com.(*jujuc.RelationSetCommand) 163 c.Assert(rset.RelationId, gc.Equals, t.relid) 164 settings := t.settings 165 if settings == nil { 166 settings = map[string]string{} 167 } 168 c.Assert(rset.Settings, gc.DeepEquals, settings) 169 } else { 170 c.Assert(err, gc.ErrorMatches, t.err) 171 } 172 } 173 } 174 175 // Tests start with a relation with the settings {"base": "value"} 176 var relationSetRunTests = []struct { 177 change map[string]string 178 expect Settings 179 }{ 180 { 181 map[string]string{"base": ""}, 182 Settings{}, 183 }, { 184 map[string]string{"foo": "bar"}, 185 Settings{"base": "value", "foo": "bar"}, 186 }, { 187 map[string]string{"base": "changed"}, 188 Settings{"base": "changed"}, 189 }, 190 } 191 192 func (s *RelationSetSuite) TestRun(c *gc.C) { 193 hctx := s.GetHookContext(c, 0, "") 194 for i, t := range relationSetRunTests { 195 c.Logf("test %d", i) 196 197 pristine := Settings{"pristine": "untouched"} 198 hctx.rels[0].units["u/0"] = pristine 199 basic := Settings{"base": "value"} 200 hctx.rels[1].units["u/0"] = basic 201 202 // Run the command. 203 com, err := jujuc.NewCommand(hctx, cmdString("relation-set")) 204 c.Assert(err, jc.ErrorIsNil) 205 rset := com.(*jujuc.RelationSetCommand) 206 rset.RelationId = 1 207 rset.Settings = t.change 208 ctx := testing.Context(c) 209 err = com.Run(ctx) 210 c.Assert(err, jc.ErrorIsNil) 211 212 // Check changes. 213 c.Assert(hctx.rels[0].units["u/0"], gc.DeepEquals, pristine) 214 c.Assert(hctx.rels[1].units["u/0"], gc.DeepEquals, t.expect) 215 } 216 } 217 218 func (s *RelationSetSuite) TestRunDeprecationWarning(c *gc.C) { 219 hctx := s.GetHookContext(c, 0, "") 220 com, _ := jujuc.NewCommand(hctx, cmdString("relation-set")) 221 222 // The rel= is needed to make this a valid command. 223 ctx, err := testing.RunCommand(c, com, "--format", "foo", "rel=") 224 225 c.Assert(err, jc.ErrorIsNil) 226 c.Assert(testing.Stdout(ctx), gc.Equals, "") 227 c.Assert(testing.Stderr(ctx), gc.Equals, "--format flag deprecated for command \"relation-set\"") 228 }