Skip to content

Commit

Permalink
test(mongo-core): cover with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Aug 23, 2017
1 parent b142b94 commit 8374502
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ notifications:
email: false
services:
- postgresql
- mongodb
node_js:
- 'stable'
- '8'
env:
- PG_URI=postgres://postgres:@localhost:5432/travis_ci_test
- PG_URI=postgres://postgres:@localhost:5432/test_jaeger
- PG_URI=mongodb://travis:@localhost/test_jaeger
before_script:
- psql -c 'create database travis_ci_test;' -U postgres
- psql -c 'create database test_jaeger;' -U postgres
# MongoDB does not immediately accept connections
- sleep 15
- mongo mydb_test --eval 'db.addUser("travis", "test_jaeger");'
- npm prune
branches:
except:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"express": "4.15.4",
"knex": "0.13.0",
"mocha": "3.5.0",
"mongodb-core": "2.1.15",
"monk": "6.0.3",
"nock": "9.0.14",
"npm-run-all": "4.0.2",
Expand Down
73 changes: 73 additions & 0 deletions src/instrumentation/mongoCore.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const mongodbCore = require('mongodb-core')
const monk = require('monk')
const { expect } = require('chai')
const { Tracer, Tags } = require('opentracing')
const cls = require('../cls')
const instrumentation = require('./mongodbCore')

describe('instrumentation: mongodb-core', () => {
let tracer
let mockChildSpan
let db
let dbSites

beforeEach(function (done) {
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(mongodbCore, tracer)

db = monk('localhost/mydb', done)
dbSites = db.get('sites')
})

afterEach(() => {
instrumentation.unpatch(mongodbCore)
})

describe('#patch', () => {
it('should start and finish span', async () => {
const site = {
name: 'risingstack',
url: 'https://risingstack.com'
}
const result = await dbSites.insert(site)

expect(result).to.be.eql(site)

expect(cls.startChildSpan).to.be.calledWith(tracer, `${instrumentation.OPERATION_NAME}_insert`)
expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_TYPE, instrumentation.DB_TYPE)
expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_STATEMENT, JSON.stringify([site]))
})

it('should flag error', async () => {
db.close() // trigger error

try {
await dbSites.insert({
name: 'risingstack',
url: 'https://risingstack.com'
})
} catch (err) {
expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true)
expect(mockChildSpan.log).to.be.calledWith({
event: 'error',
'error.object': err,
message: err.message,
stack: err.stack
})
return
}

throw new Error('Uncaught exception')
})
})
})
2 changes: 2 additions & 0 deletions src/instrumentation/mongodbCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ function unpatch (mongodb) {
module.exports = {
module: 'mongodb-core',
supportedVersions: ['1.x', '2.x'],
OPERATION_NAME,
DB_TYPE,
patch,
unpatch
}
3 changes: 3 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ 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}`

// mongodb
process.env.MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost/test_jaeger'

before(() => {
chai.use(sinonChai)
})
Expand Down

0 comments on commit 8374502

Please sign in to comment.