Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: avoid test crash getting @elastic/elasticsearch version #2351

Merged
merged 1 commit into from Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for this being in devDependencies

"mongodb": "^4.1.0",
"mongodb-core": "^3.2.7",
"mysql": "^2.18.1",
Expand Down
35 changes: 34 additions & 1 deletion test/_utils.js
@@ -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
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
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/elasticsearch@7.7.0.

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