Skip to content

Commit a129ed3

Browse files
committed
add tests to mongodb-core
1 parent 8c06bdc commit a129ed3

File tree

2 files changed

+123
-8
lines changed

2 files changed

+123
-8
lines changed

packages/datadog-plugin-mongodb-core/test/core.spec.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
'use strict'
22

3+
const sinon = require('sinon')
34
const semver = require('semver')
45
const agent = require('../../dd-trace/test/plugins/agent')
56
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
67
const { expectedSchema, rawExpectedSchema } = require('./naming')
78

9+
const MongodbCorePlugin = require('../../datadog-plugin-mongodb-core/src/index')
10+
const ddpv = require('mocha/package.json').version
11+
812
const withTopologies = fn => {
913
withVersions('mongodb-core', ['mongodb-core', 'mongodb'], '<4', (version, moduleName) => {
1014
describe('using the server topology', () => {
@@ -29,6 +33,7 @@ describe('Plugin', () => {
2933
let id
3034
let tracer
3135
let collection
36+
let injectDbmCommandSpy
3237

3338
describe('mongodb-core (core)', () => {
3439
withTopologies(getServer => {
@@ -397,6 +402,119 @@ describe('Plugin', () => {
397402
}
398403
)
399404
})
405+
406+
describe('with dbmPropagationMode service', () => {
407+
before(() => {
408+
return agent.load('mongodb-core', { dbmPropagationMode: 'service' })
409+
})
410+
411+
after(() => {
412+
return agent.close({ ritmReset: false })
413+
})
414+
415+
beforeEach(done => {
416+
const Server = getServer()
417+
418+
server = new Server({
419+
host: '127.0.0.1',
420+
port: 27017,
421+
reconnect: false
422+
})
423+
424+
server.on('connect', () => done())
425+
server.on('error', done)
426+
427+
server.connect()
428+
429+
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
430+
})
431+
432+
afterEach(() => {
433+
injectDbmCommandSpy?.restore()
434+
})
435+
436+
it('DBM propagation should inject service mode as comment', done => {
437+
agent
438+
.use(traces => {
439+
const span = traces[0][0]
440+
441+
expect(injectDbmCommandSpy.called).to.be.true
442+
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
443+
expect(instrumentedCommand).to.have.property('comment')
444+
expect(instrumentedCommand.comment).to.equal(
445+
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
446+
'dddbs=\'test-mongodb\',' +
447+
'dde=\'tester\',' +
448+
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
449+
`ddps='${encodeURIComponent(span.meta.service)}',` +
450+
`ddpv='${ddpv}',` +
451+
`ddprs='${encodeURIComponent(span.meta['peer.service'])}'`
452+
)
453+
})
454+
.then(done)
455+
.catch(done)
456+
457+
server.insert(`test.${collection}`, [{ a: 1 }], () => {})
458+
})
459+
})
460+
461+
describe('with dbmPropagationMode full', () => {
462+
before(() => {
463+
return agent.load('mongodb-core', { dbmPropagationMode: 'service' })
464+
})
465+
466+
after(() => {
467+
return agent.close({ ritmReset: false })
468+
})
469+
470+
beforeEach(done => {
471+
const Server = getServer()
472+
473+
server = new Server({
474+
host: '127.0.0.1',
475+
port: 27017,
476+
reconnect: false
477+
})
478+
479+
server.on('connect', () => done())
480+
server.on('error', done)
481+
482+
server.connect()
483+
484+
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
485+
})
486+
487+
afterEach(() => {
488+
injectDbmCommandSpy?.restore()
489+
})
490+
491+
it('DBM propagation should inject full mode with traceparent as comment', done => {
492+
agent
493+
.use(traces => {
494+
const span = traces[0][0]
495+
const traceId = span.meta['_dd.p.tid'] + span.trace_id.toString(16).padStart(16, '0')
496+
const spanId = span.span_id.toString(16).padStart(16, '0')
497+
498+
expect(injectDbmCommandSpy.called).to.be.true
499+
const instrumentedCommand = injectDbmCommandSpy.getCall(0).returnValue
500+
expect(instrumentedCommand).to.have.property('comment')
501+
expect(instrumentedCommand.comment).to.equal(
502+
`dddb='${encodeURIComponent(span.meta['db.name'])}',` +
503+
'dddbs=\'test-mongodb\',' +
504+
'dde=\'tester\',' +
505+
`ddh='${encodeURIComponent(span.meta['out.host'])}',` +
506+
`ddps='${encodeURIComponent(span.meta.service)}',` +
507+
`ddpv='${ddpv}',` +
508+
`ddprs='${encodeURIComponent(span.meta['peer.service'])}',` +
509+
`traceparent='00-${traceId}-${spanId}-00'`
510+
)
511+
})
512+
.then(done)
513+
.catch(done)
514+
515+
server.insert(`test.${collection}`, [{ a: 1 }], () => {})
516+
})
517+
})
400518
})
401519
})
402520
})

packages/datadog-plugin-mongodb-core/test/mongodb.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ describe('Plugin', () => {
4949
let db
5050
let BSON
5151
let injectDbmCommandSpy
52-
let clock
5352

5453
describe('mongodb-core', () => {
5554
withTopologies(createClient => {
@@ -342,7 +341,8 @@ describe('Plugin', () => {
342341
})
343342

344343
describe('with dbmPropagationMode service', () => {
345-
before(() => {
344+
before(function () {
345+
this.timeout(10000)
346346
return agent.load('mongodb-core', {
347347
dbmPropagationMode: 'service'
348348
})
@@ -357,7 +357,6 @@ describe('Plugin', () => {
357357
db = client.db('test')
358358
collection = db.collection(collectionName)
359359

360-
sinon.restore()
361360
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
362361
})
363362

@@ -393,7 +392,8 @@ describe('Plugin', () => {
393392
})
394393

395394
describe('with dbmPropagationMode full', () => {
396-
before(() => {
395+
before(function () {
396+
this.timeout(10000)
397397
return agent.load('mongodb-core', {
398398
dbmPropagationMode: 'full'
399399
})
@@ -408,21 +408,18 @@ describe('Plugin', () => {
408408
db = client.db('test')
409409
collection = db.collection(collectionName)
410410

411-
clock = sinon.useFakeTimers(new Date())
412411
injectDbmCommandSpy = sinon.spy(MongodbCorePlugin.prototype, 'injectDbmCommand')
413412
})
414413

415414
afterEach(() => {
416-
clock?.restore()
417415
injectDbmCommandSpy?.restore()
418416
})
419417

420418
it('DBM propagation should inject full mode with traceparent as comment', done => {
421419
agent
422420
.use(traces => {
423421
const span = traces[0][0]
424-
const expectedTimePrefix = Math.floor(clock.now / 1000).toString(16).padStart(8, '0').padEnd(16, '0')
425-
const traceId = expectedTimePrefix + span.trace_id.toString(16).padStart(16, '0')
422+
const traceId = span.meta['_dd.p.tid'] + span.trace_id.toString(16).padStart(16, '0')
426423
const spanId = span.span_id.toString(16).padStart(16, '0')
427424

428425
expect(injectDbmCommandSpy.called).to.be.true

0 commit comments

Comments
 (0)