github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/action-get_test.go (about) 1 // Copyright 2014 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 ActionGetSuite struct { 19 ContextSuite 20 } 21 22 var _ = gc.Suite(&ActionGetSuite{}) 23 24 type actionGetContext struct { 25 actionParams map[string]interface{} 26 jujuc.Context 27 } 28 29 func (ctx *actionGetContext) ActionParams() (map[string]interface{}, error) { 30 return ctx.actionParams, nil 31 } 32 33 type nonActionContext struct { 34 jujuc.Context 35 } 36 37 func (ctx *nonActionContext) ActionParams() (map[string]interface{}, error) { 38 return nil, fmt.Errorf("ActionParams queried from non-Action hook context") 39 } 40 41 func (s *ActionGetSuite) TestNonActionRunFail(c *gc.C) { 42 hctx := &nonActionContext{} 43 com, err := jujuc.NewCommand(hctx, cmdString("action-get")) 44 c.Assert(err, jc.ErrorIsNil) 45 ctx := testing.Context(c) 46 code := cmd.Main(com, ctx, []string{}) 47 c.Check(code, gc.Equals, 1) 48 c.Check(bufferString(ctx.Stdout), gc.Equals, "") 49 expect := fmt.Sprintf(`(\n)*error: %s\n`, "ActionParams queried from non-Action hook context") 50 c.Check(bufferString(ctx.Stderr), gc.Matches, expect) 51 } 52 53 func (s *ActionGetSuite) TestActionGet(c *gc.C) { 54 var actionGetTestMaps = []map[string]interface{}{ 55 { 56 "outfile": "foo.bz2", 57 }, 58 59 { 60 "outfile": map[string]interface{}{ 61 "filename": "foo.bz2", 62 "format": "bzip", 63 }, 64 }, 65 66 { 67 "outfile": map[string]interface{}{ 68 "type": map[string]interface{}{ 69 "1": "raw", 70 "2": "gzip", 71 "3": "bzip", 72 }, 73 }, 74 }, 75 76 // A map with a non-string key is not usable. 77 { 78 "outfile": map[interface{}]interface{}{ 79 5: map[string]interface{}{ 80 "1": "raw", 81 "2": "gzip", 82 "3": "bzip", 83 }, 84 }, 85 }, 86 87 // A map with an inner map[interface{}]interface{} is OK if 88 // the keys are strings. 89 { 90 "outfile": map[interface{}]interface{}{ 91 "type": map[string]interface{}{ 92 "1": "raw", 93 "2": "gzip", 94 "3": "bzip", 95 }, 96 }, 97 }, 98 } 99 100 var actionGetTests = []struct { 101 summary string 102 args []string 103 actionParams map[string]interface{} 104 code int 105 out string 106 errMsg string 107 }{{ 108 summary: "a simple empty map with nil key", 109 args: []string{}, 110 out: "{}\n", 111 }, { 112 summary: "a simple empty map with nil key", 113 args: []string{"--format", "yaml"}, 114 out: "{}\n", 115 }, { 116 summary: "a simple empty map with nil key", 117 args: []string{"--format", "json"}, 118 out: "null\n", 119 }, { 120 summary: "a nonexistent key", 121 args: []string{"foo"}, 122 }, { 123 summary: "a nonexistent key", 124 args: []string{"--format", "yaml", "foo"}, 125 }, { 126 summary: "a nonexistent key", 127 args: []string{"--format", "json", "foo"}, 128 out: "null\n", 129 }, { 130 summary: "a nonexistent inner key", 131 args: []string{"outfile.type"}, 132 actionParams: actionGetTestMaps[1], 133 }, { 134 summary: "a nonexistent inner key", 135 args: []string{"--format", "yaml", "outfile.type"}, 136 actionParams: actionGetTestMaps[1], 137 }, { 138 summary: "a nonexistent inner key", 139 args: []string{"--format", "json", "outfile.type"}, 140 actionParams: actionGetTestMaps[1], 141 out: "null\n", 142 }, { 143 summary: "a nonexistent inner key", 144 args: []string{"outfile.type.1"}, 145 actionParams: actionGetTestMaps[1], 146 }, { 147 summary: "a nonexistent inner key", 148 args: []string{"--format", "yaml", "outfile.type.1"}, 149 actionParams: actionGetTestMaps[1], 150 }, { 151 summary: "a nonexistent inner key", 152 args: []string{"--format", "json", "outfile.type.1"}, 153 actionParams: actionGetTestMaps[1], 154 out: "null\n", 155 }, { 156 summary: "a map with a non-string key", 157 args: []string{"outfile.type"}, 158 actionParams: actionGetTestMaps[3], 159 }, { 160 summary: "a map with a non-string key", 161 args: []string{"--format", "yaml", "outfile.type"}, 162 actionParams: actionGetTestMaps[3], 163 }, { 164 summary: "a map with a non-string key", 165 args: []string{"--format", "json", "outfile.type"}, 166 actionParams: actionGetTestMaps[3], 167 out: "null\n", 168 }, { 169 summary: "a simple map of one value to one key", 170 args: []string{}, 171 actionParams: actionGetTestMaps[0], 172 out: "outfile: foo.bz2\n", 173 }, { 174 summary: "a simple map of one value to one key", 175 args: []string{"--format", "yaml"}, 176 actionParams: actionGetTestMaps[0], 177 out: "outfile: foo.bz2\n", 178 }, { 179 summary: "a simple map of one value to one key", 180 args: []string{"--format", "json"}, 181 actionParams: actionGetTestMaps[0], 182 out: `{"outfile":"foo.bz2"}` + "\n", 183 }, { 184 summary: "an entire map", 185 args: []string{}, 186 actionParams: actionGetTestMaps[2], 187 out: "outfile:\n" + 188 " type:\n" + 189 " \"1\": raw\n" + 190 " \"2\": gzip\n" + 191 " \"3\": bzip\n", 192 }, { 193 summary: "an entire map", 194 args: []string{"--format", "yaml"}, 195 actionParams: actionGetTestMaps[2], 196 out: "outfile:\n" + 197 " type:\n" + 198 " \"1\": raw\n" + 199 " \"2\": gzip\n" + 200 " \"3\": bzip\n", 201 }, { 202 summary: "an entire map", 203 args: []string{"--format", "json"}, 204 actionParams: actionGetTestMaps[2], 205 out: `{"outfile":{"type":{"1":"raw","2":"gzip","3":"bzip"}}}` + "\n", 206 }, { 207 summary: "an inner map value which is itself a map", 208 args: []string{"outfile.type"}, 209 actionParams: actionGetTestMaps[2], 210 out: "\"1\": raw\n" + 211 "\"2\": gzip\n" + 212 "\"3\": bzip\n", 213 }, { 214 summary: "an inner map value which is itself a map", 215 args: []string{"--format", "yaml", "outfile.type"}, 216 actionParams: actionGetTestMaps[2], 217 out: "\"1\": raw\n" + 218 "\"2\": gzip\n" + 219 "\"3\": bzip\n", 220 }, { 221 summary: "an inner map value which is itself a map", 222 args: []string{"--format", "json", "outfile.type"}, 223 actionParams: actionGetTestMaps[2], 224 out: `{"1":"raw","2":"gzip","3":"bzip"}` + "\n", 225 }, { 226 summary: "a map with an inner map keyed by interface{}", 227 args: []string{"outfile.type"}, 228 actionParams: actionGetTestMaps[4], 229 out: "\"1\": raw\n" + 230 "\"2\": gzip\n" + 231 "\"3\": bzip\n", 232 }, { 233 summary: "a map with an inner map keyed by interface{}", 234 args: []string{"--format", "yaml", "outfile.type"}, 235 actionParams: actionGetTestMaps[4], 236 out: "\"1\": raw\n" + 237 "\"2\": gzip\n" + 238 "\"3\": bzip\n", 239 }, { 240 summary: "a map with an inner map keyed by interface{}", 241 args: []string{"--format", "json", "outfile.type"}, 242 actionParams: actionGetTestMaps[4], 243 out: `{"1":"raw","2":"gzip","3":"bzip"}` + "\n", 244 }, { 245 summary: "too many arguments", 246 args: []string{"multiple", "keys"}, 247 code: 2, 248 errMsg: `unrecognized args: \["keys"\]`, 249 }} 250 251 for i, t := range actionGetTests { 252 c.Logf("test %d: %s\n args: %#v", i, t.summary, t.args) 253 hctx := &actionGetContext{} 254 hctx.actionParams = t.actionParams 255 com, err := jujuc.NewCommand(hctx, cmdString("action-get")) 256 c.Assert(err, jc.ErrorIsNil) 257 ctx := testing.Context(c) 258 code := cmd.Main(com, ctx, t.args) 259 c.Check(code, gc.Equals, t.code) 260 if code == 0 { 261 c.Check(bufferString(ctx.Stdout), gc.Equals, t.out) 262 c.Check(bufferString(ctx.Stderr), gc.Equals, "") 263 } else { 264 c.Check(bufferString(ctx.Stdout), gc.Equals, "") 265 expect := fmt.Sprintf(`(\n)*error: %s\n`, t.errMsg) 266 c.Check(bufferString(ctx.Stderr), gc.Matches, expect) 267 } 268 } 269 } 270 271 func (s *ActionGetSuite) TestHelp(c *gc.C) { 272 hctx := &actionGetContext{} 273 com, err := jujuc.NewCommand(hctx, cmdString("action-get")) 274 c.Assert(err, jc.ErrorIsNil) 275 ctx := testing.Context(c) 276 code := cmd.Main(com, ctx, []string{"--help"}) 277 c.Assert(code, gc.Equals, 0) 278 c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: action-get [options] [<key>[.<key>.<key>...]] 279 280 Summary: 281 get action parameters 282 283 Options: 284 --format (= smart) 285 Specify output format (json|smart|yaml) 286 -o, --output (= "") 287 Specify an output file 288 289 Details: 290 action-get will print the value of the parameter at the given key, serialized 291 as YAML. If multiple keys are passed, action-get will recurse into the param 292 map as needed. 293 `) 294 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 295 }