github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/is-leader_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	"github.com/juju/errors"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/testing"
    14  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    15  )
    16  
    17  type isLeaderSuite struct {
    18  	testing.BaseSuite
    19  }
    20  
    21  var _ = gc.Suite(&isLeaderSuite{})
    22  
    23  func (s *isLeaderSuite) TestInitError(c *gc.C) {
    24  	command, err := jujuc.NewIsLeaderCommand(nil)
    25  	c.Assert(err, jc.ErrorIsNil)
    26  	err = command.Init([]string{"blah"})
    27  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["blah"\]`)
    28  }
    29  
    30  func (s *isLeaderSuite) TestInitSuccess(c *gc.C) {
    31  	command, err := jujuc.NewIsLeaderCommand(nil)
    32  	c.Assert(err, jc.ErrorIsNil)
    33  	err = command.Init(nil)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  }
    36  
    37  func (s *isLeaderSuite) TestFormatError(c *gc.C) {
    38  	command, err := jujuc.NewIsLeaderCommand(nil)
    39  	c.Assert(err, jc.ErrorIsNil)
    40  	runContext := cmdtesting.Context(c)
    41  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(command), runContext, []string{"--format", "bad"})
    42  	c.Check(code, gc.Equals, 2)
    43  	c.Check(bufferString(runContext.Stdout), gc.Equals, "")
    44  	c.Check(bufferString(runContext.Stderr), gc.Equals, `ERROR invalid value "bad" for option --format: unknown format "bad"`+"\n")
    45  }
    46  
    47  func (s *isLeaderSuite) TestIsLeaderError(c *gc.C) {
    48  	jujucContext := &isLeaderContext{err: errors.New("pow")}
    49  	command, err := jujuc.NewIsLeaderCommand(jujucContext)
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	runContext := cmdtesting.Context(c)
    52  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(command), runContext, nil)
    53  	c.Check(code, gc.Equals, 1)
    54  	c.Check(jujucContext.called, jc.IsTrue)
    55  	c.Check(bufferString(runContext.Stdout), gc.Equals, "")
    56  	c.Check(bufferString(runContext.Stderr), gc.Equals, "ERROR leadership status unknown: pow\n")
    57  }
    58  
    59  func (s *isLeaderSuite) TestFormatDefaultYes(c *gc.C) {
    60  	s.testOutput(c, true, nil, "True\n")
    61  }
    62  
    63  func (s *isLeaderSuite) TestFormatDefaultNo(c *gc.C) {
    64  	s.testOutput(c, false, nil, "False\n")
    65  }
    66  
    67  func (s *isLeaderSuite) TestFormatSmartYes(c *gc.C) {
    68  	s.testOutput(c, true, []string{"--format", "smart"}, "True\n")
    69  }
    70  
    71  func (s *isLeaderSuite) TestFormatSmartNo(c *gc.C) {
    72  	s.testOutput(c, false, []string{"--format", "smart"}, "False\n")
    73  }
    74  
    75  func (s *isLeaderSuite) TestFormatYamlYes(c *gc.C) {
    76  	s.testParseOutput(c, true, []string{"--format", "yaml"}, jc.YAMLEquals)
    77  }
    78  
    79  func (s *isLeaderSuite) TestFormatYamlNo(c *gc.C) {
    80  	s.testParseOutput(c, false, []string{"--format", "yaml"}, jc.YAMLEquals)
    81  }
    82  
    83  func (s *isLeaderSuite) TestFormatJsonYes(c *gc.C) {
    84  	s.testParseOutput(c, true, []string{"--format", "json"}, jc.JSONEquals)
    85  }
    86  
    87  func (s *isLeaderSuite) TestFormatJsonNo(c *gc.C) {
    88  	s.testParseOutput(c, false, []string{"--format", "json"}, jc.JSONEquals)
    89  }
    90  
    91  func (s *isLeaderSuite) testOutput(c *gc.C, leader bool, args []string, expect string) {
    92  	jujucContext := &isLeaderContext{leader: leader}
    93  	command, err := jujuc.NewIsLeaderCommand(jujucContext)
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	runContext := cmdtesting.Context(c)
    96  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(command), runContext, args)
    97  	c.Check(code, gc.Equals, 0)
    98  	c.Check(jujucContext.called, jc.IsTrue)
    99  	c.Check(bufferString(runContext.Stdout), gc.Equals, expect)
   100  	c.Check(bufferString(runContext.Stderr), gc.Equals, "")
   101  }
   102  
   103  func (s *isLeaderSuite) testParseOutput(c *gc.C, leader bool, args []string, checker gc.Checker) {
   104  	jujucContext := &isLeaderContext{leader: leader}
   105  	command, err := jujuc.NewIsLeaderCommand(jujucContext)
   106  	c.Assert(err, jc.ErrorIsNil)
   107  	runContext := cmdtesting.Context(c)
   108  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(command), runContext, args)
   109  	c.Check(code, gc.Equals, 0)
   110  	c.Check(jujucContext.called, jc.IsTrue)
   111  	c.Check(bufferString(runContext.Stdout), checker, leader)
   112  	c.Check(bufferString(runContext.Stderr), gc.Equals, "")
   113  }
   114  
   115  type isLeaderContext struct {
   116  	jujuc.Context
   117  	called bool
   118  	leader bool
   119  	err    error
   120  }
   121  
   122  func (ctx *isLeaderContext) IsLeader() (bool, error) {
   123  	ctx.called = true
   124  	return ctx.leader, ctx.err
   125  }