github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/modelcmd" 17 "github.com/juju/juju/testing" 18 ) 19 20 type DebugLogSuite struct { 21 testing.FakeJujuXDGDataHomeSuite 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{"--no-tail"}, 84 expected: api.DebugLogParams{ 85 Backlog: 10, 86 NoTail: true, 87 }, 88 }, { 89 args: []string{"--limit", "100"}, 90 expected: api.DebugLogParams{ 91 Backlog: 10, 92 Limit: 100, 93 }, 94 }, 95 } { 96 c.Logf("test %v", i) 97 command := &debugLogCommand{} 98 err := testing.InitCommand(modelcmd.Wrap(command), test.args) 99 if test.errMatch == "" { 100 c.Check(err, jc.ErrorIsNil) 101 c.Check(command.params, jc.DeepEquals, test.expected) 102 } else { 103 c.Check(err, gc.ErrorMatches, test.errMatch) 104 } 105 } 106 } 107 108 func (s *DebugLogSuite) TestParamsPassed(c *gc.C) { 109 fake := &fakeDebugLogAPI{} 110 s.PatchValue(&getDebugLogAPI, func(_ *debugLogCommand) (DebugLogAPI, error) { 111 return fake, nil 112 }) 113 _, err := testing.RunCommand(c, newDebugLogCommand(), 114 "-i", "machine-1*", "-x", "machine-1-lxc-1", 115 "--include-module=juju.provisioner", 116 "--lines=500", 117 "--level=WARNING", 118 "--no-tail", 119 ) 120 c.Assert(err, jc.ErrorIsNil) 121 c.Assert(fake.params, gc.DeepEquals, api.DebugLogParams{ 122 IncludeEntity: []string{"machine-1*"}, 123 IncludeModule: []string{"juju.provisioner"}, 124 ExcludeEntity: []string{"machine-1-lxc-1"}, 125 Backlog: 500, 126 Level: loggo.WARNING, 127 NoTail: true, 128 }) 129 } 130 131 func (s *DebugLogSuite) TestLogOutput(c *gc.C) { 132 s.PatchValue(&getDebugLogAPI, func(_ *debugLogCommand) (DebugLogAPI, error) { 133 return &fakeDebugLogAPI{log: "this is the log output"}, nil 134 }) 135 ctx, err := testing.RunCommand(c, newDebugLogCommand()) 136 c.Assert(err, jc.ErrorIsNil) 137 c.Assert(testing.Stdout(ctx), gc.Equals, "this is the log output") 138 } 139 140 func newFakeDebugLogAPI(log string) DebugLogAPI { 141 return &fakeDebugLogAPI{log: log} 142 } 143 144 type fakeDebugLogAPI struct { 145 log string 146 params api.DebugLogParams 147 err error 148 } 149 150 func (fake *fakeDebugLogAPI) WatchDebugLog(params api.DebugLogParams) (io.ReadCloser, error) { 151 if fake.err != nil { 152 return nil, fake.err 153 } 154 fake.params = params 155 return ioutil.NopCloser(strings.NewReader(fake.log)), nil 156 } 157 158 func (fake *fakeDebugLogAPI) Close() error { 159 return nil 160 }