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