launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	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 UnitGetSuite struct {
    17  	ContextSuite
    18  }
    19  
    20  var _ = gc.Suite(&UnitGetSuite{})
    21  
    22  var unitGetTests = []struct {
    23  	args []string
    24  	out  string
    25  }{
    26  	{[]string{"private-address"}, "192.168.0.99\n"},
    27  	{[]string{"private-address", "--format", "yaml"}, "192.168.0.99\n"},
    28  	{[]string{"private-address", "--format", "json"}, `"192.168.0.99"` + "\n"},
    29  	{[]string{"public-address"}, "gimli.minecraft.testing.invalid\n"},
    30  	{[]string{"public-address", "--format", "yaml"}, "gimli.minecraft.testing.invalid\n"},
    31  	{[]string{"public-address", "--format", "json"}, `"gimli.minecraft.testing.invalid"` + "\n"},
    32  }
    33  
    34  func (s *UnitGetSuite) createCommand(c *gc.C) cmd.Command {
    35  	hctx := s.GetHookContext(c, -1, "")
    36  	com, err := jujuc.NewCommand(hctx, "unit-get")
    37  	c.Assert(err, gc.IsNil)
    38  	return com
    39  }
    40  
    41  func (s *UnitGetSuite) TestOutputFormat(c *gc.C) {
    42  	for _, t := range unitGetTests {
    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 *UnitGetSuite) 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: unit-get [options] <setting>
    58  purpose: print public-address or private-address
    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 *UnitGetSuite) TestOutputPath(c *gc.C) {
    70  	com := s.createCommand(c)
    71  	ctx := testing.Context(c)
    72  	code := cmd.Main(com, ctx, []string{"--output", "some-file", "private-address"})
    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, gc.IsNil)
    78  	c.Assert(string(content), gc.Equals, "192.168.0.99\n")
    79  }
    80  
    81  func (s *UnitGetSuite) TestUnknownSetting(c *gc.C) {
    82  	com := s.createCommand(c)
    83  	err := testing.InitCommand(com, []string{"protected-address"})
    84  	c.Assert(err, gc.ErrorMatches, `unknown setting "protected-address"`)
    85  }
    86  
    87  func (s *UnitGetSuite) TestUnknownArg(c *gc.C) {
    88  	com := s.createCommand(c)
    89  	err := testing.InitCommand(com, []string{"private-address", "blah"})
    90  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["blah"\]`)
    91  }