2011-10-22 15:15:46 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These are provided to ease testing code that uses the logging pkg
|
|
|
|
|
2011-11-05 05:56:45 +00:00
|
|
|
// TODO(fluffle): Assumes at most one logging line will be written
|
|
|
|
// between calls to Expect*. Change to be Expect(exp []string)?
|
|
|
|
|
2011-10-22 15:15:46 +00:00
|
|
|
type mockWriter struct {
|
|
|
|
written []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *mockWriter) Write(p []byte) (n int, err os.Error) {
|
|
|
|
w.written = append(w.written, p...)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2011-11-05 05:56:45 +00:00
|
|
|
func (w *mockWriter) getLine() string {
|
|
|
|
// 20 bytes covers the date and time in
|
|
|
|
// 2011/10/22 10:22:57 <file>:<line>: <level> <log message>
|
|
|
|
if len(w.written) < 20 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
s := string(w.written)
|
|
|
|
idx := strings.Index(s, "\n")
|
|
|
|
s = s[20:idx]
|
|
|
|
w.written = w.written[idx+1:]
|
|
|
|
// consume '<file>:<line>: '
|
|
|
|
idx = strings.Index(s, ":") + 1
|
|
|
|
idx += strings.Index(s[idx:], ":") + 2
|
|
|
|
return s[idx:]
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:15:46 +00:00
|
|
|
func (w *mockWriter) reset() {
|
|
|
|
w.written = w.written[:0]
|
|
|
|
}
|
|
|
|
|
2011-11-05 05:56:45 +00:00
|
|
|
type WriterMap struct {
|
|
|
|
t *testing.T
|
|
|
|
m map[LogLevel]*mockWriter
|
|
|
|
}
|
2011-10-22 15:15:46 +00:00
|
|
|
|
|
|
|
// This doesn't create a mock Logger but a Logger that writes to mock outputs
|
2011-11-05 05:56:45 +00:00
|
|
|
func NewMock(t *testing.T) (*logger, *WriterMap) {
|
|
|
|
wMap := &WriterMap{
|
|
|
|
t: t,
|
|
|
|
m: map[LogLevel]*mockWriter{
|
|
|
|
Debug: &mockWriter{make([]byte, 0)},
|
|
|
|
Info: &mockWriter{make([]byte, 0)},
|
|
|
|
Warn: &mockWriter{make([]byte, 0)},
|
|
|
|
Error: &mockWriter{make([]byte, 0)},
|
|
|
|
Fatal: &mockWriter{make([]byte, 0)},
|
|
|
|
},
|
2011-10-22 15:15:46 +00:00
|
|
|
}
|
|
|
|
logMap := make(LogMap)
|
2011-11-05 05:56:45 +00:00
|
|
|
for lv, w := range wMap.m {
|
2011-10-22 15:15:46 +00:00
|
|
|
logMap[lv] = makeLogger(w)
|
|
|
|
}
|
2011-10-27 17:24:08 +00:00
|
|
|
// Set the default log level high enough that everything will get logged
|
|
|
|
return New(logMap, (1 << 31) - 1, false), wMap
|
2011-10-22 15:15:46 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 05:56:45 +00:00
|
|
|
// When you expect something to be logged but don't care so much what level at.
|
|
|
|
func (wm *WriterMap) Expect(exp string) {
|
|
|
|
found := false
|
|
|
|
for lv, w := range wm.m {
|
|
|
|
if s := w.getLine(); s != "" && !found {
|
|
|
|
// Since we don't know what log level we're expecting, compare
|
|
|
|
// exp against the log line with the level stripped.
|
|
|
|
idx := strings.Index(s, " ") + 1
|
|
|
|
if s[idx:] == exp {
|
|
|
|
found = true
|
|
|
|
} else {
|
|
|
|
wm.t.Errorf("Unexpected log message encountered at level %s:",
|
|
|
|
LogString(lv))
|
|
|
|
wm.t.Errorf("exp: %s\ngot: %s", exp, s[idx:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wm.ExpectNothing()
|
|
|
|
if !found {
|
|
|
|
wm.t.Errorf("Expected log message not encountered:")
|
|
|
|
wm.t.Errorf("exp: %s", exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// When you expect nothing to be logged
|
|
|
|
func (wm *WriterMap) ExpectNothing() {
|
|
|
|
for lv, w := range wm.m {
|
|
|
|
if s := w.getLine(); s != "" {
|
|
|
|
wm.t.Errorf("Unexpected log message at level %s:",
|
|
|
|
LogString(lv))
|
|
|
|
wm.t.Errorf("%s", s)
|
2011-10-22 15:15:46 +00:00
|
|
|
w.reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-05 05:56:45 +00:00
|
|
|
// When you expect something to be logged at a specific level.
|
|
|
|
func (wm *WriterMap) ExpectAt(lv LogLevel, exp string) {
|
2011-10-22 15:15:46 +00:00
|
|
|
var w *mockWriter
|
2011-11-05 05:56:45 +00:00
|
|
|
if _, ok := wm.m[lv]; !ok {
|
|
|
|
w = wm.m[Debug]
|
2011-10-22 15:15:46 +00:00
|
|
|
} else {
|
2011-11-05 05:56:45 +00:00
|
|
|
w = wm.m[lv]
|
2011-10-22 15:15:46 +00:00
|
|
|
}
|
2011-11-05 05:56:45 +00:00
|
|
|
s := w.getLine()
|
|
|
|
exp = strings.Join([]string{LogString(lv), exp}, " ")
|
|
|
|
if s == "" {
|
|
|
|
wm.t.Errorf("Nothing logged at level %s:", LogString(lv))
|
|
|
|
wm.t.Errorf("exp: %s", exp)
|
2011-10-27 16:48:15 +00:00
|
|
|
// Check nothing was written to a different log level here, too.
|
2011-11-05 05:56:45 +00:00
|
|
|
wm.ExpectNothing()
|
2011-10-22 15:59:28 +00:00
|
|
|
return
|
|
|
|
}
|
2011-10-22 15:15:46 +00:00
|
|
|
if s != exp {
|
2011-11-05 05:56:45 +00:00
|
|
|
wm.t.Errorf("Log message at level %s differed.", LogString(lv))
|
|
|
|
wm.t.Errorf("exp: %s\ngot: %s", exp, s)
|
2011-10-22 15:15:46 +00:00
|
|
|
}
|
2011-11-05 05:56:45 +00:00
|
|
|
wm.ExpectNothing()
|
2011-10-22 15:15:46 +00:00
|
|
|
}
|