-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathlogger_test.go
150 lines (124 loc) · 3.79 KB
/
logger_test.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package logger_test
import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/testutil"
)
// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }
var _ = Suite(&LogSuite{})
type LogSuite struct {
testutil.BaseTest
logbuf *bytes.Buffer
restoreLogger func()
}
func (s *LogSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.logbuf, s.restoreLogger = logger.MockLogger()
}
func (s *LogSuite) TearDownTest(c *C) {
s.restoreLogger()
}
func (s *LogSuite) TestDefault(c *C) {
// env shenanigans
runtime.LockOSThread()
defer runtime.UnlockOSThread()
oldTerm, hadTerm := os.LookupEnv("TERM")
defer func() {
if hadTerm {
os.Setenv("TERM", oldTerm)
} else {
os.Unsetenv("TERM")
}
}()
if logger.GetLogger() != nil {
logger.SetLogger(nil)
}
c.Check(logger.GetLogger(), IsNil)
os.Setenv("TERM", "dumb")
err := logger.SimpleSetup()
c.Assert(err, IsNil)
c.Check(logger.GetLogger(), NotNil)
c.Check(logger.GetLoggerFlags(), Equals, logger.DefaultFlags)
os.Unsetenv("TERM")
err = logger.SimpleSetup()
c.Assert(err, IsNil)
c.Check(logger.GetLogger(), NotNil)
c.Check(logger.GetLoggerFlags(), Equals, log.Lshortfile)
}
func (s *LogSuite) TestNew(c *C) {
var buf bytes.Buffer
l, err := logger.New(&buf, logger.DefaultFlags)
c.Assert(err, IsNil)
c.Assert(l, NotNil)
}
func (s *LogSuite) TestDebugf(c *C) {
logger.Debugf("xyzzy")
c.Check(s.logbuf.String(), Equals, "")
}
func (s *LogSuite) TestDebugfEnv(c *C) {
os.Setenv("SNAPD_DEBUG", "1")
defer os.Unsetenv("SNAPD_DEBUG")
logger.Debugf("xyzzy")
c.Check(s.logbuf.String(), testutil.Contains, `DEBUG: xyzzy`)
}
func (s *LogSuite) TestNoticef(c *C) {
logger.Noticef("xyzzy")
c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`)
}
func (s *LogSuite) TestPanicf(c *C) {
c.Check(func() { logger.Panicf("xyzzy") }, Panics, "xyzzy")
c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: PANIC xyzzy`)
}
func (s *LogSuite) TestWithLoggerLock(c *C) {
logger.Noticef("xyzzy")
called := false
logger.WithLoggerLock(func() {
called = true
c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`)
})
c.Check(called, Equals, true)
}
func (s *LogSuite) TestIntegrationDebugFromKernelCmdline(c *C) {
// must enable actually checking the command line, because by default the
// logger package will skip checking for the kernel command line parameter
// if it detects it is in a test because otherwise we would have to mock the
// cmdline in many many many more tests that end up using a logger
restore := logger.ProcCmdlineMustMock(false)
defer restore()
mockProcCmdline := filepath.Join(c.MkDir(), "proc-cmdline")
err := ioutil.WriteFile(mockProcCmdline, []byte("console=tty panic=-1 snapd.debug=1\n"), 0644)
c.Assert(err, IsNil)
restore = osutil.MockProcCmdline(mockProcCmdline)
defer restore()
var buf bytes.Buffer
l, err := logger.New(&buf, logger.DefaultFlags)
c.Assert(err, IsNil)
l.Debug("xyzzy")
c.Check(buf.String(), testutil.Contains, `DEBUG: xyzzy`)
}