-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(log): new unit tests #7988
Open
velvetrevolver
wants to merge
9
commits into
master
Choose a base branch
from
capture-logs-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3621d65
write unit tests for logCapture
ffe45a4
Changes after code review
fa67a1f
sort imports
julien-f a17cf03
Update logger.test.js
julien-f 77309d2
Update @xen-orchestra/log/capture.test.js
velvetrevolver a399e4c
Changes after code review
e293aed
Update @xen-orchestra/log/logger.test.js
velvetrevolver 27370ee
Update @xen-orchestra/log/logger.test.js
velvetrevolver 2bc6ddf
Update @xen-orchestra/log/capture.test.js
velvetrevolver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
const assert = require('node:assert/strict') | ||
const { beforeEach, describe, it } = require('node:test') | ||
const { captureLogs, createCaptureTransport } = require('@xen-orchestra/log/capture') | ||
const { configure } = require('@xen-orchestra/log/configure') | ||
const { createLogger } = require('@xen-orchestra/log') | ||
|
||
describe('captureLogs()', () => { | ||
const logger = createLogger('test-logger') | ||
|
||
const logsTransportDefault = [] | ||
const transportTest = message => { | ||
logsTransportDefault.push(message) | ||
} | ||
|
||
beforeEach(() => { | ||
configure( | ||
createCaptureTransport([ | ||
{ | ||
level: 'debug', | ||
transport: transportTest, | ||
}, | ||
]) | ||
) | ||
|
||
logsTransportDefault.length = 0 | ||
}) | ||
|
||
it('should capture logs', async () => { | ||
const captureLog = [] | ||
await captureLogs( | ||
log => { | ||
// every logs emitted in the async context of `fn` will arrive here | ||
// | ||
// do not emit logs in this function or this will create a loop. | ||
captureLog.push(log) | ||
// console.log(`[captureLog] ${JSON.stringify(log)}`) | ||
// console.log(`[captureLog]`, captureLog) // ${JSON.stringify(captureLog)}`) | ||
}, | ||
async () => { | ||
logger.debug('synchronous logs are captured') | ||
|
||
setTimeout(() => { | ||
logger.debug('logs from asynchronous callbacks too') | ||
}, 50) | ||
|
||
await new Promise(resolve => setTimeout(resolve, 50)) | ||
|
||
logger.debug('logs in async functions or promise chains too') | ||
|
||
// To escape capture, run code in `captureLogs` with `undefined` | ||
// as the first param | ||
captureLogs(undefined, () => { | ||
logger.debug('this log will not be captured') | ||
}) | ||
|
||
// Returned value and error is forwarded by `captureLogs` | ||
return 'returned value' | ||
} | ||
) | ||
|
||
assert.equal(captureLog[0].message, 'synchronous logs are captured') | ||
assert.equal(captureLog[1].message, 'logs from asynchronous callbacks too') | ||
assert.equal(captureLog[2].message, 'logs in async functions or promise chains too') | ||
assert.equal(captureLog[3], undefined, 'this log will not be captured') | ||
assert.equal(logsTransportDefault[0].message, 'this log will not be captured') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
'use strict' | ||
|
||
const assert = require('node:assert/strict') | ||
const { beforeEach, describe, it } = require('node:test') | ||
const { configure } = require('@xen-orchestra/log/configure') | ||
const { createLogger } = require('@xen-orchestra/log') | ||
const { createCaptureTransport } = require('@xen-orchestra/log/capture') | ||
const { dedupe } = require('@xen-orchestra/log/dedupe') | ||
|
||
describe('logger', () => { | ||
const logger = createLogger('test-logger') | ||
|
||
const logsTransportDebug = [] | ||
const transportDebug = message => { | ||
logsTransportDebug.push(message) | ||
} | ||
|
||
const logsTransportFatal = [] | ||
const transportFatal = message => { | ||
logsTransportFatal.push(message) | ||
} | ||
|
||
beforeEach(() => { | ||
configure( | ||
createCaptureTransport([ | ||
{ | ||
level: 'fatal', | ||
transport: transportFatal, | ||
}, | ||
{ | ||
level: 'debug', | ||
transport: transportDebug, | ||
}, | ||
]) | ||
) | ||
|
||
logsTransportDebug.length = 0 | ||
logsTransportFatal.length = 0 | ||
}) | ||
|
||
it('should log test with all its attributes', () => { | ||
const message = 'test 1 with all its attributes' | ||
logger.debug(message) | ||
assert.equal(logsTransportDebug[0].message, message) | ||
assert.equal(typeof logsTransportDebug[0].level, 'number', `level attribute should exist`) | ||
assert.equal(typeof logsTransportDebug[0].namespace, 'string', `namespace attribute should exist`) | ||
assert.equal(logsTransportDebug[0].time instanceof Date, true, 'time attribute should exist') | ||
}) | ||
it('should log test 1 and 2', () => { | ||
const expected1 = { | ||
level: 20, | ||
namespace: 'test-logger', | ||
message: 'should log test 1/2', | ||
time: new Date(), | ||
} | ||
const expected2 = { | ||
level: 20, | ||
namespace: 'test-logger', | ||
message: 'should log test 2/2', | ||
time: new Date(), | ||
} | ||
logger.debug(expected1.message) | ||
logger.debug(expected2.message) | ||
assert.equal(logsTransportDebug[0].message, expected1.message) | ||
assert.equal(logsTransportDebug[1].message, expected2.message) | ||
}) | ||
it('should log tests for debug', () => { | ||
const expectedDebug = { | ||
data: undefined, | ||
level: 20, | ||
namespace: 'test-logger', | ||
message: 'synchronous log debug', | ||
time: new Date(), | ||
} | ||
logger.debug(expectedDebug.message) | ||
assert.deepEqual(logsTransportDebug[0].message, expectedDebug.message) | ||
assert.deepEqual(logsTransportDebug[0].level, expectedDebug.level) | ||
}) | ||
it('should log tests for info', () => { | ||
const expectedInfo = { | ||
data: undefined, | ||
level: 30, | ||
namespace: 'test-logger', | ||
message: 'synchronous log info', | ||
time: new Date(), | ||
} | ||
logger.info(expectedInfo.message) | ||
assert.deepEqual(logsTransportDebug[0].message, expectedInfo.message) | ||
assert.deepEqual(logsTransportDebug[0].level, expectedInfo.level) | ||
}) | ||
it('should log tests for warn', () => { | ||
const expectedWarn = { | ||
data: undefined, | ||
level: 40, | ||
namespace: 'test-logger', | ||
message: 'synchronous log warn', | ||
time: new Date(), | ||
} | ||
logger.warn(expectedWarn.message) | ||
assert.deepEqual(logsTransportDebug[0].message, expectedWarn.message) | ||
assert.deepEqual(logsTransportDebug[0].level, expectedWarn.level) | ||
}) | ||
it('should log tests for error', () => { | ||
const expectedError = { | ||
data: undefined, | ||
level: 50, | ||
namespace: 'test-logger', | ||
message: 'synchronous log error', | ||
time: new Date(), | ||
} | ||
logger.error(expectedError.message) | ||
assert.deepEqual(logsTransportDebug[0].message, expectedError.message) | ||
assert.deepEqual(logsTransportDebug[0].level, expectedError.level) | ||
assert.ok(logsTransportFatal.length === 0, 'fatal logs should be empty') | ||
}) | ||
it('should log tests for fatal', () => { | ||
const expectedFatal = { | ||
data: undefined, | ||
level: 60, | ||
namespace: 'test-logger', | ||
message: 'synchronous log fatal', | ||
time: new Date(), | ||
} | ||
logger.fatal(expectedFatal.message) | ||
assert.deepEqual(logsTransportFatal[0].message, expectedFatal.message) | ||
assert.deepEqual(logsTransportFatal[0].level, expectedFatal.level) | ||
}) | ||
it('should log an error on null', () => { | ||
const expected1 = { | ||
data: { level: 20, value: null }, | ||
level: 40, | ||
namespace: 'test-logger', | ||
message: 'incorrect value passed to logger', | ||
time: new Date(), | ||
} | ||
logger.debug(null) | ||
assert.deepEqual(logsTransportDebug[0].data, expected1.data) | ||
}) | ||
it('should log an error on undefined', () => { | ||
const expected1 = { | ||
data: { level: 20, value: undefined }, | ||
level: 40, | ||
namespace: 'test-logger', | ||
message: 'incorrect value passed to logger', | ||
time: new Date(), | ||
} | ||
logger.debug(undefined) | ||
assert.deepEqual(logsTransportDebug[0].data, expected1.data) | ||
}) | ||
it('should not dedup logs', () => { | ||
configure( | ||
createCaptureTransport([ | ||
{ | ||
transport: transportDebug, | ||
}, | ||
]) | ||
) | ||
|
||
for (let i = 0; i < 3; i++) { | ||
logger.debug('this line should be logged 3 times') | ||
} | ||
assert.equal(logsTransportDebug.length, 3) | ||
}) | ||
it('should dedup logs', () => { | ||
configure( | ||
createCaptureTransport([ | ||
dedupe({ | ||
timeout: 50, // without a defined timeout, the test will wait for 10 minutes | ||
transport: transportDebug, | ||
}), | ||
]) | ||
) | ||
|
||
for (let i = 0; i < 3; i++) { | ||
logger.debug('this line should be logged only once') | ||
} | ||
assert.equal(logsTransportDebug.length, 1) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What feature does this case test that the the previous one did not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It test the "test transport" can contain many logs, and not only one constantly overwriten. Needed by the captureLog test after when we test multiple logs.