From e2485cdade4a13f566948e1a59843cd857fe0d89 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Sun, 19 Sep 2021 13:38:55 +0200 Subject: [PATCH 1/6] fix cli args usage --- pino-mongodb.js | 5 +++-- test/end-to-end/assert.js | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/end-to-end/assert.js diff --git a/pino-mongodb.js b/pino-mongodb.js index a948631..f189e8e 100755 --- a/pino-mongodb.js +++ b/pino-mongodb.js @@ -27,6 +27,7 @@ function cli () { .option('-e, --errors', 'output insertion errors into stderr', false) .parse(process.argv) + const cliOptions = program.opts() const mongoUrl = (program.args[0] || transport.defaultOption.uri) function handleConnection (e, mClient) { @@ -38,8 +39,8 @@ function cli () { const db = mClient.db(dbName) const emitter = carrier.carry(process.stdin) - const collection = db.collection(program.collection) - const insert = makeInsert(program.errors, program.stdout) + const collection = db.collection(cliOptions.collection) + const insert = makeInsert(cliOptions.errors, cliOptions.stdout) emitter.on('line', (line) => { insert(collection, log(line)) diff --git a/test/end-to-end/assert.js b/test/end-to-end/assert.js new file mode 100644 index 0000000..421bfaa --- /dev/null +++ b/test/end-to-end/assert.js @@ -0,0 +1,45 @@ +'use strict' + +const t = require('tap') +const { spawn } = require('child_process') +const { promisify } = require('util') +const { MongoClient } = require('mongodb') +const { once } = require('events') + +const mongoUrl = 'mongodb://one:two@localhost:27017/dbname?authSource=admin' +const setTimeout = promisify(global.setTimeout) + +t.test('must log to a custom collection', async () => { + const customCollection = 'custom-collection' + const process = spawn('node', [ + '../../pino-mongodb.js', + mongoUrl, + '-c', + customCollection + ], { + cwd: __dirname, + killSignal: 'SIGINT', + stdio: ['pipe', 'inherit', 'inherit'] + }) + + const client = new MongoClient(mongoUrl) + await client.connect() + t.teardown(client.close.bind(client)) + const db = client.db() + const collection = db.collection(customCollection) + + const rowsBefore = await collection.countDocuments() + + process.stdin.write('hello pino-mongo 1\n') + process.stdin.write(`${JSON.stringify({ hello: 'pino' })}\n`) + process.stdin.write('hello pino-mongo 2\n') + + await setTimeout(1000) + + const rowsAfter = await collection.countDocuments() + t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') + + process.kill('SIGINT') + + await once(process, 'close') +}) From a21c9f85f371ec1322c256cb2af212db554c0223 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Sun, 19 Sep 2021 13:53:23 +0200 Subject: [PATCH 2/6] fix tests --- test/end-to-end/assert.js | 10 +++++----- test/end-to-end/transport.js | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/end-to-end/assert.js b/test/end-to-end/assert.js index 421bfaa..4f4ffb0 100644 --- a/test/end-to-end/assert.js +++ b/test/end-to-end/assert.js @@ -19,7 +19,7 @@ t.test('must log to a custom collection', async () => { ], { cwd: __dirname, killSignal: 'SIGINT', - stdio: ['pipe', 'inherit', 'inherit'] + stdio: ['pipe', null, null] }) const client = new MongoClient(mongoUrl) @@ -36,10 +36,10 @@ t.test('must log to a custom collection', async () => { await setTimeout(1000) - const rowsAfter = await collection.countDocuments() - t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') - process.kill('SIGINT') - await once(process, 'close') + + const rowsAfter = await collection.countDocuments() + t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') + await client.close() }) diff --git a/test/end-to-end/transport.js b/test/end-to-end/transport.js index 309863d..c8ccfda 100644 --- a/test/end-to-end/transport.js +++ b/test/end-to-end/transport.js @@ -116,4 +116,6 @@ t.test('log blocked items', async (t) => { await setTimeout(1000) const rowsInserted = await collection.countDocuments() t.equal(rowsInserted, rowsAfter + 1, 'logs are still working') + + await client.close() }) From 45fce7a68fdbb779fcb940647b13c1d4289e0e65 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Sun, 19 Sep 2021 15:00:06 +0200 Subject: [PATCH 3/6] debugging ci --- test/end-to-end/{assert.js => pipe-usage.js} | 28 +++++++++++--------- test/end-to-end/transport.js | 2 -- 2 files changed, 15 insertions(+), 15 deletions(-) rename test/end-to-end/{assert.js => pipe-usage.js} (54%) diff --git a/test/end-to-end/assert.js b/test/end-to-end/pipe-usage.js similarity index 54% rename from test/end-to-end/assert.js rename to test/end-to-end/pipe-usage.js index 4f4ffb0..9a94a54 100644 --- a/test/end-to-end/assert.js +++ b/test/end-to-end/pipe-usage.js @@ -6,20 +6,19 @@ const { promisify } = require('util') const { MongoClient } = require('mongodb') const { once } = require('events') -const mongoUrl = 'mongodb://one:two@localhost:27017/dbname?authSource=admin' +const mongoUrl = 'mongodb://one:two@localhost:27017/saymyname?authSource=admin' const setTimeout = promisify(global.setTimeout) t.test('must log to a custom collection', async () => { const customCollection = 'custom-collection' - const process = spawn('node', [ + const childProcess = spawn('node', [ '../../pino-mongodb.js', mongoUrl, '-c', customCollection ], { cwd: __dirname, - killSignal: 'SIGINT', - stdio: ['pipe', null, null] + stdio: ['pipe', 'inherit', 'inherit'] }) const client = new MongoClient(mongoUrl) @@ -29,17 +28,20 @@ t.test('must log to a custom collection', async () => { const collection = db.collection(customCollection) const rowsBefore = await collection.countDocuments() + t.pass(`rows count ${rowsBefore}`) - process.stdin.write('hello pino-mongo 1\n') - process.stdin.write(`${JSON.stringify({ hello: 'pino' })}\n`) - process.stdin.write('hello pino-mongo 2\n') + childProcess.stdin.write('hello pino-mongo 1\n') + childProcess.stdin.write(`${JSON.stringify({ hello: 'pino' })}\n`) + childProcess.stdin.write('hello pino-mongo 2\n') await setTimeout(1000) - process.kill('SIGINT') - await once(process, 'close') - - const rowsAfter = await collection.countDocuments() - t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') - await client.close() + childProcess.kill('SIGTERM') + try { + await once(childProcess, 'close') + const rowsAfter = await collection.countDocuments() + t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') + } catch (error) { + t.error(error) + } }) diff --git a/test/end-to-end/transport.js b/test/end-to-end/transport.js index c8ccfda..309863d 100644 --- a/test/end-to-end/transport.js +++ b/test/end-to-end/transport.js @@ -116,6 +116,4 @@ t.test('log blocked items', async (t) => { await setTimeout(1000) const rowsInserted = await collection.countDocuments() t.equal(rowsInserted, rowsAfter + 1, 'logs are still working') - - await client.close() }) From 2a22907de1844388e8901ea3a35163c1dddea915 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Mon, 20 Sep 2021 19:44:54 +0200 Subject: [PATCH 4/6] skip test on node12 --- test/end-to-end/pipe-usage.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/end-to-end/pipe-usage.js b/test/end-to-end/pipe-usage.js index 9a94a54..68d401d 100644 --- a/test/end-to-end/pipe-usage.js +++ b/test/end-to-end/pipe-usage.js @@ -9,7 +9,7 @@ const { once } = require('events') const mongoUrl = 'mongodb://one:two@localhost:27017/saymyname?authSource=admin' const setTimeout = promisify(global.setTimeout) -t.test('must log to a custom collection', async () => { +t.test('must log to a custom collection', async (t) => { const customCollection = 'custom-collection' const childProcess = spawn('node', [ '../../pino-mongodb.js', @@ -18,7 +18,7 @@ t.test('must log to a custom collection', async () => { customCollection ], { cwd: __dirname, - stdio: ['pipe', 'inherit', 'inherit'] + stdio: ['pipe', null, null] }) const client = new MongoClient(mongoUrl) @@ -36,11 +36,11 @@ t.test('must log to a custom collection', async () => { await setTimeout(1000) - childProcess.kill('SIGTERM') + childProcess.kill('SIGINT') try { await once(childProcess, 'close') const rowsAfter = await collection.countDocuments() - t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') + t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows', { skip: process.version.startsWith('v12.') }) } catch (error) { t.error(error) } From 5becea381e9590f4319bc4fb99ad9945d5309939 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Mon, 20 Sep 2021 20:15:49 +0200 Subject: [PATCH 5/6] restore test --- package.json | 2 +- pino-mongodb.js | 2 +- test/end-to-end/pipe-usage.js | 5 ++--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index de29593..5bfdec9 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ ], "scripts": { "test": "tap test/*.js", - "test:end2end": "npm test && tap --no-check-coverage test/end-to-end/*.js", + "test:end2end": "npm test && tap --no-check-coverage -t 0 test/end-to-end/*.js", "lint": "eslint .", "lint:fix": "npm run lint -- --fix", "prepack": "npm shrinkwrap", diff --git a/pino-mongodb.js b/pino-mongodb.js index f189e8e..024f504 100755 --- a/pino-mongodb.js +++ b/pino-mongodb.js @@ -46,7 +46,7 @@ function cli () { insert(collection, log(line)) }) - process.on('SIGINT', () => { + process.once('SIGINT', () => { mClient.close(process.exit) }) } diff --git a/test/end-to-end/pipe-usage.js b/test/end-to-end/pipe-usage.js index 68d401d..4a9c1e6 100644 --- a/test/end-to-end/pipe-usage.js +++ b/test/end-to-end/pipe-usage.js @@ -6,7 +6,7 @@ const { promisify } = require('util') const { MongoClient } = require('mongodb') const { once } = require('events') -const mongoUrl = 'mongodb://one:two@localhost:27017/saymyname?authSource=admin' +const mongoUrl = 'mongodb://one:two@localhost:27017/newdb?authSource=admin' const setTimeout = promisify(global.setTimeout) t.test('must log to a custom collection', async (t) => { @@ -35,12 +35,11 @@ t.test('must log to a custom collection', async (t) => { childProcess.stdin.write('hello pino-mongo 2\n') await setTimeout(1000) - childProcess.kill('SIGINT') try { await once(childProcess, 'close') const rowsAfter = await collection.countDocuments() - t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows', { skip: process.version.startsWith('v12.') }) + t.equal(rowsAfter, rowsBefore + 3, 'logged 3 rows') } catch (error) { t.error(error) } From 6944c3b8f56696c959a7cf961d0f566906f5a5f7 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Mon, 20 Sep 2021 20:22:06 +0200 Subject: [PATCH 6/6] debugging ci --- test/end-to-end/pipe-usage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/end-to-end/pipe-usage.js b/test/end-to-end/pipe-usage.js index 4a9c1e6..3aea665 100644 --- a/test/end-to-end/pipe-usage.js +++ b/test/end-to-end/pipe-usage.js @@ -34,7 +34,7 @@ t.test('must log to a custom collection', async (t) => { childProcess.stdin.write(`${JSON.stringify({ hello: 'pino' })}\n`) childProcess.stdin.write('hello pino-mongo 2\n') - await setTimeout(1000) + await setTimeout(5000) childProcess.kill('SIGINT') try { await once(childProcess, 'close')