overleaf/libraries/logger/test/unit/loggingManagerTests.js

378 lines
12 KiB
JavaScript
Raw Normal View History

2019-03-08 11:06:24 -05:00
const SandboxedModule = require('sandboxed-module')
const bunyan = require('bunyan')
const chai = require('chai')
const path = require('path')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
2019-03-06 15:08:38 -05:00
2019-03-08 11:06:24 -05:00
chai.use(sinonChai)
chai.should()
2019-03-06 15:08:38 -05:00
2019-03-08 11:06:24 -05:00
const modulePath = path.join(__dirname, '../../logging-manager.js')
2019-03-08 11:06:24 -05:00
describe('LoggingManager', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.start = Date.now()
this.clock = sinon.useFakeTimers(this.start)
this.captureException = sinon.stub()
this.mockBunyanLogger = {
debug: sinon.stub(),
error: sinon.stub(),
fatal: sinon.stub(),
info: sinon.stub(),
level: sinon.stub(),
warn: sinon.stub()
2019-03-08 11:06:24 -05:00
}
this.mockRavenClient = {
captureException: this.captureException,
once: sinon.stub().yields()
2019-03-08 11:06:24 -05:00
}
this.LoggingManager = SandboxedModule.require(modulePath, {
globals: { console },
requires: {
bunyan: (this.Bunyan = {
createLogger: sinon.stub().returns(this.mockBunyanLogger),
RingBuffer: bunyan.RingBuffer
}),
raven: (this.Raven = {
Client: sinon.stub().returns(this.mockRavenClient)
}),
request: (this.Request = sinon.stub())
}
2019-03-08 11:06:24 -05:00
})
this.loggerName = 'test'
this.logger = this.LoggingManager.initialize(this.loggerName)
this.logger.initializeErrorReporting('test_dsn')
})
afterEach(function() {
2019-03-08 11:06:24 -05:00
this.clock.restore()
})
2019-03-08 11:06:24 -05:00
describe('initialize', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.checkLogLevelStub = sinon.stub(this.LoggingManager, 'checkLogLevel')
this.Bunyan.createLogger.reset()
})
afterEach(function() {
2019-03-08 11:06:24 -05:00
this.checkLogLevelStub.restore()
})
2019-03-08 11:06:24 -05:00
describe('not in production', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.logger = this.LoggingManager.initialize(this.loggerName)
})
2019-03-08 11:06:24 -05:00
it('should default to log level debug', function() {
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
2019-03-08 11:06:24 -05:00
'debug'
)
})
2019-03-08 11:06:24 -05:00
it('should not run checkLogLevel', function() {
this.checkLogLevelStub.should.not.have.been.called
})
})
2019-03-08 11:06:24 -05:00
describe('in production', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
process.env.NODE_ENV = 'production'
this.logger = this.LoggingManager.initialize(this.loggerName)
})
2019-03-08 11:06:24 -05:00
afterEach(() => delete process.env.NODE_ENV)
2019-03-08 11:06:24 -05:00
it('should default to log level warn', function() {
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
2019-03-08 11:06:24 -05:00
'warn'
)
})
it('should run checkLogLevel', function() {
this.checkLogLevelStub.should.have.been.calledOnce
})
describe('after 1 minute', () =>
it('should run checkLogLevel again', function() {
this.clock.tick(61 * 1000)
this.checkLogLevelStub.should.have.been.calledTwice
}))
describe('after 2 minutes', () =>
it('should run checkLogLevel again', function() {
this.clock.tick(121 * 1000)
this.checkLogLevelStub.should.have.been.calledThrice
}))
})
describe('when LOG_LEVEL set in env', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
process.env.LOG_LEVEL = 'trace'
this.LoggingManager.initialize()
})
2019-03-08 11:06:24 -05:00
afterEach(() => delete process.env.LOG_LEVEL)
2019-03-08 11:06:24 -05:00
it('should use custom log level', function() {
this.Bunyan.createLogger.firstCall.args[0].streams[0].level.should.equal(
2019-03-08 11:06:24 -05:00
'trace'
)
})
})
})
2019-03-08 11:06:24 -05:00
describe('bunyan logging', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.logArgs = [{ foo: 'bar' }, 'foo', 'bar']
})
it('should log debug', function() {
this.logger.debug(this.logArgs)
this.mockBunyanLogger.debug.should.have.been.calledWith(this.logArgs)
})
it('should log error', function() {
this.logger.error(this.logArgs)
this.mockBunyanLogger.error.should.have.been.calledWith(this.logArgs)
})
it('should log fatal', function() {
this.logger.fatal(this.logArgs)
this.mockBunyanLogger.fatal.should.have.been.calledWith(this.logArgs)
})
it('should log info', function() {
this.logger.info(this.logArgs)
this.mockBunyanLogger.info.should.have.been.calledWith(this.logArgs)
})
it('should log warn', function() {
this.logger.warn(this.logArgs)
this.mockBunyanLogger.warn.should.have.been.calledWith(this.logArgs)
})
it('should log err', function() {
this.logger.err(this.logArgs)
this.mockBunyanLogger.error.should.have.been.calledWith(this.logArgs)
})
it('should log log', function() {
this.logger.log(this.logArgs)
this.mockBunyanLogger.info.should.have.been.calledWith(this.logArgs)
})
})
describe('logger.error', function() {
it('should report a single error to sentry', function() {
this.logger.error({ foo: 'bar' }, 'message')
this.captureException.called.should.equal(true)
})
it('should report the same error to sentry only once', function() {
const error1 = new Error('this is the error')
this.logger.error({ foo: error1 }, 'first message')
this.logger.error({ bar: error1 }, 'second message')
this.captureException.callCount.should.equal(1)
})
it('should report two different errors to sentry individually', function() {
const error1 = new Error('this is the error')
const error2 = new Error('this is the error')
this.logger.error({ foo: error1 }, 'first message')
this.logger.error({ bar: error2 }, 'second message')
this.captureException.callCount.should.equal(2)
})
it('should remove the path from fs errors', function() {
const fsError = new Error(
"Error: ENOENT: no such file or directory, stat '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'"
2019-03-08 11:06:24 -05:00
)
fsError.path = '/tmp/3279b8d0-da10-11e8-8255-efd98985942b'
this.logger.error({ err: fsError }, 'message')
this.captureException
.calledWith(
sinon.match.has(
2019-03-08 11:06:24 -05:00
'message',
'Error: ENOENT: no such file or directory, stat'
)
)
2019-03-08 11:06:24 -05:00
.should.equal(true)
})
it('for multiple errors should only report a maximum of 5 errors to sentry', function() {
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.captureException.callCount.should.equal(5)
})
it('for multiple errors with a minute delay should report 10 errors to sentry', function() {
// the first five errors should be reported to sentry
2019-03-08 11:06:24 -05:00
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
// the following errors should not be reported
2019-03-08 11:06:24 -05:00
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
// allow a minute to pass
2019-03-08 11:06:24 -05:00
this.clock.tick(this.start + 61 * 1000)
// after a minute the next five errors should be reported to sentry
2019-03-08 11:06:24 -05:00
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
// the following errors should not be reported to sentry
2019-03-08 11:06:24 -05:00
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.logger.error({ foo: 'bar' }, 'message')
this.captureException.callCount.should.equal(10)
})
})
describe('checkLogLevel', function() {
it('should request log level override from google meta data service', function() {
this.logger.checkLogLevel()
const options = {
headers: {
2019-03-08 11:06:24 -05:00
'Metadata-Flavor': 'Google'
},
uri: `http://metadata.google.internal/computeMetadata/v1/project/attributes/${
this.loggerName
}-setLogLevelEndTime`
2019-03-08 11:06:24 -05:00
}
this.Request.should.have.been.calledWithMatch(options)
})
2019-03-08 11:06:24 -05:00
describe('when request has error', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.Request.yields('error')
this.logger.checkLogLevel()
})
2019-03-08 11:06:24 -05:00
it('should only set default level', function() {
this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
2019-03-08 11:06:24 -05:00
'debug'
)
})
})
2019-03-08 11:06:24 -05:00
describe('when statusCode is not 200', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.Request.yields(null, { statusCode: 404 })
this.logger.checkLogLevel()
})
2019-03-08 11:06:24 -05:00
it('should only set default level', function() {
this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
2019-03-08 11:06:24 -05:00
'debug'
)
})
})
2019-03-08 11:06:24 -05:00
describe('when time value returned that is less than current time', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.Request.yields(null, { statusCode: 200 }, '1')
this.logger.checkLogLevel()
})
2019-03-08 11:06:24 -05:00
it('should only set default level', function() {
this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
2019-03-08 11:06:24 -05:00
'debug'
)
})
})
2019-03-08 11:06:24 -05:00
describe('when time value returned that is less than current time', function() {
describe('when level is already set', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.mockBunyanLogger.level.returns(10)
this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
this.logger.checkLogLevel()
})
2019-03-08 11:06:24 -05:00
it('should set trace level', function() {
this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
2019-03-08 11:06:24 -05:00
'trace'
)
})
})
2019-03-08 11:06:24 -05:00
describe('when level is not already set', function() {
beforeEach(function() {
2019-03-08 11:06:24 -05:00
this.mockBunyanLogger.level.returns(20)
this.Request.yields(null, { statusCode: 200 }, this.start + 1000)
this.logger.checkLogLevel()
})
2019-03-08 11:06:24 -05:00
it('should set trace level', function() {
this.mockBunyanLogger.level.should.have.been.calledOnce.and.calledWith(
2019-03-08 11:06:24 -05:00
'trace'
)
})
})
})
})
describe('ringbuffer', function() {
beforeEach(function() {
this.logBufferMock = [
{
2019-03-08 11:06:24 -05:00
msg: 'log 1'
},
{
2019-03-08 11:06:24 -05:00
msg: 'log 2'
}
2019-03-08 11:06:24 -05:00
]
})
2019-03-11 10:27:35 -04:00
describe('when ring buffer size is positive', function() {
beforeEach(function() {
2019-03-11 10:27:35 -04:00
process.env['LOG_RING_BUFFER_SIZE'] = '20'
2019-03-08 11:06:24 -05:00
this.logger = this.LoggingManager.initialize(this.loggerName)
this.logger.ringBuffer.records = this.logBufferMock
this.logger.error({}, 'error')
})
afterEach(function() {
2019-03-11 10:27:35 -04:00
process.env['LOG_RING_BUFFER_SIZE'] = undefined
2019-03-08 11:06:24 -05:00
})
2019-03-08 11:06:24 -05:00
it('should include buffered logs in error log', function() {
this.mockBunyanLogger.error.lastCall.args[0].logBuffer.should.equal(
this.logBufferMock
2019-03-08 11:06:24 -05:00
)
})
})
2019-03-11 10:27:35 -04:00
describe('when ring buffer size is zero', function() {
beforeEach(function() {
2019-03-11 10:27:35 -04:00
process.env['LOG_RING_BUFFER_SIZE'] = '0'
2019-03-08 11:06:24 -05:00
this.logger = this.LoggingManager.initialize(this.loggerName)
this.logger.error({}, 'error')
})
2019-03-11 10:27:35 -04:00
afterEach(function() {
process.env['LOG_RING_BUFFER_SIZE'] = undefined
})
2019-03-08 11:06:24 -05:00
it('should not include buffered logs in error log', function() {
chai.expect(this.mockBunyanLogger.error.lastCall.args[0].logBuffer).be
2019-03-08 11:06:24 -05:00
.undefined
})
})
})
})