2018-07-18 00:40:09 +00:00
|
|
|
https = require 'https'
|
|
|
|
stream = require 'stream'
|
|
|
|
zlib = require 'zlib'
|
|
|
|
|
|
|
|
Promise = require 'bluebird'
|
2017-11-01 06:47:48 +00:00
|
|
|
m = require 'mochainon'
|
|
|
|
{ expect } = m.chai
|
2018-07-18 00:40:09 +00:00
|
|
|
{ stub } = m.sinon
|
2017-11-01 06:47:48 +00:00
|
|
|
|
2018-08-27 00:17:11 +00:00
|
|
|
{ Logger } = require '../src/logger'
|
2017-11-01 06:47:48 +00:00
|
|
|
describe 'Logger', ->
|
2018-07-18 00:40:09 +00:00
|
|
|
beforeEach ->
|
|
|
|
@_req = new stream.PassThrough()
|
|
|
|
@_req.flushHeaders = m.sinon.spy()
|
|
|
|
@_req.end = m.sinon.spy()
|
|
|
|
|
|
|
|
@_req.body = ''
|
|
|
|
@_req
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.on 'data', (chunk) =>
|
|
|
|
@_req.body += chunk
|
|
|
|
|
|
|
|
stub(https, 'request').returns(@_req)
|
|
|
|
|
2017-11-01 06:47:48 +00:00
|
|
|
@fakeEventTracker = {
|
2018-07-18 00:40:09 +00:00
|
|
|
track: m.sinon.spy()
|
2017-11-01 06:47:48 +00:00
|
|
|
}
|
2018-07-18 00:40:09 +00:00
|
|
|
|
2018-11-02 14:15:01 +00:00
|
|
|
@logger = new Logger({ eventTracker: @fakeEventTracker })
|
2018-07-18 00:40:09 +00:00
|
|
|
@logger.init({
|
|
|
|
apiEndpoint: 'https://example.com'
|
|
|
|
uuid: 'deadbeef'
|
|
|
|
deviceApiKey: 'secretkey'
|
|
|
|
offlineMode: false
|
2018-08-27 00:17:11 +00:00
|
|
|
enableLogs: true
|
2018-10-09 11:02:38 +00:00
|
|
|
localMode: false
|
2018-07-18 00:40:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
https.request.restore()
|
|
|
|
|
|
|
|
it 'waits the grace period before sending any logs', ->
|
|
|
|
clock = m.sinon.useFakeTimers()
|
2018-11-02 14:15:01 +00:00
|
|
|
@logger.log({ message: 'foobar', serviceId: 15 })
|
2018-07-18 00:40:09 +00:00
|
|
|
clock.tick(4999)
|
|
|
|
clock.restore()
|
|
|
|
|
2018-10-25 20:36:53 +00:00
|
|
|
Promise.delay(100)
|
2018-07-18 00:40:09 +00:00
|
|
|
.then =>
|
|
|
|
expect(@_req.body).to.equal('')
|
|
|
|
|
|
|
|
it 'tears down the connection after inactivity', ->
|
|
|
|
clock = m.sinon.useFakeTimers()
|
2018-11-02 14:15:01 +00:00
|
|
|
@logger.log({ message: 'foobar', serviceId: 15 })
|
2018-07-18 00:40:09 +00:00
|
|
|
clock.tick(61000)
|
|
|
|
clock.restore()
|
|
|
|
|
2018-10-25 20:36:53 +00:00
|
|
|
Promise.delay(100)
|
2018-07-18 00:40:09 +00:00
|
|
|
.then =>
|
|
|
|
expect(@_req.end.calledOnce).to.be.true
|
|
|
|
|
|
|
|
|
|
|
|
it 'sends logs as gzipped ndjson', ->
|
|
|
|
clock = m.sinon.useFakeTimers()
|
|
|
|
@logger.log({ message: 'foobar', serviceId: 15 })
|
|
|
|
@logger.log({ timestamp: 1337, message: 'foobar', serviceId: 15 })
|
|
|
|
@logger.log({ message: 'foobar' }) # shold be ignored
|
|
|
|
clock.tick(10000)
|
|
|
|
clock.restore()
|
|
|
|
|
|
|
|
expect(https.request.calledOnce).to.be.true
|
|
|
|
opts = https.request.firstCall.args[0]
|
|
|
|
|
|
|
|
expect(opts.href).to.equal('https://example.com/device/v2/deadbeef/log-stream')
|
|
|
|
expect(opts.method).to.equal('POST')
|
|
|
|
expect(opts.headers).to.deep.equal({
|
|
|
|
'Authorization': 'Bearer secretkey'
|
|
|
|
'Content-Type': 'application/x-ndjson'
|
|
|
|
'Content-Encoding': 'gzip'
|
|
|
|
})
|
|
|
|
|
|
|
|
# small delay for the streams to propagate data
|
2018-10-25 20:36:53 +00:00
|
|
|
Promise.delay(100)
|
2018-07-18 00:40:09 +00:00
|
|
|
.then =>
|
|
|
|
lines = @_req.body.split('\n')
|
|
|
|
expect(lines.length).to.equal(3)
|
|
|
|
expect(lines[2]).to.equal('')
|
|
|
|
|
|
|
|
msg = JSON.parse(lines[0])
|
|
|
|
expect(msg).to.deep.equal({ timestamp: 0, message: 'foobar', serviceId: 15 })
|
|
|
|
msg = JSON.parse(lines[1])
|
|
|
|
expect(msg).to.deep.equal({ timestamp: 1337, message: 'foobar', serviceId: 15 })
|
|
|
|
|
|
|
|
it 'allows logging system messages which are also reported to the eventTracker', ->
|
|
|
|
clock = m.sinon.useFakeTimers()
|
2017-11-01 06:47:48 +00:00
|
|
|
@logger.logSystemMessage('Hello there!', { someProp: 'someVal' }, 'Some event name')
|
2018-07-18 00:40:09 +00:00
|
|
|
clock.tick(10000)
|
2017-11-01 06:47:48 +00:00
|
|
|
clock.restore()
|
2018-07-18 00:40:09 +00:00
|
|
|
|
2018-10-25 20:36:53 +00:00
|
|
|
Promise.delay(100)
|
2018-07-18 00:40:09 +00:00
|
|
|
.then =>
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(@fakeEventTracker.track).to.be.calledWith('Some event name', { someProp: 'someVal' })
|
2018-07-18 00:40:09 +00:00
|
|
|
lines = @_req.body.split('\n')
|
|
|
|
expect(lines.length).to.equal(2)
|
|
|
|
expect(lines[1]).to.equal('')
|
|
|
|
|
|
|
|
msg = JSON.parse(lines[0])
|
|
|
|
expect(msg).to.deep.equal({ message: 'Hello there!', timestamp: 0, isSystem: true })
|
2018-09-21 14:49:43 +00:00
|
|
|
|
|
|
|
it 'should support non-tty log lines', ->
|
|
|
|
message = '\u0001\u0000\u0000\u0000\u0000\u0000\u0000?2018-09-21T12:37:09.819134000Z this is the message'
|
|
|
|
buffer = Buffer.from(message)
|
|
|
|
|
|
|
|
expect(Logger.extractContainerMessage(buffer)).to.deep.equal({
|
|
|
|
message: 'this is the message',
|
|
|
|
timestamp: 1537533429819
|
|
|
|
})
|