github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  purpose: print public-address or private-address
    62  
    63  options:
    64  --format  (= smart)
    65      specify output format (json|smart|yaml)
    66  -o, --output (= "")
    67      specify an output file
    68  `)
    69  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    70  }
    71  
    72  func (s *UnitGetSuite) TestOutputPath(c *gc.C) {
    73  	com := s.createCommand(c)
    74  	ctx := testing.Context(c)
    75  	code := cmd.Main(com, ctx, []string{"--output", "some-file", "private-address"})
    76  	c.Assert(code, gc.Equals, 0)
    77  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    78  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    79  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(string(content), gc.Equals, "192.168.0.99\n")
    82  }
    83  
    84  func (s *UnitGetSuite) TestUnknownSetting(c *gc.C) {
    85  	com := s.createCommand(c)
    86  	err := testing.InitCommand(com, []string{"protected-address"})
    87  	c.Assert(err, gc.ErrorMatches, `unknown setting "protected-address"`)
    88  }
    89  
    90  func (s *UnitGetSuite) TestUnknownArg(c *gc.C) {
    91  	com := s.createCommand(c)
    92  	err := testing.InitCommand(com, []string{"private-address", "blah"})
    93  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["blah"\]`)
    94  }