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