Skip to content

Commit 2160e98

Browse files
committed
add tests for the new guards on POSITIVE_TIME_OPTS
1 parent f9e992a commit 2160e98

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

lib/config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,12 @@ function normalizeBytes (opts) {
519519
function normalizePositiveTime (opts, logger) {
520520
for (const key of POSITIVE_TIME_OPTS) {
521521
if (key in opts) {
522-
let val = toSeconds(String(opts[key]))
522+
let val = secondsFromTimeStr(String(opts[key]))
523523
if (val === null) {
524524
const def = DEFAULTS[key]
525525
logger.warn('invalid time value "%s" for "%s" config option: using default "%s"',
526526
opts[key], key, def)
527-
val = toSeconds(def)
527+
val = secondsFromTimeStr(def)
528528
}
529529
opts[key] = val
530530
}
@@ -533,7 +533,7 @@ function normalizePositiveTime (opts, logger) {
533533

534534
function normalizeTime (opts) {
535535
for (const key of TIME_OPTS) {
536-
if (key in opts) opts[key] = toSeconds(String(opts[key]), true)
536+
if (key in opts) opts[key] = secondsFromTimeStr(String(opts[key]), true)
537537
}
538538
}
539539

@@ -613,7 +613,7 @@ function bytes (input) {
613613
}
614614
}
615615

616-
function toSeconds (value, allowNegative) {
616+
function secondsFromTimeStr (value, allowNegative) {
617617
let matches
618618
if (allowNegative) {
619619
matches = /^(-?\d+)(m|ms|s)?$/.exec(value)
@@ -747,7 +747,7 @@ function getBaseClientConfig (conf, agent) {
747747
cloudMetadataFetcher: (new CloudMetadata(cloudProvider, conf.logger, conf.serviceName))
748748
}
749749

750-
if (conf.errorMessageMaxLength !== undefined) {
750+
if (conf.errorMessageMaxLength !== undefined) {O
751751
// As of v10 of the http client, truncation of error messages will default
752752
// to `truncateLongFieldsAt` if `truncateErrorMessagesAt` is not specified.
753753
clientConfig.truncateErrorMessagesAt = conf.errorMessageMaxLength
@@ -762,4 +762,7 @@ module.exports.INTAKE_STRING_MAX_SIZE = INTAKE_STRING_MAX_SIZE
762762
module.exports.CAPTURE_ERROR_LOG_STACK_TRACES_NEVER = CAPTURE_ERROR_LOG_STACK_TRACES_NEVER
763763
module.exports.CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES = CAPTURE_ERROR_LOG_STACK_TRACES_MESSAGES
764764
module.exports.CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS = CAPTURE_ERROR_LOG_STACK_TRACES_ALWAYS
765+
766+
// The following are exported for tests.
765767
module.exports.DEFAULTS = DEFAULTS
768+
module.exports.secondsFromTimeStr = secondsFromTimeStr

test/config.test.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,66 @@ bytesValues.forEach(function (key) {
354354
})
355355
})
356356

357-
var timeValues = [
357+
var POSITIVE_TIME_OPTS = [
358358
'abortedErrorThreshold',
359359
'apiRequestTime',
360360
'metricsInterval',
361361
'serverTimeout'
362362
]
363+
// Time options allowing negative values.
364+
var TIME_OPTS = [
365+
'spanFramesMinDuration'
366+
]
367+
368+
POSITIVE_TIME_OPTS.forEach(function (key) {
369+
test(key + ' should guard against a negative time', function (t) {
370+
var agent = new Agent()
371+
var logger = new CaptureLogger()
372+
agent.start(Object.assign(
373+
{},
374+
agentOptsNoopTransport,
375+
{
376+
[key]: '-1s',
377+
logger
378+
}
379+
))
380+
381+
t.strictEqual(agent._conf[key], config.secondsFromTimeStr(config.DEFAULTS[key]),
382+
'fell back to default value')
383+
const warning = logger.calls[0]
384+
t.equal(warning.type, 'warn', 'got a log.warn')
385+
t.ok(warning.message.indexOf('-1s') !== -1, 'warning message includes the invalid value')
386+
t.ok(warning.message.indexOf(key) !== -1, 'warning message includes the invalid key')
387+
388+
agent.destroy()
389+
t.end()
390+
})
391+
392+
test(key + ' should guard against a bogus non-time', function (t) {
393+
var agent = new Agent()
394+
var logger = new CaptureLogger()
395+
agent.start(Object.assign(
396+
{},
397+
agentOptsNoopTransport,
398+
{
399+
[key]: 'bogusvalue',
400+
logger
401+
}
402+
))
403+
404+
t.strictEqual(agent._conf[key], config.secondsFromTimeStr(config.DEFAULTS[key]),
405+
'fell back to default value')
406+
const warning = logger.calls[0]
407+
t.equal(warning.type, 'warn', 'got a log.warn')
408+
t.ok(warning.message.indexOf('bogusvalue') !== -1, 'warning message includes the invalid value')
409+
t.ok(warning.message.indexOf(key) !== -1, 'warning message includes the invalid key')
410+
411+
agent.destroy()
412+
t.end()
413+
})
414+
})
363415

364-
timeValues.forEach(function (key) {
416+
POSITIVE_TIME_OPTS.concat(TIME_OPTS).forEach(function (key) {
365417
test(key + ' should convert minutes to seconds', function (t) {
366418
var agent = new Agent()
367419
var opts = {}

0 commit comments

Comments
 (0)