From 4125ab68f845e9caf17919b21b584d8425478a1c Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 19 Oct 2023 00:52:55 +0200 Subject: [PATCH] fix: fix mongodb tests for TAV (#3683) The recent refactor of mongodb.test.js missed handling the MONGODB_HOST env var used in Docker tests. Refs: #3665 --- .../modules/mongodb/mongodb.test.js | 74 ++++++++----------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/test/instrumentation/modules/mongodb/mongodb.test.js b/test/instrumentation/modules/mongodb/mongodb.test.js index 7ef781d76e..2e937c5c56 100644 --- a/test/instrumentation/modules/mongodb/mongodb.test.js +++ b/test/instrumentation/modules/mongodb/mongodb.test.js @@ -37,7 +37,7 @@ const { const MONGODB_VERSION = safeGetPackageVersion('mongodb'); // Setting `localhost` will set `span.context.destination.address` to [::1] sometimes -const TEST_HOST = '127.0.0.1'; +const TEST_HOST = process.env.MONGODB_HOST || '127.0.0.1'; const TEST_PORT = '27017'; const TEST_DB = 'elasticapm'; const TEST_COLLECTION = 'test'; @@ -109,6 +109,14 @@ const testFixtures = [ delete s.sample_rate; }); + // We can't easily assert destination.address because mongodb >3.5.0 + // returns a resolved IP for the given connection hostname. In our CI + // setup, the host is set to "mongodb" which is a Docker container with + // some IP. We could `dns.resolve4()` here, but that's overkill I think. + const RESOLVED_ADDRESS = spans[0].context.destination.address; + + t.ok(RESOLVED_ADDRESS, 'context.destination.address is defined'); + // Work through each of the pipeline functions (insertMany, findOne, ...) in the script: const insertManySpan = { name: 'elasticapm.test.insert', @@ -123,7 +131,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: RESOLVED_ADDRESS, port: Number(TEST_PORT), service: { type: '', @@ -165,7 +173,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: RESOLVED_ADDRESS, port: Number(TEST_PORT), service: { type: '', @@ -207,7 +215,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: RESOLVED_ADDRESS, port: Number(TEST_PORT), service: { type: '', @@ -249,7 +257,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: RESOLVED_ADDRESS, port: Number(TEST_PORT), service: { type: '', @@ -293,7 +301,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: RESOLVED_ADDRESS, port: Number(TEST_PORT), service: { type: '', @@ -324,7 +332,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: RESOLVED_ADDRESS, port: Number(TEST_PORT), service: { type: '', @@ -373,7 +381,7 @@ const testFixtures = [ TEST_COLLECTION, TEST_USE_CALLBACKS: String(TEST_USE_CALLBACKS), }, - verbose: false, + verbose: true, checkApmServer: (t, apmServer) => { t.ok(apmServer.events[0].metadata, 'metadata'); const events = sortApmEvents(apmServer.events); @@ -382,7 +390,10 @@ const testFixtures = [ t.ok(tx, 'got the transaction'); const spans = events - .filter((e) => e.span && e.span.type !== 'external') + .filter( + (e) => + e.span && e.span.type !== 'external' && e.span.action === 'find', + ) .map((e) => e.span); spans.forEach((s) => { @@ -400,8 +411,16 @@ const testFixtures = [ const connectionsMade = TEST_USE_CALLBACKS ? 4 : 1; for (let i = 0; i < connectionsMade; i++) { + let span = spans.shift(); + // We can't easily assert destination.address because mongodb >3.5.0 + // returns a resolved IP for the given connection hostname. In our CI + // setup, the host is set to "mongodb" which is a Docker container with + // some IP. We could `dns.resolve4()` here, but that's overkill I think. + let addr = span.context.destination.address; + + t.ok(addr, 'context.destination.address is defined'); t.deepEqual( - spans.shift(), + span, { name: 'elasticapm.test.find', type: 'db', @@ -415,7 +434,7 @@ const testFixtures = [ }, }, destination: { - address: TEST_HOST, + address: addr, port: Number(TEST_PORT), service: { type: '', @@ -432,39 +451,6 @@ const testFixtures = [ }, 'findOne produced expected span', ); - - t.deepEqual( - spans.shift(), - { - name: 'admin.$cmd.endSessions', - type: 'db', - subtype: 'mongodb', - action: 'endSessions', - context: { - service: { - target: { - type: 'mongodb', - name: 'admin', - }, - }, - destination: { - address: TEST_HOST, - port: Number(TEST_PORT), - service: { - type: '', - name: '', - resource: 'mongodb/admin', - }, - }, - db: { - type: 'mongodb', - instance: 'admin', - }, - }, - outcome: 'success', - }, - 'close produced expected span', - ); } t.equal(spans.length, 0, 'all spans accounted for');