This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
forked from t0mk/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dblog_test.go
259 lines (222 loc) · 7.39 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package featuretests
import (
"time"
"github.com/juju/loggo"
jujutesting "github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/names.v2"
"gopkg.in/mgo.v2/bson"
"github.com/juju/juju/agent"
"github.com/juju/juju/api/common"
agentcmd "github.com/juju/juju/cmd/jujud/agent"
"github.com/juju/juju/cmd/jujud/agent/agenttest"
"github.com/juju/juju/state"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
"github.com/juju/juju/version"
"github.com/juju/juju/worker/logsender"
"github.com/juju/juju/worker/peergrouper"
)
// 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) TestMachineAgentLogsGoToDB(c *gc.C) {
foundLogs := s.runMachineAgentTest(c)
c.Assert(foundLogs, jc.IsTrue)
}
func (s *dblogSuite) TestUnitAgentLogsGoToDB(c *gc.C) {
foundLogs := s.runUnitAgentTest(c)
c.Assert(foundLogs, jc.IsTrue)
}
func (s *dblogSuite) runMachineAgentTest(c *gc.C) bool {
// Create a machine and an agent for it.
m, password := s.Factory.MakeMachineReturningPassword(c, &factory.MachineParams{
Nonce: agent.BootstrapNonce,
})
s.PrimeAgent(c, m.Tag(), password)
agentConf := agentcmd.NewAgentConf(s.DataDir())
agentConf.ReadConfig(m.Tag().String())
logger, err := logsender.InstallBufferedLogWriter(1000)
c.Assert(err, jc.ErrorIsNil)
machineAgentFactory := agentcmd.MachineAgentFactoryFn(
agentConf,
logger,
agentcmd.DefaultIntrospectionSocketName,
noPreUpgradeSteps,
c.MkDir(),
)
a, err := machineAgentFactory(m.Id())
c.Assert(err, jc.ErrorIsNil)
// Ensure there's no logs to begin with.
c.Assert(s.getLogCount(c, m.Tag()), gc.Equals, 0)
// Start the agent.
go func() { c.Check(a.Run(nil), jc.ErrorIsNil) }()
defer a.Stop()
return s.waitForLogs(c, m.Tag())
}
func (s *dblogSuite) runUnitAgentTest(c *gc.C) bool {
// Create a unit and an agent for it.
u, password := s.Factory.MakeUnitReturningPassword(c, nil)
s.PrimeAgent(c, u.Tag(), password)
logger, err := logsender.InstallBufferedLogWriter(1000)
c.Assert(err, jc.ErrorIsNil)
a, err := agentcmd.NewUnitAgent(nil, logger)
c.Assert(err, jc.ErrorIsNil)
s.InitAgent(c, a, "--unit-name", u.Name(), "--log-to-stderr=true")
// Ensure there's no logs to begin with.
c.Assert(s.getLogCount(c, u.Tag()), gc.Equals, 0)
// Start the agent.
go func() { c.Assert(a.Run(nil), jc.ErrorIsNil) }()
defer a.Stop()
return s.waitForLogs(c, u.Tag())
}
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")
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.
type debugLogDbSuite struct {
agenttest.AgentSuite
}
var _ = gc.Suite(&debugLogDbSuite{})
func (s *debugLogDbSuite) SetUpSuite(c *gc.C) {
// Restart mongod with a the replicaset enabled.
mongod := jujutesting.MgoServer
mongod.Params = []string{"--replSet", "juju"}
mongod.Restart()
// Initiate the replicaset.
info := mongod.DialInfo()
args := peergrouper.InitiateMongoParams{
DialInfo: info,
MemberHostPort: mongod.Addr(),
}
err := peergrouper.InitiateMongoServer(args)
c.Assert(err, jc.ErrorIsNil)
s.AgentSuite.SetUpSuite(c)
}
func (s *debugLogDbSuite) TearDownSuite(c *gc.C) {
// Restart mongod without the replicaset enabled so as not to
// affect other test that reply on this mongod instance in this
// package.
mongod := jujutesting.MgoServer
mongod.Params = []string{}
mongod.Restart()
s.AgentSuite.TearDownSuite(c)
}
func (s *debugLogDbSuite) TestLogsAPI(c *gc.C) {
dbLogger := state.NewEntityDbLogger(s.State, names.NewMachineTag("99"), version.Current)
defer dbLogger.Close()
t := time.Date(2015, 6, 23, 13, 8, 49, 0, time.UTC)
dbLogger.Log(t, "juju.foo", "code.go:42", loggo.INFO, "all is well")
dbLogger.Log(t.Add(time.Second), "juju.bar", "go.go:99", loggo.ERROR, "no it isn't")
messages := make(chan common.LogMessage)
go func(numMessages int) {
client := s.APIState.Client()
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.Assert(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: "machine-99",
Timestamp: t,
Severity: "INFO",
Module: "juju.foo",
Location: "code.go:42",
Message: "all is well",
})
assertMessage(common.LogMessage{
Entity: "machine-99",
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.
dbLogger.Log(t.Add(2*time.Second), "ju.jitsu", "no.go:3", loggo.WARNING, "beep beep")
assertMessage(common.LogMessage{
Entity: "machine-99",
Timestamp: t.Add(2 * time.Second),
Severity: "WARNING",
Module: "ju.jitsu",
Location: "no.go:3",
Message: "beep beep",
})
}
func (s *debugLogDbSuite) TestLogsUsesStartTime(c *gc.C) {
dbLogger := state.NewEntityDbLogger(s.State, names.NewMachineTag("99"), version.Current)
defer dbLogger.Close()
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)
dbLogger.Log(t1, "juju.foo", "code.go:42", loggo.INFO, "spinto band")
dbLogger.Log(t2, "juju.quux", "ok.go:101", loggo.INFO, "king gizzard and the lizard wizard")
dbLogger.Log(t3, "juju.bar", "go.go:99", loggo.ERROR, "born ruffians")
dbLogger.Log(t4, "juju.baz", "go.go.go:23", loggo.WARNING, "cold war kids")
client := s.APIState.Client()
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: "machine-99",
Timestamp: t3,
Severity: "ERROR",
Module: "juju.bar",
Location: "go.go:99",
Message: "born ruffians",
})
assertMessage(common.LogMessage{
Entity: "machine-99",
Timestamp: t4,
Severity: "WARNING",
Module: "juju.baz",
Location: "go.go.go:23",
Message: "cold war kids",
})
}