-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
243 lines (201 loc) · 7.18 KB
/
logging_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.
package cmd_test
import (
"io/ioutil"
"path/filepath"
"github.com/juju/loggo/v2"
"github.com/juju/testing"
gc "gopkg.in/check.v1"
"github.com/juju/cmd/v4"
"github.com/juju/cmd/v4/cmdtesting"
)
var logger = loggo.GetLogger("juju.test")
type LogSuite struct {
testing.LoggingCleanupSuite
}
var _ = gc.Suite(&LogSuite{})
func newLogWithFlags(c *gc.C, defaultConfig string, flags ...string) *cmd.Log {
log := &cmd.Log{
DefaultConfig: defaultConfig,
}
flagSet := cmdtesting.NewFlagSet()
log.AddFlags(flagSet)
err := flagSet.Parse(false, flags)
c.Assert(err, gc.IsNil)
return log
}
func (s *LogSuite) TestNoFlags(c *gc.C) {
log := newLogWithFlags(c, "")
c.Assert(log.Path, gc.Equals, "")
c.Assert(log.Quiet, gc.Equals, false)
c.Assert(log.Verbose, gc.Equals, false)
c.Assert(log.Debug, gc.Equals, false)
c.Assert(log.Config, gc.Equals, "")
}
func (s *LogSuite) TestFlags(c *gc.C) {
log := newLogWithFlags(c, "", "--log-file", "foo", "--verbose", "--debug", "--show-log",
"--logging-config=juju.cmd=INFO;juju.worker.deployer=DEBUG")
c.Assert(log.Path, gc.Equals, "foo")
c.Assert(log.Verbose, gc.Equals, true)
c.Assert(log.Debug, gc.Equals, true)
c.Assert(log.ShowLog, gc.Equals, true)
c.Assert(log.Config, gc.Equals, "juju.cmd=INFO;juju.worker.deployer=DEBUG")
}
func (s *LogSuite) TestLogConfigFromDefault(c *gc.C) {
config := "juju.cmd=INFO;juju.worker.deployer=DEBUG"
log := newLogWithFlags(c, config)
log.DefaultConfig = config
c.Assert(log.Path, gc.Equals, "")
c.Assert(log.Verbose, gc.Equals, false)
c.Assert(log.Debug, gc.Equals, false)
c.Assert(log.Config, gc.Equals, config)
}
func (s *LogSuite) TestDebugSetsLogLevel(c *gc.C) {
l := &cmd.Log{Debug: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
c.Assert(loggo.GetLogger("").LogLevel(), gc.Equals, loggo.DEBUG)
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
}
func (s *LogSuite) TestShowLogSetsLogLevel(c *gc.C) {
l := &cmd.Log{ShowLog: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
c.Assert(loggo.GetLogger("").LogLevel(), gc.Equals, loggo.INFO)
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
}
func (s *LogSuite) TestStderr(c *gc.C) {
l := &cmd.Log{ShowLog: true, Config: "<root>=INFO"}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
logger.Infof("hello")
c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.* INFO .* hello\n`)
}
func (s *LogSuite) TestRelPathLog(c *gc.C) {
l := &cmd.Log{Path: "foo.log", Config: "<root>=INFO"}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
logger.Infof("hello")
content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log"))
c.Assert(err, gc.IsNil)
c.Assert(string(content), gc.Matches, `^.* INFO .* hello\n`)
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
}
func (s *LogSuite) TestAbsPathLog(c *gc.C) {
path := filepath.Join(c.MkDir(), "foo.log")
l := &cmd.Log{Path: path, Config: "<root>=INFO"}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
logger.Infof("hello")
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
content, err := ioutil.ReadFile(path)
c.Assert(err, gc.IsNil)
c.Assert(string(content), gc.Matches, `^.* INFO .* hello\n`)
}
func (s *LogSuite) TestLoggingToFileAndStderr(c *gc.C) {
l := &cmd.Log{Path: "foo.log", Config: "<root>=INFO", ShowLog: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
logger.Infof("hello")
content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log"))
c.Assert(err, gc.IsNil)
c.Assert(string(content), gc.Matches, `^.* INFO .* hello\n`)
c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.* INFO .* hello\n`)
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
}
func (s *LogSuite) TestErrorAndWarningLoggingToStderr(c *gc.C) {
// Error and warning go to stderr even with ShowLog=false
l := &cmd.Log{Config: "<root>=INFO", ShowLog: false}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
logger.Warningf("a warning")
logger.Errorf("an error")
logger.Infof("an info")
c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.*WARNING a warning\n.*ERROR an error\n.*`)
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
}
func (s *LogSuite) TestQuietAndVerbose(c *gc.C) {
l := &cmd.Log{Verbose: true, Quiet: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.ErrorMatches, `"verbose" and "quiet" flags clash, please use one or the other, not both`)
}
func (s *LogSuite) TestOutputDefault(c *gc.C) {
l := &cmd.Log{}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Infof("Writing info output")
ctx.Verbosef("Writing verbose output")
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Writing info output\n")
}
func (s *LogSuite) TestOutputVerbose(c *gc.C) {
l := &cmd.Log{Verbose: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Infof("Writing info output")
ctx.Verbosef("Writing verbose output")
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Writing info output\nWriting verbose output\n")
}
func (s *LogSuite) TestOutputQuiet(c *gc.C) {
l := &cmd.Log{Quiet: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Infof("Writing info output")
ctx.Verbosef("Writing verbose output")
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
}
func (s *LogSuite) TestOutputQuietLogs(c *gc.C) {
l := &cmd.Log{Quiet: true, Path: "foo.log", Config: "<root>=INFO"}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Infof("Writing info output")
ctx.Verbosef("Writing verbose output")
content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log"))
c.Assert(err, gc.IsNil)
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
c.Assert(string(content), gc.Matches, `^.*INFO .* Writing info output\n.*INFO .*Writing verbose output\n.*`)
}
func (s *LogSuite) TestOutputDefaultLogsVerbose(c *gc.C) {
l := &cmd.Log{Path: "foo.log", Config: "<root>=INFO"}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Infof("Writing info output")
ctx.Verbosef("Writing verbose output")
content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log"))
c.Assert(err, gc.IsNil)
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Writing info output\n")
c.Assert(string(content), gc.Matches, `^.*INFO .*Writing verbose output\n.*`)
}
func (s *LogSuite) TestOutputDebugForcesQuiet(c *gc.C) {
l := &cmd.Log{Verbose: true, Debug: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Infof("Writing info output")
ctx.Verbosef("Writing verbose output")
c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.*INFO .* Writing info output\n.*INFO .*Writing verbose output\n.*`)
}
func (s *LogSuite) TestOutputWarning(c *gc.C) {
l := &cmd.Log{Verbose: true, Debug: true}
ctx := cmdtesting.Context(c)
err := l.Start(ctx)
c.Assert(err, gc.IsNil)
ctx.Warningf("Writing warning output")
c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.* WARN .* Writing warning output\n.*`)
}