gitee.com/mysnapcore/mysnapd@v0.1.0/logger/logger_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package logger_test 21 22 import ( 23 "bytes" 24 "encoding/json" 25 "io/ioutil" 26 "log" 27 "os" 28 "path/filepath" 29 "runtime" 30 "strings" 31 "testing" 32 "time" 33 34 . "gopkg.in/check.v1" 35 36 "gitee.com/mysnapcore/mysnapd/logger" 37 "gitee.com/mysnapcore/mysnapd/osutil" 38 "gitee.com/mysnapcore/mysnapd/testutil" 39 ) 40 41 // Hook up check.v1 into the "go test" runner 42 func Test(t *testing.T) { TestingT(t) } 43 44 var _ = Suite(&LogSuite{}) 45 46 type LogSuite struct { 47 testutil.BaseTest 48 logbuf *bytes.Buffer 49 restoreLogger func() 50 } 51 52 func (s *LogSuite) SetUpTest(c *C) { 53 s.BaseTest.SetUpTest(c) 54 s.logbuf, s.restoreLogger = logger.MockLogger() 55 } 56 57 func (s *LogSuite) TearDownTest(c *C) { 58 s.restoreLogger() 59 } 60 61 func (s *LogSuite) TestDefault(c *C) { 62 // env shenanigans 63 runtime.LockOSThread() 64 defer runtime.UnlockOSThread() 65 66 oldTerm, hadTerm := os.LookupEnv("TERM") 67 defer func() { 68 if hadTerm { 69 os.Setenv("TERM", oldTerm) 70 } else { 71 os.Unsetenv("TERM") 72 } 73 }() 74 75 if logger.GetLogger() != nil { 76 logger.SetLogger(nil) 77 } 78 c.Check(logger.GetLogger(), IsNil) 79 80 os.Setenv("TERM", "dumb") 81 err := logger.SimpleSetup() 82 c.Assert(err, IsNil) 83 c.Check(logger.GetLogger(), NotNil) 84 c.Check(logger.GetLoggerFlags(), Equals, logger.DefaultFlags) 85 86 os.Unsetenv("TERM") 87 err = logger.SimpleSetup() 88 c.Assert(err, IsNil) 89 c.Check(logger.GetLogger(), NotNil) 90 c.Check(logger.GetLoggerFlags(), Equals, log.Lshortfile) 91 } 92 93 func (s *LogSuite) TestNew(c *C) { 94 var buf bytes.Buffer 95 l, err := logger.New(&buf, logger.DefaultFlags) 96 c.Assert(err, IsNil) 97 c.Assert(l, NotNil) 98 } 99 100 func (s *LogSuite) TestDebugf(c *C) { 101 logger.Debugf("xyzzy") 102 c.Check(s.logbuf.String(), Equals, "") 103 } 104 105 func (s *LogSuite) TestDebugfEnv(c *C) { 106 os.Setenv("SNAPD_DEBUG", "1") 107 defer os.Unsetenv("SNAPD_DEBUG") 108 109 logger.Debugf("xyzzy") 110 c.Check(s.logbuf.String(), testutil.Contains, `DEBUG: xyzzy`) 111 } 112 113 func (s *LogSuite) TestNoticef(c *C) { 114 logger.Noticef("xyzzy") 115 c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`) 116 } 117 118 func (s *LogSuite) TestPanicf(c *C) { 119 c.Check(func() { logger.Panicf("xyzzy") }, Panics, "xyzzy") 120 c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: PANIC xyzzy`) 121 } 122 123 func (s *LogSuite) TestWithLoggerLock(c *C) { 124 logger.Noticef("xyzzy") 125 126 called := false 127 logger.WithLoggerLock(func() { 128 called = true 129 c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`) 130 }) 131 c.Check(called, Equals, true) 132 } 133 134 func (s *LogSuite) TestIntegrationDebugFromKernelCmdline(c *C) { 135 // must enable actually checking the command line, because by default the 136 // logger package will skip checking for the kernel command line parameter 137 // if it detects it is in a test because otherwise we would have to mock the 138 // cmdline in many many many more tests that end up using a logger 139 restore := logger.ProcCmdlineMustMock(false) 140 defer restore() 141 142 mockProcCmdline := filepath.Join(c.MkDir(), "proc-cmdline") 143 err := ioutil.WriteFile(mockProcCmdline, []byte("console=tty panic=-1 snapd.debug=1\n"), 0644) 144 c.Assert(err, IsNil) 145 restore = osutil.MockProcCmdline(mockProcCmdline) 146 defer restore() 147 148 var buf bytes.Buffer 149 l, err := logger.New(&buf, logger.DefaultFlags) 150 c.Assert(err, IsNil) 151 l.Debug("xyzzy") 152 c.Check(buf.String(), testutil.Contains, `DEBUG: xyzzy`) 153 } 154 155 func (s *LogSuite) TestStartupTimestampMsg(c *C) { 156 os.Setenv("SNAPD_DEBUG", "1") 157 defer os.Unsetenv("SNAPD_DEBUG") 158 159 type msgTimestamp struct { 160 Stage string `json:"stage"` 161 Time string `json:"time"` 162 } 163 164 now := time.Date(2022, time.May, 16, 10, 43, 12, 22312000, time.UTC) 165 logger.MockTimeNow(func() time.Time { 166 return now 167 }) 168 logger.StartupStageTimestamp("foo to bar") 169 msg := strings.TrimSpace(s.logbuf.String()) 170 c.Assert(msg, Matches, `.* DEBUG: -- snap startup \{"stage":"foo to bar", "time":"1652697792.022312"\}$`) 171 172 var m msgTimestamp 173 start := strings.LastIndex(msg, "{") 174 c.Assert(start, Not(Equals), -1) 175 stamp := msg[start:] 176 err := json.Unmarshal([]byte(stamp), &m) 177 c.Assert(err, IsNil) 178 c.Check(m, Equals, msgTimestamp{ 179 Stage: "foo to bar", 180 Time: "1652697792.022312", 181 }) 182 }