1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package checkers_test
import (
"github.com/juju/loggo/v2"
gc "gopkg.in/check.v1"
jc "github.com/juju/testing/checkers"
)
type SimpleMessageSuite struct{}
var _ = gc.Suite(&SimpleMessageSuite{})
func (s *SimpleMessageSuite) TestSimpleMessageString(c *gc.C) {
m := jc.SimpleMessage{
Level: loggo.INFO,
Message: `hello
world
`,
}
c.Check(m.String(), gc.Matches, "INFO hello\nworld\n")
}
func (s *SimpleMessageSuite) TestSimpleMessagesGoString(c *gc.C) {
m := jc.SimpleMessages{{
Level: loggo.DEBUG,
Message: "debug",
}, {
Level: loggo.ERROR,
Message: "Error",
}}
c.Check(m.GoString(), gc.Matches, `SimpleMessages{
DEBUG debug
ERROR Error
}`)
}
type LogMatchesSuite struct{}
var _ = gc.Suite(&LogMatchesSuite{})
func (s *LogMatchesSuite) TestMatchSimpleMessage(c *gc.C) {
log := []loggo.Entry{
{Level: loggo.INFO, Message: "foo bar"},
{Level: loggo.INFO, Message: "12345"},
}
c.Check(log, jc.LogMatches, []jc.SimpleMessage{
{loggo.INFO, "foo bar"},
{loggo.INFO, "12345"},
})
c.Check(log, jc.LogMatches, []jc.SimpleMessage{
{loggo.INFO, "foo .*"},
{loggo.INFO, "12345"},
})
// UNSPECIFIED means we don't care what the level is,
// just check the message string matches.
c.Check(log, jc.LogMatches, []jc.SimpleMessage{
{loggo.UNSPECIFIED, "foo .*"},
{loggo.INFO, "12345"},
})
c.Check(log, gc.Not(jc.LogMatches), []jc.SimpleMessage{
{loggo.INFO, "foo bar"},
{loggo.DEBUG, "12345"},
})
}
func (s *LogMatchesSuite) TestMatchSimpleMessages(c *gc.C) {
log := []loggo.Entry{
{Level: loggo.INFO, Message: "foo bar"},
{Level: loggo.INFO, Message: "12345"},
}
c.Check(log, jc.LogMatches, jc.SimpleMessages{
{loggo.INFO, "foo bar"},
{loggo.INFO, "12345"},
})
c.Check(log, jc.LogMatches, jc.SimpleMessages{
{loggo.INFO, "foo .*"},
{loggo.INFO, "12345"},
})
// UNSPECIFIED means we don't care what the level is,
// just check the message string matches.
c.Check(log, jc.LogMatches, jc.SimpleMessages{
{loggo.UNSPECIFIED, "foo .*"},
{loggo.INFO, "12345"},
})
c.Check(log, gc.Not(jc.LogMatches), jc.SimpleMessages{
{loggo.INFO, "foo bar"},
{loggo.DEBUG, "12345"},
})
}
func (s *LogMatchesSuite) TestMatchStrings(c *gc.C) {
log := []loggo.Entry{
{Level: loggo.INFO, Message: "foo bar"},
{Level: loggo.INFO, Message: "12345"},
}
c.Check(log, jc.LogMatches, []string{"foo bar", "12345"})
c.Check(log, jc.LogMatches, []string{"foo .*", "12345"})
c.Check(log, gc.Not(jc.LogMatches), []string{"baz", "bing"})
}
func (s *LogMatchesSuite) TestMatchInexact(c *gc.C) {
log := []loggo.Entry{
{Level: loggo.INFO, Message: "foo bar"},
{Level: loggo.INFO, Message: "baz"},
{Level: loggo.DEBUG, Message: "12345"},
{Level: loggo.ERROR, Message: "12345"},
{Level: loggo.INFO, Message: "67890"},
}
c.Check(log, jc.LogMatches, []string{"foo bar", "12345"})
c.Check(log, jc.LogMatches, []string{"foo .*", "12345"})
c.Check(log, jc.LogMatches, []string{"foo .*", "67890"})
c.Check(log, jc.LogMatches, []string{"67890"})
// Matches are always left-most after the previous match.
c.Check(log, jc.LogMatches, []string{".*", "baz"})
c.Check(log, jc.LogMatches, []string{"foo bar", ".*", "12345"})
c.Check(log, jc.LogMatches, []string{"foo bar", ".*", "67890"})
// Order is important: 67890 advances to the last item in obtained,
// and so there's nothing after to match against ".*".
c.Check(log, gc.Not(jc.LogMatches), []string{"67890", ".*"})
// ALL specified patterns MUST match in the order given.
c.Check(log, gc.Not(jc.LogMatches), []string{".*", "foo bar"})
// Check that levels are matched.
c.Check(log, jc.LogMatches, []jc.SimpleMessage{
{loggo.UNSPECIFIED, "12345"},
{loggo.UNSPECIFIED, "12345"},
})
c.Check(log, jc.LogMatches, []jc.SimpleMessage{
{loggo.DEBUG, "12345"},
{loggo.ERROR, "12345"},
})
c.Check(log, jc.LogMatches, []jc.SimpleMessage{
{loggo.DEBUG, "12345"},
{loggo.INFO, ".*"},
})
c.Check(log, gc.Not(jc.LogMatches), []jc.SimpleMessage{
{loggo.DEBUG, "12345"},
{loggo.INFO, ".*"},
{loggo.UNSPECIFIED, ".*"},
})
}
func (s *LogMatchesSuite) TestFromLogMatches(c *gc.C) {
tw := &loggo.TestWriter{}
_, err := loggo.ReplaceDefaultWriter(tw)
c.Assert(err, gc.IsNil)
defer loggo.ResetWriters()
logger := loggo.GetLogger("test")
logger.SetLogLevel(loggo.DEBUG)
logger.Infof("foo")
logger.Debugf("bar")
logger.Tracef("hidden")
c.Check(tw.Log(), jc.LogMatches, []string{"foo", "bar"})
c.Check(tw.Log(), gc.Not(jc.LogMatches), []string{"foo", "bad"})
c.Check(tw.Log(), gc.Not(jc.LogMatches), []jc.SimpleMessage{
{loggo.INFO, "foo"},
{loggo.INFO, "bar"},
})
}
func (s *LogMatchesSuite) TestLogMatchesOnlyAcceptsSliceTestLogValues(c *gc.C) {
obtained := []string{"banana"} // specifically not []loggo.TestLogValues
expected := jc.SimpleMessages{}
result, err := jc.LogMatches.Check([]interface{}{obtained, expected}, nil)
c.Assert(result, gc.Equals, false)
c.Assert(err, gc.Equals, "Obtained value must be of type []loggo.Entry or SimpleMessage")
}
func (s *LogMatchesSuite) TestLogMatchesOnlyAcceptsStringOrSimpleMessages(c *gc.C) {
obtained := []loggo.Entry{
{Level: loggo.INFO, Message: "foo bar"},
{Level: loggo.INFO, Message: "baz"},
{Level: loggo.DEBUG, Message: "12345"},
}
expected := "totally wrong"
result, err := jc.LogMatches.Check([]interface{}{obtained, expected}, nil)
c.Assert(result, gc.Equals, false)
c.Assert(err, gc.Equals, "Expected value must be of type []string or []SimpleMessage")
}
func (s *LogMatchesSuite) TestLogMatchesFailsOnInvalidRegex(c *gc.C) {
var obtained interface{} = []loggo.Entry{{Level: loggo.INFO, Message: "foo bar"}}
var expected interface{} = []string{"[]foo"}
result, err := jc.LogMatches.Check([]interface{}{obtained, expected}, nil /* unused */)
c.Assert(result, gc.Equals, false)
c.Assert(err, gc.Equals, "bad message regexp \"[]foo\": error parsing regexp: missing closing ]: `[]foo`")
}
|