Skip to content

Commit

Permalink
tests: avoid test crash getting @elastic/elasticsearch version (#2351)
Browse files Browse the repository at this point in the history
The tests were using `require('@elastic/elasticsearch/package.json')`
which runs afoul of EOL'd folder mappings in "exports"
(https://nodejs.org/api/all.html#DEP0148) with current ES client
versions and the latest node v17 nightly.

Fixes: #2350
  • Loading branch information
trentm authored Sep 27, 2021
1 parent 60ed413 commit d894e3c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"memcached": "^2.2.2",
"mimic-response": "^2.1.0",
"mkdirp": "^0.5.1",
"module-details-from-path": "^1.0.3",
"mongodb": "^4.1.0",
"mongodb-core": "^3.2.7",
"mysql": "^2.18.1",
Expand Down
35 changes: 34 additions & 1 deletion test/_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict'

const fs = require('fs')

const moduleDetailsFromPath = require('module-details-from-path')

// Lookup the property "str" (given in dot-notation) in the object "obj".
// If the property isn't found, then `undefined` is returned.
function dottedLookup (obj, str) {
Expand Down Expand Up @@ -30,7 +34,36 @@ function findObjInArray (arr, key, val) {
return result
}

// "Safely" get the version of the given package, if possible. Otherwise return
// null.
//
// Here "safely" means avoiding `require("$packageName/package.json")` because
// that can fail if the package uses an old form of "exports"
// (e.g. https://github.com/elastic/apm-agent-nodejs/issues/2350).
function safeGetPackageVersion (packageName) {
let file
try {
file = require.resolve(packageName)
} catch (_err) {
return null
}

// Use the same logic as require-in-the-middle for finding the 'basedir' of
// the package from `file`.
const details = moduleDetailsFromPath(file)
if (!details) {
return null
}

try {
return JSON.parse(fs.readFileSync(details.basedir + '/package.json')).version
} catch (_err) {
return null
}
}

module.exports = {
dottedLookup,
findObjInArray
findObjInArray,
safeGetPackageVersion
}
3 changes: 2 additions & 1 deletion test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var test = require('tape')
const Agent = require('../lib/agent')
const { MockAPMServer } = require('./_mock_apm_server')
const { NoopTransport } = require('../lib/noop-transport')
const { safeGetPackageVersion } = require('./_utils')
var config = require('../lib/config')
var Instrumentation = require('../lib/instrumentation')
var apmVersion = require('../package').version
Expand Down Expand Up @@ -860,7 +861,7 @@ usePathAsTransactionNameTests.forEach(function (usePathAsTransactionNameTest) {

test('disableInstrumentations', function (t) {
var expressGraphqlVersion = require('express-graphql/package.json').version
var esVersion = require('@elastic/elasticsearch/package.json').version
var esVersion = safeGetPackageVersion('@elastic/elasticsearch')

// require('apollo-server-core') is a hard crash on nodes < 12.0.0
const apolloServerCoreVersion = require('apollo-server-core/package.json').version
Expand Down
9 changes: 5 additions & 4 deletions test/instrumentation/modules/@elastic/elasticsearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ const agent = require('../../../..').start({
spanFramesMinDuration: -1 // always capture stack traces with spans
})

const { safeGetPackageVersion } = require('../../../_utils')

// Skip (exit the process) if this package version doesn't support this version
// of node.
const esVersion = require('@elastic/elasticsearch/package.json').version
const esVersion = safeGetPackageVersion('@elastic/elasticsearch')
const semver = require('semver')
if (semver.lt(process.version, '10.0.0') && semver.gte(esVersion, '7.12.0')) {
console.log(`# SKIP @elastic/elasticsearch@${esVersion} does not support node ${process.version}`)
Expand All @@ -34,7 +36,6 @@ const { MockES } = require('./_mock_es')

const host = (process.env.ES_HOST || 'localhost') + ':9200'
const node = 'http://' + host
const pkgVersion = require('@elastic/elasticsearch/package.json').version

test('client.ping with promise', function (t) {
resetAgent(checkDataAndEnd(t, 'HEAD', '/', null))
Expand Down Expand Up @@ -334,7 +335,7 @@ test('DeserializationError', function (t) {
})
})

if (semver.gte(pkgVersion, '7.14.0')) {
if (semver.gte(esVersion, '7.14.0')) {
test('ProductNotSupportedError', function (t) {
// Create a mock Elasticsearch server that responds to "GET /" with a body
// that triggers ProductNotSupportedError.
Expand Down Expand Up @@ -377,7 +378,7 @@ if (semver.gte(pkgVersion, '7.14.0')) {
})
}

if (semver.gte(pkgVersion, '7.7.0')) {
if (semver.gte(esVersion, '7.7.0')) {
// Abort handling was added to @elastic/[email protected].

test('request.abort() works', function (t) {
Expand Down

0 comments on commit d894e3c

Please sign in to comment.