1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
"github.com/juju/errors"
9
jujutesting "github.com/juju/testing"
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
13
"github.com/juju/juju/testing"
14
"github.com/juju/juju/worker/uniter/runner/jujuc"
17
type isLeaderSuite struct {
18
jujutesting.IsolationSuite
21
var _ = gc.Suite(&isLeaderSuite{})
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"\]`)
29
func (s *isLeaderSuite) TestInitSuccess(c *gc.C) {
30
command := jujuc.NewIsLeaderCommand(nil)
31
err := command.Init(nil)
32
c.Assert(err, jc.ErrorIsNil)
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")
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")
55
func (s *isLeaderSuite) TestFormatDefaultYes(c *gc.C) {
56
s.testOutput(c, true, nil, "True\n")
59
func (s *isLeaderSuite) TestFormatDefaultNo(c *gc.C) {
60
s.testOutput(c, false, nil, "False\n")
63
func (s *isLeaderSuite) TestFormatSmartYes(c *gc.C) {
64
s.testOutput(c, true, []string{"--format", "smart"}, "True\n")
67
func (s *isLeaderSuite) TestFormatSmartNo(c *gc.C) {
68
s.testOutput(c, false, []string{"--format", "smart"}, "False\n")
71
func (s *isLeaderSuite) TestFormatYamlYes(c *gc.C) {
72
s.testParseOutput(c, true, []string{"--format", "yaml"}, jc.YAMLEquals)
75
func (s *isLeaderSuite) TestFormatYamlNo(c *gc.C) {
76
s.testParseOutput(c, false, []string{"--format", "yaml"}, jc.YAMLEquals)
79
func (s *isLeaderSuite) TestFormatJsonYes(c *gc.C) {
80
s.testParseOutput(c, true, []string{"--format", "json"}, jc.JSONEquals)
83
func (s *isLeaderSuite) TestFormatJsonNo(c *gc.C) {
84
s.testParseOutput(c, false, []string{"--format", "json"}, jc.JSONEquals)
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, "")
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, "")
109
type isLeaderContext struct {
116
func (ctx *isLeaderContext) IsLeader() (bool, error) {
118
return ctx.leader, ctx.err