github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/worker/uniter/jujuc/unit-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 "io/ioutil" 8 "path/filepath" 9 10 gc "launchpad.net/gocheck" 11 12 "github.com/juju/juju/cmd" 13 "github.com/juju/juju/testing" 14 "github.com/juju/juju/worker/uniter/jujuc" 15 ) 16 17 type UnitGetSuite struct { 18 ContextSuite 19 } 20 21 var _ = gc.Suite(&UnitGetSuite{}) 22 23 var unitGetTests = []struct { 24 args []string 25 out string 26 }{ 27 {[]string{"private-address"}, "192.168.0.99\n"}, 28 {[]string{"private-address", "--format", "yaml"}, "192.168.0.99\n"}, 29 {[]string{"private-address", "--format", "json"}, `"192.168.0.99"` + "\n"}, 30 {[]string{"public-address"}, "gimli.minecraft.testing.invalid\n"}, 31 {[]string{"public-address", "--format", "yaml"}, "gimli.minecraft.testing.invalid\n"}, 32 {[]string{"public-address", "--format", "json"}, `"gimli.minecraft.testing.invalid"` + "\n"}, 33 } 34 35 func (s *UnitGetSuite) createCommand(c *gc.C) cmd.Command { 36 hctx := s.GetHookContext(c, -1, "") 37 com, err := jujuc.NewCommand(hctx, "unit-get") 38 c.Assert(err, gc.IsNil) 39 return com 40 } 41 42 func (s *UnitGetSuite) TestOutputFormat(c *gc.C) { 43 for _, t := range unitGetTests { 44 com := s.createCommand(c) 45 ctx := testing.Context(c) 46 code := cmd.Main(com, ctx, t.args) 47 c.Assert(code, gc.Equals, 0) 48 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 49 c.Assert(bufferString(ctx.Stdout), gc.Matches, t.out) 50 } 51 } 52 53 func (s *UnitGetSuite) TestHelp(c *gc.C) { 54 com := s.createCommand(c) 55 ctx := testing.Context(c) 56 code := cmd.Main(com, ctx, []string{"--help"}) 57 c.Assert(code, gc.Equals, 0) 58 c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: unit-get [options] <setting> 59 purpose: print public-address or private-address 60 61 options: 62 --format (= smart) 63 specify output format (json|smart|yaml) 64 -o, --output (= "") 65 specify an output file 66 `) 67 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 68 } 69 70 func (s *UnitGetSuite) TestOutputPath(c *gc.C) { 71 com := s.createCommand(c) 72 ctx := testing.Context(c) 73 code := cmd.Main(com, ctx, []string{"--output", "some-file", "private-address"}) 74 c.Assert(code, gc.Equals, 0) 75 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 76 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 77 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) 78 c.Assert(err, gc.IsNil) 79 c.Assert(string(content), gc.Equals, "192.168.0.99\n") 80 } 81 82 func (s *UnitGetSuite) TestUnknownSetting(c *gc.C) { 83 com := s.createCommand(c) 84 err := testing.InitCommand(com, []string{"protected-address"}) 85 c.Assert(err, gc.ErrorMatches, `unknown setting "protected-address"`) 86 } 87 88 func (s *UnitGetSuite) TestUnknownArg(c *gc.C) { 89 com := s.createCommand(c) 90 err := testing.InitCommand(com, []string{"private-address", "blah"}) 91 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["blah"\]`) 92 }