Skip to content

Commit

Permalink
Update @xen-orchestra/log/logger.test.js
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Fontanet <[email protected]>
  • Loading branch information
2 people authored and stephane-m-dev committed Sep 24, 2024
1 parent 928e364 commit e0400e7
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions @xen-orchestra/log/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('logger', () => {

const logsTransportDebug = []
const transportDebug = message => {
logsTransportDefault.push(message)
logsTransportDebug.push(message)
}

const logsTransportFatal = []
Expand All @@ -29,27 +29,22 @@ describe('logger', () => {
},
{
level: 'debug',
transport: transportTest,
transport: transportDebug,
},
])
)

logsTransportDefault.length = 0
logsTransportDebug.length = 0
logsTransportFatal.length = 0
})

it('should log test with all its attributes', () => {
const expected1 = {
level: 20,
namespace: 'test-logger',
message: 'test 1 with all its attributes',
time: new Date(),
}
logger.debug(expected1.message)
assert.equal(logsTransportDefault[0].message, expected1.message)
assert.equal(typeof logsTransportDefault[0].level, 'number', `level attribute should exist`)
assert.equal(typeof logsTransportDefault[0].namespace, 'string', `namespace attribute should exist`)
assert.equal(logsTransportDefault[0].time instanceof Date, true, 'time attribute should exist')
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 = {
Expand All @@ -66,8 +61,8 @@ describe('logger', () => {
}
logger.debug(expected1.message)
logger.debug(expected2.message)
assert.equal(logsTransportDefault[0].message, expected1.message)
assert.equal(logsTransportDefault[1].message, expected2.message)
assert.equal(logsTransportDebug[0].message, expected1.message)
assert.equal(logsTransportDebug[1].message, expected2.message)
})
it('should log tests for debug', () => {
const expectedDebug = {
Expand All @@ -78,8 +73,8 @@ describe('logger', () => {
time: new Date(),
}
logger.debug(expectedDebug.message)
assert.deepEqual(logsTransportDefault[0].message, expectedDebug.message)
assert.deepEqual(logsTransportDefault[0].level, expectedDebug.level)
assert.deepEqual(logsTransportDebug[0].message, expectedDebug.message)
assert.deepEqual(logsTransportDebug[0].level, expectedDebug.level)
})
it('should log tests for info', () => {
const expectedInfo = {
Expand All @@ -90,8 +85,8 @@ describe('logger', () => {
time: new Date(),
}
logger.info(expectedInfo.message)
assert.deepEqual(logsTransportDefault[0].message, expectedInfo.message)
assert.deepEqual(logsTransportDefault[0].level, expectedInfo.level)
assert.deepEqual(logsTransportDebug[0].message, expectedInfo.message)
assert.deepEqual(logsTransportDebug[0].level, expectedInfo.level)
})
it('should log tests for warn', () => {
const expectedWarn = {
Expand All @@ -102,8 +97,8 @@ describe('logger', () => {
time: new Date(),
}
logger.warn(expectedWarn.message)
assert.deepEqual(logsTransportDefault[0].message, expectedWarn.message)
assert.deepEqual(logsTransportDefault[0].level, expectedWarn.level)
assert.deepEqual(logsTransportDebug[0].message, expectedWarn.message)
assert.deepEqual(logsTransportDebug[0].level, expectedWarn.level)
})
it('should log tests for error', () => {
const expectedError = {
Expand All @@ -114,8 +109,8 @@ describe('logger', () => {
time: new Date(),
}
logger.error(expectedError.message)
assert.deepEqual(logsTransportDefault[0].message, expectedError.message)
assert.deepEqual(logsTransportDefault[0].level, expectedError.level)
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', () => {
Expand All @@ -139,7 +134,7 @@ describe('logger', () => {
time: new Date(),
}
logger.debug(null)
assert.deepEqual(logsTransportDefault[0].data, expected1.data)
assert.deepEqual(logsTransportDebug[0].data, expected1.data)
})
it('should log an error on undefined', () => {
const expected1 = {
Expand All @@ -150,35 +145,35 @@ describe('logger', () => {
time: new Date(),
}
logger.debug(undefined)
assert.deepEqual(logsTransportDefault[0].data, expected1.data)
assert.deepEqual(logsTransportDebug[0].data, expected1.data)
})
it('should not dedup logs', () => {
configure(
createCaptureTransport([
{
transport: transportTest,
transport: transportDebug,
},
])
)

for (let i = 0; i < 3; i++) {
logger.debug('this line should be logged 3 times')
}
assert.equal(logsTransportDefault.length, 3)
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: transportTest,
transport: transportDebug,
}),
])
)

for (let i = 0; i < 3; i++) {
logger.debug('this line should be logged only once')
}
assert.equal(logsTransportDefault.length, 1)
assert.equal(logsTransportDebug.length, 1)
})
})

0 comments on commit e0400e7

Please sign in to comment.