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

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/loggo/loggo"
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"launchpad.net/juju-core/cmd"
    13  	"launchpad.net/juju-core/testing"
    14  	"launchpad.net/juju-core/worker/uniter/jujuc"
    15  )
    16  
    17  type JujuLogSuite struct {
    18  	ContextSuite
    19  }
    20  
    21  var _ = gc.Suite(&JujuLogSuite{})
    22  
    23  func assertLogs(c *gc.C, ctx jujuc.Context, writer *loggo.TestWriter, unitname, badge string) {
    24  	msg1 := "the chickens"
    25  	msg2 := "are 110% AWESOME"
    26  	com, err := jujuc.NewCommand(ctx, "juju-log")
    27  	c.Assert(err, gc.IsNil)
    28  	for _, t := range []struct {
    29  		args  []string
    30  		level loggo.Level
    31  	}{
    32  		{
    33  			level: loggo.INFO,
    34  		}, {
    35  			args:  []string{"--debug"},
    36  			level: loggo.DEBUG,
    37  		}, {
    38  			args:  []string{"--log-level", "TRACE"},
    39  			level: loggo.TRACE,
    40  		}, {
    41  			args:  []string{"--log-level", "info"},
    42  			level: loggo.INFO,
    43  		}, {
    44  			args:  []string{"--log-level", "WaRnInG"},
    45  			level: loggo.WARNING,
    46  		}, {
    47  			args:  []string{"--log-level", "error"},
    48  			level: loggo.ERROR,
    49  		},
    50  	} {
    51  		writer.Clear()
    52  		c.Assert(err, gc.IsNil)
    53  
    54  		args := append(t.args, msg1, msg2)
    55  		code := cmd.Main(com, &cmd.Context{}, args)
    56  		c.Assert(code, gc.Equals, 0)
    57  		c.Assert(writer.Log, gc.HasLen, 1)
    58  		c.Assert(writer.Log[0].Level, gc.Equals, t.level)
    59  		c.Assert(writer.Log[0].Module, gc.Equals, fmt.Sprintf("unit.%s.juju-log", unitname))
    60  		c.Assert(writer.Log[0].Message, gc.Equals, fmt.Sprintf("%s%s %s", badge, msg1, msg2))
    61  	}
    62  }
    63  
    64  func (s *JujuLogSuite) TestBadges(c *gc.C) {
    65  	tw := &loggo.TestWriter{}
    66  	_, err := loggo.ReplaceDefaultWriter(tw)
    67  	loggo.GetLogger("unit").SetLogLevel(loggo.TRACE)
    68  	c.Assert(err, gc.IsNil)
    69  	hctx := s.GetHookContext(c, -1, "")
    70  	assertLogs(c, hctx, tw, "u/0", "")
    71  	hctx = s.GetHookContext(c, 1, "u/1")
    72  	assertLogs(c, hctx, tw, "u/0", "peer1:1: ")
    73  }
    74  
    75  func newJujuLogCommand(c *gc.C) cmd.Command {
    76  	ctx := &Context{}
    77  	com, err := jujuc.NewCommand(ctx, "juju-log")
    78  	c.Assert(err, gc.IsNil)
    79  	return com
    80  }
    81  
    82  func (s *JujuLogSuite) TestRequiresMessage(c *gc.C) {
    83  	com := newJujuLogCommand(c)
    84  	testing.TestInit(c, com, nil, "no message specified")
    85  }
    86  
    87  func (s *JujuLogSuite) TestLogInitMissingLevel(c *gc.C) {
    88  	com := newJujuLogCommand(c)
    89  	testing.TestInit(c, com, []string{"-l"}, "flag needs an argument.*")
    90  
    91  	com = newJujuLogCommand(c)
    92  	testing.TestInit(c, com, []string{"--log-level"}, "flag needs an argument.*")
    93  }
    94  
    95  func (s *JujuLogSuite) TestLogInitMissingMessage(c *gc.C) {
    96  	com := newJujuLogCommand(c)
    97  	testing.TestInit(c, com, []string{"-l", "FATAL"}, "no message specified")
    98  
    99  	com = newJujuLogCommand(c)
   100  	testing.TestInit(c, com, []string{"--log-level", "FATAL"}, "no message specified")
   101  }
   102  
   103  func (s *JujuLogSuite) TestLogDeprecation(c *gc.C) {
   104  	com := newJujuLogCommand(c)
   105  	ctx, err := testing.RunCommand(c, com, []string{"--format", "foo", "msg"})
   106  	c.Assert(err, gc.IsNil)
   107  	c.Assert(testing.Stderr(ctx), gc.Equals, "--format flag deprecated for command \"juju-log\"")
   108  }