forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdblog_test.go
355 lines (317 loc) · 9.96 KB
/
dblog_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package featuretests
import (
"context"
"time"
"github.com/juju/cmd/v3/cmdtesting"
"github.com/juju/loggo"
"github.com/juju/mgo/v3/bson"
mgotesting "github.com/juju/mgo/v3/testing"
"github.com/juju/names/v5"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v3"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
"github.com/juju/juju/agent"
"github.com/juju/juju/agent/addons"
apiclient "github.com/juju/juju/api/client/client"
"github.com/juju/juju/api/common"
"github.com/juju/juju/caas/kubernetes/provider"
k8stesting "github.com/juju/juju/caas/kubernetes/provider/testing"
agentcmd "github.com/juju/juju/cmd/jujud/agent"
"github.com/juju/juju/cmd/jujud/agent/agentconf"
"github.com/juju/juju/cmd/jujud/agent/agenttest"
"github.com/juju/juju/controller"
"github.com/juju/juju/core/arch"
coredatabase "github.com/juju/juju/core/database"
corelogger "github.com/juju/juju/core/logger"
"github.com/juju/juju/database"
"github.com/juju/juju/state"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
jujuversion "github.com/juju/juju/version"
"github.com/juju/juju/worker/logsender"
)
// dblogSuite tests that logs flow correctly from the machine and unit
// agents over the API into MongoDB. These are very much integration
// tests with more detailed testing of the individual components
// being done in unit tests.
type dblogSuite struct {
agenttest.AgentSuite
}
func (s *dblogSuite) SetUpTest(c *gc.C) {
s.AgentSuite.SetUpTest(c)
}
func (s *dblogSuite) TestControllerAgentLogsGoToDBCAAS(c *gc.C) {
s.PatchValue(&provider.NewK8sClients, k8stesting.NoopFakeK8sClients)
// Set up a CAAS model to replace the IAAS one.
// Ensure major version 1 is used to prevent an upgrade
// from being attempted.
modelVers := jujuversion.Current
modelVers.Major = 1
extraAttrs := coretesting.Attrs{
"agent-version": modelVers.String(),
}
st := s.Factory.MakeCAASModel(c, &factory.ModelParams{ConfigAttrs: extraAttrs})
s.CleanupSuite.AddCleanup(func(*gc.C) { st.Close() })
s.State = st
s.Factory = factory.NewFactory(st, s.StatePool)
node, err := s.State.AddControllerNode()
c.Assert(err, jc.ErrorIsNil)
password, err := utils.RandomPassword()
c.Assert(err, jc.ErrorIsNil)
err = node.SetPassword(password)
c.Assert(err, jc.ErrorIsNil)
// Ensure controller config matches agent config so the agent worker
// does not exist with ErrRestartAgent.
err = s.State.UpdateControllerConfig(map[string]interface{}{
controller.MongoMemoryProfile: controller.MongoProfLow,
controller.QueryTracingEnabled: controller.DefaultQueryTracingEnabled,
controller.QueryTracingThreshold: controller.DefaultQueryTracingThreshold.String(),
}, nil)
c.Assert(err, jc.ErrorIsNil)
vers := version.Binary{
Number: jujuversion.Current,
Arch: arch.HostArch(),
Release: "kubernetes",
}
cfg, _ := s.PrimeAgentVersion(c, node.Tag(), password, vers)
logger := loggo.GetLogger("juju.featuretests")
err = database.BootstrapDqlite(
context.Background(),
database.NewNodeManager(cfg, true, logger, coredatabase.NoopSlowQueryLogger{}),
logger,
s.InitialDBOps...)
c.Assert(err, jc.ErrorIsNil)
s.assertAgentLogsGoToDB(c, node.Tag(), true)
}
func (s *dblogSuite) TestMachineAgentLogsGoToDBIAAS(c *gc.C) {
// Create a machine and an agent for it.
m, password := s.Factory.MakeMachineReturningPassword(c, &factory.MachineParams{
Nonce: agent.BootstrapNonce,
Base: state.UbuntuBase("12.10"),
})
s.PrimeAgent(c, m.Tag(), password)
s.assertAgentLogsGoToDB(c, m.Tag(), false)
}
func noPreUpgradeSteps(_ *state.StatePool, _ agent.Config, isController, isCaas bool) error {
return nil
}
func (s *dblogSuite) assertAgentLogsGoToDB(c *gc.C, tag names.Tag, isCaas bool) {
aCfg := agentconf.NewAgentConf(s.DataDir())
err := aCfg.ReadConfig(tag.String())
c.Assert(err, jc.ErrorIsNil)
logger, err := logsender.InstallBufferedLogWriter(loggo.DefaultContext(), 1000)
c.Assert(err, jc.ErrorIsNil)
machineAgentFactory := agentcmd.MachineAgentFactoryFn(
aCfg,
logger,
addons.DefaultIntrospectionSocketName,
noPreUpgradeSteps,
c.MkDir(),
)
a, err := machineAgentFactory(tag, isCaas)
c.Assert(err, jc.ErrorIsNil)
// Ensure there's no logs to begin with.
c.Assert(s.getLogCount(c, tag), gc.Equals, 0)
// Start the agent.
ctx := cmdtesting.Context(c)
go func() { c.Check(a.Run(ctx), jc.ErrorIsNil) }()
defer a.Stop()
foundLogs := s.waitForLogs(c, tag)
c.Assert(foundLogs, jc.IsTrue)
}
func (s *dblogSuite) getLogCount(c *gc.C, entity names.Tag) int {
// TODO(mjs) - replace this with State's functionality for reading
// logs from the DB, once it gets this. This will happen before
// the DB logging feature branch is merged.
logs := s.Session.DB("logs").C("logs." + s.State.ModelUUID())
count, err := logs.Find(bson.M{"n": entity.String()}).Count()
c.Assert(err, jc.ErrorIsNil)
return count
}
func (s *dblogSuite) waitForLogs(c *gc.C, entityTag names.Tag) bool {
for a := coretesting.LongAttempt.Start(); a.Next(); {
if s.getLogCount(c, entityTag) > 0 {
return true
}
}
return false
}
// debugLogDbSuite tests that the debuglog API works when logs are
// being read from the database.
// NOTE: the actual tests had to be split as the resetting causes
// mongo on bionic to have issues, see note below.
type debugLogDbSuite struct {
agenttest.AgentSuite
}
func (s *debugLogDbSuite) SetUpSuite(c *gc.C) {
mgotesting.MgoServer.Restart()
s.AgentSuite.SetUpSuite(c)
}
func (s *debugLogDbSuite) TearDownSuite(c *gc.C) {
mgotesting.MgoServer.Restart()
s.AgentSuite.TearDownSuite(c)
}
// NOTE: this is terrible, however due to a bug in mongod on bionic
// when resetting a mongo service with repl set on, we hit an inveriant bug
// which causes the second test to fail always.
// NOTE: do not merge with debugLogDbSuite2
type debugLogDbSuite1 struct {
debugLogDbSuite
}
func (s *debugLogDbSuite1) TestLogsAPI(c *gc.C) {
dbLogger := state.NewDbLogger(s.State)
defer dbLogger.Close()
t := time.Date(2015, 6, 23, 13, 8, 49, 0, time.UTC)
err := dbLogger.Log([]corelogger.LogRecord{{
Time: t,
Entity: "not-a-tag",
Version: jujuversion.Current,
Module: "juju.foo",
Location: "code.go:42",
Level: loggo.INFO,
Message: "all is well",
}, {
Time: t.Add(time.Second),
Entity: "not-a-tag",
Version: jujuversion.Current,
Module: "juju.bar",
Location: "go.go:99",
Level: loggo.ERROR,
Message: "no it isn't",
}})
c.Assert(err, jc.ErrorIsNil)
messages := make(chan common.LogMessage)
go func(numMessages int) {
client := apiclient.NewClient(s.APIState, coretesting.NoopLogger{})
logMessages, err := client.WatchDebugLog(common.DebugLogParams{})
c.Assert(err, jc.ErrorIsNil)
for n := 0; n < numMessages; n++ {
messages <- <-logMessages
}
}(3)
assertMessage := func(expected common.LogMessage) {
select {
case actual := <-messages:
c.Check(actual, jc.DeepEquals, expected)
case <-time.After(coretesting.LongWait):
c.Fatal("timed out waiting for log line")
}
}
// Read the 2 lines that are in the logs collection.
assertMessage(common.LogMessage{
Entity: "not-a-tag",
Timestamp: t,
Severity: "INFO",
Module: "juju.foo",
Location: "code.go:42",
Message: "all is well",
})
assertMessage(common.LogMessage{
Entity: "not-a-tag",
Timestamp: t.Add(time.Second),
Severity: "ERROR",
Module: "juju.bar",
Location: "go.go:99",
Message: "no it isn't",
})
// Now write and observe another log. This should be read from the oplog.
err = dbLogger.Log([]corelogger.LogRecord{{
Time: t.Add(2 * time.Second),
Entity: "not-a-tag",
Version: jujuversion.Current,
Module: "ju.jitsu",
Location: "no.go:3",
Level: loggo.WARNING,
Message: "beep beep",
}})
c.Assert(err, jc.ErrorIsNil)
assertMessage(common.LogMessage{
Entity: "not-a-tag",
Timestamp: t.Add(2 * time.Second),
Severity: "WARNING",
Module: "ju.jitsu",
Location: "no.go:3",
Message: "beep beep",
})
}
// NOTE: do not merge with debugLogDbSuite1
type debugLogDbSuite2 struct {
debugLogDbSuite
}
func (s *debugLogDbSuite2) TestLogsUsesStartTime(c *gc.C) {
dbLogger := state.NewDbLogger(s.State)
defer dbLogger.Close()
entity := "not-a-tag"
vers := jujuversion.Current
t1 := time.Date(2015, 6, 23, 13, 8, 49, 100, time.UTC)
// Check that start time has subsecond resolution.
t2 := time.Date(2015, 6, 23, 13, 8, 51, 50, time.UTC)
t3 := t1.Add(2 * time.Second)
t4 := t1.Add(4 * time.Second)
err := dbLogger.Log([]corelogger.LogRecord{{
Time: t1,
Entity: entity,
Version: vers,
Module: "juju.foo",
Location: "code.go:42",
Level: loggo.INFO,
Message: "spinto band",
}, {
Time: t2,
Entity: entity,
Version: vers,
Module: "juju.quux",
Location: "ok.go:101",
Level: loggo.INFO,
Message: "king gizzard and the lizard wizard",
}, {
Time: t3,
Entity: entity,
Version: vers,
Module: "juju.bar",
Location: "go.go:99",
Level: loggo.ERROR,
Message: "born ruffians",
}, {
Time: t4,
Entity: entity,
Version: vers,
Module: "juju.baz",
Location: "go.go.go:23",
Level: loggo.WARNING,
Message: "cold war kids",
}})
c.Assert(err, jc.ErrorIsNil)
client := apiclient.NewClient(s.APIState, coretesting.NoopLogger{})
logMessages, err := client.WatchDebugLog(common.DebugLogParams{
StartTime: t3,
})
c.Assert(err, jc.ErrorIsNil)
assertMessage := func(expected common.LogMessage) {
select {
case actual := <-logMessages:
c.Assert(actual, jc.DeepEquals, expected)
case <-time.After(coretesting.LongWait):
c.Fatal("timed out waiting for log line")
}
}
assertMessage(common.LogMessage{
Entity: "not-a-tag",
Timestamp: t3,
Severity: "ERROR",
Module: "juju.bar",
Location: "go.go:99",
Message: "born ruffians",
})
assertMessage(common.LogMessage{
Entity: "not-a-tag",
Timestamp: t4,
Severity: "WARNING",
Module: "juju.baz",
Location: "go.go.go:23",
Message: "cold war kids",
})
}