-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogstream_test.go
237 lines (209 loc) · 5.93 KB
/
logstream_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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package logstream_test
import (
"net/url"
"time"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/api/base"
basetesting "github.com/juju/juju/api/base/testing"
"github.com/juju/juju/api/logstream"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/logfwd"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/version"
)
type LogReaderSuite struct {
coretesting.BaseSuite
}
var _ = gc.Suite(&LogReaderSuite{})
func (s *LogReaderSuite) TestOpenFullConfig(c *gc.C) {
cUUID := "feebdaed-2f18-4fd2-967d-db9663db7bea"
stub := &testing.Stub{}
conn := &mockConnector{stub: stub}
stream := mockStream{stub: stub}
conn.ReturnConnectStream = stream
cfg := params.LogStreamConfig{
AllModels: true,
Sink: "spam",
}
_, err := logstream.Open(conn, cfg, cUUID)
c.Assert(err, gc.IsNil)
stub.CheckCallNames(c, "ConnectStream")
stub.CheckCall(c, 0, "ConnectStream", `/logstream`, url.Values{
"all": []string{"true"},
"sink": []string{"spam"},
})
}
func (s *LogReaderSuite) TestOpenError(c *gc.C) {
cUUID := "feebdaed-2f18-4fd2-967d-db9663db7bea"
stub := &testing.Stub{}
conn := &mockConnector{stub: stub}
failure := errors.New("foo")
stub.SetErrors(failure)
var cfg params.LogStreamConfig
_, err := logstream.Open(conn, cfg, cUUID)
c.Check(err, gc.ErrorMatches, "cannot connect to /logstream: foo")
stub.CheckCallNames(c, "ConnectStream")
}
func (s *LogReaderSuite) TestNextOneRecord(c *gc.C) {
ts := time.Now()
apiRec := params.LogStreamRecord{
ModelUUID: "deadbeef-2f18-4fd2-967d-db9663db7bea",
Entity: "machine-99",
Version: version.Current.String(),
Timestamp: ts,
Module: "api.logstream.test",
Location: "test.go:42",
Level: loggo.INFO.String(),
Message: "test message",
}
apiRecords := params.LogStreamRecords{
Records: []params.LogStreamRecord{apiRec},
}
cUUID := "feebdaed-2f18-4fd2-967d-db9663db7bea"
stub := &testing.Stub{}
conn := &mockConnector{stub: stub}
jsonReader := mockStream{stub: stub}
logsCh := make(chan params.LogStreamRecords, 1)
logsCh <- apiRecords
jsonReader.ReturnReadJSON = logsCh
conn.ReturnConnectStream = jsonReader
var cfg params.LogStreamConfig
stream, err := logstream.Open(conn, cfg, cUUID)
c.Assert(err, gc.IsNil)
stub.ResetCalls()
// Check the record we injected into the stream.
var records []logfwd.Record
done := make(chan struct{})
go func() {
records, err = stream.Next()
c.Assert(err, jc.ErrorIsNil)
close(done)
}()
select {
case <-done:
case <-time.After(coretesting.LongWait):
c.Errorf("timed out waiting for record")
}
c.Assert(records, gc.HasLen, 1)
c.Check(records[0], jc.DeepEquals, logfwd.Record{
Origin: logfwd.Origin{
ControllerUUID: cUUID,
ModelUUID: "deadbeef-2f18-4fd2-967d-db9663db7bea",
Hostname: "machine-99.deadbeef-2f18-4fd2-967d-db9663db7bea",
Type: logfwd.OriginTypeMachine,
Name: "99",
Software: logfwd.Software{
PrivateEnterpriseNumber: 28978,
Name: "jujud-machine-agent",
Version: version.Current,
},
},
Timestamp: ts,
Level: loggo.INFO,
Location: logfwd.SourceLocation{
Module: "api.logstream.test",
Filename: "test.go",
Line: 42,
},
Message: "test message",
})
stub.CheckCallNames(c, "ReadJSON")
// Make sure we don't get extras.
done = make(chan struct{})
go func() {
records, err = stream.Next()
c.Assert(err, jc.ErrorIsNil)
close(done)
}()
select {
case <-done:
c.Errorf("got extra record: %#v", records)
case <-time.After(coretesting.ShortWait):
}
}
func (s *LogReaderSuite) TestNextError(c *gc.C) {
cUUID := "feebdaed-2f18-4fd2-967d-db9663db7bea"
stub := &testing.Stub{}
conn := &mockConnector{stub: stub}
jsonReader := mockStream{stub: stub}
conn.ReturnConnectStream = jsonReader
failure := errors.New("an error")
stub.SetErrors(nil, failure)
var cfg params.LogStreamConfig
stream, err := logstream.Open(conn, cfg, cUUID)
c.Assert(err, gc.IsNil)
var nextErr error
done := make(chan struct{})
go func() {
_, nextErr = stream.Next()
c.Check(errors.Cause(nextErr), gc.Equals, failure)
close(done)
}()
select {
case <-done:
case <-time.After(coretesting.LongWait):
c.Errorf("timed out waiting for record")
}
stub.CheckCallNames(c, "ConnectStream", "ReadJSON")
}
func (s *LogReaderSuite) TestClose(c *gc.C) {
cUUID := "feebdaed-2f18-4fd2-967d-db9663db7bea"
stub := &testing.Stub{}
conn := &mockConnector{stub: stub}
jsonReader := mockStream{stub: stub}
conn.ReturnConnectStream = jsonReader
var cfg params.LogStreamConfig
stream, err := logstream.Open(conn, cfg, cUUID)
c.Assert(err, gc.IsNil)
stub.ResetCalls()
err = stream.Close()
c.Assert(err, jc.ErrorIsNil)
err = stream.Close() // idempotent
c.Assert(err, jc.ErrorIsNil)
_, err = stream.Next()
c.Check(err, gc.ErrorMatches, `cannot read from closed stream`)
stub.CheckCallNames(c, "Close")
}
type mockConnector struct {
basetesting.APICallerFunc
stub *testing.Stub
ReturnConnectStream base.Stream
}
func (c *mockConnector) ConnectStream(path string, values url.Values) (base.Stream, error) {
c.stub.AddCall("ConnectStream", path, values)
if err := c.stub.NextErr(); err != nil {
return nil, errors.Trace(err)
}
return c.ReturnConnectStream, nil
}
type mockStream struct {
base.Stream
stub *testing.Stub
ReturnReadJSON chan params.LogStreamRecords
}
func (s mockStream) ReadJSON(v interface{}) error {
s.stub.AddCall("ReadJSON", v)
if err := s.stub.NextErr(); err != nil {
return errors.Trace(err)
}
switch vt := v.(type) {
case *params.LogStreamRecords:
*vt = <-s.ReturnReadJSON
return nil
default:
return errors.Errorf("unexpected output type: %T", v)
}
}
func (s mockStream) Close() error {
s.stub.AddCall("Close")
if err := s.stub.NextErr(); err != nil {
return errors.Trace(err)
}
return nil
}