From 8a110ac02502814d72e187068d12bf54376dccc5 Mon Sep 17 00:00:00 2001 From: Peter Marton Date: Wed, 23 Aug 2017 09:20:32 -0700 Subject: [PATCH] test(pg): cover with tests --- .travis.yml | 5 +++ src/instrumentation/pg.js | 1 + src/instrumentation/pg.spec.js | 68 ++++++++++++++++++++++++++++++++++ test/setup.js | 6 +++ 4 files changed, 80 insertions(+) create mode 100644 src/instrumentation/pg.spec.js diff --git a/.travis.yml b/.travis.yml index 10cf0d1..f5f4065 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,15 @@ cache: - node_modules notifications: email: false +services: + - postgresql node_js: - 'stable' - '8' +env: + - PG_URI=postgres://postgres:@localhost:5432/travis_ci_test before_script: + - psql -c 'create database travis_ci_test;' -U postgres - npm prune branches: except: diff --git a/src/instrumentation/pg.js b/src/instrumentation/pg.js index b64d65a..17b2e95 100644 --- a/src/instrumentation/pg.js +++ b/src/instrumentation/pg.js @@ -55,6 +55,7 @@ module.exports = { module: 'pg', supportedVersions: ['6.x'], OPERATION_NAME, + DB_TYPE, patch, unpatch } diff --git a/src/instrumentation/pg.spec.js b/src/instrumentation/pg.spec.js new file mode 100644 index 0000000..2908d97 --- /dev/null +++ b/src/instrumentation/pg.spec.js @@ -0,0 +1,68 @@ +'use strict' + +const pg = require('pg') +const knex = require('knex') +const { expect } = require('chai') +const { Tracer, Tags } = require('opentracing') +const cls = require('../cls') +const instrumentation = require('./pg') + +describe('instrumentation: pg', () => { + let tracer + let mockChildSpan + let db + + beforeEach(function () { + tracer = new Tracer() + mockChildSpan = { + setTag: this.sandbox.spy(), + log: this.sandbox.spy(), + finish: this.sandbox.spy() + } + + this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan) + + instrumentation.patch(pg, tracer) + + db = knex({ + client: 'pg', + connection: process.env.PG_URI + }) + }) + + afterEach(() => { + instrumentation.unpatch(pg) + }) + + describe('#patch', () => { + it('should start and finish span', async () => { + const query = 'SELECT 1 AS result' + const { rows } = await db.raw(query) + + expect(rows).to.be.eql([{ result: 1 }]) + + expect(cls.startChildSpan).to.be.calledWith(tracer, `${instrumentation.OPERATION_NAME}_query`) + expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_TYPE, instrumentation.DB_TYPE) + expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_STATEMENT, query) + }) + + it('should flag error', async () => { + const query = 'SELECT invalid AS result' + + try { + await db.raw(query) + } catch (err) { + expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true) + expect(mockChildSpan.log).to.be.calledWith({ + event: 'error', + 'error.object': err, + message: 'column "invalid" does not exist', + stack: err.stack + }) + return + } + + throw new Error('Uncaught exception') + }) + }) +}) diff --git a/test/setup.js b/test/setup.js index 7908bcc..2b2943b 100644 --- a/test/setup.js +++ b/test/setup.js @@ -4,6 +4,12 @@ const sinon = require('sinon') const chai = require('chai') const sinonChai = require('sinon-chai') +// postgres +const pgUser = process.env.PG_USER || process.env.USER || 'root' +const pgPw = process.env.PG_PASSWORD || '' +const pgDB = process.env.PG_DATABASE || 'test_jaeger' +process.env.PG_URI = process.env.PG_URI || `postgres://${pgUser}:${pgPw}@localhost:5432/${pgDB}` + before(() => { chai.use(sinonChai) })