github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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/testutil"
    35  )
    36  
    37  // Hook up check.v1 into the "go test" runner
    38  func Test(t *testing.T) { TestingT(t) }
    39  
    40  var _ = Suite(&LogSuite{})
    41  
    42  type LogSuite struct {
    43  	logbuf        *bytes.Buffer
    44  	restoreLogger func()
    45  }
    46  
    47  func (s *LogSuite) SetUpTest(c *C) {
    48  	s.logbuf, s.restoreLogger = logger.MockLogger()
    49  }
    50  
    51  func (s *LogSuite) TearDownTest(c *C) {
    52  	s.restoreLogger()
    53  }
    54  
    55  func (s *LogSuite) TestDefault(c *C) {
    56  	// env shenanigans
    57  	runtime.LockOSThread()
    58  	defer runtime.UnlockOSThread()
    59  
    60  	oldTerm, hadTerm := os.LookupEnv("TERM")
    61  	defer func() {
    62  		if hadTerm {
    63  			os.Setenv("TERM", oldTerm)
    64  		} else {
    65  			os.Unsetenv("TERM")
    66  		}
    67  	}()
    68  
    69  	if logger.GetLogger() != nil {
    70  		logger.SetLogger(nil)
    71  	}
    72  	c.Check(logger.GetLogger(), IsNil)
    73  
    74  	os.Setenv("TERM", "dumb")
    75  	err := logger.SimpleSetup()
    76  	c.Assert(err, IsNil)
    77  	c.Check(logger.GetLogger(), NotNil)
    78  	c.Check(logger.GetLoggerFlags(), Equals, logger.DefaultFlags)
    79  
    80  	os.Unsetenv("TERM")
    81  	err = logger.SimpleSetup()
    82  	c.Assert(err, IsNil)
    83  	c.Check(logger.GetLogger(), NotNil)
    84  	c.Check(logger.GetLoggerFlags(), Equals, log.Lshortfile)
    85  }
    86  
    87  func (s *LogSuite) TestNew(c *C) {
    88  	var buf bytes.Buffer
    89  	l, err := logger.New(&buf, logger.DefaultFlags)
    90  	c.Assert(err, IsNil)
    91  	c.Assert(l, NotNil)
    92  }
    93  
    94  func (s *LogSuite) TestDebugf(c *C) {
    95  	logger.Debugf("xyzzy")
    96  	c.Check(s.logbuf.String(), Equals, "")
    97  }
    98  
    99  func (s *LogSuite) TestDebugfEnv(c *C) {
   100  	os.Setenv("SNAPD_DEBUG", "1")
   101  	defer os.Unsetenv("SNAPD_DEBUG")
   102  
   103  	logger.Debugf("xyzzy")
   104  	c.Check(s.logbuf.String(), testutil.Contains, `DEBUG: xyzzy`)
   105  }
   106  
   107  func (s *LogSuite) TestNoticef(c *C) {
   108  	logger.Noticef("xyzzy")
   109  	c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`)
   110  }
   111  
   112  func (s *LogSuite) TestPanicf(c *C) {
   113  	c.Check(func() { logger.Panicf("xyzzy") }, Panics, "xyzzy")
   114  	c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: PANIC xyzzy`)
   115  }
   116  
   117  func (s *LogSuite) TestWithLoggerLock(c *C) {
   118  	logger.Noticef("xyzzy")
   119  
   120  	called := false
   121  	logger.WithLoggerLock(func() {
   122  		called = true
   123  		c.Check(s.logbuf.String(), Matches, `(?m).*logger_test\.go:\d+: xyzzy`)
   124  	})
   125  	c.Check(called, Equals, true)
   126  }
   127  
   128  func (s *LogSuite) TestIntegrationDebugFromKernelCmdline(c *C) {
   129  	mockProcCmdline := filepath.Join(c.MkDir(), "proc-cmdline")
   130  	err := ioutil.WriteFile(mockProcCmdline, []byte("console=tty panic=-1 snapd.debug=1"), 0644)
   131  	c.Assert(err, IsNil)
   132  	restore := logger.MockProcCmdline(mockProcCmdline)
   133  	defer restore()
   134  
   135  	var buf bytes.Buffer
   136  	l, err := logger.New(&buf, logger.DefaultFlags)
   137  	c.Assert(err, IsNil)
   138  	l.Debug("xyzzy")
   139  	c.Check(buf.String(), testutil.Contains, `DEBUG: xyzzy`)
   140  }