| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package logger |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
"os" |
| 24 |
"regexp" |
| 25 |
"runtime" |
| 26 |
"testing" |
| 27 |
|
| 28 |
. "gopkg.in/check.v1" |
| 29 |
|
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/config" |
| 31 |
) |
| 32 |
|
| 33 |
func TestLogger(t *testing.T) { TestingT(t) } |
| 34 |
|
| 35 |
type loggerSuite struct{} |
| 36 |
|
| 37 |
var _ = Suite(&loggerSuite{}) |
| 38 |
|
| 39 |
func (s *loggerSuite) TestErrorf(c *C) { |
| 40 |
buf := &bytes.Buffer{} |
| 41 |
logger := NewSimpleLogger(buf, "error") |
| 42 |
logger.Errorf("%v %d", "error", 1) |
| 43 |
c.Check(buf.String(), Matches, ".* ERROR error 1\n") |
| 44 |
} |
| 45 |
|
| 46 |
func (s *loggerSuite) TestFatalf(c *C) { |
| 47 |
defer func() { |
| 48 |
osExit = os.Exit |
| 49 |
}() |
| 50 |
var exitCode int |
| 51 |
osExit = func(code int) { |
| 52 |
exitCode = code |
| 53 |
} |
| 54 |
buf := &bytes.Buffer{} |
| 55 |
logger := NewSimpleLogger(buf, "error") |
| 56 |
logger.Fatalf("%v %v", "error", "fatal") |
| 57 |
c.Check(buf.String(), Matches, ".* ERROR error fatal\n") |
| 58 |
c.Check(exitCode, Equals, 1) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *loggerSuite) TestInfof(c *C) { |
| 62 |
buf := &bytes.Buffer{} |
| 63 |
logger := NewSimpleLogger(buf, "info") |
| 64 |
logger.Infof("%v %d", "info", 1) |
| 65 |
c.Check(buf.String(), Matches, ".* INFO info 1\n") |
| 66 |
} |
| 67 |
|
| 68 |
func (s *loggerSuite) TestDebugf(c *C) { |
| 69 |
buf := &bytes.Buffer{} |
| 70 |
logger := NewSimpleLogger(buf, "debug") |
| 71 |
logger.Debugf("%v %d", "debug", 1) |
| 72 |
c.Check(buf.String(), Matches, `.* DEBUG debug 1\n`) |
| 73 |
} |
| 74 |
|
| 75 |
func (s *loggerSuite) TestFormat(c *C) { |
| 76 |
buf := &bytes.Buffer{} |
| 77 |
logger := NewSimpleLogger(buf, "error") |
| 78 |
logger.Errorf("%v %d", "error", 2) |
| 79 |
c.Check(buf.String(), Matches, `.* .*\.\d+ ERROR error 2\n`) |
| 80 |
} |
| 81 |
|
| 82 |
func (s *loggerSuite) TestLevel(c *C) { |
| 83 |
buf := &bytes.Buffer{} |
| 84 |
logger := NewSimpleLogger(buf, "error") |
| 85 |
logger.Errorf("%s%d", "e", 3) |
| 86 |
logger.Infof("%s%d", "i", 3) |
| 87 |
logger.Debugf("%s%d", "d", 3) |
| 88 |
c.Check(buf.String(), Matches, `.* ERROR e3\n`) |
| 89 |
|
| 90 |
buf.Reset() |
| 91 |
logger = NewSimpleLogger(buf, "info") |
| 92 |
logger.Errorf("%s%d", "e", 4) |
| 93 |
logger.Debugf("%s%d", "d", 4) |
| 94 |
logger.Infof("%s%d", "i", 4) |
| 95 |
c.Check(buf.String(), Matches, `.* ERROR e4\n.* INFO i4\n`) |
| 96 |
|
| 97 |
buf.Reset() |
| 98 |
logger = NewSimpleLogger(buf, "debug") |
| 99 |
logger.Errorf("%s%d", "e", 5) |
| 100 |
logger.Debugf("%s%d", "d", 5) |
| 101 |
logger.Infof("%s%d", "i", 5) |
| 102 |
c.Check(buf.String(), Matches, `.* ERROR e5\n.* DEBUG d5\n.* INFO i5\n`) |
| 103 |
} |
| 104 |
|
| 105 |
func panicAndRecover(logger Logger, n int, doPanic bool, line *int, ok *bool) { |
| 106 |
defer func() { |
| 107 |
if err := recover(); err != nil { |
| 108 |
logger.PanicStackf("%v %d", err, n) |
| 109 |
} |
| 110 |
}() |
| 111 |
_, _, *line, *ok = runtime.Caller(0) |
| 112 |
if doPanic { |
| 113 |
panic("Troubles") // @ line + 2 |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (s *loggerSuite) TestPanicStackfPanicScenario(c *C) { |
| 118 |
buf := &bytes.Buffer{} |
| 119 |
logger := NewSimpleLogger(buf, "error") |
| 120 |
var line int |
| 121 |
var ok bool |
| 122 |
panicAndRecover(logger, 6, true, &line, &ok) |
| 123 |
c.Assert(ok, Equals, true) |
| 124 |
c.Check(buf.String(), Matches, fmt.Sprintf("(?s).* ERROR\\(PANIC\\) Troubles 6:.*panicAndRecover.*logger_test.go:%d.*", line+2)) |
| 125 |
} |
| 126 |
|
| 127 |
func (s *loggerSuite) TestPanicStackfNoPanicScenario(c *C) { |
| 128 |
buf := &bytes.Buffer{} |
| 129 |
logger := NewSimpleLogger(buf, "error") |
| 130 |
var line int |
| 131 |
var ok bool |
| 132 |
panicAndRecover(logger, 6, false, &line, &ok) |
| 133 |
c.Check(buf.String(), Equals, "") |
| 134 |
} |
| 135 |
|
| 136 |
func (s *loggerSuite) TestReexposeOutput(c *C) { |
| 137 |
buf := &bytes.Buffer{} |
| 138 |
baselog := log.New(buf, "", log.Lshortfile) |
| 139 |
logger := NewSimpleLoggerFromMinimalLogger(baselog, "error") |
| 140 |
baselog.Output(1, "foobar") |
| 141 |
logger.Output(1, "foobaz") |
| 142 |
|
| 143 |
out := buf.String() |
| 144 |
|
| 145 |
/* Workaround for https://github.com/golang/go/issues/16406 which is |
| 146 |
wontfix with a comment about a need to upgrade. */ |
| 147 |
re := regexp.MustCompile(".*linux64\\.S.*|.*sysv\\.S.*") |
| 148 |
if re.MatchString(out) { |
| 149 |
c.Skip("log.Lshortfile failed to provide the expected output") |
| 150 |
} |
| 151 |
|
| 152 |
c.Check(out, Matches, "logger_test.go:[0-9]+: foobar\n(logger_test|value).go:[0-9]+: foobaz\n") |
| 153 |
} |
| 154 |
|
| 155 |
type testLogLevelConfig struct { |
| 156 |
Lvl ConfigLogLevel |
| 157 |
} |
| 158 |
|
| 159 |
func (s *loggerSuite) TestReadConfigLogLevel(c *C) { |
| 160 |
buf := bytes.NewBufferString(`{"lvl": "debug"}`) |
| 161 |
var cfg testLogLevelConfig |
| 162 |
err := config.ReadConfig(buf, &cfg) |
| 163 |
c.Assert(err, IsNil) |
| 164 |
c.Check(cfg.Lvl.Level(), Equals, "debug") |
| 165 |
} |
| 166 |
|
| 167 |
func (s *loggerSuite) TestReadConfigLogLevelErrors(c *C) { |
| 168 |
var cfg testLogLevelConfig |
| 169 |
checkError := func(jsonCfg string, expectedError string) { |
| 170 |
buf := bytes.NewBufferString(jsonCfg) |
| 171 |
err := config.ReadConfig(buf, &cfg) |
| 172 |
c.Check(err, ErrorMatches, expectedError) |
| 173 |
} |
| 174 |
checkError(`{"lvl": 1}`, "lvl:.*type string") |
| 175 |
checkError(`{"lvl": "foo"}`, "lvl: not a log level: foo") |
| 176 |
} |
| 177 |
|
| 178 |
func (s *loggerSuite) TestLogLineNo(c *C) { |
| 179 |
buf := &bytes.Buffer{} |
| 180 |
logger := NewSimpleLogger(buf, "debug") |
| 181 |
logger.Output(1, "foobaz") |
| 182 |
|
| 183 |
out := buf.String() |
| 184 |
|
| 185 |
/* Workaround for https://github.com/golang/go/issues/16406 which is |
| 186 |
wontfix with a comment about a need to upgrade. */ |
| 187 |
re := regexp.MustCompile(".*linux64\\.S.*|.*sysv\\.S.*") |
| 188 |
if re.MatchString(out) { |
| 189 |
c.Skip("log.Lshortfile failed to provide the expected output") |
| 190 |
} |
| 191 |
c.Check(out, Matches, ".* .* (logger_test|value).go:[0-9]+: foobaz\n") |
| 192 |
|
| 193 |
buf.Reset() |
| 194 |
logger = NewSimpleLogger(buf, "error") |
| 195 |
logger.Output(1, "foobaz") |
| 196 |
c.Check(buf.String(), Matches, ".* .* foobaz\n") |
| 197 |
} |