github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/runner/jujuc/owner-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 OwnerGetSuite struct { 20 ContextSuite 21 } 22 23 var _ = gc.Suite(&OwnerGetSuite{}) 24 25 var ownerGetTests = []struct { 26 args []string 27 out string 28 }{ 29 {[]string{"tag"}, "test-owner\n"}, 30 {[]string{"tag", "--format", "yaml"}, "test-owner\n"}, 31 {[]string{"tag", "--format", "json"}, `"test-owner"` + "\n"}, 32 } 33 34 func (s *OwnerGetSuite) createCommand(c *gc.C) cmd.Command { 35 hctx := s.GetHookContext(c, -1, "") 36 com, err := jujuc.NewCommand(hctx, cmdString("owner-get")) 37 c.Assert(err, jc.ErrorIsNil) 38 return com 39 } 40 41 func (s *OwnerGetSuite) TestOutputFormat(c *gc.C) { 42 for _, t := range ownerGetTests { 43 com := s.createCommand(c) 44 ctx := testing.Context(c) 45 code := cmd.Main(com, ctx, t.args) 46 c.Assert(code, gc.Equals, 0) 47 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 48 c.Assert(bufferString(ctx.Stdout), gc.Matches, t.out) 49 } 50 } 51 52 func (s *OwnerGetSuite) TestHelp(c *gc.C) { 53 com := s.createCommand(c) 54 ctx := testing.Context(c) 55 code := cmd.Main(com, ctx, []string{"--help"}) 56 c.Assert(code, gc.Equals, 0) 57 c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: owner-get [options] <setting> 58 purpose: print information about the owner of the service. The only valid value for <setting> is currently tag 59 60 options: 61 --format (= smart) 62 specify output format (json|smart|yaml) 63 -o, --output (= "") 64 specify an output file 65 `) 66 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 67 } 68 69 func (s *OwnerGetSuite) TestOutputPath(c *gc.C) { 70 com := s.createCommand(c) 71 ctx := testing.Context(c) 72 code := cmd.Main(com, ctx, []string{"--output", "some-file", "tag"}) 73 c.Assert(code, gc.Equals, 0) 74 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 75 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 76 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) 77 c.Assert(err, jc.ErrorIsNil) 78 c.Assert(string(content), gc.Equals, "test-owner\n") 79 } 80 81 func (s *OwnerGetSuite) TestUnknownSetting(c *gc.C) { 82 com := s.createCommand(c) 83 err := testing.InitCommand(com, []string{"unknown-setting"}) 84 c.Assert(err, gc.ErrorMatches, `unknown setting "unknown-setting"`) 85 } 86 87 func (s *OwnerGetSuite) TestUnknownArg(c *gc.C) { 88 com := s.createCommand(c) 89 err := testing.InitCommand(com, []string{"tag", "blah"}) 90 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["blah"\]`) 91 }