launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/uniter/jujuc/relation-get_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 "io/ioutil" 9 gc "launchpad.net/gocheck" 10 "path/filepath" 11 12 "launchpad.net/juju-core/cmd" 13 "launchpad.net/juju-core/testing" 14 "launchpad.net/juju-core/worker/uniter/jujuc" 15 ) 16 17 type RelationGetSuite struct { 18 ContextSuite 19 } 20 21 var _ = gc.Suite(&RelationGetSuite{}) 22 23 func (s *RelationGetSuite) SetUpTest(c *gc.C) { 24 s.ContextSuite.SetUpTest(c) 25 s.rels[0].units["u/0"]["private-address"] = "foo: bar\n" 26 s.rels[1].units["m/0"] = Settings{"pew": "pew\npew\n"} 27 s.rels[1].units["u/1"] = Settings{"value": "12345"} 28 } 29 30 var relationGetTests = []struct { 31 summary string 32 relid int 33 unit string 34 args []string 35 code int 36 out string 37 checkctx func(*gc.C, *cmd.Context) 38 }{ 39 { 40 summary: "no default relation", 41 relid: -1, 42 code: 2, 43 out: `no relation id specified`, 44 }, { 45 summary: "explicit relation, not known", 46 relid: -1, 47 code: 2, 48 args: []string{"-r", "burble:123"}, 49 out: `invalid value "burble:123" for flag -r: unknown relation id`, 50 }, { 51 summary: "default relation, no unit chosen", 52 relid: 1, 53 code: 2, 54 out: `no unit id specified`, 55 }, { 56 summary: "explicit relation, no unit chosen", 57 relid: -1, 58 code: 2, 59 args: []string{"-r", "burble:1"}, 60 out: `no unit id specified`, 61 }, { 62 summary: "missing key", 63 relid: 1, 64 unit: "m/0", 65 args: []string{"ker-plunk"}, 66 }, { 67 summary: "missing unit", 68 relid: 1, 69 unit: "bad/0", 70 code: 1, 71 out: `unknown unit bad/0`, 72 }, { 73 summary: "all keys with implicit member", 74 relid: 1, 75 unit: "m/0", 76 out: "pew: 'pew\n\n pew\n\n'", 77 }, { 78 summary: "all keys with explicit member", 79 relid: 1, 80 args: []string{"-", "m/0"}, 81 out: "pew: 'pew\n\n pew\n\n'", 82 }, { 83 summary: "all keys with explicit non-member", 84 relid: 1, 85 args: []string{"-", "u/1"}, 86 out: `value: "12345"`, 87 }, { 88 summary: "all keys with explicit local", 89 relid: 0, 90 args: []string{"-", "u/0"}, 91 out: "private-address: 'foo: bar\n\n'", 92 }, { 93 summary: "specific key with implicit member", 94 relid: 1, 95 unit: "m/0", 96 args: []string{"pew"}, 97 out: "pew\npew\n", 98 }, { 99 summary: "specific key with explicit member", 100 relid: 1, 101 args: []string{"pew", "m/0"}, 102 out: "pew\npew\n", 103 }, { 104 summary: "specific key with explicit non-member", 105 relid: 1, 106 args: []string{"value", "u/1"}, 107 out: "12345", 108 }, { 109 summary: "specific key with explicit local", 110 relid: 0, 111 args: []string{"private-address", "u/0"}, 112 out: "foo: bar\n", 113 }, { 114 summary: "explicit smart formatting 1", 115 relid: 1, 116 unit: "m/0", 117 args: []string{"--format", "smart"}, 118 out: "pew: 'pew\n\n pew\n\n'", 119 }, { 120 summary: "explicit smart formatting 2", 121 relid: 1, 122 unit: "m/0", 123 args: []string{"pew", "--format", "smart"}, 124 out: "pew\npew\n", 125 }, { 126 summary: "explicit smart formatting 3", 127 relid: 1, 128 args: []string{"value", "u/1", "--format", "smart"}, 129 out: "12345", 130 }, { 131 summary: "explicit smart formatting 4", 132 relid: 1, 133 args: []string{"missing", "u/1", "--format", "smart"}, 134 out: "", 135 }, { 136 summary: "json formatting 1", 137 relid: 1, 138 unit: "m/0", 139 args: []string{"--format", "json"}, 140 out: `{"pew":"pew\npew\n"}`, 141 }, { 142 summary: "json formatting 2", 143 relid: 1, 144 unit: "m/0", 145 args: []string{"pew", "--format", "json"}, 146 out: `"pew\npew\n"`, 147 }, { 148 summary: "json formatting 3", 149 relid: 1, 150 args: []string{"value", "u/1", "--format", "json"}, 151 out: `"12345"`, 152 }, { 153 summary: "json formatting 4", 154 relid: 1, 155 args: []string{"missing", "u/1", "--format", "json"}, 156 out: `null`, 157 }, { 158 summary: "yaml formatting 1", 159 relid: 1, 160 unit: "m/0", 161 args: []string{"--format", "yaml"}, 162 out: "pew: 'pew\n\n pew\n\n'", 163 }, { 164 summary: "yaml formatting 2", 165 relid: 1, 166 unit: "m/0", 167 args: []string{"pew", "--format", "yaml"}, 168 out: "'pew\n\n pew\n\n'", 169 }, { 170 summary: "yaml formatting 3", 171 relid: 1, 172 args: []string{"value", "u/1", "--format", "yaml"}, 173 out: `"12345"`, 174 }, { 175 summary: "yaml formatting 4", 176 relid: 1, 177 args: []string{"missing", "u/1", "--format", "yaml"}, 178 out: ``, 179 }, 180 } 181 182 func (s *RelationGetSuite) TestRelationGet(c *gc.C) { 183 for i, t := range relationGetTests { 184 c.Logf("test %d: %s", i, t.summary) 185 hctx := s.GetHookContext(c, t.relid, t.unit) 186 com, err := jujuc.NewCommand(hctx, "relation-get") 187 c.Assert(err, gc.IsNil) 188 ctx := testing.Context(c) 189 code := cmd.Main(com, ctx, t.args) 190 c.Check(code, gc.Equals, t.code) 191 if code == 0 { 192 c.Check(bufferString(ctx.Stderr), gc.Equals, "") 193 expect := t.out 194 if expect != "" { 195 expect = expect + "\n" 196 } 197 c.Check(bufferString(ctx.Stdout), gc.Equals, expect) 198 } else { 199 c.Check(bufferString(ctx.Stdout), gc.Equals, "") 200 expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out) 201 c.Check(bufferString(ctx.Stderr), gc.Matches, expect) 202 } 203 } 204 } 205 206 var helpTemplate = ` 207 usage: %s 208 purpose: get relation settings 209 210 options: 211 --format (= smart) 212 specify output format (json|smart|yaml) 213 -o, --output (= "") 214 specify an output file 215 -r (= %s) 216 specify a relation by id 217 218 relation-get prints the value of a unit's relation setting, specified by key. 219 If no key is given, or if the key is "-", all keys and values will be printed. 220 %s`[1:] 221 222 var relationGetHelpTests = []struct { 223 summary string 224 relid int 225 unit string 226 usage string 227 rel string 228 }{ 229 { 230 summary: "no default relation", 231 relid: -1, 232 usage: "relation-get [options] <key> <unit id>", 233 }, { 234 summary: "no default unit", 235 relid: 1, 236 usage: "relation-get [options] <key> <unit id>", 237 rel: "peer1:1", 238 }, { 239 summary: "default unit", 240 relid: 1, 241 unit: "any/1", 242 usage: `relation-get [options] [<key> [<unit id>]]`, 243 rel: "peer1:1", 244 }, 245 } 246 247 func (s *RelationGetSuite) TestHelp(c *gc.C) { 248 for i, t := range relationGetHelpTests { 249 c.Logf("test %d", i) 250 hctx := s.GetHookContext(c, t.relid, t.unit) 251 com, err := jujuc.NewCommand(hctx, "relation-get") 252 c.Assert(err, gc.IsNil) 253 ctx := testing.Context(c) 254 code := cmd.Main(com, ctx, []string{"--help"}) 255 c.Assert(code, gc.Equals, 0) 256 unitHelp := "" 257 if t.unit != "" { 258 unitHelp = fmt.Sprintf("Current default unit id is %q.\n", t.unit) 259 } 260 expect := fmt.Sprintf(helpTemplate, t.usage, t.rel, unitHelp) 261 c.Assert(bufferString(ctx.Stdout), gc.Equals, expect) 262 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 263 } 264 } 265 266 func (s *RelationGetSuite) TestOutputPath(c *gc.C) { 267 hctx := s.GetHookContext(c, 1, "m/0") 268 com, err := jujuc.NewCommand(hctx, "relation-get") 269 c.Assert(err, gc.IsNil) 270 ctx := testing.Context(c) 271 code := cmd.Main(com, ctx, []string{"--output", "some-file", "pew"}) 272 c.Assert(code, gc.Equals, 0) 273 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 274 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 275 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) 276 c.Assert(err, gc.IsNil) 277 c.Assert(string(content), gc.Equals, "pew\npew\n\n") 278 }