Skip to content

Commit 669704f

Browse files
committed
test: parameterize some of these tests cases, no functional change
1 parent 320610e commit 669704f

File tree

1 file changed

+80
-42
lines changed

1 file changed

+80
-42
lines changed

loggers/pino/test/basic.test.js

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
// Test everything that doesn't fit in a separate file.
2121

2222
const http = require('http')
23+
const os = require('os')
2324

2425
const addFormats = require('ajv-formats').default
2526
const Ajv = require('ajv').default
2627
const pino = require('pino')
2728
const split = require('split2')
2829
const test = require('tap').test
30+
const ecsVersion = require('@elastic/ecs-helpers').version
2931

3032
const ecsFormat = require('../')
3133
const { ecsLoggingValidate } = require('../../../utils/lib/ecs-logging-validate')
@@ -37,54 +39,90 @@ const ajv = new Ajv({
3739
addFormats(ajv)
3840
const validate = ajv.compile(require('../../../utils/schema.json'))
3941

40-
test('Should produce valid ecs logs', t => {
41-
const stream = split().once('data', line => {
42-
const rec = JSON.parse(line)
43-
t.deepEqual(rec['log.level'], 'info')
44-
t.ok(validate(rec))
45-
t.equal(ecsLoggingValidate(line, { ignoreIndex: true }), null)
46-
t.end()
47-
})
42+
test('ecsPinoFormat cases', suite => {
43+
const formatCases = [
44+
{
45+
name: 'hello world',
46+
pinoOpts: ecsFormat(),
47+
loggingFn: (log) => {
48+
log.info('Hello, world!')
49+
},
50+
rec: {
51+
'log.level': 'info',
52+
ecs: { version: ecsVersion },
53+
process: { pid: process.pid },
54+
host: { hostname: os.hostname },
55+
message: 'Hello, world!'
56+
}
57+
},
58+
{
59+
name: 'should map "name" to "log.logger"',
60+
pinoOpts: { name: 'myName', ...ecsFormat() },
61+
loggingFn: (log) => {
62+
log.info('hi')
63+
},
64+
rec: {
65+
'log.level': 'info',
66+
ecs: { version: ecsVersion },
67+
process: { pid: process.pid },
68+
host: { hostname: os.hostname },
69+
log: { logger: 'myName' },
70+
message: 'hi'
71+
}
72+
},
73+
{
74+
name: 'should add fields to the record',
75+
pinoOpts: ecsFormat(),
76+
loggingFn: (log) => {
77+
log.info({ foo: 'bar' }, 'hi')
78+
},
79+
rec: {
80+
'log.level': 'info',
81+
ecs: { version: ecsVersion },
82+
process: { pid: process.pid },
83+
host: { hostname: os.hostname },
84+
message: 'hi',
85+
foo: 'bar'
86+
}
87+
},
88+
{
89+
name: 'can log non-HTTP res & req fields',
90+
pinoOpts: ecsFormat(),
91+
loggingFn: (log) => {
92+
log.info({ req: { id: 42 }, res: { status: 'OK' } }, 'hi')
93+
},
94+
rec: {
95+
'log.level': 'info',
96+
ecs: { version: ecsVersion },
97+
process: { pid: process.pid },
98+
host: { hostname: os.hostname },
99+
message: 'hi',
100+
req: { id: 42 },
101+
res: { status: 'OK' }
102+
}
103+
}
104+
]
48105

49-
const log = pino({ ...ecsFormat() }, stream)
50-
log.info('Hello world')
51-
})
106+
formatCases.forEach((fc) => {
107+
suite.test('ecsPinoFormat case: ' + fc.name, t => {
108+
const lines = []
109+
const capture = split().on('data', line => { lines.push(line) })
110+
const log = pino(fc.pinoOpts, capture)
52111

53-
test('Should map "name" to "log.logger"', t => {
54-
const stream = split().once('data', line => {
55-
const rec = JSON.parse(line)
56-
t.deepEqual(rec.log, { logger: 'myName' })
57-
t.ok(validate(rec))
58-
t.equal(ecsLoggingValidate(line, { ignoreIndex: true }), null)
59-
t.end()
60-
})
112+
fc.loggingFn(log)
61113

62-
// Pass in empty opts object to ecsFormat() for coverage.
63-
const log = pino({ name: 'myName', ...ecsFormat({}) }, stream)
64-
log.info('Hello world')
65-
})
114+
const rec = JSON.parse(lines[0])
115+
t.ok(validate(rec))
116+
t.equal(ecsLoggingValidate(lines[0], { ignoreIndex: true }), null)
66117

67-
test('Should append any additional property to the log message', t => {
68-
const stream = split().once('data', line => {
69-
const rec = JSON.parse(line)
70-
t.equal(rec.foo, 'bar')
71-
t.ok(validate(rec))
72-
t.equal(ecsLoggingValidate(line, { ignoreIndex: true }), null)
73-
t.end()
74-
})
118+
delete rec['@timestamp'] // normalize before comparison
119+
t.deepEqual(rec, fc.rec, 'logged record matches expected record')
75120

76-
const log = pino({ ...ecsFormat() }, stream)
77-
log.info({ foo: 'bar' }, 'Hello world')
78-
})
121+
t.end()
122+
})
123+
})
79124

80-
test('can log non-HTTP res & req fields', t => {
81-
const recs = []
82-
const stream = split(JSON.parse).on('data', rec => { recs.push(rec) })
83-
const log = pino({ ...ecsFormat() }, stream)
84-
log.info({ req: { id: 42 }, res: { status: 'OK' } }, 'hi')
85-
t.equal(recs[0].req.id, 42)
86-
t.equal(recs[0].res.status, 'OK')
87-
t.end()
125+
suite.end()
88126
})
89127

90128
test('convertReqRes:true and HTTP req, res', t => {

0 commit comments

Comments
 (0)