Skip to content

Commit

Permalink
fix: fix mongodb tests for TAV (#3683)
Browse files Browse the repository at this point in the history
The recent refactor of mongodb.test.js missed handling the MONGODB_HOST
env var used in Docker tests.

Refs: #3665
  • Loading branch information
david-luna authored Oct 18, 2023
1 parent 3ff3395 commit 4125ab6
Showing 1 changed file with 30 additions and 44 deletions.
74 changes: 30 additions & 44 deletions test/instrumentation/modules/mongodb/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -123,7 +131,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: RESOLVED_ADDRESS,
port: Number(TEST_PORT),
service: {
type: '',
Expand Down Expand Up @@ -165,7 +173,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: RESOLVED_ADDRESS,
port: Number(TEST_PORT),
service: {
type: '',
Expand Down Expand Up @@ -207,7 +215,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: RESOLVED_ADDRESS,
port: Number(TEST_PORT),
service: {
type: '',
Expand Down Expand Up @@ -249,7 +257,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: RESOLVED_ADDRESS,
port: Number(TEST_PORT),
service: {
type: '',
Expand Down Expand Up @@ -293,7 +301,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: RESOLVED_ADDRESS,
port: Number(TEST_PORT),
service: {
type: '',
Expand Down Expand Up @@ -324,7 +332,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: RESOLVED_ADDRESS,
port: Number(TEST_PORT),
service: {
type: '',
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {
Expand All @@ -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',
Expand All @@ -415,7 +434,7 @@ const testFixtures = [
},
},
destination: {
address: TEST_HOST,
address: addr,
port: Number(TEST_PORT),
service: {
type: '',
Expand All @@ -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');
Expand Down

0 comments on commit 4125ab6

Please sign in to comment.