From e9f8556750a88314f0faeb993364fe2d67adc231 Mon Sep 17 00:00:00 2001 From: Maurice Rickard Date: Wed, 14 Jun 2023 15:07:26 -0400 Subject: [PATCH] chore: Added test for getRedisParams (#1670) Signed-off-by: mrickard --- lib/instrumentation/@node-redis/client.js | 4 +- test/unit/instrumentation/redis.test.js | 76 +++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 test/unit/instrumentation/redis.test.js diff --git a/lib/instrumentation/@node-redis/client.js b/lib/instrumentation/@node-redis/client.js index 5f1b767a73..5c4f01b832 100644 --- a/lib/instrumentation/@node-redis/client.js +++ b/lib/instrumentation/@node-redis/client.js @@ -67,6 +67,8 @@ function getRedisParams(clientOpts) { return { host: clientOpts?.socket?.host || 'localhost', port_path_or_id: clientOpts?.socket?.path || clientOpts?.socket?.port || '6379', - database_name: clientOpts.database || 0 + database_name: clientOpts?.database || 0 } } + +module.exports.getRedisParams = getRedisParams diff --git a/test/unit/instrumentation/redis.test.js b/test/unit/instrumentation/redis.test.js new file mode 100644 index 0000000000..8ee2406bb5 --- /dev/null +++ b/test/unit/instrumentation/redis.test.js @@ -0,0 +1,76 @@ +/* + * Copyright 2023 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const client = require('../../../lib/instrumentation/@node-redis/client') + +tap.test('getRedisParams should behave as expected', function (t) { + t.autoend() + + t.test('given no opts, should return sensible defaults', function (t) { + t.autoend() + const params = client.getRedisParams() + const expected = { + host: 'localhost', + port_path_or_id: '6379', + database_name: 0 + } + t.match(params, expected, 'redis client should be definable without params') + }) + t.test('if host/port are defined incorrectly, should return expected defaults', function (t) { + t.autoend() + const params = client.getRedisParams({ host: 'myLocalHost', port: '1234' }) + const expected = { + host: 'localhost', + port_path_or_id: '6379', + database_name: 0 + } + t.match(params, expected, 'should return sensible defaults if defined without socket') + }) + t.test('if host/port are defined correctly, we should see them in config', function (t) { + t.autoend() + const params = client.getRedisParams({ socket: { host: 'myLocalHost', port: '1234' } }) + const expected = { + host: 'myLocalHost', + port_path_or_id: '1234', + database_name: 0 + } + t.match(params, expected, 'host/port should be returned when defined correctly') + }) + t.test('path should be used if defined', function (t) { + t.autoend() + const params = client.getRedisParams({ socket: { path: '5678' } }) + const expected = { + host: 'localhost', + port_path_or_id: '5678', + database_name: 0 + } + t.match(params, expected, 'path should show up in params') + }) + t.test('path should be preferred over port', function (t) { + t.autoend() + const params = client.getRedisParams({ + socket: { host: 'myLocalHost', port: '1234', path: '5678' } + }) + const expected = { + host: 'myLocalHost', + port_path_or_id: '5678', + database_name: 0 + } + t.match(params, expected, 'path should show up in params') + }) + t.test('database name should be definable', function (t) { + t.autoend() + const params = client.getRedisParams({ database: 12 }) + const expected = { + host: 'localhost', + port_path_or_id: '6379', + database_name: 12 + } + t.match(params, expected, 'database should be definable') + }) +})