github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/debuglog_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "io" 8 "io/ioutil" 9 "strings" 10 11 "github.com/juju/loggo" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/api" 16 "github.com/juju/juju/cmd/envcmd" 17 "github.com/juju/juju/testing" 18 ) 19 20 type DebugLogSuite struct { 21 testing.FakeJujuHomeSuite 22 } 23 24 var _ = gc.Suite(&DebugLogSuite{}) 25 26 func (s *DebugLogSuite) TestArgParsing(c *gc.C) { 27 for i, test := range []struct { 28 args []string 29 expected api.DebugLogParams 30 errMatch string 31 }{ 32 { 33 expected: api.DebugLogParams{ 34 Backlog: 10, 35 }, 36 }, { 37 args: []string{"-n0"}, 38 }, { 39 args: []string{"--lines=50"}, 40 expected: api.DebugLogParams{ 41 Backlog: 50, 42 }, 43 }, { 44 args: []string{"-l", "foo"}, 45 errMatch: `level value "foo" is not one of "TRACE", "DEBUG", "INFO", "WARNING", "ERROR"`, 46 }, { 47 args: []string{"--level=INFO"}, 48 expected: api.DebugLogParams{ 49 Backlog: 10, 50 Level: loggo.INFO, 51 }, 52 }, { 53 args: []string{"--include", "machine-1", "-i", "machine-2"}, 54 expected: api.DebugLogParams{ 55 IncludeEntity: []string{"machine-1", "machine-2"}, 56 Backlog: 10, 57 }, 58 }, { 59 args: []string{"--exclude", "machine-1", "-x", "machine-2"}, 60 expected: api.DebugLogParams{ 61 ExcludeEntity: []string{"machine-1", "machine-2"}, 62 Backlog: 10, 63 }, 64 }, { 65 args: []string{"--include-module", "juju.foo", "--include-module", "unit"}, 66 expected: api.DebugLogParams{ 67 IncludeModule: []string{"juju.foo", "unit"}, 68 Backlog: 10, 69 }, 70 }, { 71 args: []string{"--exclude-module", "juju.foo", "--exclude-module", "unit"}, 72 expected: api.DebugLogParams{ 73 ExcludeModule: []string{"juju.foo", "unit"}, 74 Backlog: 10, 75 }, 76 }, { 77 args: []string{"--replay"}, 78 expected: api.DebugLogParams{ 79 Backlog: 10, 80 Replay: true, 81 }, 82 }, { 83 args: []string{"--limit", "100"}, 84 expected: api.DebugLogParams{ 85 Backlog: 10, 86 Limit: 100, 87 }, 88 }, 89 } { 90 c.Logf("test %v", i) 91 command := &DebugLogCommand{} 92 err := testing.InitCommand(envcmd.Wrap(command), test.args) 93 if test.errMatch == "" { 94 c.Check(err, jc.ErrorIsNil) 95 c.Check(command.params, jc.DeepEquals, test.expected) 96 } else { 97 c.Check(err, gc.ErrorMatches, test.errMatch) 98 } 99 } 100 } 101 102 func (s *DebugLogSuite) TestParamsPassed(c *gc.C) { 103 fake := &fakeDebugLogAPI{} 104 s.PatchValue(&getDebugLogAPI, func(_ *DebugLogCommand) (DebugLogAPI, error) { 105 return fake, nil 106 }) 107 _, err := testing.RunCommand(c, envcmd.Wrap(&DebugLogCommand{}), 108 "-i", "machine-1*", "-x", "machine-1-lxc-1", 109 "--include-module=juju.provisioner", 110 "--lines=500", 111 "--level=WARNING", 112 ) 113 c.Assert(err, jc.ErrorIsNil) 114 c.Assert(fake.params, gc.DeepEquals, api.DebugLogParams{ 115 IncludeEntity: []string{"machine-1*"}, 116 IncludeModule: []string{"juju.provisioner"}, 117 ExcludeEntity: []string{"machine-1-lxc-1"}, 118 Backlog: 500, 119 Level: loggo.WARNING, 120 }) 121 } 122 123 func (s *DebugLogSuite) TestLogOutput(c *gc.C) { 124 s.PatchValue(&getDebugLogAPI, func(_ *DebugLogCommand) (DebugLogAPI, error) { 125 return &fakeDebugLogAPI{log: "this is the log output"}, nil 126 }) 127 ctx, err := testing.RunCommand(c, envcmd.Wrap(&DebugLogCommand{})) 128 c.Assert(err, jc.ErrorIsNil) 129 c.Assert(testing.Stdout(ctx), gc.Equals, "this is the log output") 130 } 131 132 func newFakeDebugLogAPI(log string) DebugLogAPI { 133 return &fakeDebugLogAPI{log: log} 134 } 135 136 type fakeDebugLogAPI struct { 137 log string 138 params api.DebugLogParams 139 err error 140 } 141 142 func (fake *fakeDebugLogAPI) WatchDebugLog(params api.DebugLogParams) (io.ReadCloser, error) { 143 if fake.err != nil { 144 return nil, fake.err 145 } 146 fake.params = params 147 return ioutil.NopCloser(strings.NewReader(fake.log)), nil 148 } 149 150 func (fake *fakeDebugLogAPI) Close() error { 151 return nil 152 }