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