github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/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  	"io/ioutil"
    25  	"log"
    26  	"os"
    27  	"path/filepath"
    28  	"runtime"
    29  	"testing"
    30  
    31  	. "gopkg.in/check.v1"
    32  
    33  	"github.com/snapcore/snapd/logger"
    34  	"github.com/snapcore/snapd/osutil"
    35  	"github.com/snapcore/snapd/testutil"
    36  )
    37  
    38  // Hook up check.v1 into the "go test" runner
    39  func Test(t *testing.T) { TestingT(t) }
    40  
    41  var _ = Suite(&LogSuite{})
    42  
    43  type LogSuite struct {
    44  	logbuf        *bytes.Buffer
    45  	restoreLogger func()
    46  }
    47  
    48  func (s *LogSuite) SetUpTest(c *C) {
    49  	s.logbuf, s.restoreLogger = logger.MockLogger()
    50  }
    51  
    52  func (s *LogSuite) TearDownTest(c *C) {
    53  	s.restoreLogger()
    54  }
    55  
    56  func (s *LogSuite) TestDefault(c *C) {
    57  	// env shenanigans
    58  	runtime.LockOSThread()
    59  	defer runtime.UnlockOSThread()
    60  
    61  	oldTerm, hadTerm := os.LookupEnv("TERM")
    62  	defer func() {
    63  		if hadTerm {
    64  			os.Setenv("TERM", oldTerm)
    65  		} else {
    66  			os.Unsetenv("TERM")
    67  		}
    68  	}()
    69  
    70  	if logger.GetLogger() != nil {
    71  		logger.SetLogger(nil)
    72  	}
    73  	c.Check(logger.GetLogger(), IsNil)
    74  
    75  	os.Setenv("TERM", "dumb")
    76  	err := logger.SimpleSetup()
    77  	c.Assert(err, IsNil)
    78  	c.Check(logger.GetLogger(), NotNil)
    79  	c.Check(logger.GetLoggerFlags(), Equals, logger.DefaultFlags)
    80  
    81  	os.Unsetenv("TERM")
    82  	err = logger.SimpleSetup()
    83  	c.Assert(err, IsNil)
    84  	c.Check(logger.GetLogger(), NotNil)
    85  	c.Check(logger.GetLoggerFlags(), Equals, log.Lshortfile)
    86  }
    87  
    88  func (s *LogSuite) TestNew(c *C) {
    89  	var buf bytes.Buffer
    90  	l, err := logger.New(&buf, logger.DefaultFlags)
    91  	c.Assert(err, IsNil)
    92  	c.Assert(l, NotNil)
    93  }
    94  
    95  func (s *LogSuite) TestDebugf(c *C) {
    96  	logger.Debugf("xyzzy")
    97  	c.Check(s.logbuf.String(), Equals, "")
    98  }
    99  
   100  func (s *LogSuite) TestDebugfEnv(c *C) {
   101  	os.Setenv("SNAPD_DEBUG", "1")
   102  	defer os.Unsetenv("SNAPD_DEBUG")
   103  
   104  	logger.Debugf("xyzzy")
   105  	c.Check(s.logbuf.String(), testutil.Contains, `DEBUG: xyzzy`)
   106  }
   107  
   108  func (s *LogSuite) TestNoticef(c *C) {
   109  	logger.Noticef("xyzzy")
   110  	c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`)
   111  }
   112  
   113  func (s *LogSuite) TestPanicf(c *C) {
   114  	c.Check(func() { logger.Panicf("xyzzy") }, Panics, "xyzzy")
   115  	c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: PANIC xyzzy`)
   116  }
   117  
   118  func (s *LogSuite) TestWithLoggerLock(c *C) {
   119  	logger.Noticef("xyzzy")
   120  
   121  	called := false
   122  	logger.WithLoggerLock(func() {
   123  		called = true
   124  		c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`)
   125  	})
   126  	c.Check(called, Equals, true)
   127  }
   128  
   129  func (s *LogSuite) TestIntegrationDebugFromKernelCmdline(c *C) {
   130  	// must enable actually checking the command line, because by default the
   131  	// logger package will skip checking for the kernel command line parameter
   132  	// if it detects it is in a test because otherwise we would have to mock the
   133  	// cmdline in many many many more tests that end up using a logger
   134  	restore := logger.ProcCmdlineMustMock(false)
   135  	defer restore()
   136  
   137  	mockProcCmdline := filepath.Join(c.MkDir(), "proc-cmdline")
   138  	err := ioutil.WriteFile(mockProcCmdline, []byte("console=tty panic=-1 snapd.debug=1\n"), 0644)
   139  	c.Assert(err, IsNil)
   140  	restore = osutil.MockProcCmdline(mockProcCmdline)
   141  	defer restore()
   142  
   143  	var buf bytes.Buffer
   144  	l, err := logger.New(&buf, logger.DefaultFlags)
   145  	c.Assert(err, IsNil)
   146  	l.Debug("xyzzy")
   147  	c.Check(buf.String(), testutil.Contains, `DEBUG: xyzzy`)
   148  }