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