~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/worker/uniter/jujuc/juju-log_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package jujuc_test
 
2
 
 
3
import (
 
4
        "bytes"
 
5
        "fmt"
 
6
        . "launchpad.net/gocheck"
 
7
        "launchpad.net/juju-core/cmd"
 
8
        "launchpad.net/juju-core/log"
 
9
        "launchpad.net/juju-core/testing"
 
10
        "launchpad.net/juju-core/worker/uniter/jujuc"
 
11
        stdlog "log"
 
12
)
 
13
 
 
14
type JujuLogSuite struct {
 
15
        ContextSuite
 
16
}
 
17
 
 
18
var _ = Suite(&JujuLogSuite{})
 
19
 
 
20
func pushLog(debug bool) (*bytes.Buffer, func()) {
 
21
        oldTarget, oldDebug := log.Target(), log.Debug
 
22
        var buf bytes.Buffer
 
23
        log.SetTarget(stdlog.New(&buf, "JUJU:", 0))
 
24
        log.Debug = debug
 
25
        return &buf, func() {
 
26
                log.SetTarget(oldTarget)
 
27
                log.Debug = oldDebug
 
28
        }
 
29
}
 
30
 
 
31
var commonLogTests = []struct {
 
32
        debugEnabled bool
 
33
        debugFlag    bool
 
34
        target       string
 
35
}{
 
36
        {false, false, "JUJU:INFO"},
 
37
        {false, true, ""},
 
38
        {true, false, "JUJU:INFO"},
 
39
        {true, true, "JUJU:DEBUG"},
 
40
}
 
41
 
 
42
func assertLogs(c *C, ctx jujuc.Context, badge string) {
 
43
        msg1 := "the chickens"
 
44
        msg2 := "are 110% AWESOME"
 
45
        com, err := jujuc.NewCommand(ctx, "juju-log")
 
46
        c.Assert(err, IsNil)
 
47
        for _, t := range commonLogTests {
 
48
                buf, pop := pushLog(t.debugEnabled)
 
49
                defer pop()
 
50
 
 
51
                var args []string
 
52
                if t.debugFlag {
 
53
                        args = []string{"--debug", msg1, msg2}
 
54
                } else {
 
55
                        args = []string{msg1, msg2}
 
56
                }
 
57
                code := cmd.Main(com, &cmd.Context{}, args)
 
58
                c.Assert(code, Equals, 0)
 
59
 
 
60
                if t.target == "" {
 
61
                        c.Assert(buf.String(), Equals, "")
 
62
                } else {
 
63
                        expect := fmt.Sprintf("%s %s: %s %s\n", t.target, badge, msg1, msg2)
 
64
                        c.Assert(buf.String(), Equals, expect)
 
65
                }
 
66
        }
 
67
}
 
68
 
 
69
func (s *JujuLogSuite) TestBadges(c *C) {
 
70
        hctx := s.GetHookContext(c, -1, "")
 
71
        assertLogs(c, hctx, "u/0")
 
72
        hctx = s.GetHookContext(c, 1, "u/1")
 
73
        assertLogs(c, hctx, "u/0 peer1:1")
 
74
}
 
75
 
 
76
func newJujuLogCommand(c *C) cmd.Command {
 
77
        ctx := &Context{}
 
78
        com, err := jujuc.NewCommand(ctx, "juju-log")
 
79
        c.Assert(err, IsNil)
 
80
        return com
 
81
}
 
82
 
 
83
func (s *JujuLogSuite) TestRequiresMessage(c *C) {
 
84
        com := newJujuLogCommand(c)
 
85
        testing.TestInit(c, com, nil, "no message specified")
 
86
}
 
87
 
 
88
func (s *JujuLogSuite) TestLogInitMissingLevel(c *C) {
 
89
        com := newJujuLogCommand(c)
 
90
        testing.TestInit(c, com, []string{"-l"}, "flag needs an argument.*")
 
91
}
 
92
 
 
93
func (s *JujuLogSuite) TestLogInitMissingMessage(c *C) {
 
94
        com := newJujuLogCommand(c)
 
95
        testing.TestInit(c, com, []string{"-l", "FATAL"}, "no message specified")
 
96
}
 
97
 
 
98
func (s *JujuLogSuite) TestLogDeprecation(c *C) {
 
99
        com := newJujuLogCommand(c)
 
100
        ctx, err := testing.RunCommand(c, com, []string{"--format", "foo", "msg"})
 
101
        c.Assert(err, IsNil)
 
102
        c.Assert(testing.Stderr(ctx), Equals, "--format flag deprecated for command \"juju-log\"")
 
103
}