launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/testing/checkers/log_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package checkers_test
     5  
     6  import (
     7  	"github.com/loggo/loggo"
     8  	gc "launchpad.net/gocheck"
     9  
    10  	jc "launchpad.net/juju-core/testing/checkers"
    11  )
    12  
    13  type LogMatchesSuite struct{}
    14  
    15  var _ = gc.Suite(&LogMatchesSuite{})
    16  
    17  func (s *LogMatchesSuite) TestMatchSimpleMessage(c *gc.C) {
    18  	log := []loggo.TestLogValues{
    19  		{Level: loggo.INFO, Message: "foo bar"},
    20  		{Level: loggo.INFO, Message: "12345"},
    21  	}
    22  	c.Check(log, jc.LogMatches, []jc.SimpleMessage{
    23  		{loggo.INFO, "foo bar"},
    24  		{loggo.INFO, "12345"},
    25  	})
    26  	c.Check(log, jc.LogMatches, []jc.SimpleMessage{
    27  		{loggo.INFO, "foo .*"},
    28  		{loggo.INFO, "12345"},
    29  	})
    30  	// UNSPECIFIED means we don't care what the level is,
    31  	// just check the message string matches.
    32  	c.Check(log, jc.LogMatches, []jc.SimpleMessage{
    33  		{loggo.UNSPECIFIED, "foo .*"},
    34  		{loggo.INFO, "12345"},
    35  	})
    36  	c.Check(log, gc.Not(jc.LogMatches), []jc.SimpleMessage{
    37  		{loggo.INFO, "foo bar"},
    38  		{loggo.DEBUG, "12345"},
    39  	})
    40  }
    41  
    42  func (s *LogMatchesSuite) TestMatchStrings(c *gc.C) {
    43  	log := []loggo.TestLogValues{
    44  		{Level: loggo.INFO, Message: "foo bar"},
    45  		{Level: loggo.INFO, Message: "12345"},
    46  	}
    47  	c.Check(log, jc.LogMatches, []string{"foo bar", "12345"})
    48  	c.Check(log, jc.LogMatches, []string{"foo .*", "12345"})
    49  	c.Check(log, gc.Not(jc.LogMatches), []string{"baz", "bing"})
    50  }
    51  
    52  func (s *LogMatchesSuite) TestMatchInexact(c *gc.C) {
    53  	log := []loggo.TestLogValues{
    54  		{Level: loggo.INFO, Message: "foo bar"},
    55  		{Level: loggo.INFO, Message: "baz"},
    56  		{Level: loggo.DEBUG, Message: "12345"},
    57  		{Level: loggo.ERROR, Message: "12345"},
    58  		{Level: loggo.INFO, Message: "67890"},
    59  	}
    60  	c.Check(log, jc.LogMatches, []string{"foo bar", "12345"})
    61  	c.Check(log, jc.LogMatches, []string{"foo .*", "12345"})
    62  	c.Check(log, jc.LogMatches, []string{"foo .*", "67890"})
    63  	c.Check(log, jc.LogMatches, []string{"67890"})
    64  
    65  	// Matches are always left-most after the previous match.
    66  	c.Check(log, jc.LogMatches, []string{".*", "baz"})
    67  	c.Check(log, jc.LogMatches, []string{"foo bar", ".*", "12345"})
    68  	c.Check(log, jc.LogMatches, []string{"foo bar", ".*", "67890"})
    69  
    70  	// Order is important: 67890 advances to the last item in obtained,
    71  	// and so there's nothing after to match against ".*".
    72  	c.Check(log, gc.Not(jc.LogMatches), []string{"67890", ".*"})
    73  	// ALL specified patterns MUST match in the order given.
    74  	c.Check(log, gc.Not(jc.LogMatches), []string{".*", "foo bar"})
    75  
    76  	// Check that levels are matched.
    77  	c.Check(log, jc.LogMatches, []jc.SimpleMessage{
    78  		{loggo.UNSPECIFIED, "12345"},
    79  		{loggo.UNSPECIFIED, "12345"},
    80  	})
    81  	c.Check(log, jc.LogMatches, []jc.SimpleMessage{
    82  		{loggo.DEBUG, "12345"},
    83  		{loggo.ERROR, "12345"},
    84  	})
    85  	c.Check(log, jc.LogMatches, []jc.SimpleMessage{
    86  		{loggo.DEBUG, "12345"},
    87  		{loggo.INFO, ".*"},
    88  	})
    89  	c.Check(log, gc.Not(jc.LogMatches), []jc.SimpleMessage{
    90  		{loggo.DEBUG, "12345"},
    91  		{loggo.INFO, ".*"},
    92  		{loggo.UNSPECIFIED, ".*"},
    93  	})
    94  }
    95  
    96  func (s *LogMatchesSuite) TestFromLogMatches(c *gc.C) {
    97  	tw := &loggo.TestWriter{}
    98  	_, err := loggo.ReplaceDefaultWriter(tw)
    99  	c.Assert(err, gc.IsNil)
   100  	defer loggo.ResetWriters()
   101  	logger := loggo.GetLogger("test")
   102  	logger.SetLogLevel(loggo.DEBUG)
   103  	logger.Infof("foo")
   104  	logger.Debugf("bar")
   105  	logger.Tracef("hidden")
   106  	c.Check(tw.Log, jc.LogMatches, []string{"foo", "bar"})
   107  	c.Check(tw.Log, gc.Not(jc.LogMatches), []string{"foo", "bad"})
   108  	c.Check(tw.Log, gc.Not(jc.LogMatches), []jc.SimpleMessage{
   109  		{loggo.INFO, "foo"},
   110  		{loggo.INFO, "bar"},
   111  	})
   112  }