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