From 297fdf9fe2c02922d69f236ec2ace816384788ab Mon Sep 17 00:00:00 2001 From: glendc Date: Tue, 8 Nov 2022 00:50:59 +0100 Subject: [PATCH 01/13] shim logger and env requirements for browser use --- package.json | 7 ++++++- shims/runtime/env.js | 9 +++++++++ shims/runtime/logger.js | 35 +++++++++++++++++++++++++++++++++++ src/runtime/env.js | 9 +++++++++ src/runtime/index.js | 4 +++- 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 shims/runtime/env.js create mode 100644 shims/runtime/logger.js create mode 100644 src/runtime/env.js diff --git a/package.json b/package.json index ae5b483..f81838e 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,10 @@ "engines": { "node": ">=14.0.0" }, + "browser": { + "./src/runtime/env.js": "./shims/runtime/env.js", + "./src/runtime/logger.js": "./shims/runtime/logger.js" + }, "aegir": { "test": { "target": [ @@ -66,6 +70,7 @@ }, "contributors": [ "Henrique Dias ", - "Laurent Senta " + "Laurent Senta ", + "Glen De Cauwsemaecker " ] } diff --git a/shims/runtime/env.js b/shims/runtime/env.js new file mode 100644 index 0000000..aa0744e --- /dev/null +++ b/shims/runtime/env.js @@ -0,0 +1,9 @@ +/** + * Gets the environment that can be used by the environment + * to create the runtime. + * + * @returns {Record} + */ + export function getCurrentEnv() { + return window.testground.env +} diff --git a/shims/runtime/logger.js b/shims/runtime/logger.js new file mode 100644 index 0000000..1243b20 --- /dev/null +++ b/shims/runtime/logger.js @@ -0,0 +1,35 @@ +'use strict' + +const winston = require('winston') + +/** + * @param {import('./params').RunParams} params + * @returns {winston.Logger} + */ +function getLogger (params) { + const format = winston.format.combine( + winston.format((info, opts = {}) => { + info.ts = Date.now() * 1000000 // timestamp with nanoseconds, doesn't have precision, + info.group_id = params.testGroupId + info.run_id = params.testRun + return info + })(), + winston.format.json() + ) + + const transports = [ + new winston.transports.Console({ format }) + ] + + return winston.createLogger({ + level: process.env.LOG_LEVEL !== '' + ? process.env.LOG_LEVEL + : 'info', + format, + transports + }) +} + +module.exports = { + getLogger +} diff --git a/src/runtime/env.js b/src/runtime/env.js new file mode 100644 index 0000000..bbbd6fa --- /dev/null +++ b/src/runtime/env.js @@ -0,0 +1,9 @@ +/** + * Gets the environment that can be used by the environment + * to create the runtime. + * + * @returns {Record} + */ +export function getCurrentEnv() { + return process.env +} diff --git a/src/runtime/index.js b/src/runtime/index.js index bdc80d2..6124f92 100644 --- a/src/runtime/index.js +++ b/src/runtime/index.js @@ -3,6 +3,7 @@ const { getLogger } = require('./logger') const { newEvents } = require('./events') const { parseRunParams } = require('./params') +const { getCurrentEnv } = require('./env'); /** @typedef {import('./types').RunParams} RunParams */ /** @typedef {import('./types').SignalEmitter} SignalEmitter */ @@ -14,7 +15,8 @@ const { parseRunParams } = require('./params') * @returns {RunEnv} */ function currentRunEnv () { - return parseRunEnv(process.env) + const env = getCurrentEnv() + return parseRunEnv(env) } /** From ec61e2e4ea07d56a9365e504c1d4ee2d554c0ceb Mon Sep 17 00:00:00 2001 From: glendc Date: Tue, 8 Nov 2022 01:04:49 +0100 Subject: [PATCH 02/13] continue shimming of codebase --- package.json | 5 +++-- shims/{ => src}/runtime/env.js | 0 shims/{ => src}/runtime/logger.js | 0 shims/std/os.js | 13 +++++++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) rename shims/{ => src}/runtime/env.js (100%) rename shims/{ => src}/runtime/logger.js (100%) create mode 100644 shims/std/os.js diff --git a/package.json b/package.json index f81838e..c652f51 100644 --- a/package.json +++ b/package.json @@ -52,8 +52,9 @@ "node": ">=14.0.0" }, "browser": { - "./src/runtime/env.js": "./shims/runtime/env.js", - "./src/runtime/logger.js": "./shims/runtime/logger.js" + "os": "./shims/std/os.js", + "./src/runtime/env.js": "./shims/src/runtime/env.js", + "./src/runtime/logger.js": "./shims/src/runtime/logger.js" }, "aegir": { "test": { diff --git a/shims/runtime/env.js b/shims/src/runtime/env.js similarity index 100% rename from shims/runtime/env.js rename to shims/src/runtime/env.js diff --git a/shims/runtime/logger.js b/shims/src/runtime/logger.js similarity index 100% rename from shims/runtime/logger.js rename to shims/src/runtime/logger.js diff --git a/shims/std/os.js b/shims/std/os.js new file mode 100644 index 0000000..d446578 --- /dev/null +++ b/shims/std/os.js @@ -0,0 +1,13 @@ +/** + * @returns {os.NetworkInterfaceInfo[]} + */ +export function networkInterfaces () { + return [] +} + +/** + * @returns {string} + */ + export function hostname () { + return 'browser' +} From 9553b3b323393614e0723399388600071565ad55 Mon Sep 17 00:00:00 2001 From: glendc Date: Tue, 8 Nov 2022 01:08:17 +0100 Subject: [PATCH 03/13] attempt in fixing the shims for browser --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c652f51..9657da2 100644 --- a/package.json +++ b/package.json @@ -53,8 +53,8 @@ }, "browser": { "os": "./shims/std/os.js", - "./src/runtime/env.js": "./shims/src/runtime/env.js", - "./src/runtime/logger.js": "./shims/src/runtime/logger.js" + "./src/runtime/env": "./shims/src/runtime/env.js", + "./src/runtime/logger": "./shims/src/runtime/logger.js" }, "aegir": { "test": { From ce1db89af099b470159c1becc4091850a12953aa Mon Sep 17 00:00:00 2001 From: glendc Date: Tue, 8 Nov 2022 01:56:50 +0100 Subject: [PATCH 04/13] finish shimming for browser --- package-lock.json | 4 +-- package.json | 6 ++--- shims/src/runtime/logger.js | 35 ------------------------- shims/std/os.js | 13 --------- src/shims/os.js | 20 ++++++++++++++ {shims/src => src/shims}/runtime/env.js | 11 ++++++-- src/shims/runtime/logger.js | 9 +++++++ 7 files changed, 43 insertions(+), 55 deletions(-) delete mode 100644 shims/src/runtime/logger.js delete mode 100644 shims/std/os.js create mode 100644 src/shims/os.js rename {shims/src => src/shims}/runtime/env.js (53%) create mode 100644 src/shims/runtime/logger.js diff --git a/package-lock.json b/package-lock.json index 84b5d9d..47f5e0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@testground/sdk", - "version": "0.0.0", + "version": "0.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@testground/sdk", - "version": "0.0.0", + "version": "0.1.2", "license": "MIT", "dependencies": { "emittery": "^0.10.0", diff --git a/package.json b/package.json index 9657da2..5a5a219 100644 --- a/package.json +++ b/package.json @@ -52,9 +52,9 @@ "node": ">=14.0.0" }, "browser": { - "os": "./shims/std/os.js", - "./src/runtime/env": "./shims/src/runtime/env.js", - "./src/runtime/logger": "./shims/src/runtime/logger.js" + "os": "./src/shims/os.js", + "./src/runtime/env": "./src/shims/runtime/env.js", + "./src/runtime/logger": "./src/shims/runtime/logger.js" }, "aegir": { "test": { diff --git a/shims/src/runtime/logger.js b/shims/src/runtime/logger.js deleted file mode 100644 index 1243b20..0000000 --- a/shims/src/runtime/logger.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict' - -const winston = require('winston') - -/** - * @param {import('./params').RunParams} params - * @returns {winston.Logger} - */ -function getLogger (params) { - const format = winston.format.combine( - winston.format((info, opts = {}) => { - info.ts = Date.now() * 1000000 // timestamp with nanoseconds, doesn't have precision, - info.group_id = params.testGroupId - info.run_id = params.testRun - return info - })(), - winston.format.json() - ) - - const transports = [ - new winston.transports.Console({ format }) - ] - - return winston.createLogger({ - level: process.env.LOG_LEVEL !== '' - ? process.env.LOG_LEVEL - : 'info', - format, - transports - }) -} - -module.exports = { - getLogger -} diff --git a/shims/std/os.js b/shims/std/os.js deleted file mode 100644 index d446578..0000000 --- a/shims/std/os.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * @returns {os.NetworkInterfaceInfo[]} - */ -export function networkInterfaces () { - return [] -} - -/** - * @returns {string} - */ - export function hostname () { - return 'browser' -} diff --git a/src/shims/os.js b/src/shims/os.js new file mode 100644 index 0000000..9d9a1bc --- /dev/null +++ b/src/shims/os.js @@ -0,0 +1,20 @@ +'use strict' + +/** + * @returns {any[]} + */ +function networkInterfaces () { + return [] +} + +/** + * @returns {string} + */ +function hostname () { + return 'browser' +} + +module.exports = { + networkInterfaces, + hostname +} diff --git a/shims/src/runtime/env.js b/src/shims/runtime/env.js similarity index 53% rename from shims/src/runtime/env.js rename to src/shims/runtime/env.js index aa0744e..68b14b2 100644 --- a/shims/src/runtime/env.js +++ b/src/shims/runtime/env.js @@ -1,9 +1,16 @@ +'use strict' + /** * Gets the environment that can be used by the environment * to create the runtime. * * @returns {Record} */ - export function getCurrentEnv() { - return window.testground.env +function getCurrentEnv () { + // @ts-ignore + return window.testground.env +} + +module.exports = { + getCurrentEnv } diff --git a/src/shims/runtime/logger.js b/src/shims/runtime/logger.js new file mode 100644 index 0000000..c0d791b --- /dev/null +++ b/src/shims/runtime/logger.js @@ -0,0 +1,9 @@ +'use strict' + +function getLogger () { + return console +} + +module.exports = { + getLogger +} From 18576866761ccaaf926ef7058061929fa4fa5201 Mon Sep 17 00:00:00 2001 From: glendc Date: Thu, 10 Nov 2022 00:47:01 +0100 Subject: [PATCH 05/13] improve logger capabilities --- src/shims/runtime/logger.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/shims/runtime/logger.js b/src/shims/runtime/logger.js index c0d791b..170e808 100644 --- a/src/shims/runtime/logger.js +++ b/src/shims/runtime/logger.js @@ -1,7 +1,20 @@ 'use strict' function getLogger () { - return console + return { + debug: function (...args) { + console.debug(...args) + }, + info: function (...args) { + console.info(...args) + }, + warn: function (...args) { + console.warn(...args) + }, + error: function (...args) { + console.error(...args); + } + }; } module.exports = { From b1f6c49734c6455e146a8da889c8031915a2564c Mon Sep 17 00:00:00 2001 From: glendc Date: Sat, 12 Nov 2022 10:09:19 +0100 Subject: [PATCH 06/13] use 'console' from WebAPI as is in browser for logging purposes --- src/shims/runtime/logger.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/shims/runtime/logger.js b/src/shims/runtime/logger.js index 170e808..c0d791b 100644 --- a/src/shims/runtime/logger.js +++ b/src/shims/runtime/logger.js @@ -1,20 +1,7 @@ 'use strict' function getLogger () { - return { - debug: function (...args) { - console.debug(...args) - }, - info: function (...args) { - console.info(...args) - }, - warn: function (...args) { - console.warn(...args) - }, - error: function (...args) { - console.error(...args); - } - }; + return console } module.exports = { From c4583ec8fe748beeb200ac6729e93af39ebb166f Mon Sep 17 00:00:00 2001 From: glendc Date: Sat, 12 Nov 2022 10:14:20 +0100 Subject: [PATCH 07/13] fix linter errors (aegir) --- src/runtime/env.js | 10 ++++++++-- src/runtime/events.js | 4 ---- src/runtime/index.js | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/runtime/env.js b/src/runtime/env.js index bbbd6fa..f7ffa53 100644 --- a/src/runtime/env.js +++ b/src/runtime/env.js @@ -1,9 +1,15 @@ +'use strict' + /** * Gets the environment that can be used by the environment * to create the runtime. * * @returns {Record} */ -export function getCurrentEnv() { - return process.env +function getCurrentEnv () { + return process.env +} + +module.exports = { + getCurrentEnv } diff --git a/src/runtime/events.js b/src/runtime/events.js index f5500ca..da7bdd6 100644 --- a/src/runtime/events.js +++ b/src/runtime/events.js @@ -47,7 +47,6 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) - // TODO(metrics): re.metrics.recordEvent(&evt) }, recordSuccess: () => { const event = { @@ -58,7 +57,6 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) - // TODO(metrics): re.metrics.recordEvent(&evt) }, recordFailure: (err) => { const event = { @@ -70,7 +68,6 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) - // TODO(metrics): re.metrics.recordEvent(&evt) }, recordCrash: (err) => { const event = { @@ -83,7 +80,6 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) - // TODO(metrics): re.metrics.recordEvent(&evt) } } } diff --git a/src/runtime/index.js b/src/runtime/index.js index 6124f92..3ad3b62 100644 --- a/src/runtime/index.js +++ b/src/runtime/index.js @@ -3,7 +3,7 @@ const { getLogger } = require('./logger') const { newEvents } = require('./events') const { parseRunParams } = require('./params') -const { getCurrentEnv } = require('./env'); +const { getCurrentEnv } = require('./env') /** @typedef {import('./types').RunParams} RunParams */ /** @typedef {import('./types').SignalEmitter} SignalEmitter */ From 06df3ce6c9c904d52fa6d081aa1ffd204b5de69a Mon Sep 17 00:00:00 2001 From: glendc Date: Sat, 12 Nov 2022 15:21:03 +0100 Subject: [PATCH 08/13] provide functionality to extract the required params from the env --- src/runtime/env.js | 4 ++-- src/runtime/index.js | 5 ++--- src/runtime/params.js | 37 +++++++++++++++++++++++++++++++++++++ src/shims/runtime/env.js | 4 ++-- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/runtime/env.js b/src/runtime/env.js index f7ffa53..aa38414 100644 --- a/src/runtime/env.js +++ b/src/runtime/env.js @@ -6,10 +6,10 @@ * * @returns {Record} */ -function getCurrentEnv () { +function getProcessEnv () { return process.env } module.exports = { - getCurrentEnv + getProcessEnv } diff --git a/src/runtime/index.js b/src/runtime/index.js index 3ad3b62..be8349c 100644 --- a/src/runtime/index.js +++ b/src/runtime/index.js @@ -2,8 +2,7 @@ const { getLogger } = require('./logger') const { newEvents } = require('./events') -const { parseRunParams } = require('./params') -const { getCurrentEnv } = require('./env') +const { getEnvParameters, parseRunParams } = require('./params') /** @typedef {import('./types').RunParams} RunParams */ /** @typedef {import('./types').SignalEmitter} SignalEmitter */ @@ -15,7 +14,7 @@ const { getCurrentEnv } = require('./env') * @returns {RunEnv} */ function currentRunEnv () { - const env = getCurrentEnv() + const env = getEnvParameters() return parseRunEnv(env) } diff --git a/src/runtime/params.js b/src/runtime/params.js index 4dd2a53..74f6860 100644 --- a/src/runtime/params.js +++ b/src/runtime/params.js @@ -2,6 +2,8 @@ const ipaddr = require('ipaddr.js') +const { getProcessEnv } = require('./env') + /** @typedef {import('./types').RunParams} RunParams */ const ENV_TEST_BRANCH = 'TEST_BRANCH' @@ -20,6 +22,40 @@ const ENV_TEST_START_TIME = 'TEST_START_TIME' const ENV_TEST_SUBNET = 'TEST_SUBNET' const ENV_TEST_TAG = 'TEST_TAG' +const ENV_TEST_PARAMETERS = [ + ENV_TEST_BRANCH, + ENV_TEST_CASE, + ENV_TEST_GROUP_ID, + ENV_TEST_GROUP_INSTANCE_COUNT, + ENV_TEST_INSTANCE_COUNT, + ENV_TEST_INSTANCE_PARAMS, + ENV_TEST_INSTANCE_ROLE, + ENV_TEST_OUTPUTS_PATH, + ENV_TEST_PLAN, + ENV_TEST_REPO, + ENV_TEST_RUN, + ENV_TEST_SIDECAR, + ENV_TEST_START_TIME, + ENV_TEST_SUBNET, + ENV_TEST_TAG +] + +/** + * Gets the parameters from the environment + * that can be used by the environment to create the runtime. + * + * @returns {Record} + */ +function getEnvParameters () { + const env = getProcessEnv() + return Object.keys(env) + .filter(key => ENV_TEST_PARAMETERS.includes(key)) + .reduce((/** @type {Record} */params, key) => { + params[key] = env[key] + return params + }, {}) +} + /** * @param {Record} env * @returns {RunParams} @@ -95,5 +131,6 @@ function unpackParams (packed) { } module.exports = { + getEnvParameters, parseRunParams } diff --git a/src/shims/runtime/env.js b/src/shims/runtime/env.js index 68b14b2..f9495fe 100644 --- a/src/shims/runtime/env.js +++ b/src/shims/runtime/env.js @@ -6,11 +6,11 @@ * * @returns {Record} */ -function getCurrentEnv () { +function getProcessEnv () { // @ts-ignore return window.testground.env } module.exports = { - getCurrentEnv + getProcessEnv } From 46c347a1e39f3014d789f13b9065a0d08a492ac2 Mon Sep 17 00:00:00 2001 From: glendc Date: Sat, 12 Nov 2022 15:25:29 +0100 Subject: [PATCH 09/13] expose getEnvParameters from runtime pkg --- src/runtime/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runtime/index.js b/src/runtime/index.js index be8349c..4a227f5 100644 --- a/src/runtime/index.js +++ b/src/runtime/index.js @@ -53,5 +53,6 @@ function newRunEnv (params) { module.exports = { newRunEnv, currentRunEnv, - parseRunEnv + parseRunEnv, + getEnvParameters } From 03fc268de60f1cc4286ec490fe5ef9eb9e402263 Mon Sep 17 00:00:00 2001 From: glendc Date: Sat, 12 Nov 2022 22:08:39 +0100 Subject: [PATCH 10/13] register result of testcase at the end this way browser can wait for it, given it lives in a different process than the main driver --- src/index.js | 24 ++++++++++++++++++++++-- src/runtime/env.js | 11 ++++++++++- src/shims/runtime/env.js | 17 +++++++++++++++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 519e882..db8b250 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const runtime = require('./runtime') const sync = require('./sync') const network = require('./network') +const { registerTestcaseResult } = require('./runtime/env') /** @typedef {import('./runtime').RunEnv} RunEnv */ /** @typedef {import('./sync').SyncClient} SyncClient */ @@ -17,12 +18,28 @@ async function invokeMap (cases) { const runenv = runtime.currentRunEnv() if (cases[runenv.testCase]) { - await invokeHelper(runenv, cases[runenv.testCase]) + try { + await invokeHelper(runenv, cases[runenv.testCase]) + } catch (err) { + registerAndMessageTestcaseResult(err, runenv) + throw err + } } else { - throw new Error(`unrecognized test case: ${runenv.testCase}`) + const err = new Error(`unrecognized test case: ${runenv.testCase}`) + registerAndMessageTestcaseResult(err, runenv) + throw err } } +/** + * @param {unknown} result + * @param {RunEnv} runenv + */ +function registerAndMessageTestcaseResult(result, runenv) { + runenv.recordMessage(`registerTestcaseResult: ${result}`) + registerTestcaseResult(result) +} + /** * Runs the passed test-case and reports the result. * @@ -47,15 +64,18 @@ async function invokeHelper (runenv, fn) { await runenv.recordStart() + let /** @type {unknown} */ testResult = true try { await fn(runenv, client) await runenv.recordSuccess() } catch (err) { await runenv.recordFailure(err) + testResult = err } finally { if (client) { client.close() } + registerAndMessageTestcaseResult(testResult, runenv) } } diff --git a/src/runtime/env.js b/src/runtime/env.js index aa38414..8d04688 100644 --- a/src/runtime/env.js +++ b/src/runtime/env.js @@ -10,6 +10,15 @@ function getProcessEnv () { return process.env } +/** + * @param {unknown} _result + */ +function registerTestcaseResult (_result) { + // function is used in the browser shim + // to gain the ability to wait until invokeMap is finished +} + module.exports = { - getProcessEnv + getProcessEnv, + registerTestcaseResult } diff --git a/src/shims/runtime/env.js b/src/shims/runtime/env.js index f9495fe..41464be 100644 --- a/src/shims/runtime/env.js +++ b/src/shims/runtime/env.js @@ -8,9 +8,22 @@ */ function getProcessEnv () { // @ts-ignore - return window.testground.env + return (window.testground || {}).env +} +/** + * @param {unknown} result + */ +function registerTestcaseResult (result) { + // @ts-ignore + if (!window.testground) { + // @ts-ignore + window.testground = {} + } + // @ts-ignore + window.testground.result = result } module.exports = { - getProcessEnv + getProcessEnv, + registerTestcaseResult } From 1552b793500d4bd1eb5b8cf93b3fca98d42bc5f5 Mon Sep 17 00:00:00 2001 From: glendc Date: Sun, 13 Nov 2022 00:18:36 +0100 Subject: [PATCH 11/13] improve logger, remove process use and expose env better - logger needs to handle winston-like usage, making sure we log everything nicely as-is, instead of passing it directly we first need to manipualte it in a winston like manner to get a good effect - process was used in sync, but we cannot use this, env extraction is now cross-inner-package - this comples the env usage for node-browser-crossing --- package.json | 2 +- src/{runtime => env}/env.js | 0 src/env/index.js | 46 +++++++++++++++ src/env/runtime.js | 35 +++++++++++ src/env/sync.js | 9 +++ src/index.js | 7 ++- src/runtime/index.js | 3 +- src/runtime/params.js | 41 +------------ src/shims/{runtime => env}/env.js | 0 src/shims/runtime/logger.js | 96 ++++++++++++++++++++++++++++++- src/sync/socket.js | 15 +++-- 11 files changed, 207 insertions(+), 47 deletions(-) rename src/{runtime => env}/env.js (100%) create mode 100644 src/env/index.js create mode 100644 src/env/runtime.js create mode 100644 src/env/sync.js rename src/shims/{runtime => env}/env.js (100%) diff --git a/package.json b/package.json index 5a5a219..9de9c88 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ }, "browser": { "os": "./src/shims/os.js", - "./src/runtime/env": "./src/shims/runtime/env.js", + "./src/env/env": "./src/shims/env/env.js", "./src/runtime/logger": "./src/shims/runtime/logger.js" }, "aegir": { diff --git a/src/runtime/env.js b/src/env/env.js similarity index 100% rename from src/runtime/env.js rename to src/env/env.js diff --git a/src/env/index.js b/src/env/index.js new file mode 100644 index 0000000..ea69e91 --- /dev/null +++ b/src/env/index.js @@ -0,0 +1,46 @@ +'use strict' + +const runtime = require('./runtime') +const sync = require('./sync') +const { getProcessEnv } = require('./env') + +const ENV_TEST_PARAMETERS = [ + runtime.ENV_TEST_BRANCH, + runtime.ENV_TEST_CASE, + runtime.ENV_TEST_GROUP_ID, + runtime.ENV_TEST_GROUP_INSTANCE_COUNT, + runtime.ENV_TEST_INSTANCE_COUNT, + runtime.ENV_TEST_INSTANCE_PARAMS, + runtime.ENV_TEST_INSTANCE_ROLE, + runtime.ENV_TEST_OUTPUTS_PATH, + runtime.ENV_TEST_PLAN, + runtime.ENV_TEST_REPO, + runtime.ENV_TEST_RUN, + runtime.ENV_TEST_SIDECAR, + runtime.ENV_TEST_START_TIME, + runtime.ENV_TEST_SUBNET, + runtime.ENV_TEST_TAG, + + sync.ENV_SYNC_SERVICE_HOST, + sync.ENV_SYNC_SERVICE_PORT +] + +/** + * Gets the parameters from the environment + * that can be used by the environment to create the runtime. + * + * @returns {Record} + */ +function getEnvParameters () { + const env = getProcessEnv() + return Object.keys(env) + .filter(key => ENV_TEST_PARAMETERS.includes(key)) + .reduce((/** @type {Record} */params, key) => { + params[key] = env[key] + return params + }, {}) +} + +module.exports = { + getEnvParameters +} diff --git a/src/env/runtime.js b/src/env/runtime.js new file mode 100644 index 0000000..44d11c9 --- /dev/null +++ b/src/env/runtime.js @@ -0,0 +1,35 @@ +'use strict' + +const ENV_TEST_BRANCH = 'TEST_BRANCH' +const ENV_TEST_CASE = 'TEST_CASE' +const ENV_TEST_GROUP_ID = 'TEST_GROUP_ID' +const ENV_TEST_GROUP_INSTANCE_COUNT = 'TEST_GROUP_INSTANCE_COUNT' +const ENV_TEST_INSTANCE_COUNT = 'TEST_INSTANCE_COUNT' +const ENV_TEST_INSTANCE_PARAMS = 'TEST_INSTANCE_PARAMS' +const ENV_TEST_INSTANCE_ROLE = 'TEST_INSTANCE_ROLE' +const ENV_TEST_OUTPUTS_PATH = 'TEST_OUTPUTS_PATH' +const ENV_TEST_PLAN = 'TEST_PLAN' +const ENV_TEST_REPO = 'TEST_REPO' +const ENV_TEST_RUN = 'TEST_RUN' +const ENV_TEST_SIDECAR = 'TEST_SIDECAR' +const ENV_TEST_START_TIME = 'TEST_START_TIME' +const ENV_TEST_SUBNET = 'TEST_SUBNET' +const ENV_TEST_TAG = 'TEST_TAG' + +module.exports = { + ENV_TEST_BRANCH, + ENV_TEST_CASE, + ENV_TEST_GROUP_ID, + ENV_TEST_GROUP_INSTANCE_COUNT, + ENV_TEST_INSTANCE_COUNT, + ENV_TEST_INSTANCE_PARAMS, + ENV_TEST_INSTANCE_ROLE, + ENV_TEST_OUTPUTS_PATH, + ENV_TEST_PLAN, + ENV_TEST_REPO, + ENV_TEST_RUN, + ENV_TEST_SIDECAR, + ENV_TEST_START_TIME, + ENV_TEST_SUBNET, + ENV_TEST_TAG +} diff --git a/src/env/sync.js b/src/env/sync.js new file mode 100644 index 0000000..d9d87e5 --- /dev/null +++ b/src/env/sync.js @@ -0,0 +1,9 @@ +'use strict' + +const ENV_SYNC_SERVICE_HOST = 'SYNC_SERVICE_HOST' +const ENV_SYNC_SERVICE_PORT = 'SYNC_SERVICE_PORT' + +module.exports = { + ENV_SYNC_SERVICE_HOST, + ENV_SYNC_SERVICE_PORT +} diff --git a/src/index.js b/src/index.js index db8b250..f17ebd8 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,9 @@ const runtime = require('./runtime') const sync = require('./sync') const network = require('./network') -const { registerTestcaseResult } = require('./runtime/env') +const env = require('./env') + +const { registerTestcaseResult } = require('./env/env') /** @typedef {import('./runtime').RunEnv} RunEnv */ /** @typedef {import('./sync').SyncClient} SyncClient */ @@ -35,7 +37,7 @@ async function invokeMap (cases) { * @param {unknown} result * @param {RunEnv} runenv */ -function registerAndMessageTestcaseResult(result, runenv) { +function registerAndMessageTestcaseResult (result, runenv) { runenv.recordMessage(`registerTestcaseResult: ${result}`) registerTestcaseResult(result) } @@ -83,6 +85,7 @@ module.exports = { invoke, invokeMap, + env, network, runtime, sync diff --git a/src/runtime/index.js b/src/runtime/index.js index 4a227f5..7add39b 100644 --- a/src/runtime/index.js +++ b/src/runtime/index.js @@ -2,7 +2,8 @@ const { getLogger } = require('./logger') const { newEvents } = require('./events') -const { getEnvParameters, parseRunParams } = require('./params') +const { parseRunParams } = require('./params') +const { getEnvParameters } = require('../env') /** @typedef {import('./types').RunParams} RunParams */ /** @typedef {import('./types').SignalEmitter} SignalEmitter */ diff --git a/src/runtime/params.js b/src/runtime/params.js index 74f6860..9ec4956 100644 --- a/src/runtime/params.js +++ b/src/runtime/params.js @@ -2,27 +2,7 @@ const ipaddr = require('ipaddr.js') -const { getProcessEnv } = require('./env') - -/** @typedef {import('./types').RunParams} RunParams */ - -const ENV_TEST_BRANCH = 'TEST_BRANCH' -const ENV_TEST_CASE = 'TEST_CASE' -const ENV_TEST_GROUP_ID = 'TEST_GROUP_ID' -const ENV_TEST_GROUP_INSTANCE_COUNT = 'TEST_GROUP_INSTANCE_COUNT' -const ENV_TEST_INSTANCE_COUNT = 'TEST_INSTANCE_COUNT' -const ENV_TEST_INSTANCE_PARAMS = 'TEST_INSTANCE_PARAMS' -const ENV_TEST_INSTANCE_ROLE = 'TEST_INSTANCE_ROLE' -const ENV_TEST_OUTPUTS_PATH = 'TEST_OUTPUTS_PATH' -const ENV_TEST_PLAN = 'TEST_PLAN' -const ENV_TEST_REPO = 'TEST_REPO' -const ENV_TEST_RUN = 'TEST_RUN' -const ENV_TEST_SIDECAR = 'TEST_SIDECAR' -const ENV_TEST_START_TIME = 'TEST_START_TIME' -const ENV_TEST_SUBNET = 'TEST_SUBNET' -const ENV_TEST_TAG = 'TEST_TAG' - -const ENV_TEST_PARAMETERS = [ +const { ENV_TEST_BRANCH, ENV_TEST_CASE, ENV_TEST_GROUP_ID, @@ -38,23 +18,9 @@ const ENV_TEST_PARAMETERS = [ ENV_TEST_START_TIME, ENV_TEST_SUBNET, ENV_TEST_TAG -] +} = require('../env/runtime') -/** - * Gets the parameters from the environment - * that can be used by the environment to create the runtime. - * - * @returns {Record} - */ -function getEnvParameters () { - const env = getProcessEnv() - return Object.keys(env) - .filter(key => ENV_TEST_PARAMETERS.includes(key)) - .reduce((/** @type {Record} */params, key) => { - params[key] = env[key] - return params - }, {}) -} +/** @typedef {import('./types').RunParams} RunParams */ /** * @param {Record} env @@ -131,6 +97,5 @@ function unpackParams (packed) { } module.exports = { - getEnvParameters, parseRunParams } diff --git a/src/shims/runtime/env.js b/src/shims/env/env.js similarity index 100% rename from src/shims/runtime/env.js rename to src/shims/env/env.js diff --git a/src/shims/runtime/logger.js b/src/shims/runtime/logger.js index c0d791b..f02942d 100644 --- a/src/shims/runtime/logger.js +++ b/src/shims/runtime/logger.js @@ -1,7 +1,101 @@ +/* eslint-disable no-console */ 'use strict' +// Loosely based on Winston's Logger logic, +// simplified for our minimal needs. +// See license at https://github.com/winstonjs/winston/blob/master/LICENSE +// for original source code. + function getLogger () { - return console + return { + /** + * @param {any[]} args + */ + debug (...args) { + return this.log(console.debug, ...args) + }, + + /** + * @param {any[]} args + */ + info (...args) { + return this.log(console.info, ...args) + }, + + /** + * @param {any[]} args + */ + warn (...args) { + return this.log(console.warn, ...args) + }, + + /** + * @param {any[]} args + */ + error (...args) { + return this.log(console.error, ...args) + }, + + /** + * @param {CallableFunction} fn + * @param {any[]} args + */ + log (fn, ...args) { + // Optimize the hot-path which is the single object. + if (args.length === 1) { + const [msg] = args + const info = (msg && msg.message && msg) || { message: msg } + this._write(fn, info) + return this + } + + // When provided nothing assume the empty string + if (args.length === 0) { + this._log(fn, '') + return this + } + + // Otherwise build argument list + return this._log(fn, args[0], ...args.slice(1)) + }, + + /** + * @param {CallableFunction} fn + * @param {any} msg + * @param {any[]} splat + */ + _log (fn, msg, ...splat) { + // eslint-disable-line max-params + // Optimize for the hotpath of logging JSON literals + if (arguments.length === 1) { + this._write(fn, '') + return this + } + + // Slightly less hotpath, but worth optimizing for. + if (arguments.length === 2) { + if (msg && typeof msg === 'object') { + this._write(fn, msg) + return this + } + + this._write(fn, { message: msg }) + return this + } + + this._write(fn, { message: msg, splat }) + return this + }, + + /** + * @param {CallableFunction} fn + * @param {any} msg + */ + _write (fn, msg) { + const s = JSON.stringify(msg) + fn(s) + } + } } module.exports = { diff --git a/src/sync/socket.js b/src/sync/socket.js index 13f2172..46d5f84 100644 --- a/src/sync/socket.js +++ b/src/sync/socket.js @@ -3,8 +3,13 @@ const Emittery = require('emittery') const WebSocket = require('isomorphic-ws') -const ENV_SYNC_SERVICE_HOST = 'SYNC_SERVICE_HOST' -const ENV_SYNC_SERVICE_PORT = 'SYNC_SERVICE_PORT' +const { + ENV_SYNC_SERVICE_HOST, + ENV_SYNC_SERVICE_PORT +} = require('../env/sync') +const { + getEnvParameters +} = require('../env') /** @typedef {import('winston').Logger} Logger */ /** @typedef {import('events').EventEmitter} EventEmitter */ @@ -97,8 +102,10 @@ function createSocket (logger) { } function socketAddress () { - let host = process.env[ENV_SYNC_SERVICE_HOST] - let port = process.env[ENV_SYNC_SERVICE_PORT] + const env = getEnvParameters() + + let host = env[ENV_SYNC_SERVICE_HOST] + let port = env[ENV_SYNC_SERVICE_PORT] if (!port) { port = '5050' From 4ef19cfc03bc307c4d10ca885d100e4271643b57 Mon Sep 17 00:00:00 2001 From: glendc Date: Tue, 15 Nov 2022 02:07:13 +0100 Subject: [PATCH 12/13] re-add TODO comments from the past --- src/runtime/events.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/runtime/events.js b/src/runtime/events.js index da7bdd6..4e1980c 100644 --- a/src/runtime/events.js +++ b/src/runtime/events.js @@ -47,6 +47,8 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) + // eslint-disable-next-line no-warning-comments + // TODO(metrics): re.metrics.recordEvent(&evt) }, recordSuccess: () => { const event = { @@ -57,6 +59,8 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) + // eslint-disable-next-line no-warning-comments + // TODO(metrics): re.metrics.recordEvent(&evt) }, recordFailure: (err) => { const event = { @@ -68,6 +72,8 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) + // eslint-disable-next-line no-warning-comments + // TODO(metrics): re.metrics.recordEvent(&evt) }, recordCrash: (err) => { const event = { @@ -80,6 +86,8 @@ function newEvents (runParams, logger, getSignalEmitter) { logger.info('', { event }) return emitEvent(event) + // eslint-disable-next-line no-warning-comments + // TODO(metrics): re.metrics.recordEvent(&evt) } } } From 36fca7f6cac6e5407c07bd73059f51c566dc3e1b Mon Sep 17 00:00:00 2001 From: glendc Date: Tue, 15 Nov 2022 17:27:39 +0100 Subject: [PATCH 13/13] simplify logging for sdk-js (log it all as-is) --- src/shims/runtime/logger.js | 54 +------------------------------------ 1 file changed, 1 insertion(+), 53 deletions(-) diff --git a/src/shims/runtime/logger.js b/src/shims/runtime/logger.js index f02942d..0d378ee 100644 --- a/src/shims/runtime/logger.js +++ b/src/shims/runtime/logger.js @@ -41,59 +41,7 @@ function getLogger () { * @param {any[]} args */ log (fn, ...args) { - // Optimize the hot-path which is the single object. - if (args.length === 1) { - const [msg] = args - const info = (msg && msg.message && msg) || { message: msg } - this._write(fn, info) - return this - } - - // When provided nothing assume the empty string - if (args.length === 0) { - this._log(fn, '') - return this - } - - // Otherwise build argument list - return this._log(fn, args[0], ...args.slice(1)) - }, - - /** - * @param {CallableFunction} fn - * @param {any} msg - * @param {any[]} splat - */ - _log (fn, msg, ...splat) { - // eslint-disable-line max-params - // Optimize for the hotpath of logging JSON literals - if (arguments.length === 1) { - this._write(fn, '') - return this - } - - // Slightly less hotpath, but worth optimizing for. - if (arguments.length === 2) { - if (msg && typeof msg === 'object') { - this._write(fn, msg) - return this - } - - this._write(fn, { message: msg }) - return this - } - - this._write(fn, { message: msg, splat }) - return this - }, - - /** - * @param {CallableFunction} fn - * @param {any} msg - */ - _write (fn, msg) { - const s = JSON.stringify(msg) - fn(s) + fn(JSON.stringify(args)) } } }