diff --git a/tools/testing-server/constants.js b/tools/testing-server/constants.js index 300f8f316..215ccd91a 100644 --- a/tools/testing-server/constants.js +++ b/tools/testing-server/constants.js @@ -1,16 +1,39 @@ const path = require('path') +const yargs = require('yargs/yargs') +const { hideBin } = require('yargs/helpers') +const fs = require('fs') +const args = yargs(hideBin(process.argv)).argv -const defaultAgentConfig = { - licenseKey: 'asdf', - applicationID: 42, - accountID: 123, - agentID: 456, - trustKey: 789 +let agentConfig +if (args.B) { + try { + agentConfig = JSON.parse(args.B) + } catch (err) { + try { + agentConfig = JSON.parse(fs.readFileSync(args.B, 'utf-8')) + } catch (err) { + console.error('Failed to parse BAM config:', err) + } + } +} + +/** fallback if no override was provided or override outright failed to parse correctly */ +if (!agentConfig) { + agentConfig = { + licenseKey: 'asdf', + applicationID: 42, + accountID: 123, + agentID: 456, + trustKey: 789 + } } -module.exports.defaultAgentConfig = defaultAgentConfig + +module.exports.agentConfig = agentConfig + +console.log('Starting service using agent config -- ', agentConfig) const mockEntityGuid = () => { - return btoa(`${defaultAgentConfig.accountID}|BROWSER|APPLICATION|${Math.floor(Math.random() * 1000000)}`).replace(/=/g, '') + return btoa(`${agentConfig.accountID}|BROWSER|APPLICATION|${Math.floor(Math.random() * 1000000)}`).replace(/=/g, '') } module.exports.paths = { diff --git a/tools/testing-server/plugins/agent-injector/config-transform.js b/tools/testing-server/plugins/agent-injector/config-transform.js index 69f2d1a89..47f091af6 100644 --- a/tools/testing-server/plugins/agent-injector/config-transform.js +++ b/tools/testing-server/plugins/agent-injector/config-transform.js @@ -2,7 +2,7 @@ const { Transform } = require('stream') const debugShim = require('./debug-shim') const sslShim = require('./ssl-shim') const { - defaultAgentConfig, + agentConfig, loaderConfigKeys, loaderOnlyConfigKeys } = require('../../constants') @@ -33,9 +33,9 @@ function getConfigContent (request, reply, testServer) { const config = { agent: `${testServer.assetServer.host}:${testServer.assetServer.port}/build/nr.js`, - beacon: `${testServer.bamServer.host}:${testServer.bamServer.port}`, - errorBeacon: `${testServer.bamServer.host}:${testServer.bamServer.port}`, - ...defaultAgentConfig, + beacon: `${testServer.bamServer.host}:${testServer.bamServer.port}`, // these will be overridden by agentConfig if supplied + errorBeacon: `${testServer.bamServer.host}:${testServer.bamServer.port}`, // these will be overridden by agentConfig if supplied + ...agentConfig, ...queryConfig } @@ -67,7 +67,7 @@ function getConfigContent (request, reply, testServer) { ? `NREUM.loader_config=${loaderConfigJSON};` : '' - return `${sslShim}window.NREUM||(NREUM={});NREUM.info=${infoJSON};${loaderConfigAssignment}${ + return `${sslShim()}window.NREUM||(NREUM={});NREUM.info=${infoJSON};${loaderConfigAssignment}${ testServer.config.debugShim ? debugShim : '' }` } diff --git a/tools/testing-server/plugins/agent-injector/init-transform.js b/tools/testing-server/plugins/agent-injector/init-transform.js index 20868b8cf..6bf353a9e 100644 --- a/tools/testing-server/plugins/agent-injector/init-transform.js +++ b/tools/testing-server/plugins/agent-injector/init-transform.js @@ -2,6 +2,7 @@ const { Transform } = require('stream') const { defaultInitBlock } = require('../../constants') const { deepmerge } = require('deepmerge-ts') const { deserialize } = require('../../../shared/serializer.js') +const sslShim = require('./ssl-shim.js') /** * Constructs the agent init script block based on the init query. @@ -35,7 +36,7 @@ function getInitContent (request, reply, testServer) { initJSON = initJSON.replace(/"new RegExp\((.*?)\)"/g, 'new RegExp($1)') - return `window.NREUM||(NREUM={});NREUM.init=${initJSON};NREUM.init.ssl=false;` + return sslShim(initJSON) } /** diff --git a/tools/testing-server/plugins/agent-injector/loader-transform.js b/tools/testing-server/plugins/agent-injector/loader-transform.js index fd63723ce..72d64695e 100644 --- a/tools/testing-server/plugins/agent-injector/loader-transform.js +++ b/tools/testing-server/plugins/agent-injector/loader-transform.js @@ -70,7 +70,7 @@ module.exports = function (request, reply, testServer) { function replaceLoaderPlaceholder (chunkString, loaderScript) { return chunkString.replace( '{loader}', - `${loaderScript}` + `${loaderScript}` ) } while (chunkString.indexOf('{loader}') > -1) { diff --git a/tools/testing-server/plugins/agent-injector/ssl-shim.js b/tools/testing-server/plugins/agent-injector/ssl-shim.js index ff7a5af01..db75fd215 100644 --- a/tools/testing-server/plugins/agent-injector/ssl-shim.js +++ b/tools/testing-server/plugins/agent-injector/ssl-shim.js @@ -1,4 +1,8 @@ +const yargs = require('yargs') +const { hideBin } = require('yargs/helpers') +const args = yargs(hideBin(process.argv)).argv + /** * This script is injected into test HTML pages to disable SSL in the agent. */ -module.exports = 'window.NREUM||(NREUM={});NREUM.init||(NREUM.init={});NREUM.init.ssl=false;' +module.exports = (initString = '{}') => `window.NREUM||(NREUM={});NREUM.init||(NREUM.init=${initString});NREUM.init.ssl=${!!args.B};` diff --git a/tools/wdio/args.mjs b/tools/wdio/args.mjs index 3edaf6500..71393d887 100644 --- a/tools/wdio/args.mjs +++ b/tools/wdio/args.mjs @@ -81,6 +81,10 @@ const args = yargs(hideBin(process.argv)) .default('webview', false) .describe('webview', 'Run webview tests') + .string('B') + .alias('B', 'report-to-bam') + .describe('B', 'Supply a JSON string or file path with expected BAM configs to send agent traffic to BAM instead of local service') + .middleware(argv => { if (argv.webview && (!argv.browsers || argv.browsers === 'chrome@latest')) { argv.browsers = argv.b = 'ios@latest,android@latest'