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